Answer first:
The `objective function' is in fact a functional. The task is to find a pdf $g(y)$ folowed by $c_2$ and a real number $c_1$ that maximise the following Lagrangian
$$ \mathcal{L}(c_1, g) = u(c_1) + \int u(x)g(x) \mathrm{d}x + \int \lambda(y) \left[ g(y) - \eta(c_1, y) \right] \mathrm{d}y. $$
Here, $\eta(c_1, y)$ is the pdf followed by the random variable $f(c_1, Z)$. $\lambda(y)$ is a Lagrange multiplier, used to enforce the constraint $g(y)=\eta(c_1,y)$. All integrals are over $\mathbb{R}$. The first order conditions are
$$ u'(c_1) - \frac{\partial}{\partial c_1} \int \lambda(y) \eta(c_1,y) \mathrm{d} y =0, \\
u(y) + \lambda(y) =0, \\
g(y) - \eta(c_1, y) = 0. \\
$$
Combining the first two gives
$$ u'(c_1) + \frac{\partial}{\partial c_1} \int u(y) \eta(c_1,y) \mathrm{d} y =0. $$
Now using the 'law of the unconscious statistician' gives
$$ u'(c_1) + \frac{\partial}{\partial c_1} \int u(f(c_1,z)) \phi(z) \mathrm{d} z =0 \\
\Rightarrow u'(c_1) + \int u'(f(c_1,z)) \frac{\partial f(c_1,z)}{\partial c_1} \phi(z) \mathrm{d} z =0 .$$
Here, $\phi(z)$ is the pdf followed by the random variable $Z$. This is equivalent to the solution given by OP.
Now I want to address a few other things. I would ideally do this in the comments, but I don't have enough reputation to comment.
First of all, I am OP. I really am at a loss as to why I couldn't log in with original credentials. Still trying to figure that one out.
As denesp points out, I wrote an answer not so long ago which I immediately deleted. I did this for a few reasons:
- I didn't actually answer the question in that post.
- I was on the bus at the time, typing on my phone. The formatting I had wasn't correct, and the wording was very loose. I wanted to take more time over it and give a better and more precise answer. So, for example, some of denesp's comments are directed at imprecise wording in a post I deleted.
To address denesp's comments on the original post: I commented briefly that things don't work because
- I thought it was very obvious why the suggested things don't work.
- I didn't think a detailed post explaining exactly why they don't work with lots of maths was appropriate for the comments
Alecos' 'answer' isn't an answer at all. It may better be read as an expansion upon why the naive approach in the original post doesn't work.
Now let me address the matter of differentiation. It's true that I wasn't precise in the original post, but this was to illustrate the naive approach I was taking and why it wasn't working.
The confusion here stems from the following. Say I have a differentiable function $f: \mathbb{R} \rightarrow \mathbb{R}$. I can certainly talk meaningfully about the function that is the derivative of $f$, that I will denote by $f'$.
On the other hand, sometimes people 'allow the argument to be a random, $\mathbb{R}$-valued variable'. When they do this, they should probably call the resulting random variables something other than $f$ and $f'$. All the confusion has arisen from this. I did not make this distinction in the original post, because my thinking on the question was not clear, and this has been confounded by other comments not recognising this base cause of the confusion.