Earlier quoted context omitted.
For me the strlen call appears directly before loops backwards jump when set to -O0, resulting in a call every iteration as far as I can tell. However -O1 already seems to optimize it to a single call at the start of the function.
I find it every time funny that when using languages that want to give you "total control" over the execution of your code (mostly C/C++) you actually almost never know what code gets executed in the end. It depends on the compiler, it's version, it's flags, and likely "the position of the moon". Of course the compiler is only allowed to do transformations that the spec permits. But it's impossible for a human being…
When the compiler is optimizing it might very well realize that the parameter to strlen doesn't change and the output can be saved first.
If the intent is to compute the length only once, then we can save the length in a variable.
Of course; as others have pointed out; there is no need to call strlen at all in this example.