#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 /*<<< orphan*/ childproc();
int fork();
int /*<<< orphan*/ printf(char *);
void main() {
  int i, cpid;
  for (i = 0; i < 5; i++) {
    cpid = fork();
    if (cpid < 0)
      printf("fork failed\n");
    else if (cpid == 0)
      childproc();
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
cmpl   $0x5,-0x4(%rbp)
jge    1197 <main+0x67>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    116e <main+0x3e>
lea    0xe9c(%rip),%rdi
call   1040 <printf@plt>
jmp    1184 <main+0x54>
cmpl   $0x0,-0x8(%rbp)
jne    117f <main+0x4f>
mov    $0x0,%al
call   1030 <childproc@plt>
jmp    1184 <main+0x54>
jmp    1189 <main+0x59>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    113f <main+0xf>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
js     1147 <main+0x17>
jne    1153 <main+0x23>
xor    %eax,%eax
call   1030 <childproc@plt>
jmp    1153 <main+0x23>
lea    0xeb2(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
js     1169 <main+0x39>
jne    1175 <main+0x45>
xor    %eax,%eax
call   1030 <childproc@plt>
jmp    1175 <main+0x45>
lea    0xe90(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
js     118b <main+0x5b>
jne    1197 <main+0x67>
xor    %eax,%eax
call   1030 <childproc@plt>
jmp    1197 <main+0x67>
lea    0xe6e(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
js     11ad <main+0x7d>
jne    11b9 <main+0x89>
xor    %eax,%eax
call   1030 <childproc@plt>
jmp    11b9 <main+0x89>
lea    0xe4c(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
js     11c8 <main+0x98>
je     11d5 <main+0xa5>
pop    %rax
ret
lea    0xe31(%rip),%rdi
pop    %rax
jmp    1040 <puts@plt>
xor    %eax,%eax
pop    %rcx
jmp    1030 <childproc@plt>

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

LLVM IR (-O2):
@str = [12 x i8] c"fork failed\00"
define void @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %9
5: ; preds = %0
%6 = icmp eq i32 %1, 0
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call i32 (...) @childproc()
br label %9
9: ; preds = %3, %7, %5
%10 = tail call i32 (...) @fork()
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = icmp eq i32 %10, 0
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = tail call i32 (...) @childproc()
br label %18
16: ; preds = %9
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %18
18: ; preds = %16, %14, %12
%19 = tail call i32 (...) @fork()
%20 = icmp slt i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = icmp eq i32 %19, 0
br i1 %22, label %23, label %27
23: ; preds = %21
%24 = tail call i32 (...) @childproc()
br label %27
25: ; preds = %18
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %27
27: ; preds = %25, %23, %21
%28 = tail call i32 (...) @fork()
%29 = icmp slt i32 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = icmp eq i32 %28, 0
br i1 %31, label %32, label %36
32: ; preds = %30
%33 = tail call i32 (...) @childproc()
br label %36
34: ; preds = %27
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %34, %32, %30
%37 = tail call i32 (...) @fork()
%38 = icmp slt i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %36
%40 = icmp eq i32 %37, 0
br i1 %40, label %41, label %45
41: ; preds = %39
%42 = tail call i32 (...) @childproc()
br label %45
43: ; preds = %36
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %43, %41, %39
ret void
}