Liverpool produced a clinical first-half performance to score three set-piece goals in a chaotic 5-2 win over relegation-fighting West Ham.