<s>[INST] How do you integrate $\int \frac{1}{a + \cos x} dx$? How do you integrate $\int \frac{1}{a + \cos x} dx$?  Is it solvable by elementary methods?  I was trying to do it while incorrectly solving a homework problem but I couldn't find the answer.
Thanks!
 [/INST] Also a generalised solution, borrowing from and expanding upon user1357113's answer,
I. For the case $|a| > |b|$, note that the substitution $t=\tan \left( \frac{x}{2} \right)$ is not injective. So to retrieve a continuous antiderivative, we have as a complete answer
$$
\int \frac{1}{a+b\cos(x)} {\rm d}x
= \frac{2}{\sqrt{a^2-b^2}} \arctan \left( \sqrt{\frac{a-b}{a+b}}\tan \left( \frac{x}{2} \right) \right) + \frac{2\pi}{\sqrt{a^2-b^2}} \left\lfloor \frac{x+\pi}{2\pi} \right\rfloor +C
$$
However, we can simplify the ugly floor function in terms of arctangents and tangents. First, we have
$$ 
\pi \left\lfloor \frac{x+\pi}{2\pi} \right\rfloor
= \frac{x}{2} - \arctan \left( \tan \left( \frac{x}{2} \right) \right)
$$
So, implementing this and then asking What is $\arctan(x) + \arctan(y)$? We get
$$
\begin{align}
\int \frac{1}{a+b\cos(x)} {\rm d}x
& = \frac{2}{\sqrt{a^2-b^2}} \arctan \left( \sqrt{\frac{a-b}{a+b}}\tan \left( \frac{x}{2} \right) \right) + \frac{2}{\sqrt{a^2-b^2}} \left( \frac{x}{2} - \arctan \left( \tan \left( \frac{x}{2} \right) \right) \right) +C \\
& = \frac{1}{\sqrt{a^2-b^2}} \left( x + 2\arctan \left( \sqrt{\frac{a-b}{a+b}}\tan \left( \frac{x}{2} \right) \right) - 2\arctan \left( \tan \left( \frac{x}{2} \right) \right) \right) + C \\
& = \frac{1}{\sqrt{a^2-b^2}} \left( x + 2\arctan \left( \frac{ \sqrt{\frac{a-b}{a+b}}\tan \left(\frac{x}{2}\right) - \tan \left(\frac{x}{2}\right) }{1 + \sqrt{\frac{a-b}{a+b}} \tan^2 \left(\frac{x}{2}\right) } \right) \right) + C \\
\end{align}
$$
Which ultimately simplifies to a satisfying compact

$$ \int \frac{1}{a+b\cos(x)} {\rm d}x
= \frac{1}{\sqrt{a^2-b^2}} \left( x - 2\arctan \left( \frac{ (\sqrt{a+b}-\sqrt{a-b})\tan \left(\frac{x}{2}\right) }{\sqrt{a+b} + \sqrt{a-b} \tan^2 \left(\frac{x}{2}\right) } \right) \right) + C
$$

II. And for the case $|a|<|b|$, we will have, from user1357113's partial fraction decomposition,

$$ \int \frac{1}{a+b\cos(x)} {\rm d}x
= \frac{1}{\sqrt{b^2-a^2}}\ln\left(\left|\frac{b+a\cos\left(x\right)+\sqrt{b^2-a^2}\sin\left(x\right)}{a+b\cos\left(x\right)}\right|\right) +C
$$

III a. And for the case $a = b$, we will have
$$ 
\begin{align}
\int \frac{1}{a+b\cos(x)} {\rm d}x
& = \frac1a \int \frac{1}{1+\cos(x)} {\rm d}x \\
& = \frac1a \int \frac{1}{1+2\cos^2(\frac{x}{2})-1}{\rm d}x \\
& = \frac1{2a} \int \sec^2 \left(\frac{x}{2}\right) {\rm d}x \\
& = \frac1a \tan\left(\frac{x}{2}\right) + C
\end{align}
$$
III b. Finally, for the case $a = -b$, we will have
$$ 
\begin{align}
\int \frac{1}{a+b\cos(x)} {\rm d}x
& = \frac1a \int \frac{1}{1-\cos(x)} {\rm d}x \\
& = \frac1a \int \frac{1}{1 - 1 + 2\sin^2(\frac{x}{2})}{\rm d}x \\
& = \frac1{2a} \int \csc^2 \left(\frac{x}{2}\right) {\rm d}x \\
& = \frac1b \cot\left(\frac{x}{2}\right) + C
\end{align}
$$
</s>