#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 */
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ wait();
int main() {
  pid_t pid;
  pid = fork();
  if (pid == 0) {
    pid = fork();
    if (pid == 0)
      printf("Process 3 = %d, parent = %d.\n", getpid(), getppid());
    else if (pid > 0) {
      wait();
      printf("Process 2 = %d, parent = %d.\n", getpid(), getppid());
    }
  } else if (pid > 0) {
    wait();
    printf("Process 1 = %d, parent = %d.\n", getpid(), getppid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f0 <main+0xa0>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b2 <main+0x62>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
jmp    11eb <main+0x9b>
cmpq   $0x0,-0x10(%rbp)
jle    11e6 <main+0x96>
mov    $0x0,%al
call   1060 <wait@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
jmp    11eb <main+0x9b>
jmp    1229 <main+0xd9>
cmpq   $0x0,-0x10(%rbp)
jle    1224 <main+0xd4>
mov    $0x0,%al
call   1060 <wait@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
jmp    1229 <main+0xd9>
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>
test   %rax,%rax
je     116a <main+0x1a>
jle    11ab <main+0x5b>
lea    0xed4(%rip),%rbx
jmp    117f <main+0x2f>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     1188 <main+0x38>
jle    11ab <main+0x5b>
lea    0xe9f(%rip),%rbx
xor    %eax,%eax
call   1060 <wait@plt>
jmp    118f <main+0x3f>
lea    0xe71(%rip),%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getppid@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Process 3 = %d, parent = %d.\0A\00"
@.str.1 = [30 x i8] c"Process 2 = %d, parent = %d.\0A\00"
@.str.2 = [30 x i8] c"Process 1 = %d, parent = %d.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = load i64, i64* %2
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %24
6: ; preds = %0
%7 = call i64 (...) @fork()
store i64 %7, i64* %2
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = call i32 (...) @getpid()
%12 = call i32 (...) @getppid()
%13 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
br label %23
14: ; preds = %6
%15 = load i64, i64* %2
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = call i32 (...) @wait()
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getppid()
%21 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %20)
br label %22
22: ; preds = %17, %14
br label %23
23: ; preds = %22, %10
br label %33
24: ; preds = %0
%25 = load i64, i64* %2
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = call i32 (...) @wait()
%29 = call i32 (...) @getpid()
%30 = call i32 (...) @getppid()
%31 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %30)
br label %32
32: ; preds = %27, %24
br label %33
33: ; preds = %32, %23
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"Process 3 = %d, parent = %d.\0A\00"
@.str.1 = [30 x i8] c"Process 2 = %d, parent = %d.\0A\00"
@.str.2 = [30 x i8] c"Process 1 = %d, parent = %d.\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %3
%7 = icmp sgt i64 %4, 0
br i1 %7, label %10, label %18
8: ; preds = %0
%9 = icmp sgt i64 %1, 0
br i1 %9, label %10, label %18
10: ; preds = %8, %6
%11 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), %8 ]
%12 = tail call i32 (...) @wait()
br label %13
13: ; preds = %10, %3
%14 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), %3 ], [ %11, %10 ]
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (...) @getppid()
%17 = tail call i32 @printf(i8* %14, i32 %15, i32 %16)
br label %18
18: ; preds = %13, %8, %6
ret i32 0
}