I'm trying to evaluate the following integral using the residue theorem
\begin{align}\label{eq:int_1} S(z) = \dfrac{1}{2\pi}\int_{0}^{2\pi} \dfrac{e^{i\phi}+z}{e^{i\phi}-z} e^{-\lambda\sin^{2}(\phi/2)} \mathrm d\phi \end{align} where $\lambda$ is a real positive parameter. Now here's my attempt which I'm pretty sure is incorrect. We can transform the above real integral into a contour integral over the unit circle using the substitution $w = e^{i\phi}$. First note that - \begin{align} \exp(-\lambda\sin^{2}(\phi/2)) &= \exp(-\lambda/2)\exp((\lambda/4)(e^{i\phi} + e^{-i\phi})) \\ &= \exp(-\lambda/2)\exp((\lambda/4)(w + 1/w)) \end{align}
Therefore the above integral now becomes -
$$S(z) = \dfrac{\exp(-\lambda/2)}{2\pi i}\oint_{\mathcal{C}} \dfrac{w+z}{w-z} \exp\bigg(\dfrac{\lambda}{4}\bigg(w + \dfrac{1}{w}\bigg)\bigg)\dfrac{\mathrm dw}{w} $$
The integrand has singularities at $w = z$ and $w = 0$ (I'm interested in the case when $|z|<1$) so we can evaluate the residues at both of these singularities and then employ the residue theorem. The residue at $w = z$ is found to be
$$2 \exp\bigg(\dfrac{\lambda}{4}\bigg(z + \dfrac{1}{z}\bigg)\bigg) $$
whereas to evaluate the residues at $w = 0$, we need to expand the exponential as a power series and keep only all those terms which possess a simple pole at $w=0$. Since
$$\exp\bigg(\dfrac{\lambda}{4}\bigg(w + \dfrac{1}{w}\bigg)\bigg)\dfrac{1}{w} = \sum_{n=0}^{\infty} \dfrac{1}{n!}\bigg(\dfrac{\lambda}{4}\bigg)^{n}\dfrac{(w^2+1)^{n}}{w^{n+1}}$$ and from the binomial expansion we know that
$$(w^2+1)^{n} = \sum_{k=0}^{n} {n\choose k } w^{2k} $$
It's clear that only the terms with even n yield a simple pole since we must have $n=2k$. And I think finally the residue evaluates to a modified Bessel function of the form $ - I_{0}(\lambda/2)$ where the negative sign comes about from the $(w+z)/(w-z)$ part. And so we obtain
$$S(z) = \exp(-\lambda/2)\bigg( 2 \exp\bigg(\dfrac{\lambda}{4}\bigg(z + \dfrac{1}{z}\bigg)\bigg) - I_{0}(\lambda/2) \bigg)$$
But something seems to have gone wrong here since the above expression has a singularity at $z = 0$ whereas the original expression for the function is clearly finite at $z=0$. I'm really unsure where I've made the mistake. Any help is hugely appreciated. Thanks !!
Preliminaries
$\newcommand{\Res}{\operatorname*{Res}}$ $\Res\limits_{w=0}\left(w^k\,e^{\lambda/4(w+1/w)}\right)$ is a modified Bessel function of the first kind: $$ \begin{align} \Res_{w=0}\left(w^k\,e^{\lambda/4(w+1/w)}\right) &=\left[w^{-k-1}\right]\sum_{n=0}^\infty\frac{(\lambda/4)^n}{n!}\sum_{j=0}^n\binom{n}{j}w^{-n+2j}\tag{1a}\\ &=\sum_{j=0}^\infty\sum_{n=j}^\infty\frac{(\lambda/4)^n}{n!}\binom{n}{j}[n=2j+k+1]\tag{1b}\\ &=\sum_{j=0}^\infty\frac{(\lambda/4)^{2j+k+1}}{(2j+k+1)!}\binom{2j+k+1}{j}\tag{1c}\\ &=\sum_{n=0}^\infty\frac{(\lambda/4)^{2n+k+1}}{n!(n+k+1)!}\tag{1d}\\[9pt] &=I_{k+1}(\lambda/2)\tag{1e} \end{align} $$ Explanation:
$\text{(1a)}$: residue is the coefficient of $1/w$; apply the Binomial Theorem
$\text{(1b)}$: change order of summation and apply coefficient-of operator
$\text{(1c)}$: apply Iverson brackets
$\text{(1d)}$: simplify
$\text{(1e)}$: compare series
Evaluation of the Integral
For $\boldsymbol{|z|\gt1}$
Using $(1)$, we can evaluate the integral as a sum of modified Bessel functions of the first kind: $$ \begin{align} S(z) &=\frac1{2\pi}\int_0^{2\pi}\frac{e^{i\phi}+z}{e^{i\phi}-z}e^{-\lambda\sin^2(\phi/2)}\,\mathrm{d}\phi\tag{2a}\\ &=\frac1{2\pi i}\oint_{\partial B_1}\frac{w+z}{w-z}e^{-\lambda/4(2-w-1/w)}\frac{\mathrm{d}w}w\tag{2b}\\ &=\frac{e^{-\lambda/2}}{2\pi i}\oint_{\partial B_1}\left(\frac2{w-z}-\frac1w\right)e^{\lambda/4(w+1/w)}\,\mathrm{d}w\tag{2c}\\[3pt] &=-e^{-\lambda/2}\Res_{w=0}\left(\left(\frac2z\frac1{1-w/z}+\frac1w\right)e^{\lambda/4(w+1/w)}\right)\tag{2d}\\ &=-e^{-\lambda/2}\left(I_0(\lambda/2)+\sum_{k=0}^\infty\frac2{z^{k+1}}I_{k+1}(\lambda/2)\right)\tag{2e} \end{align} $$ Explanation:
$\text{(2a)}$: definition
$\text{(2b)}$: $w=e^{i\phi}$
$\text{(2c)}$: partial fractions
$\text{(2d)}$: since $|z|\gt1$, ignore the residue at $w=z$
$\text{(2e)}$: apply $(1)$
For $\boldsymbol{|z|\lt1}$
If we substitute $w\mapsto1/w$ in $(2b)$ we see that $$ S(z)=-S(1/z)\tag3 $$ Therefore, $$ S(z)=e^{-\lambda/2}\left(I_0(\lambda/2)+\sum_{k=0}^\infty2z^{k+1}I_{k+1}(\lambda/2)\right)\tag4 $$
The Real Part near $\boldsymbol{|z|=1}$
Since all the coefficients in the Laurent expansion are real, we have that $$ S\left(\bar z\right)=\overline{S(z)}\tag5 $$ Combining $(3)$ and $(5)$ gives $$ \overline{S(z)}=-S(1/\bar z)\tag6 $$ If $z$ is just inside the unit circle, then $1/\bar z$ is just on the other side of the circle and $$\newcommand{\Re}{\operatorname{Re}} \begin{align} 2\Re(S(z)) &=S(z)+\overline{S(z)}\tag{7a}\\ &=S(z)-S(1/\bar z)\tag{7b}\\ &=2e^{-\lambda/2}e^{\lambda/4(z+1/z)}\tag{7c}\\ \Re(S(z)) &=e^{\lambda/2\Re(z-1)}\tag{7d}\\ \Re(S(1/\bar z)) &=-e^{\lambda/2\Re(z-1)}\tag{7e} \end{align} $$ Explanation:
$\text{(7a)}$: formula for the real part of a complex number
$\text{(7b)}$: apply $(6)$
$\text{(7c)}$: $\text{(2c)}$ says that we include the residue at $w=z$
$\phantom{\text{(7c):}}$ for $|z|\lt1$, but not for $|z|\gt1$
$\text{(7d)}$: for $|z|=1$, $z+1/z=2\Re(z)$
$\text{(7e)}$: apply the real part of $(6)$
Graph Along the Real Axis
Along the real axis, for $\lambda=1$:
The function is discontinuous at $|x|=1$, which is to be expected since $(7)$ says $$ S(1^{\pm})=\mp1\qquad\text{and}\qquad S(-1^{\pm})=\pm e^{-\lambda}\tag8 $$ However, $S(x)$ is continuous at $x=0$, and $$ S(0)=e^{-\lambda/2}I_0(\lambda/2)\tag9 $$