Pressure-dependence of electrolysis voltage
Of course, we're starting by acknowledging a perpetual motion machine is impossible.  The question, then, is how do we understand, through chemical thermodynamics, why your specific set of steps can't constitute a perpetual motion machine.  

The answer is that what you're not accounting for is that the electrical energy required for your step 2 is greater than you get back from your step 4, even if we don't have losses.  That's because, everything else being equal, the work needed to create gases against a high pressure is greater than that needed to create gases against a low pressure. I.e., $|w_2| &gt; |w_4|$.  

To understand why, let's simplify things by considering the pressure-dependence of the free energy of a pure substance at constant T: dG = VdP.  In step 2, because the pressure is so, high, you have increased the free energies of both the reactants and the products relative to what they'd be at atmospheric pressure.  To get the actual changes, you would need to calculate $\int^{P_f}_{P_i}V(P) dP$ for the reactants and products.  Since the products are gases (oxygen and hydrogen), and the volume of gases is much larger than that of liquids, the free energies of the products would be raised far more than that of the reactant (a liquid).  Thus the separation between the free energies of the reactants and products, (i.e.,  the magnitude of $\Delta G_r$) for step 2 (which is at high pressure), will be substantially greater than it is for step 4 (which is at low pressure).

You could also understand it using Le Chatelier's principle:  The volume of the products is higher than that of the reactants, and thus higher pressure will favor the reactants over the products.