#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 main() {
  int pid, i, successes = 0, failures = 0;
  for (i = 0; i < 100; 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);
  printf("parent process exiting now\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x64,-0xc(%rbp)
jge    11a5 <main+0x85>
mov    $0x0,%al
call   1040 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1170 <main+0x50>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1192 <main+0x72>
cmpl   $0x0,-0x18(%rbp)
jne    1184 <main+0x64>
jmp    117f <main+0x5f>
jmp    117f <main+0x5f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1192 <main+0x72>
jmp    1197 <main+0x77>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1144 <main+0x24>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x64,%ebx
xor    %r14d,%r14d
xor    %ebp,%ebp
jmp    1149 <main+0x19>
add    $0x1,%r14d
add    $0xffffffff,%ebx
je     1161 <main+0x31>
xor    %eax,%eax
call   1050 <fork@plt>
cmp    $0xffffffff,%eax
je     1140 <main+0x10>
test   %eax,%eax
je     1190 <main+0x60>
add    $0x1,%ebp
add    $0xffffffff,%ebx
jne    1149 <main+0x19>
lea    0xe98(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
nopw   0x0(%rax,%rax,1)
jmp    1190 <main+0x60>

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

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