Motivating the Legendre Transform Mathematically If I  begin with a functional of the form
$$J[y] = \smallint_a^b f(x,y,y')dx$$
& find it's Euler-Lagrange equations
$$\tfrac{\partial f}{\partial y} - \tfrac{d}{dx}\tfrac{\partial f}{\partial y'} = 0 =  \tfrac{d}{dx}\tfrac{\partial f}{\partial y'} - \tfrac{\partial f}{\partial y}$$
I end up with a second order ODE
$$\tfrac{d}{dx}\tfrac{\partial f}{\partial y'} - \tfrac{\partial f}{\partial y} =  ( \tfrac{\partial ^2 f}{\partial y' \partial y'} ) \tfrac{d^2 y}{dx^2} + (\tfrac{\partial ^2 f}{\partial y \partial y'}) \tfrac{dy}{dx}  + \tfrac{\partial ^2 f}{\partial x \partial y'}   -  \tfrac{\partial f}{\partial y} = 0$$
Now every higher order ode can be broken up into a system of first order ode's
$$M = \tfrac{dy}{dx}$$
$$ \tfrac{d}{dx}\tfrac{\partial f}{\partial y'} - \tfrac{\partial f}{\partial y} =  ( \tfrac{\partial ^2 f}{\partial y' \partial y'} ) \tfrac{d M}{dx} + (\tfrac{\partial ^2 f}{\partial y \partial y'})M  + \tfrac{\partial ^2 f}{\partial x \partial y'}   -  \tfrac{\partial f}{\partial y} = 0 $$
From this perspective, Hamilton's equations
$$ \tfrac{dy}{dx} = \tfrac{\partial \mathcal{H}}{\partial p} $$ 
$$ \tfrac{d p}{dx} = - \tfrac{\partial \mathcal{H}}{\partial y} $$
are merely a system of first order equations making my above system of first order ode's look more symmetric, after a suitable change of variables.
My question is, looking at 
$$\tfrac{d}{dx}\tfrac{\partial f}{\partial y'} - \tfrac{\partial f}{\partial y} =  ( \tfrac{\partial ^2 f}{\partial y' \partial y'} ) \tfrac{d^2 y}{dx^2} + (\tfrac{\partial ^2 f}{\partial y \partial y'}) \tfrac{dy}{dx}  + \tfrac{\partial ^2 f}{\partial x \partial y'}   -  \tfrac{\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!
 A: Let me know if your definition of the Legendre transform is not this:
Let $M$ be a manifold with coordinates $y^1,\dots, y^n$ and $TM$ the tangent bundle with induced coordinates $y^1,\dots,y^n,(y^1)^\prime,\dots, (y^n)^\prime$.  Let $y^1,\dots,y^n,\eta_1,\dots,\eta_n$ denote the coordinates on $T^\ast M$. I will denote $y=(y^1,\dots, y^n)$ and $y^\prime=((y^1)^\prime,\dots, (y^n)^\prime)$. Given a `Lagrangian' $f\in C^\infty(TM)$ we have the Legendre transform assocoiated to $f$ which is $\Phi_f:T^\ast M\to \mathbb{R}$ defined by $$\Phi_f(y,\xi):=\sup\{\xi\cdot v-f(y,v); v\in T_y M\}$$
In your case, the Lagrangian $f$ is also dependent on $x$, that is $\ $  $f=f(x,y,y^\prime)$.
In the case that $f$ is strictly convex (i.e. $\frac{\partial ^2f}{\partial y^\prime\partial y^\prime}$=Hess$(f)$ is positive definite), which it is in most practical applications, we can define a new coordinate system on the cotangent bundle by setting $\xi_i=\frac{\partial f}{\partial (y^i)^\prime}$. This is just the inverse function theorem. That is, our cotangent bundle has coordinates $(y,\xi)$.
Also, again assuming $f$ is strictly convex, you can show that $$\Phi_f(y,\rho)=\rho\cdot \left((\frac{\partial f}{\partial y^\prime})^{-1}(\rho)\right)-f\left(y,\left(\frac{\partial f}{\partial y^\prime}\right)^{-1}(\rho)\right).$$Note that the function $\frac{\partial f}{\partial y^\prime}:TM\to T^\ast M$ is a diffeomorphism (this requires some work but is true under the hypothesis). The involutivity of the Legendre transform gives you in particular that $$\Phi_f(y,\xi)=\xi\cdot y^\prime-f(x,y,y^\prime).$$ What I am calling $\xi$ you called $p$. we set our Hamiltonian to be the Legendre transform of the Lagrangian, that is $$H=\xi\cdot y^\prime-f(x,y,y^\prime)=\frac{\partial f}{\partial y^\prime}\cdot y^\prime-f(x,y,y^\prime)$$
Now to your question. You're asking where the Legendre transform pops up in $$\frac{\partial}{\partial y^\prime}\frac{\partial f}{\partial y^\prime}\left(\frac{\partial}{\partial x}y^\prime\right)+\left(\frac{\partial}{\partial y}\frac{\partial f}{\partial y^\prime}\right)y^\prime+\frac{\partial f}{\partial x\partial y^\prime}-\frac{\partial f}{\partial y}$$
We have that
$$\frac{\partial}{\partial y^\prime}\frac{\partial f}{\partial y^\prime}\left(\frac{\partial}{\partial x}y^\prime\right)+\left(\frac{\partial}{\partial y}\frac{\partial f}{\partial y^\prime}\right)y^\prime+\frac{\partial f}{\partial x\partial y^\prime}-\frac{\partial f}{\partial y}$$
$$=\frac{\partial}{\partial y^\prime}\frac{\partial f}{\partial y^\prime}\left(\frac{\partial}{\partial x}y^\prime\right)+\frac{\partial f}{\partial x\partial y^\prime}+\frac{\partial}{\partial y}\left(\frac{\partial f}{\partial y^\prime}y^\prime-f\right)$$
$$=\frac{\partial}{\partial y^\prime}\frac{\partial f}{\partial y^\prime}\left(\frac{\partial}{\partial x}y^\prime\right)+\frac{\partial f}{\partial x\partial y^\prime}+\frac{\partial H}{\partial y}$$
$$=\frac{\partial}{\partial y^\prime}\frac{\partial f}{\partial y^\prime}\left(\frac{\partial}{\partial x}y^\prime\right)+\frac{\partial f}{\partial x\partial y^\prime}-\frac{d\xi}{d x} \text{ by Hamilton's equations}$$ 
$$=\frac{\partial}{\partial y^\prime}\frac{\partial f}{\partial y^\prime}\left(\frac{\partial}{\partial x}y^\prime\right)+\frac{\partial\xi}{\partial x}-\frac{d\xi}{d x}$$
$$=\frac{\partial}{\partial y^\prime}\frac{\partial f}{\partial y^\prime}\left(\frac{\partial}{\partial x}y^\prime\right)+\frac{\partial\xi}{\partial x}-\frac{\partial \xi}{\partial y}\frac{\partial y}{\partial x}-\frac{\partial \xi}{\partial y^\prime}\frac{\partial y^\prime}{\partial x}$$
$$=\frac{\partial \xi}{\partial y^\prime}\frac{\partial y^\prime}{\partial x}+\frac{\partial\xi}{\partial x}-\frac{\partial \xi}{\partial y}\frac{\partial y}{\partial x}-\frac{\partial \xi}{\partial y^\prime}\frac{\partial y^\prime}{\partial x}$$
