#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*/ change_pname(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ explain_wait_status(scalar_t__, int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ show_pstree(scalar_t__);
int /*<<< orphan*/ sleep(int);
scalar_t__ wait(int *);
int main(void) {
  pid_t pid;
  int status;
  pid = fork();
  if (pid < 0) {
    perror("main: fork");
    exit(1);
  }
  if (pid == 0) {
    printf("A created! \n");
    change_pname("A");
    pid = fork();
    if (pid < 0) {
      perror("A: fork");
      exit(1);
    }
    if (pid == 0) {
      change_pname("B");
      printf("B created! \n");
      pid = fork();
      if (pid < 0) {
        perror("B: fork");
        exit(1);
      }
      if (pid == 0) {
        change_pname("D");
        printf("D created! \n");
        printf("D: Sleeping...\n");
        sleep(10);
        printf("D: Exiting...\n");
        exit(13);
      }
      printf("B: Waiting... \n");
      pid = wait(&status);
      explain_wait_status(pid, status);
      printf("B: Exiting...\n");
      exit(19);
    }
    pid = fork();
    if (pid < 0) {
      perror("A: fork");
      exit(1);
    }
    if (pid == 0) {
      change_pname("C");
      printf("C created! \n");
      printf("C: Sleeping...\n");
      sleep(10);
      printf("C: Exiting...\n");
      exit(17);
    }
    printf("A: Waiting... \n");
    pid = wait(&status);
    explain_wait_status(pid, status);
    pid = wait(&status);
    explain_wait_status(pid, status);
    printf("A: Exiting...\n");
    exit(16);
  }
  sleep(3);
  show_pstree(pid);
  pid = wait(&status);
  explain_wait_status(pid, status);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11cb <main+0x3b>
lea    0xe44(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    13c2 <main+0x232>
lea    0xe2e(%rip),%rdi
call   1040 <printf@plt>
lea    0xe2f(%rip),%rdi
call   1030 <change_pname@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    121a <main+0x8a>
lea    0xe0f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    12f3 <main+0x163>
lea    0xdf6(%rip),%rdi
call   1030 <change_pname@plt>
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1269 <main+0xd9>
lea    0xdd7(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    12b8 <main+0x128>
lea    0xdbe(%rip),%rdi
call   1030 <change_pname@plt>
lea    0xdb4(%rip),%rdi
call   1040 <printf@plt>
lea    0xdb5(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
lea    0xdaf(%rip),%rdi
call   1040 <printf@plt>
mov    $0xd,%edi
call   1070 <exit@plt>
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1090 <wait@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10b0 <explain_wait_status@plt>
lea    0xd93(%rip),%rdi
call   1040 <printf@plt>
mov    $0x13,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    131f <main+0x18f>
lea    0xd0a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    136e <main+0x1de>
lea    0xd55(%rip),%rdi
call   1030 <change_pname@plt>
lea    0xd4b(%rip),%rdi
call   1040 <printf@plt>
lea    0xd4c(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
lea    0xd46(%rip),%rdi
call   1040 <printf@plt>
mov    $0x11,%edi
call   1070 <exit@plt>
lea    0xd3f(%rip),%rdi
call   1040 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1090 <wait@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10b0 <explain_wait_status@plt>
lea    -0x14(%rbp),%rdi
call   1090 <wait@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10b0 <explain_wait_status@plt>
lea    0xd11(%rip),%rdi
call   1040 <printf@plt>
mov    $0x10,%edi
call   1070 <exit@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
mov    -0x10(%rbp),%rdi
call   1050 <show_pstree@plt>
lea    -0x14(%rbp),%rdi
call   1090 <wait@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10b0 <explain_wait_status@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11d6 <main+0x46>
je     11df <main+0x4f>
mov    %rax,%rbx
mov    $0x3,%edi
call   1080 <sleep@plt>
mov    %rbx,%rdi
call   1050 <show_pstree@plt>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   10b0 <explain_wait_status@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe23(%rip),%rdi
jmp    120a <main+0x7a>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
lea    0xe19(%rip),%rdi
call   1040 <change_pname@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    1219 <main+0x89>
lea    0xe03(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jne    1248 <main+0xb8>
lea    0xdf3(%rip),%rdi
call   1040 <change_pname@plt>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    129e <main+0x10e>
lea    0xdd1(%rip),%rdi
jmp    120a <main+0x7a>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1203 <main+0x73>
jne    12e8 <main+0x158>
lea    0xdc0(%rip),%rdi
call   1040 <change_pname@plt>
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
lea    0xdd8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x11,%edi
call   1070 <exit@plt>
jne    1337 <main+0x1a7>
lea    0xd74(%rip),%rdi
call   1040 <change_pname@plt>
lea    0xde7(%rip),%rdi
call   1030 <puts@plt>
lea    0xde7(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
lea    0xde0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xd,%edi
call   1070 <exit@plt>
lea    0xd40(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <wait@plt>
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   10b0 <explain_wait_status@plt>
mov    %rbx,%rdi
call   1090 <wait@plt>
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   10b0 <explain_wait_status@plt>
lea    0xd16(%rip),%rdi
call   1030 <puts@plt>
mov    $0x10,%edi
call   1070 <exit@plt>
lea    0xd43(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   10b0 <explain_wait_status@plt>
lea    0xd30(%rip),%rdi
call   1030 <puts@plt>
mov    $0x13,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"main: fork\00"
@.str.1 = [13 x i8] c"A created! \0A\00"
@.str.2 = [2 x i8] c"A\00"
@.str.3 = [8 x i8] c"A: fork\00"
@.str.4 = [2 x i8] c"B\00"
@.str.5 = [13 x i8] c"B created! \0A\00"
@.str.6 = [8 x i8] c"B: fork\00"
@.str.7 = [2 x i8] c"D\00"
@.str.8 = [13 x i8] c"D created! \0A\00"
@.str.9 = [16 x i8] c"D: Sleeping...\0A\00"
@.str.10 = [15 x i8] c"D: Exiting...\0A\00"
@.str.11 = [16 x i8] c"B: Waiting... \0A\00"
@.str.12 = [15 x i8] c"B: Exiting...\0A\00"
@.str.13 = [2 x i8] c"C\00"
@.str.14 = [13 x i8] c"C created! \0A\00"
@.str.15 = [16 x i8] c"C: Sleeping...\0A\00"
@.str.16 = [15 x i8] c"C: Exiting...\0A\00"
@.str.17 = [16 x i8] c"A: Waiting... \0A\00"
@.str.18 = [15 x i8] c"A: Exiting...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %81
13: ; preds = %10
%14 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @change_pname(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = call i64 (...) @fork()
store i64 %16, i64* %2
%17 = load i64, i64* %2
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = load i64, i64* %2
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %52
25: ; preds = %22
%26 = call i32 @change_pname(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%28 = call i64 (...) @fork()
store i64 %28, i64* %2
%29 = load i64, i64* %2
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %25
%35 = load i64, i64* %2
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = call i32 @change_pname(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%39 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%40 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%41 = call i32 @sleep(i32 10)
%42 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%43 = call i32 @exit(i32 13)
unreachable
44: ; preds = %34
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.11, i64 0, i64 0))
%46 = call i64 @wait(i32* %3)
store i64 %46, i64* %2
%47 = load i64, i64* %2
%48 = load i32, i32* %3
%49 = call i32 @explain_wait_status(i64 %47, i32 %48)
%50 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.12, i64 0, i64 0))
%51 = call i32 @exit(i32 19)
unreachable
52: ; preds = %22
%53 = call i64 (...) @fork()
store i64 %53, i64* %2
%54 = load i64, i64* %2
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %52
%60 = load i64, i64* %2
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %69
62: ; preds = %59
%63 = call i32 @change_pname(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.13, i64 0, i64 0))
%64 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.14, i64 0, i64 0))
%65 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.15, i64 0, i64 0))
%66 = call i32 @sleep(i32 10)
%67 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.16, i64 0, i64 0))
%68 = call i32 @exit(i32 17)
unreachable
69: ; preds = %59
%70 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.17, i64 0, i64 0))
%71 = call i64 @wait(i32* %3)
store i64 %71, i64* %2
%72 = load i64, i64* %2
%73 = load i32, i32* %3
%74 = call i32 @explain_wait_status(i64 %72, i32 %73)
%75 = call i64 @wait(i32* %3)
store i64 %75, i64* %2
%76 = load i64, i64* %2
%77 = load i32, i32* %3
%78 = call i32 @explain_wait_status(i64 %76, i32 %77)
%79 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.18, i64 0, i64 0))
%80 = call i32 @exit(i32 16)
unreachable
81: ; preds = %10
%82 = call i32 @sleep(i32 3)
%83 = load i64, i64* %2
%84 = call i32 @show_pstree(i64 %83)
%85 = call i64 @wait(i32* %3)
store i64 %85, i64* %2
%86 = load i64, i64* %2
%87 = load i32, i32* %3
%88 = call i32 @explain_wait_status(i64 %86, i32 %87)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"main: fork\00"
@.str.2 = [2 x i8] c"A\00"
@.str.3 = [8 x i8] c"A: fork\00"
@.str.4 = [2 x i8] c"B\00"
@.str.6 = [8 x i8] c"B: fork\00"
@.str.7 = [2 x i8] c"D\00"
@.str.13 = [2 x i8] c"C\00"
@str = [12 x i8] c"A created! \00"
@str.19 = [15 x i8] c"A: Waiting... \00"
@str.20 = [14 x i8] c"A: Exiting...\00"
@str.21 = [12 x i8] c"C created! \00"
@str.22 = [15 x i8] c"C: Sleeping...\00"
@str.23 = [14 x i8] c"C: Exiting...\00"
@str.24 = [12 x i8] c"B created! \00"
@str.25 = [15 x i8] c"B: Waiting... \00"
@str.26 = [14 x i8] c"B: Exiting...\00"
@str.27 = [12 x i8] c"D created! \00"
@str.28 = [15 x i8] c"D: Sleeping...\00"
@str.29 = [14 x i8] c"D: Exiting...\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %3, 0
br i1 %9, label %10, label %69
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @change_pname(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = icmp eq i64 %13, 0
br i1 %19, label %20, label %44
20: ; preds = %18
%21 = tail call i32 @change_pname(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.24, i64 0, i64 0))
%23 = tail call i64 (...) @fork()
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = icmp eq i64 %23, 0
br i1 %29, label %30, label %37
30: ; preds = %28
%31 = tail call i32 @change_pname(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.27, i64 0, i64 0))
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.28, i64 0, i64 0))
%34 = tail call i32 @sleep(i32 10)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.29, i64 0, i64 0))
%36 = tail call i32 @exit(i32 13)
unreachable
37: ; preds = %28
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.25, i64 0, i64 0))
%39 = call i64 @wait(i32* %1)
%40 = load i32, i32* %1
%41 = call i32 @explain_wait_status(i64 %39, i32 %40)
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.26, i64 0, i64 0))
%43 = call i32 @exit(i32 19)
unreachable
44: ; preds = %18
%45 = tail call i64 (...) @fork()
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %44
%51 = icmp eq i64 %45, 0
br i1 %51, label %52, label %59
52: ; preds = %50
%53 = tail call i32 @change_pname(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.13, i64 0, i64 0))
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.21, i64 0, i64 0))
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.22, i64 0, i64 0))
%56 = tail call i32 @sleep(i32 10)
%57 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.23, i64 0, i64 0))
%58 = tail call i32 @exit(i32 17)
unreachable
59: ; preds = %50
%60 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.19, i64 0, i64 0))
%61 = call i64 @wait(i32* %1)
%62 = load i32, i32* %1
%63 = call i32 @explain_wait_status(i64 %61, i32 %62)
%64 = call i64 @wait(i32* %1)
%65 = load i32, i32* %1
%66 = call i32 @explain_wait_status(i64 %64, i32 %65)
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.20, i64 0, i64 0))
%68 = call i32 @exit(i32 16)
unreachable
69: ; preds = %8
%70 = tail call i32 @sleep(i32 3)
%71 = tail call i32 @show_pstree(i64 %3)
%72 = call i64 @wait(i32* %1)
%73 = load i32, i32* %1
%74 = call i32 @explain_wait_status(i64 %72, i32 %73)
ret i32 0
}