#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) {
    printf("Process 1 = %d, parent = %d.\n", getpid(), getppid());
    wait();
  } else if (pid == 0) {
    printf("Process 2 = %d, parent = %d.\n", getpid(), getppid());
    pid = fork();
    if (pid > 0)
      wait();
    else if (pid == 0)
      printf("Process 3 = %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)
jle    11a3 <main+0x53>
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    0xe6e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <wait@plt>
jmp    122e <main+0xde>
cmpq   $0x0,-0x10(%rbp)
jne    1229 <main+0xd9>
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    0xe53(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    11f2 <main+0xa2>
mov    $0x0,%al
call   1060 <wait@plt>
jmp    1224 <main+0xd4>
cmpq   $0x0,-0x10(%rbp)
jne    121f <main+0xcf>
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    0xe22(%rip),%rdi
call   1040 <printf@plt>
jmp    1224 <main+0xd4>
jmp    1229 <main+0xd9>
jmp    122e <main+0xde>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jle    1186 <main+0x36>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe8c(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <wait@plt>
jmp    1188 <main+0x38>
je     118c <main+0x3c>
xor    %eax,%eax
pop    %rbx
ret
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe7b(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jg     117d <main+0x2d>
jne    1188 <main+0x38>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe6b(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
jmp    1188 <main+0x38>

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

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