Nonsmooth optimal regulation and discontinuous stabilization