I have a function defined as:
int f_2() {
rand();
return 10;
}
clang breaks it up into 3 basic blocks. This is understandable.
however when I replace the call to rand() by exit(0), then it breaks it into 4 basic blocks. Wikipedia (http://en.wikipedia.org/wiki/Basic_block) says that functions that cannot return can be at the end of a basic block.
How does clang know that exit() function does not return? I am compiling my code with clang -c.
clang
is a C compiler. It's permitted to take advantage of guarantees made by the language standard for standard library functions.
There may also be something in the particular implementation of <stdlib.h>
that marks exit()
as a function that doesn't return, perhaps using a language extension or the _Noreturn
keyword added by the 2011 ISO C standard.
Another example of this: the call sin(0.0)
, with -O1
or higher, compiles to a literal 0.0
, because the compiler knows about the sin
function. (Which means that a program that calls sin(0.0)
needs to be linked with -lm
only if you don't optimize it.)
This is all permitted because a program that defines its own (non-static
) function with the same name as a standard library function has undefined behavior; the compiler needn't consider the possibility that a call to exit
or sin
does anything other than what the standard specifies for those functions.