#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 main(void) {
  int i = 0;
  printf("i\tson/pa\tppid\tpid\tfpid\n");
  for (i = 0; i < 2; i++) {
    pid_t fpid = fork();
    if (fpid == 0)
      printf("%d\tchild\t%4d\t%4d\t%4d\n", i, getppid(), getpid(), fpid);
    else
      printf("%d\tparent\t%4d\t%4d\t%4d\n", i, getppid(), getpid(), fpid);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1205 <main+0xc5>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c1 <main+0x81>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
mov    -0x10(%rbp),%r8
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f2 <main+0xb2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
mov    -0x10(%rbp),%r8
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f7 <main+0xb7>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    116b <main+0x2b>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbp,%rbp
je     1190 <main+0x40>
lea    0xe91(%rip),%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %eax,%ecx
mov    %rbp,%r8
jmp    11a0 <main+0x50>
lea    0xe69(%rip),%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %r8d,%r8d
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     11db <main+0x8b>
lea    0xe49(%rip),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %eax,%ecx
mov    %rbx,%r8
jmp    11ee <main+0x9e>
lea    0xe1e(%rip),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %r8d,%r8d
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"i\09son/pa\09ppid\09pid\09fpid\0A\00"
@.str.1 = [22 x i8] c"%d\09child\09%4d\09%4d\09%4d\0A\00"
@.str.2 = [23 x i8] c"%d\09parent\09%4d\09%4d\09%4d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %2
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %5
5: ; preds = %25, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 2
br i1 %7, label %8, label %28
8: ; preds = %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = load i32, i32* %2
%14 = call i32 (...) @getppid()
%15 = call i32 (...) @getpid()
%16 = load i64, i64* %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14, i32 %15, i64 %16)
br label %24
18: ; preds = %8
%19 = load i32, i32* %2
%20 = call i32 (...) @getppid()
%21 = call i32 (...) @getpid()
%22 = load i64, i64* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20, i32 %21, i64 %22)
br label %24
24: ; preds = %18, %12
br label %25
25: ; preds = %24
%26 = load i32, i32* %2
%27 = add nsw i32 %26, 1
store i32 %27, i32* %2
br label %5
28: ; preds = %5
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [22 x i8] c"%d\09child\09%4d\09%4d\09%4d\0A\00"
@.str.2 = [23 x i8] c"%d\09parent\09%4d\09%4d\09%4d\0A\00"
@str = [23 x i8] c"i\09son/pa\09ppid\09pid\09fpid\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
%4 = tail call i32 (...) @getppid()
%5 = tail call i32 (...) @getpid()
br i1 %3, label %6, label %8
6: ; preds = %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %4, i32 %5, i64 0)
br label %10
8: ; preds = %0
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %4, i32 %5, i64 %2)
br label %10
10: ; preds = %8, %6
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 (...) @getpid()
br i1 %12, label %17, label %15
15: ; preds = %10
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %13, i32 %14, i64 %11)
br label %19
17: ; preds = %10
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %13, i32 %14, i64 0)
br label %19
19: ; preds = %17, %15
ret i32 0
}