TITLE: Supremum of a smooth function is smooth
QUESTION [5 upvotes]: I'm fairly sure that this is an easy question, but I can't quite prove my suspicions. 
For $f\in\mathbf{C}^\infty(\mathbf{R})$, let $g(t):=\sup_{t\leq x} f(x)$. Then $g\in\mathbf{C}^\infty(\mathbf{R})$.
I want to say that this is true, but I'm unable to fully justify it. In my mind, I'm effectively viewing $g$ as a smooth step function that always increases, moving from one hill of $f$ to the next higher hill of $f$ as $t$ increases. $g$ moves from that earlier hill to the next hill with $f$ (aka smoothly), so the only possible points where $g$ can be not smooth are the points $\{x \mid f(x)=\max_{t\leq x}{f(t)}, f'(x)=0, f''(x)<0\}$. These are the points where $g(x)$ stops following $f(x)$ and instead is the zero function until $f(x)$ "catches up" again, heading for the next higher hill.
However, when I try and construct a proof for why these points cannot be true for contradiction, I can't get anywhere. Can anyone give me a counterexample, or some hint for how to prove this?

REPLY [8 votes]: This is pretty clearly not true. Consider for example the map
$$f(x) = e^x\sin(x).$$
However, the statement becomes true if you replace $C^\infty$ by $C^0$ everywhere, or by piecewise $C^\infty$ (at least under the mild assumption that the set of critical points of $f(x)$ is discrete).

As remarked in the comments, this only makes sense when $f(x)$ is bounded above as $x\to-\infty$.
The reason for the "piecewise continuous" part is the following: We all have agreed that $g(t)$ is continuous. Let $t_0\in\mathbb{R}$, then we have three possible cases:

If $f(t_0)<\max_{x\le t_0}f(x)$, then by continuity of $f(x)$ we know that this remains true in a small neighborhood of $t$. Therefore, $g(t)$ is constant in said neighborhood, and in particular locally smooth.
If $f(t_0)=\max_{x\le t_0}f(x)$ and it is either a local maximum, or $f(t_0)=g(t_0)$ and for all $\epsilon>0$ small enough we have that $f(t-\epsilon)<g(t-\epsilon)$ (i.e. a point where $f$ "joins up" with $g$), then the $g(t)$ might be non-smooth at $t_0$. Notice that these points must be isolated (or we could have a piece where $f$ is maximal but constant, which gives us a smooth piece and thus no problems).
The remaining case is when $f(t) = g(t)$ in a small neighborhood of $t_0$. Here, $g(t)$ is of course automatically smooth as $f(x)$ is.