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

/* Variables and functions */
int /*<<< orphan*/ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ syserr(char *);
int wait(int /*<<< orphan*/);
int main() {
  pid_t pid;
  int i;
  for (i = 1; i <= 5; i++)
    switch (pid = fork()) {
    case -1:
      syserr("Error in fork\n");
    case 0:
      printf("I am a child %d and my pid is %d\n", i, getpid());
      return 0;
    default:
      printf("I am a parent %d and my pid is %d\n", i, getpid());
    }
  for (i = 1; i <= 5; i++)
    if (wait(0) == -1)
      syserr("Error in wait\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jg     1204 <main+0xb4>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     119d <main+0x4d>
jmp    118d <main+0x3d>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11a9 <main+0x59>
jmp    11d3 <main+0x83>
lea    0xe5c(%rip),%rdi
call   1050 <syserr@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124b <main+0xfb>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
jmp    11f6 <main+0xa6>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1166 <main+0x16>
movl   $0x1,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jg     1244 <main+0xf4>
xor    %edi,%edi
call   1060 <wait@plt>
cmp    $0xffffffff,%eax
jne    1231 <main+0xe1>
lea    0xe28(%rip),%rdi
call   1050 <syserr@plt>
jmp    1236 <main+0xe6>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    120b <main+0xbb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
mov    $0x1,%ebx
test   %eax,%eax
je     12ea <main+0x19a>
cmp    $0xffffffff,%eax
je     12de <main+0x18e>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeb5(%rip),%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    $0x2,%ebx
cmp    $0xffffffff,%eax
je     12de <main+0x18e>
test   %eax,%eax
je     12ea <main+0x19a>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe7e(%rip),%rdi
mov    $0x2,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    $0x3,%ebx
cmp    $0xffffffff,%eax
je     12de <main+0x18e>
test   %eax,%eax
je     12ea <main+0x19a>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe47(%rip),%rdi
mov    $0x3,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    $0x4,%ebx
cmp    $0xffffffff,%eax
je     12de <main+0x18e>
test   %eax,%eax
je     12ea <main+0x19a>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe10(%rip),%rdi
mov    $0x4,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    $0x5,%ebx
cmp    $0xffffffff,%eax
je     12de <main+0x18e>
test   %eax,%eax
je     12ea <main+0x19a>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdd9(%rip),%rdi
mov    $0x5,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <wait@plt>
cmp    $0xffffffff,%eax
jne    127c <main+0x12c>
lea    0xddd(%rip),%rdi
call   1050 <syserr@plt>
xor    %edi,%edi
call   1060 <wait@plt>
cmp    $0xffffffff,%eax
jne    1294 <main+0x144>
lea    0xdc5(%rip),%rdi
call   1050 <syserr@plt>
xor    %edi,%edi
call   1060 <wait@plt>
cmp    $0xffffffff,%eax
jne    12ac <main+0x15c>
lea    0xdad(%rip),%rdi
call   1050 <syserr@plt>
xor    %edi,%edi
call   1060 <wait@plt>
cmp    $0xffffffff,%eax
jne    12c4 <main+0x174>
lea    0xd95(%rip),%rdi
call   1050 <syserr@plt>
xor    %edi,%edi
call   1060 <wait@plt>
cmp    $0xffffffff,%eax
jne    1301 <main+0x1b1>
lea    0xd7d(%rip),%rdi
call   1050 <syserr@plt>
jmp    1301 <main+0x1b1>
lea    0xd1b(%rip),%rdi
call   1050 <syserr@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd17(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Error in fork\0A\00"
@.str.1 = [34 x i8] c"I am a child %d and my pid is %d\0A\00"
@.str.2 = [35 x i8] c"I am a parent %d and my pid is %d\0A\00"
@.str.3 = [15 x i8] c"Error in wait\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %3
br label %4
4: ; preds = %20, %0
%5 = load i32, i32* %3
%6 = icmp sle i32 %5, 5
br i1 %6, label %7, label %23
7: ; preds = %4
%8 = call i32 (...) @fork()
store i32 %8, i32* %2
switch i32 %8, label %15 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %7
%10 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %7, %9
%12 = load i32, i32* %3
%13 = call i32 (...) @getpid()
%14 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %13)
store i32 0, i32* %1
br label %37
15: ; preds = %7
%16 = load i32, i32* %3
%17 = call i32 (...) @getpid()
%18 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %16, i32 %17)
br label %19
19: ; preds = %15
br label %20
20: ; preds = %19
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %4
23: ; preds = %4
store i32 1, i32* %3
br label %24
24: ; preds = %33, %23
%25 = load i32, i32* %3
%26 = icmp sle i32 %25, 5
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = call i32 @wait(i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
br label %33
33: ; preds = %32
%34 = load i32, i32* %3
%35 = add nsw i32 %34, 1
store i32 %35, i32* %3
br label %24
36: ; preds = %24
store i32 0, i32* %1
br label %37
37: ; preds = %36, %11
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str = [15 x i8] c"Error in fork\0A\00"
@.str.1 = [34 x i8] c"I am a child %d and my pid is %d\0A\00"
@.str.2 = [35 x i8] c"I am a parent %d and my pid is %d\0A\00"
@.str.3 = [15 x i8] c"Error in wait\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %9 [
i32 -1, label %2
i32 0, label %5
]
2: ; preds = %21, %17, %13, %9, %0
%3 = phi i32 [ 1, %0 ], [ 2, %9 ], [ 3, %13 ], [ 4, %17 ], [ 5, %21 ]
%4 = tail call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %0, %9, %13, %17, %21, %2
%6 = phi i32 [ %3, %2 ], [ 1, %0 ], [ 2, %9 ], [ 3, %13 ], [ 4, %17 ], [ 5, %21 ]
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %7)
br label %52
9: ; preds = %0
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %10)
%12 = tail call i32 (...) @fork()
switch i32 %12, label %13 [
i32 -1, label %2
i32 0, label %5
]
13: ; preds = %9
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %14)
%16 = tail call i32 (...) @fork()
switch i32 %16, label %17 [
i32 -1, label %2
i32 0, label %5
]
17: ; preds = %13
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 3, i32 %18)
%20 = tail call i32 (...) @fork()
switch i32 %20, label %21 [
i32 -1, label %2
i32 0, label %5
]
21: ; preds = %17
%22 = tail call i32 (...) @getpid()
%23 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 4, i32 %22)
%24 = tail call i32 (...) @fork()
switch i32 %24, label %25 [
i32 -1, label %2
i32 0, label %5
]
25: ; preds = %21
%26 = tail call i32 (...) @getpid()
%27 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 5, i32 %26)
%28 = tail call i32 @wait(i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = tail call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %25, %30
%33 = tail call i32 @wait(i32 0)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = tail call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = tail call i32 @wait(i32 0)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = tail call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = tail call i32 @wait(i32 0)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = tail call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = tail call i32 @wait(i32 0)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = tail call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %47, %50, %5
ret i32 0
}