Found this in a code base I inherited:
for (size_t i = 0; i < strlen(s); i++) {
if (s[i] == ' ')
s[i] = '_';
}
It is quadratic: strlen walks the whole string on every iteration. "But the compiler hoists it," says someone every single time. Sometimes. glibc declares strlen as a pure function, so GCC can lift the call out of the loop if it can prove nothing in the loop changes the string. Here the loop writes through s, and a char store may alias anything, so the compiler has to be conservative.
Fix:
for (char *p = s; *p; p++)
if (*p == ' ') *p = '_';
One pass, no length at all. The string tells you where it ends, that is the whole design.
On a related note of cheerful despair: with a char * whose terminator someone forgot, strlen will happily continue into whatever lies after your buffer. Not a bug in strlen. A bug in all of us.