An inverse theorem for the Gowers U^3 norm