First-and second-order optimality conditions for piecewise smooth objective functions