If I begin with a functional of the form
$$J[y] = \int_a^b f(x,y,y')dx$$
and find its Euler-Lagrange equations
$$\frac{\partial f}{\partial y} - \frac{d}{dx}\frac{\partial f}{\partial y'} = 0 = \frac{d}{dx}\frac{\partial f}{\partial y'} - \frac{\partial f}{\partial y}.$$
I end up with a second order ODE
$$\frac{d}{dx}\frac{\partial f}{\partial y'} - \frac{\partial f}{\partial y} = \left( \frac{\partial ^2 f}{\partial y' \partial y'} \right) \frac{d^2 y}{dx^2} + \left(\frac{\partial ^2 f}{\partial y \partial y'}\right) \frac{dy}{dx} + \frac{\partial ^2 f}{\partial x \partial y'} - \frac{\partial f}{\partial y} = 0$$
Now every higher order ODE can be broken up into a system of first order ODEs in $y$ and the derivative $M = \frac{dy}{dx}$, giving
$$ \frac{d}{dx}\frac{\partial f}{\partial y'} - \frac{\partial f}{\partial y} = \left( \frac{\partial ^2 f}{\partial y' \partial y'} \right) \frac{d M}{dx} + \left(\frac{\partial ^2 f}{\partial y \partial y'}\right)M + \frac{\partial ^2 f}{\partial x \partial y'} - \frac{\partial f}{\partial y} = 0. $$
From this perspective, Hamilton's equations
$$\left\{ \begin{array} & \frac{dy}{dx} = \phantom{-}\frac{\partial \mathcal{H}}{\partial p}\\ \frac{d p}{dx} = - \frac{\partial \mathcal{H}}{\partial y} \end{array}\right.$$
are merely a system of first order equations making my above system of first order ODEs look more symmetric, after a suitable change of variables.
My question is, looking at
$$\frac{d}{dx}\frac{\partial f}{\partial y'} - \frac{\partial f}{\partial y} = \left( \frac{\partial ^2 f}{\partial y' \partial y'} \right) \frac{d^2 y}{dx^2} + \left(\frac{\partial ^2 f}{\partial y \partial y'}\right) \frac{dy}{dx} + \frac{\partial ^2 f}{\partial x \partial y'} - \frac{\partial f}{\partial y} = 0$$
it should be possible to see why the Legendre transformation arises, first because its a transformation using derivatives to change variables but also because it should just make some terms go to zero in this second order ode so that everything looks nicer, but how do you see this explicitly?
It'd be great if you could use my notation, ie. $J[y]$ etc... as you see I snuck in $\mathcal{H}$ in above which shouldn't really be there, I'd love to see how that comes about in my notation - thanks!