fork
is a normal function just like other library functions it invokes glibc fork()
wrapper rather than invoking system call directly.
A compiler has no way to determine what does this function contain and a conformant compiler should always avoid optimizing this loop away and this would result in fork bomb as referred in one of the comment.
To avoid the consequences, one should avoid the maximum number of processes a user can own.
From man fork
Since version 2.3.3, rather than invoking the kernel's fork() system call, the glibc fork() wrapper that is provided as part
of the NPTL threading implementation invokes clone(2) with flags
that provide the same effect as the traditional system call. The glibc wrapper invokes any fork handlers that have been established
using pthread_atfork(3).