ss can be some kind of global variable, because you can call foo with some kind of global array, for example char str[100]; as your argument (e.g. having foo(str); in your main ) ...
and bar can change this global variable (then strlen(ss) can change in every loop).
BTW restrict may not mean you believe. Carefully read the section §6.7.3 of standard C11 and §6.7.3.1 . IMHO restrict in practice is mostly useful for two formal arguments of the same function, to express the fact that they are not “aliases” or “overlapping” pointers (if you assume that I really mean it) and maybe optimization efforts at restrict probably focused on such cases.
It is possible (but unlikely) that in your particular program the compiler can be optimized as you wish if you call it as gcc -flto -fwhole-program -O3 (for each translation unit and for the duration of the connection to the program). I will not bet on this (but I leave you to check).
Why is this the case?
As for the current GCC (or Clang ) is not optimized as you want it, because no one wrote such an optimization skip and included it in -O3 .
Compilers are not required to perform optimization, it is just allowed to perform some of them (at the choice of their developers).
Since this is free software, feel free to offer a patch contribution to GCC (or to Clang). You may need a whole year of work, and you are not sure that your optimization will be accepted (because in practice there are no codes as you show, or because your optimization will be too specific, so it is unlikely to be launched, but will slow down the compiler anyway). But you can try.
Even if §6.7.3.1 allows optimization (as the answer by the user 2357112 demonstrates), it would hardly be worth the effort to implement it.
(my intuition is that the implementation of such optimization is difficult and will not bring much results to existing programs)
By the way, you can definitely experiment with this optimization by encoding some GCC plugin by doing this (since the plugin framework was designed for such experiments). You may find that such optimization requires a lot of work and practically does not improve the performance of most existing programs (for example, in the Linux distribution), because few people code it.
Both GCC and Clang are free software projects, and their participants are (from the point of view, for example, FSF) volunteers. So, feel free to improve GCC (or Clang) as you want it to optimize . From past experience, introducing a small piece of code into GCC is time consuming. And GCC is a huge program (about ten million lines of code), so understanding its internal components is not easy.