Automatic differentiation of nonsmooth iterative algorithms