#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 fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int pid = 0;
  for (int i = 0; i < 3; i++) {
    pid = fork();
    if (pid == 0) {
      printf("%d : %d\t", getpid(), i);
      printf(
          "This is the child process. My pid is %d and my parent's id is %d.\n",
          getpid(), getppid());
      printf("%d : %d :\t-\n", getpid(), i);
    } else {
      printf("%d : %d\t", getpid(), i);
      printf("create child pid = %d\n", pid);
      printf("%d : %d\tThis is the parent process. My pid is %d and my child's "
             "id is %d.\n",
             getpid(), i, getpid(), pid);
      printf("%d : %d :\t-\n", getpid(), i);
    }
  }
  wait(NULL);
  wait(NULL);
  wait(NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    1271 <main+0x121>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11e8 <main+0x98>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0xc(%rbp),%edx
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0xc(%rbp),%edx
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125e <main+0x10e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0xc(%rbp),%edx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
mov    -0x8(%rbp),%r8d
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0xc(%rbp),%edx
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1263 <main+0x113>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116d <main+0x1d>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe96(%rip),%rdi
mov    %eax,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
je     11b2 <main+0x62>
lea    0xed9(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xed0(%rip),%rdi
mov    %ebp,%esi
xor    %edx,%edx
mov    %eax,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d4 <main+0x84>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe40(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdfc(%rip),%rdi
mov    %eax,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
je     1252 <main+0x102>
lea    0xe3c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe33(%rip),%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %eax,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1274 <main+0x124>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xda0(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdca(%rip),%rdi
mov    %eax,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd59(%rip),%rdi
mov    %eax,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
je     12f5 <main+0x1a5>
lea    0xd99(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd90(%rip),%rdi
mov    %ebp,%esi
mov    $0x2,%edx
mov    %eax,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1317 <main+0x1c7>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xcfd(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd27(%rip),%rdi
mov    %eax,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%d : %d\09\00"
@.str.1 = [67 x i8] c"This is the child process. My pid is %d and my parent's id is %d.\0A\00"
@.str.2 = [13 x i8] c"%d : %d :\09-\0A\00"
@.str.3 = [23 x i8] c"create child pid = %d\0A\00"
@.str.4 = [75 x i8] c"%d : %d\09This is the parent process. My pid is %d and my child's id is %d.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
br label %4
4: ; preds = %36, %0
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 3
br i1 %6, label %7, label %39
7: ; preds = %4
%8 = call i32 (...) @fork()
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %7
%12 = call i32 (...) @getpid()
%13 = load i32, i32* %3
%14 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = call i32 (...) @getpid()
%16 = call i32 (...) @getppid()
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16)
%18 = call i32 (...) @getpid()
%19 = load i32, i32* %3
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %19)
br label %35
21: ; preds = %7
%22 = call i32 (...) @getpid()
%23 = load i32, i32* %3
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %23)
%25 = load i32, i32* %2
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i32 (...) @getpid()
%28 = load i32, i32* %3
%29 = call i32 (...) @getpid()
%30 = load i32, i32* %2
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.4, i64 0, i64 0), i32 %27, i32 %28, i32 %29, i32 %30)
%32 = call i32 (...) @getpid()
%33 = load i32, i32* %3
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33)
br label %35
35: ; preds = %21, %11
br label %36
36: ; preds = %35
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 1
store i32 %38, i32* %3
br label %4
39: ; preds = %4
%40 = call i32 @wait(i32* null)
%41 = call i32 @wait(i32* null)
%42 = call i32 @wait(i32* null)
ret i32 0
}

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