NOTE: I went though every every previous post on contour integration (here and on youtube) looking for a resolution to this question, so please do NOT mark this question as already answered.
Let $C$ be the contour $z=|r|>1$ be the upper half of a circle or radius r centered at the origin, $(Im(z)\geq 0)$, so not closed), oriented counterclockwise and suppose you want to integrate $$\int_C \frac{1}{1+z^2} dz = \int_0^\pi \frac{i r e^{ i \theta}}{1+ r^2 e^{2 i \theta}}d\theta.$$ where the integral on the righthand side of the equality has the parametrization $z=r e^{ i \theta}, dz =i r e^{ i \theta}d\theta$.
To be clear, I want show this contour integral goes to zero directly as r goes to infinity without using ML estimation or any estimation theorem. Now according to ML, $|\int_C \frac{1}{1+z^2} dz|\leq \pi r/(r^2-1)$ (more details someone else wrote below) which goes to $0$ as $r\rightarrow \infty$, which you will find in the solution of every other related problem. In summary, using ML, $\int_C \frac{1}{1+z^2} dz \rightarrow 0$ as $r\rightarrow \infty$.
But according to Mathematica:
Integrate[ r I Exp[I x]/(1 + (r Exp[I x])^2), {x, 0, Pi}] = $ \int_0^\pi \frac{i r e^{ i \theta}}{1+ r^2 e^{2 i \theta}}d\theta= -2 \arctan(r)$. This does not go to zero as $r\rightarrow \infty$, but instead $-2 \arctan(r)\rightarrow -\pi$.
I also calculated it to be $-\pi$ below in a way that avoids expressions using the complex version of $Arctan(\cdot)$ by doing partial fraction decomposition.
$$\int_C \frac{1}{1+z^2}dz = \frac{i}{2}\int_C \frac{dz}{z+i}-\frac{i}{2}\int_C \frac{dz}{z-i}=\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}+i}d\theta -\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}-i}d\theta $$ $$= \frac{i}{2} \int_{r+i}^{-r+i} \frac{du}{u} -\frac{i}{2} \int_{r-i}^{-r-i} \frac{du}{u}.$$
From here, we can see that we can use the principal branch $\log(z)$ for each individual integral where $-\pi<Arg(z)<\pi$.
So write $r+i = R e^{i \eta}$ where $0<\eta < \pi/2$. Then $r-i = R e^{- i \eta}, -r-i = R e^{i (-\pi+\eta)}, -r+i = R e^{i (\pi-\eta)}$ give the angles corresponding to the principal branch
This gives $$\int_C \frac{1}{1+z^2}dz=\frac{i}{2} (\log(-r+i)-\log(r+i) - (\log(-r-i)-\log(r-i))) \\= \frac{i}{2}(i( (\pi-\eta -\eta)- (-\pi+\eta-(-\eta) ))) = 2\eta - \pi.$$
Then as $r\rightarrow \infty$, $\eta\rightarrow 0$, giving the answer $-\pi$, which agrees with -2 arctan(r) that wolfram gives which also goes to $-\pi$ as $r\rightarrow \infty$.
Can anyone spot where I am going wrong?
The mistake was here.
From here, we can see that we can use the principal branch $\log(z)$ for each individual integral where $-\pi<Arg(z)<\pi$.
Explanation of what went wrong
$\int_C \frac{1}{1+z^2}dz = \frac{i}{2}\int_C \frac{dz}{z+i}-\frac{i}{2}\int_C \frac{dz}{z-i}=\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}+i}d\theta -\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}-i}d\theta$
$= \frac{i}{2} \int_{r+i}^{-r+i} \frac{du}{u} -\frac{i}{2} \int_{r-i}^{-r-i} \frac{du}{u}.$
For the first integral $\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}+i}d\theta = \frac{i}{2} \int_{r+i}^{-r+i} \frac{du}{u}$
Note that with $u= r e^{i\theta}+i$ gives $u$ range of values along the circle $|u|= \sqrt{r^2+1}$ from $\theta=0, u= r+i$ to $\theta=\pi, u= -r+i$ We still need to figure out if we are traversing counterclockwise (contour stays above the real axis) or clockwise. We can determine this by checking a value of $\theta$ between 0 and $\pi$, and $\pi/2$ is the obvious choice. Notice that when $\theta=\pi/2, u= 2i$, so we can see we are taking the counterclockwise contour, which doesn't go through the principal branch of log(u) since it entirely above the real axis. So using the principal branch Log() is fine here!
Now, let's now look at the other integral, where the mistake was made
$\frac{i}{2} \int_0^\pi \frac{r i e^{i\theta}}{r e^{i\theta}-i}d\theta=\frac{i}{2} \int_{r-i}^{-r-i} \frac{du}{u}$.
This time the starting point and ending point are slightly below the real axis. Using the logic in the previous paragraph, when $\theta=\pi/2, u= r e^{i \theta/2}-i= ri-i = (r-1)i$. From here we see that for $r>1$, we are STILL taking the counter clockwise contour, meaning we have to cross the negative real axis, which means taking the principal branch Log(u) won't work!
However, if r<1, then taking the Principal branch is just fine as the contour is then the clockwise one that doesn't cross the negative real axis! In other words, the wrong computation I originally had above would holds for r<1, as wolfram alpha predicted, but wolfram mathematica did not! What we can see here choosing the principal branch in both situation gives a contour (with $r<1$) that can be deformed to $\int_{\infty}^{-\infty} \frac{dx}{1+x^2} = -\pi$ by essentially flattening out this small circle to the real line while simultaneously stretching it out along the real axis.
Corrected Solution
*In both contour integrals above, we can take the antiderivative to be $log(u)$ with the branch cut $-\pi/2 \leq Arg(u)\leq 3\pi/2$ along the neative imaginary axis. Note: we don't have to use the same branch cut for both contour integrals, it is merely more convenient to do so.
So with $R=\sqrt{r^2+1}$, write $r+i = R e^{i \eta}$ where $0<\eta < \pi/2$. Then $r-i = R e^{- i \eta}, -r-i = R e^{i (\pi+\eta)}, -r+i = R e^{i (\pi-\eta)}$ give the angles corresponding to the principal branch
This gives $$\int_C \frac{1}{1+z^2}dz=\frac{i}{2} (\log(-r+i)-\log(r+i) - (\log(-r-i)-\log(r-i))) \\= \frac{i}{2}(i( (\pi-\eta -\eta)- (\pi+\eta-(-\eta) ))) = 2\eta $$
Then as $r\rightarrow \infty$, $\eta\rightarrow 0$, giving the answer $0$