#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int main(void) {
  pid_t pid;
  int status;
  int i;
  for (i = 0; i < 1000; i++) {
    pid = fork();
    switch (pid) {
    case -1:
      if (errno == EAGAIN)
        break;
      perror("fork()");
      exit(EXIT_FAILURE);
    case 0:
      exit(EXIT_SUCCESS);
    default:
      break;
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x3e8,-0x10(%rbp)
jge    11de <main+0xae>
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0xffffffff,%eax
je     1181 <main+0x51>
jmp    1171 <main+0x41>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11b8 <main+0x88>
jmp    11c6 <main+0x96>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%eax
jne    119e <main+0x6e>
jmp    11cb <main+0x9b>
lea    0xe5b(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
jmp    11cb <main+0x9b>
jmp    11d0 <main+0xa0>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1146 <main+0x16>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    $0x3e8,%ebx
mov    0x2e9f(%rip),%r14
mov    0x2e78(%rip),%r15
jmp    115d <main+0x2d>
nopw   0x0(%rax,%rax,1)
mov    (%r14),%eax
cmp    (%r15),%eax
jne    117b <main+0x4b>
add    $0xffffffff,%ebx
je     116d <main+0x3d>
xor    %eax,%eax
call   1050 <fork@plt>
cmp    $0xffffffff,%eax
je     1150 <main+0x20>
test   %eax,%eax
jne    1158 <main+0x28>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
lea    0xe7e(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

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

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