#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 fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ waitpid(int, int *, int);
int main(void) {
  pid_t me = getpid();
  printf("Main Process ID : %d\n\n", me);
  int status = 0;
  pid_t child = fork();
  if (child == 0) {
    pid_t lv2_1 = -1, lv2_2 = -1;
    pid_t lv3_1 = -1, lv3_2 = -1;
    printf("Fork 1. I'm the child %d, my parent is %d\n", getpid(), getppid());
    lv2_1 = fork();
    if (lv2_1 != 0)
      lv2_2 = fork();
    if (lv2_1 == 0) {
      printf("Fork 2. I'm the child %d, my parent is %d\n", getpid(),
             getppid());
    }
    if (lv2_2 == 0) {
      printf("Fork 2. I'm the child %d, my parent is %d\n", getpid(),
             getppid());
    }
    if (lv2_1 == 0) {
      lv3_1 = fork();
      if (lv3_1 == 0) {
        printf("Fork 3. I'm the child %d, my parent is %d\n", getpid(),
               getppid());
      }
      wait(&status);
    }
    if (lv2_2 == 0) {
      lv3_2 = fork();
      if (lv3_2 == 0) {
        printf("Fork 3. I'm the child %d, my parent is %d\n", getpid(),
               getppid());
      }
      wait(&status);
    }
    if (lv2_1 != 0 && lv2_2 != 0) {
      waitpid(lv2_1, &status, 1);
      waitpid(lv2_2, &status, 1);
    }
  } else {
    waitpid(child, &status, 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1330 <main+0x1d0>
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1203 <main+0xa3>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1231 <main+0xd1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jne    125f <main+0xff>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jne    12aa <main+0x14a>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12a1 <main+0x141>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x30(%rbp),%esi
mov    %eax,%edx
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc(%rbp),%rdi
call   1070 <wait@plt>
cmpl   $0x0,-0x18(%rbp)
jne    12f5 <main+0x195>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    12ec <main+0x18c>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc(%rbp),%rdi
call   1070 <wait@plt>
cmpl   $0x0,-0x14(%rbp)
je     132b <main+0x1cb>
cmpl   $0x0,-0x18(%rbp)
je     132b <main+0x1cb>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x1,%edx
call   1050 <waitpid@plt>
mov    -0x18(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x1,%edx
call   1050 <waitpid@plt>
jmp    1341 <main+0x1e1>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x1,%edx
call   1050 <waitpid@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11a8 <main+0x48>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <waitpid@plt>
jmp    1258 <main+0xf8>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe58(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11ff <main+0x9f>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11ff <main+0x9f>
mov    %eax,%ebp
lea    0xc(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <waitpid@plt>
mov    %ebp,%edi
mov    %r14,%rsi
jmp    1199 <main+0x39>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe2c(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    124e <main+0xee>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe2a(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Main Process ID : %d\0A\0A\00"
@.str.1 = [43 x i8] c"Fork 1. I'm the child %d, my parent is %d\0A\00"
@.str.2 = [43 x i8] c"Fork 2. I'm the child %d, my parent is %d\0A\00"
@.str.3 = [43 x i8] c"Fork 3. I'm the child %d, my parent is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 (...) @getpid()
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 0, i32* %3
%12 = call i32 (...) @fork()
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %76
15: ; preds = %0
store i32 -1, i32* %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 -1, i32* %8
%16 = call i32 (...) @getpid()
%17 = call i32 (...) @getppid()
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17)
%19 = call i32 (...) @fork()
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 (...) @fork()
store i32 %23, i32* %6
br label %24
24: ; preds = %22, %15
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = call i32 (...) @getpid()
%29 = call i32 (...) @getppid()
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %29)
br label %31
31: ; preds = %27, %24
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = call i32 (...) @getpid()
%36 = call i32 (...) @getppid()
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %36)
br label %38
38: ; preds = %34, %31
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %38
%42 = call i32 (...) @fork()
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = call i32 (...) @getpid()
%47 = call i32 (...) @getppid()
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %46, i32 %47)
br label %49
49: ; preds = %45, %41
%50 = call i32 @wait(i32* %3)
br label %51
51: ; preds = %49, %38
%52 = load i32, i32* %6
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %64
54: ; preds = %51
%55 = call i32 (...) @fork()
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = call i32 (...) @getpid()
%60 = call i32 (...) @getppid()
%61 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %59, i32 %60)
br label %62
62: ; preds = %58, %54
%63 = call i32 @wait(i32* %3)
br label %64
64: ; preds = %62, %51
%65 = load i32, i32* %5
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %75
67: ; preds = %64
%68 = load i32, i32* %6
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %75
70: ; preds = %67
%71 = load i32, i32* %5
%72 = call i32 @waitpid(i32 %71, i32* %3, i32 1)
%73 = load i32, i32* %6
%74 = call i32 @waitpid(i32 %73, i32* %3, i32 1)
br label %75
75: ; preds = %70, %67, %64
br label %79
76: ; preds = %0
%77 = load i32, i32* %4
%78 = call i32 @waitpid(i32 %77, i32* %3, i32 1)
br label %79
79: ; preds = %76, %75
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"Main Process ID : %d\0A\0A\00"
@.str.1 = [43 x i8] c"Fork 1. I'm the child %d, my parent is %d\0A\00"
@.str.2 = [43 x i8] c"Fork 2. I'm the child %d, my parent is %d\0A\00"
@.str.3 = [43 x i8] c"Fork 3. I'm the child %d, my parent is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%5 = tail call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %43
7: ; preds = %0
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (...) @getppid()
%10 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %9)
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %7
%14 = tail call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %28, label %40
16: ; preds = %7
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (...) @getppid()
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %18)
%20 = tail call i32 (...) @fork()
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (...) @getppid()
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %24)
br label %26
26: ; preds = %16, %22
%27 = call i32 @wait(i32* %1)
br label %45
28: ; preds = %13
%29 = tail call i32 (...) @getpid()
%30 = tail call i32 (...) @getppid()
%31 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %30)
%32 = tail call i32 (...) @fork()
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = tail call i32 (...) @getpid()
%36 = tail call i32 (...) @getppid()
%37 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %35, i32 %36)
br label %38
38: ; preds = %34, %28
%39 = call i32 @wait(i32* %1)
br label %45
40: ; preds = %13
%41 = call i32 @waitpid(i32 %11, i32* %1, i32 1)
%42 = call i32 @waitpid(i32 %14, i32* %1, i32 1)
br label %45
43: ; preds = %0
%44 = call i32 @waitpid(i32 %5, i32* %1, i32 1)
br label %45
45: ; preds = %38, %26, %40, %43
ret i32 0
}