#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(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main() {
  pid_t pid;
  printf("P1: %d->%d\n", getppid(), getpid());
  pid = fork();
  if (pid == 0) {
    sleep(5);
    printf("P3: %d->%d\n", getppid(), getpid());
    pid = fork();
    if (pid == 0) {
      sleep(2);
      printf("P6: %d->%d\n", getppid(), getpid());
      exit(6);
    } else {
      wait(NULL);
    }
    printf("P8: %d->%d\n", getppid(), getpid());
    exit(8);
  } else {
    printf("P2: %d->%d\n", getppid(), getpid());
    if (fork() == 0) {
      sleep(2);
      printf("P5: %d->%d\n", getppid(), getpid());
      exit(5);
    } else {
      printf("P4: %d->%d\n", getppid(), getpid());
      wait(NULL);
    }
    printf("P7: %d->%d\n", getppid(), getpid());
    waitpid(pid, NULL, 0);
  }
  printf("P9: %d->%d\n", getppid(), getpid());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1274 <main+0xf4>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123f <main+0xbf>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
mov    $0x6,%edi
call   1070 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdbf(%rip),%rdi
call   1040 <printf@plt>
mov    $0x8,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xd9f(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    12dd <main+0x15d>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    0xd6e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x5,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
lea    0xd4e(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x30(%rbp),%esi
mov    %eax,%edx
lea    0xd2f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xd0a(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe66(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1262 <main+0xe2>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe63(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     12cc <main+0x14c>
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe4b(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe30(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe10(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x5,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd89(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jne    1300 <main+0x180>
mov    $0x2,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd5f(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    $0x6,%edi
call   1070 <exit@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd4f(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    $0x5,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1090 <wait@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xd06(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    $0x8,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"P1: %d->%d\0A\00"
@.str.1 = [12 x i8] c"P3: %d->%d\0A\00"
@.str.2 = [12 x i8] c"P6: %d->%d\0A\00"
@.str.3 = [12 x i8] c"P8: %d->%d\0A\00"
@.str.4 = [12 x i8] c"P2: %d->%d\0A\00"
@.str.5 = [12 x i8] c"P5: %d->%d\0A\00"
@.str.6 = [12 x i8] c"P4: %d->%d\0A\00"
@.str.7 = [12 x i8] c"P7: %d->%d\0A\00"
@.str.8 = [12 x i8] c"P9: %d->%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i32 (...) @getppid()
%4 = call i32 (...) @getpid()
%5 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%6 = call i64 (...) @fork()
store i64 %6, i64* %2
%7 = load i64, i64* %2
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %30
9: ; preds = %0
%10 = call i32 @sleep(i32 5)
%11 = call i32 (...) @getppid()
%12 = call i32 (...) @getpid()
%13 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12)
%14 = call i64 (...) @fork()
store i64 %14, i64* %2
%15 = load i64, i64* %2
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %9
%18 = call i32 @sleep(i32 2)
%19 = call i32 (...) @getppid()
%20 = call i32 (...) @getpid()
%21 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
%22 = call i32 @exit(i32 6)
unreachable
23: ; preds = %9
%24 = call i32 @wait(i32* null)
br label %25
25: ; preds = %23
%26 = call i32 (...) @getppid()
%27 = call i32 (...) @getpid()
%28 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27)
%29 = call i32 @exit(i32 8)
unreachable
30: ; preds = %0
%31 = call i32 (...) @getppid()
%32 = call i32 (...) @getpid()
%33 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %31, i32 %32)
%34 = call i64 (...) @fork()
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %30
%37 = call i32 @sleep(i32 2)
%38 = call i32 (...) @getppid()
%39 = call i32 (...) @getpid()
%40 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %38, i32 %39)
%41 = call i32 @exit(i32 5)
unreachable
42: ; preds = %30
%43 = call i32 (...) @getppid()
%44 = call i32 (...) @getpid()
%45 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %43, i32 %44)
%46 = call i32 @wait(i32* null)
br label %47
47: ; preds = %42
%48 = call i32 (...) @getppid()
%49 = call i32 (...) @getpid()
%50 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %48, i32 %49)
%51 = load i64, i64* %2
%52 = call i32 @waitpid(i64 %51, i32* null, i32 0)
br label %53
53: ; preds = %47
%54 = call i32 (...) @getppid()
%55 = call i32 (...) @getpid()
%56 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %54, i32 %55)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"P1: %d->%d\0A\00"
@.str.1 = [12 x i8] c"P3: %d->%d\0A\00"
@.str.2 = [12 x i8] c"P6: %d->%d\0A\00"
@.str.3 = [12 x i8] c"P8: %d->%d\0A\00"
@.str.4 = [12 x i8] c"P2: %d->%d\0A\00"
@.str.5 = [12 x i8] c"P5: %d->%d\0A\00"
@.str.6 = [12 x i8] c"P4: %d->%d\0A\00"
@.str.7 = [12 x i8] c"P7: %d->%d\0A\00"
@.str.8 = [12 x i8] c"P9: %d->%d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getppid()
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %25
6: ; preds = %0
%7 = tail call i32 @sleep(i32 5)
%8 = tail call i32 (...) @getppid()
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %9)
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %6
%14 = tail call i32 @sleep(i32 2)
%15 = tail call i32 (...) @getppid()
%16 = tail call i32 (...) @getpid()
%17 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %16)
%18 = tail call i32 @exit(i32 6)
unreachable
19: ; preds = %6
%20 = tail call i32 @wait(i32* null)
%21 = tail call i32 (...) @getppid()
%22 = tail call i32 (...) @getpid()
%23 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %22)
%24 = tail call i32 @exit(i32 8)
unreachable
25: ; preds = %0
%26 = tail call i32 (...) @getppid()
%27 = tail call i32 (...) @getpid()
%28 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %26, i32 %27)
%29 = tail call i64 (...) @fork()
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %25
%32 = tail call i32 @sleep(i32 2)
%33 = tail call i32 (...) @getppid()
%34 = tail call i32 (...) @getpid()
%35 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %33, i32 %34)
%36 = tail call i32 @exit(i32 5)
unreachable
37: ; preds = %25
%38 = tail call i32 (...) @getppid()
%39 = tail call i32 (...) @getpid()
%40 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %38, i32 %39)
%41 = tail call i32 @wait(i32* null)
%42 = tail call i32 (...) @getppid()
%43 = tail call i32 (...) @getpid()
%44 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %42, i32 %43)
%45 = tail call i32 @waitpid(i64 %4, i32* null, i32 0)
%46 = tail call i32 (...) @getppid()
%47 = tail call i32 (...) @getpid()
%48 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %46, i32 %47)
ret i32 0
}