News for hackers. Stuff that matters.
Re:Programming Idioms by Someone on Sunday August 15, 2021 @05:57PM (#28192927)
Nitpick: I don’t think the compiler needs that __attribute__((pure)) annotation to move strlen around.

__builtin_strlen is a reserved name, so if the compiler sees it, it may assume it came from an #include <string.h> and that it does what the standard says strlen does.

That is what allows compilers to do much more than move that function call out of the loop. Because they know what it does (or rather: what the programmer promises it will do, once the program get linked) they can inline its code or even, if the length of the string is known up front, replace the function call by a constant.

Re:Programming Idioms by foxfluff on Sunday August 15, 2021 @06:26PM (#28193119)
It doesn't even need to see __builtin_strlen (try modify your string.h or use musl, which has neither the attribute nor __builtin_strlen). It just needs to see strlen in a hosted environment (with the right header included?). If you want gcc to treat it otherwise, you also need -fno-builtin-strlen.