Periodicity implied by the condition on level sets $\sum_{x \in \phi^{-1}(b)} \frac{\text{e}^{i \theta(x)}}{|\phi'(x)|}=0$

Let $$\theta$$ and $$\phi$$ two real $$C^h$$ functions on $$[0,2\pi]$$, $$h\geq1$$, satisfying for all not critical values $$b$$ of $$\phi$$ $$\sum_{x \in \phi^{-1}(b)} \frac{\text{e}^{i \theta(x)}}{|\phi'(x)|}=0,$$ then, up to sign, values of $$\text{e}^{i\theta}$$ and $$\phi$$ and all their derivatives must match at the extreme points of the interval.

I can prove this statement if $$a:=\phi(0)$$ is not a critical value. In that case we find $$\delta>0$$ such that $$\phi$$ is invertible on the finitely many components of $$\phi^{-1}([a-\delta,a+\delta])$$. We index from left to right as $$\{p_j\}_{j \in [1,...,N_+]}$$ the restrictions of $$\phi$$ to $$\phi^{-1}([a,a+\delta])$$ and analogously as $$\{m_j\}_{j \in [1,...,N_-]}$$ those to $$\phi^{-1}([a-\delta,a])$$. By our hipotesis on the level sets, we have $$\lim_{\varepsilon \to 0^+} \sum_j \frac{\mathrm{e}^{i\theta\big(p_j^{-1}(a+\varepsilon)\big )}}{|\phi'(p_j^{-1}(a+\varepsilon))|} = \lim_{\varepsilon \to 0^-} \sum_j \frac{\mathrm{e}^{i\theta\big(m_j^{-1}(a+\varepsilon)\big )}}{|\phi'(m_j^{-1}(a+\varepsilon))|}=0.$$ Without loss of generality we can assume $$\phi'(0)>0$$. Observing that in the limit contributions from the restrictions to intervals in the interior of $$[0,2\pi]$$ are equal in the two sums, we have no other choice than $$\phi(0)=\phi(2\pi)$$. Now, if also $$\phi'(2\pi)>0$$, for $$0< \varepsilon <\delta$$, by our condition, we have \begin{align*} \frac{\mathrm{e}^{i\theta\big(p_1^{-1}(a+\varepsilon)\big )}}{\phi'(p_1^{-1}(a+\varepsilon))}&=- \; \sum_{1 The sums on the right side of the equations are $$C^h$$ and equal in the limit of all their derivatives, which proves that also the derivatives at extreme points must be equal. If $$\phi'(0)\phi'(2\pi)<0$$ a similar argument shows that the derivatives are equal up to alternating sign.

If $$\phi(0)$$ is a critical value I have been trying hard without success and I kind of feel I am missing something obvious. Does anyone have a proof at least for $$\theta$$ and $$\phi$$ analytic functions? Assuming the periodicity of $$\text{e}^{i\theta}$$ by hipotesis would also be fine for my purposes (but less beautiful).

An answer in the positive would have nice geometrical implications as you can read at Given $\theta$, find $f$ such that $\int_{\mathbb{T}} \text{e}^{i\theta} \cos(h \cdot f) = 0,$ for all $h \in \mathbb{N}$. Thanks.