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.
clangis 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 marksexit()as a function that doesn’t return, perhaps using a language extension or the_Noreturnkeyword added by the 2011 ISO C standard.Another example of this: the call
sin(0.0), with-O1or higher, compiles to a literal0.0, because the compiler knows about thesinfunction. (Which means that a program that callssin(0.0)needs to be linked with-lmonly 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 toexitorsindoes anything other than what the standard specifies for those functions.