#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_SUCCESS;
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, int, scalar_t__);
int /*<<< orphan*/ sleep(int);
int main(void) {
  pid_t pid;
  int i;
  for (i = 0; i < 20000; i++) {
    if ((pid = fork()) < 0) {
      err_sys("fork error");
    } else if (pid == 0) {
      sleep(20);
      exit(EXIT_SUCCESS);
    }
    printf("Created child  number %d with pid %d\n", i, pid);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x4e20,-0x14(%rbp)
jge    11e2 <main+0x92>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1199 <main+0x49>
lea    0xe71(%rip),%rdi
call   1030 <err_sys@plt>
jmp    11c1 <main+0x71>
cmpq   $0x0,-0x10(%rbp)
jne    11bc <main+0x6c>
mov    $0x14,%edi
call   1060 <sleep@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    11c1 <main+0x71>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1166 <main+0x16>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xea2(%rip),%r14
lea    0xea6(%rip),%r15
xor    %ebp,%ebp
jmp    1190 <main+0x40>
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <err_sys@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
add    $0x1,%ebp
cmp    $0x4e20,%ebp
je     11ab <main+0x5b>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     1170 <main+0x20>
jne    1178 <main+0x28>
mov    $0x14,%edi
call   1060 <sleep@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [38 x i8] c"Created child number %d with pid %d\0A\00"
define i32 @main() {
br label %1
1: ; preds = %0, %13
%2 = phi i32 [ 0, %0 ], [ %15, %13 ]
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %13
7: ; preds = %1
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = tail call i32 @sleep(i32 20)
%11 = load i32, i32* @EXIT_SUCCESS
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %7, %5
%14 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %2, i64 %3)
%15 = add nuw nsw i32 %2, 1
%16 = icmp eq i32 %15, 20000
br i1 %16, label %17, label %1
17: ; preds = %13
%18 = load i32, i32* @EXIT_SUCCESS
%19 = tail call i32 @exit(i32 %18)
unreachable
}