#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 fork();
int /*<<< orphan*/ printf(char *, int, int);
int main() {
  int successes = 0, failures = 0;
  for (int i = 0; i < 1000; i++) {
    int pid = fork();
    if (pid == -1) {
      failures += 1;
    } else if (pid == 0) {
      while (1) {
      };
    } else {
      successes += 1;
    }
  }
  printf("%d forks succeeded, %d failed\n", successes, failures);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x3e8,-0x10(%rbp)
jge    11a8 <main+0x88>
mov    $0x0,%al
call   1040 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1173 <main+0x53>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1195 <main+0x75>
cmpl   $0x0,-0x14(%rbp)
jne    1187 <main+0x67>
jmp    1182 <main+0x62>
jmp    1182 <main+0x62>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1195 <main+0x75>
jmp    119a <main+0x7a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1144 <main+0x24>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe4b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x3e8,%ebx
xor    %r14d,%r14d
xor    %ebp,%ebp
jmp    1138 <main+0x18>
add    $0x1,%ebp
add    $0xffffffff,%ebx
je     1151 <main+0x31>
xor    %eax,%eax
call   1040 <fork@plt>
cmp    $0xffffffff,%eax
je     1130 <main+0x10>
test   %eax,%eax
je     1170 <main+0x50>
add    $0x1,%r14d
add    $0xffffffff,%ebx
jne    1138 <main+0x18>
lea    0xea8(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
nopl   0x0(%rax)
jmp    1170 <main+0x50>

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

LLVM IR (-O2):
@.str = [31 x i8] c"%d forks succeeded, %d failed\0A\00"
define i32 @main() {
br label %3
1: ; preds = %13
%2 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %14)
ret i32 0
3: ; preds = %0, %13
%4 = phi i32 [ 0, %0 ], [ %15, %13 ]
%5 = phi i32 [ 0, %0 ], [ %16, %13 ]
%6 = phi i32 [ 0, %0 ], [ %14, %13 ]
%7 = tail call i32 (...) @fork()
switch i32 %7, label %11 [
i32 -1, label %8
i32 0, label %10
]
8: ; preds = %3
%9 = add nsw i32 %6, 1
br label %13
10: ; preds = %3, %10
br label %10
11: ; preds = %3
%12 = add nsw i32 %4, 1
br label %13
13: ; preds = %11, %8
%14 = phi i32 [ %9, %8 ], [ %6, %11 ]
%15 = phi i32 [ %4, %8 ], [ %12, %11 ]
%16 = add nuw nsw i32 %5, 1
%17 = icmp eq i32 %16, 1000
br i1 %17, label %1, label %3
}