Do derivative and Integral really cancel out so easily here? In a paper I came across the following equation where the loss function $\mathcal{L}$ must be minimized with respect to the reconstruction function $r_\sigma$.
With  $\bar{x} := x + \eta \quad \eta \sim \mathcal{N}(\sigma^2I)$:
$$
\begin{align*}
\mathcal{L}_{\textrm{DAE}}(\theta) &= 
\mathbb{E}_{f(\eta, x)} \left[ \|x - r_{\sigma}(\bar{x}) \|^2_2  \right]  \\
 &= \int_{\mathbb{R}^d} \int_{\mathbb{R}^d} p(\bar{x}-\eta)p(\eta|\bar{x}) \|r_{\sigma}(\bar{x}) - \bar{x} + \eta  \|^2_2 \;  d\eta d\bar{x} \\
 &= \int_{\mathbb{R}^d} \int_{\mathbb{R}^d} p(\bar{x}-\eta)p(\eta) \|r_{\sigma}(\bar{x}) - \bar{x} + \eta  \|^2_2  \; d\eta d\bar{x} \\
&=  \int_{\mathbb{R}^d} \mathbb{E}_{\eta} \left[ p(\bar{x}-\eta) \left\|r_{\sigma}(\bar{x}) - \bar{x} + \eta \right \|^2_2 \right] d\bar{x} \quad (1) 
\end{align*}
$$
We minimize this equation by differentiating it with respect to $r_{\sigma}(\bar{x})$ and setting it equal to $0$. Let $r_\sigma^*(\bar{x})$ denote the the minimum:
$$
\begin{align*}
0 &= \mathbb{E}_{\eta } \left[ p(\bar{x}-\eta) \left(r_\sigma^*(\bar{x}) - \bar{x} + \eta \right) \right] \quad(2)
\end{align*} \\
...
$$
My problem is the step between (1) and (2).
Although it looks correct, I failed to do this step more formally.
Any help is much appreciated!
It is the first part of the proof 6.1 from https://arxiv.org/pdf/1211.4246.pdf
Edit
Thanks to Milo Brandt's answer I now understand why the integral vanishes. However I am very unsure about the correctness of the  following steps:
Lets fix $\bar{x}$ and try to find a $r$ which minimizes $\mathbb{E}_{\eta} \left[ p(\bar{x}-\eta) \left\|r_{\sigma}(\bar{x}) - \bar{x} + \eta \right \|^2_2 \right] $ for this arbitrary $\bar{x}$. To do so we take the derivativ with respect $r$ and get the minima $r^*$ by setting it to 0.
$$
\begin{align}
0=&\frac{d}{dr}  \mathbb{E}_{\eta} \left[ p(\bar{x}-\eta) \left\|r_{\sigma}^*(\bar{x}) - \bar{x} + \eta \right \|^2_2 \right] \qquad (3) \\
0= &  \mathbb{E}_{\eta} \left[ \frac{d}{dr}  p(\bar{x}-\eta) \left\|r_{\sigma}^*(\bar{x}) - \bar{x} + \eta \right \|^2_2 \right] \qquad (4)\\
0 &= \mathbb{E}_{\eta } \left[ p(\bar{x}-\eta) \left(r_\sigma^*(\bar{x}) - \bar{x} + \eta \right) \right] \quad(4)
\end{align}
$$
 A: This isn't really an integral and a derivative cancelling out, so much as an artifact of optimizing over a domain of functions - basically, it's a problem involving the calculus of variations.
Without worrying too much about formality, the problem looks something like:

Find a function $f$ minimizing $L(f) = \int g(x, f(x))\, dx$

And the claim being put forward is essentially that the authors can produce a function $f^*(x)$ with the property that $g(x,f^*(x)) \leq g(x,y)$ for every $x$ and $y$ - or, in words, that $f^*(x)$ minimizes $g(x,-)$ for each $x$. From this, it follows that $L(f^*) \leq L(f)$ for every other function $f$, since the integral on the left hand side is pointwise less than or equal to the integral on the right hand side.
However, this is not what the authors of that paper wrote - perhaps because they wanted to show a derivation of the minimum, rather than just pulling a function $f^*$ out of nowhere and saying "it's got this nice property so it must be the minimum" (although, this is indeed the shortest and perhaps clearest proof of their claim). The form of their argument is somewhat suggestive (though not formally using) of some results that look like a converse to this.
Essentially, if you want to minimize $L(f)$, a good bet is to think about what happens if you slightly perturb $f$ - since the minimum, of course, should not get smaller due to perturbation (or due to anything else, of course!). Informally speaking, if you had some other candidate function $h$, one would expect, as a linear approximation,
$$L(f+\varepsilon h)\approx L(f)+\varepsilon\int g_y(x,f(x))\cdot h(x)$$
where $g_y$ is the derivative of $g$ with respect to its second argument. This is often rigorously true with derivatives, but all the usual analytical details pop up when you try to prove it. In any case, you can see from this that, for $f$ to be a minimum, it really has to be true that
$$\int g_y(x,f(x)) \cdot h(x)=0$$
for every function $h$ by which we could perturb $f$. As long as the domain of $L$ is not too constrained and $g$ is not too badly behaved, this just means that
$$g_y(x,f(x)) = 0$$
for every $x$, since otherwise you'd just choose $h$ to be negative where $g_y(x,f(x))$ is positive and positive where $g_y(x,f(x))$ is negative, and find out that perturbing $f$ by that $h$ would decrease $L$ - ruling out $f$ as a minimum. This is why the integral disappears - because we can choose to focus only on any part of the domain by choosing a perturbation wisely - not for any special reason about derivatives and integrals being inverse.
This is more or less analogous to techniques for finding critical points in finitely many dimensions. If you wanted to minimize $f_1(x_1)+f_2(x_2)+f_3(x_3)$ over tuples $(x_1,x_2,x_3)$, you would just minimize $f_1(x_1)$ and $f_2(x_2)$ and $f_3(x_3)$ separately. What the authors do here is the same, replacing a sum over a finite set with an integral over a continuous domain.
