Construct $f$ satisfying certain given conditions 
Given $\textbf x\in \mathbb R^n$ and $r>0$, construct a function $f:\mathbb R^n\to [0,1]$ of class $C^\infty$ such that
$$f^{-1}(1)=\overline{B(\textbf{x},r/2)}\\f^{-1}(0)=\mathbb R^n\setminus B(\textbf{x},r)$$

Being helpless about the original question (apart from the geometrical realization that it looks like a peninsula), I was trying the $n=1$ variant out. This translates to

Given $a<b$, find a function $g:\mathbb R\to [0,1]$ of class $C^\infty$ such that
$$g|_{(-\infty,a]}\equiv1\\g|_{[b,\infty)}\equiv0$$

Even this one seems quite out of my reach. The first idea I had was to use the Sigmoid function. But, that never gives a zero derivative except at $x\to \pm \infty$.
So, my next idea was to use the $C^\infty$ property of the function $y=e^{-\frac 1x}$ and construct $g$ locally at $x=a$ using the function
$$\alpha(x)=1-e^{-\frac 1{x-a}}$$
and locally at $x=b$ using the function
$$\beta(x)=e^{\frac 1{x-b}}$$
But, I couldn't proceed with this as well.
 A: Consider the following function
$ 
h(t)=
\begin{cases}
e^{-\frac{1}{t}} ~~~ \text{if}~~~t>0\\
0 ~~~~~~ \text{if}~~t\leq 0
\end{cases}
$
It is evident that h is smooth $\forall ~ x \neq 0$. To show that it is also smooth at $x=0$ one needs to show that the right-hand derivative vanishes (because the left-hand derivative vanishes) for all orders. This follows from the fact that the given exponential function vanishes quickly than any polynomial in $x$ - a fact that can be checked using basic exponential inequalities.
Now define the function
$
g(t)= \frac{h(b-t)}{h(b-t)+h(t-a)}.
$
For $t\geq b$, $h(b-t)=0$ and for $t\leq a$, $h(t-a)=0$. This implies
$
g(t) = 
\begin{cases}
1 ~~~\text{if } t\leq a\\
0 ~~~\text{if } t\geq b
\end{cases}
$
Notice that the denominator of $g$, namely $h(b-t)+h(t-a)$ - never vanishes. This implies that $g$ is smooth by quotient rule.
One should also check that $g$ is not unity on $(a,b)$.
Now consider the case $n=1$. We first construct a function $f_1:\mathbb{R} \to [0,1]$ such that $f_1^{-1}(1)= \overline{B(x,\frac{r}{2})}$ and
$f^{-1}(0)=\mathbb{R} \backslash B(\mathbf{x}, r)$. By the above construction one can easily construct smooth $ \tilde{g}_1,\tilde{g}_2:\mathbb{R}\to [0,1] $ such that
$
\tilde{g}_1(t) = 
\begin{cases}
1 ~~~\text{if } t\leq x+r/2\\
0 ~~~\text{if } t\geq x+r
\end{cases}
$
$
\tilde{g}_2(t) = 
\begin{cases}
1 ~~~\text{if } t\leq x-r\\
0 ~~~\text{if } t\geq x-r/2
\end{cases}
$
If we define $f_1 = \tilde{g}_1 \cdot \tilde{g}_2$, then one has $f_1^{-1}(1)= \overline{B(x,\frac{r}{2})}$ and
$f_1^{-1}(0)=\mathbb{R} \backslash B(\mathbf{x}, r)$.
Now try to construct a $f$ on $\mathbb{R}^n$.
