My program calls library functions which print to stderr. I want to intervene so that all write calls to file descriptor #2 will instead get sent to somewhere else.
You reversed the arguments: it's
dup2(from_fd, to_fd)
i.e.
dup2(fd, 2)
(see POSIX.2008 or your manpages.)