#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ do_nothing();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ waitpid(int, int, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int pid, j, status;
  for (j = 0; j < 50000; j++) {
    if ((pid = fork()) < 0) {
      printf("fork failed with error code= %d\n", pid);
      exit(0);
    } else if (pid == 0) {
      do_nothing();
      exit(0);
    } else {
      waitpid(pid, status, 0);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0xc350,-0x18(%rbp)
jge    11e0 <main+0x90>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11a3 <main+0x53>
mov    -0x14(%rbp),%esi
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11bb <main+0x6b>
mov    $0x0,%al
call   1040 <do_nothing@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    11cd <main+0x7d>
jmp    11d2 <main+0x82>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    116d <main+0x1d>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0xc350,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
js     117f <main+0x2f>
je     1194 <main+0x44>
mov    %eax,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
add    $0xffffffff,%ebx
jne    1160 <main+0x10>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe7a(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
xor    %eax,%eax
call   1040 <do_nothing@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"fork failed with error code= %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
br label %9
9: ; preds = %31, %2
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 50000
br i1 %11, label %12, label %34
12: ; preds = %9
%13 = call i32 (...) @fork()
store i32 %13, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* %6
%17 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %12
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i32 (...) @do_nothing()
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %19
%26 = load i32, i32* %6
%27 = load i32, i32* %8
%28 = call i32 @waitpid(i32 %26, i32 %27, i32 0)
br label %29
29: ; preds = %25
br label %30
30: ; preds = %29
br label %31
31: ; preds = %30
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %9
34: ; preds = %9
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [33 x i8] c"fork failed with error code= %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
br label %3
3: ; preds = %2, %15
%4 = phi i32 [ 0, %2 ], [ %17, %15 ]
%5 = tail call i32 (...) @fork()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %5)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %3
%11 = icmp eq i32 %5, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = tail call i32 (...) @do_nothing()
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %10
%16 = tail call i32 @waitpid(i32 %5, i32 undef, i32 0)
%17 = add nuw nsw i32 %4, 1
%18 = icmp eq i32 %17, 50000
br i1 %18, label %19, label %3
19: ; preds = %15
ret i32 0
}