#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ wait(int *);
int main() {
  int i;
  int status;
  pid_t pid;
  for (i = 0; i < 100000; i++) {
    pid = fork();
    if (pid == 0) {
      exit(EXIT_SUCCESS);
    } else if (pid < 0) {
      perror("fork");
      exit(EXIT_FAILURE);
    }
    wait(&status);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x186a0,-0x8(%rbp)
jge    11c8 <main+0x88>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1187 <main+0x47>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jge    11ac <main+0x6c>
lea    0xe67(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
jmp    11b1 <main+0x71>
lea    -0xc(%rbp),%rdi
call   1050 <wait@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1156 <main+0x16>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x186a0,%ebp
lea    0x4(%rsp),%rbx
nopl   (%rax)
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     1174 <main+0x34>
js     1182 <main+0x42>
mov    %rbx,%rdi
call   1050 <wait@plt>
add    $0xffffffff,%ebp
jne    1150 <main+0x10>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
lea    0xe77(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %2
br label %5
5: ; preds = %25, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 100000
br i1 %7, label %8, label %28
8: ; preds = %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @EXIT_SUCCESS
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = load i64, i64* %4
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %15
br label %23
23: ; preds = %22
%24 = call i32 @wait(i32* %3)
br label %25
25: ; preds = %23
%26 = load i32, i32* %2
%27 = add nsw i32 %26, 1
store i32 %27, i32* %2
br label %5
28: ; preds = %5
ret i32 0
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
br label %3
3: ; preds = %0, %16
%4 = phi i32 [ 0, %0 ], [ %18, %16 ]
%5 = call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @EXIT_SUCCESS
%9 = call i32 @exit(i32 %8)
unreachable
10: ; preds = %3
%11 = icmp slt i64 %5, 0
br i1 %11, label %12, label %16
12: ; preds = %10
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %10
%17 = call i32 @wait(i32* %1)
%18 = add nuw nsw i32 %4, 1
%19 = icmp eq i32 %18, 100000
br i1 %19, label %20, label %3
20: ; preds = %16
ret i32 0
}