#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 /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  pid_t pid, pidP2, pidP3, pidP4, pidP5, p, q, r;
  int num;
  int status;
  printf("P10 PID:%d\n", getpid());
  printf("P11 PID:%d\n", getpid());
  pidP2 = fork();
  if (pidP2 == 0) {
    pid = getpid();
    printf("P2 PID:%d\n", pid);
    exit(0);
  } else {
    pidP3 = fork();
    if (pidP3 == 0) {
      pid = getpid();
      printf("P3 PID:%d\n", pid);
      exit(0);
    }
  }
  p = waitpid(pidP2, (int *)0, 0);
  printf("rec is %d\n", p);
  q = waitpid(pidP3, (int *)0, 0);
  printf("rec is %d\n", q);
  printf("P13 PID:%d\n", getpid());
  pidP4 = fork();
  if (pidP4 == 0) {
    pid = getpid();
    printf("P4 PID:%d\n", pid);
    exit(0);
  } else {
    pidP5 = fork();
    if (pidP5 == 0) {
      pid = getpid();
      printf("P5 PID:%d\n", pid);
      printf("Input a 8-bit number:\n");
      scanf("%d", &num);
      exit(num);
    }
    r = waitpid(pidP5, &status, 0);
    printf("check for p5 rec is %d\n", r);
    printf("The termination code of p5 is %x\n", status);
    printf("P14 PID:%d", getpid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <main+0x79>
mov    $0x0,%al
call   1030 <getpid@plt>
cltq
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1215 <main+0xb5>
mov    $0x0,%al
call   1030 <getpid@plt>
cltq
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    121a <main+0xba>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12b7 <main+0x157>
mov    $0x0,%al
call   1030 <getpid@plt>
cltq
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1312 <main+0x1b2>
mov    $0x0,%al
call   1030 <getpid@plt>
cltq
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xda8(%rip),%rdi
lea    -0x4c(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x4c(%rbp),%edi
call   1070 <exit@plt>
mov    -0x30(%rbp),%rdi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%esi
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe7e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe73(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1288 <main+0x118>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     129b <main+0x12b>
mov    %rax,%r14
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
lea    0xe82(%rip),%rbx
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     12ae <main+0x13e>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     12cd <main+0x15d>
mov    %rsp,%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1070 <waitpid@plt>
lea    0xe04(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%esi
lea    0xe08(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe15(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1040 <getpid@plt>
movslq %eax,%rsi
lea    0xd7f(%rip),%rdi
jmp    12bf <main+0x14f>
xor    %eax,%eax
call   1040 <getpid@plt>
movslq %eax,%rsi
lea    0xd77(%rip),%rdi
jmp    12bf <main+0x14f>
xor    %eax,%eax
call   1040 <getpid@plt>
movslq %eax,%rsi
lea    0xd7b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
movslq %eax,%rsi
lea    0xd67(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xda9(%rip),%rdi
call   1030 <puts@plt>
lea    0xd9a(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x4(%rsp),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"P10 PID:%d\0A\00"
@.str.1 = [12 x i8] c"P11 PID:%d\0A\00"
@.str.2 = [11 x i8] c"P2 PID:%d\0A\00"
@.str.3 = [11 x i8] c"P3 PID:%d\0A\00"
@.str.4 = [11 x i8] c"rec is %d\0A\00"
@.str.5 = [12 x i8] c"P13 PID:%d\0A\00"
@.str.6 = [11 x i8] c"P4 PID:%d\0A\00"
@.str.7 = [11 x i8] c"P5 PID:%d\0A\00"
@.str.8 = [23 x i8] c"Input a 8-bit number:\0A\00"
@.str.9 = [3 x i8] c"%d\00"
@.str.10 = [24 x i8] c"check for p5 rec is %d\0A\00"
@.str.11 = [34 x i8] c"The termination code of p5 is %x\0A\00"
@.str.12 = [11 x i8] c"P14 PID:%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %1
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i64 (...) @fork()
store i64 %16, i64* %3
%17 = load i64, i64* %3
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %0
%20 = call i32 (...) @getpid()
%21 = sext i32 %20 to i64
store i64 %21, i64* %2
%22 = load i64, i64* %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %22)
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %0
%26 = call i64 (...) @fork()
store i64 %26, i64* %4
%27 = load i64, i64* %4
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = call i32 (...) @getpid()
%31 = sext i32 %30 to i64
store i64 %31, i64* %2
%32 = load i64, i64* %2
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i64 %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %25
br label %36
36: ; preds = %35
%37 = load i64, i64* %3
%38 = call i64 @waitpid(i64 %37, i32* null, i32 0)
store i64 %38, i64* %7
%39 = load i64, i64* %7
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %39)
%41 = load i64, i64* %4
%42 = call i64 @waitpid(i64 %41, i32* null, i32 0)
store i64 %42, i64* %8
%43 = load i64, i64* %8
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %43)
%45 = call i32 (...) @getpid()
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %45)
%47 = call i64 (...) @fork()
store i64 %47, i64* %5
%48 = load i64, i64* %5
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %36
%51 = call i32 (...) @getpid()
%52 = sext i32 %51 to i64
store i64 %52, i64* %2
%53 = load i64, i64* %2
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i64 %53)
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %36
%57 = call i64 (...) @fork()
store i64 %57, i64* %6
%58 = load i64, i64* %6
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %69
60: ; preds = %56
%61 = call i32 (...) @getpid()
%62 = sext i32 %61 to i64
store i64 %62, i64* %2
%63 = load i64, i64* %2
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i64 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
%66 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32* %10)
%67 = load i32, i32* %10
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %56
%70 = load i64, i64* %6
%71 = call i64 @waitpid(i64 %70, i32* %11, i32 0)
store i64 %71, i64* %9
%72 = load i64, i64* %9
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0), i64 %72)
%74 = load i32, i32* %11
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.11, i64 0, i64 0), i32 %74)
%76 = call i32 (...) @getpid()
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0), i32 %76)
br label %78
78: ; preds = %69
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"P10 PID:%d\0A\00"
@.str.1 = [12 x i8] c"P11 PID:%d\0A\00"
@.str.2 = [11 x i8] c"P2 PID:%d\0A\00"
@.str.3 = [11 x i8] c"P3 PID:%d\0A\00"
@.str.4 = [11 x i8] c"rec is %d\0A\00"
@.str.5 = [12 x i8] c"P13 PID:%d\0A\00"
@.str.6 = [11 x i8] c"P4 PID:%d\0A\00"
@.str.7 = [11 x i8] c"P5 PID:%d\0A\00"
@.str.9 = [3 x i8] c"%d\00"
@.str.10 = [24 x i8] c"check for p5 rec is %d\0A\00"
@.str.11 = [34 x i8] c"The termination code of p5 is %x\0A\00"
@.str.12 = [11 x i8] c"P14 PID:%d\00"
@str = [22 x i8] c"Input a 8-bit number:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = tail call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = tail call i32 (...) @getpid()
%13 = sext i32 %12 to i64
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %13)
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %0
%17 = tail call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = tail call i32 (...) @getpid()
%21 = sext i32 %20 to i64
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i64 %21)
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %16
%25 = tail call i64 @waitpid(i64 %9, i32* null, i32 0)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %25)
%27 = tail call i64 @waitpid(i64 %17, i32* null, i32 0)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %27)
%29 = tail call i32 (...) @getpid()
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = tail call i64 (...) @fork()
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %24
%34 = tail call i32 (...) @getpid()
%35 = sext i32 %34 to i64
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i64 %35)
%37 = tail call i32 @exit(i32 0)
unreachable
38: ; preds = %24
%39 = tail call i64 (...) @fork()
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %38
%42 = tail call i32 (...) @getpid()
%43 = sext i32 %42 to i64
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i64 %43)
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%46 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32* %1)
%47 = load i32, i32* %1
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %38
%50 = call i64 @waitpid(i64 %39, i32* %2, i32 0)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0), i64 %50)
%52 = load i32, i32* %2
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.11, i64 0, i64 0), i32 %52)
%54 = call i32 (...) @getpid()
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0), i32 %54)
ret i32 0
}