#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() {
  int i;
  printf("startup: %d, pid is %d\n", getpid(), getppid());
  printf("i son/pa ppid pid fpid\n");
  for (i = 0; i < 2; ++i) {
    pid_t fpid = fork();
    if (fpid == 0) {
      printf("%d child %4d %4d %4d\n", i, getppid(), getpid(), fpid);
    } else {
      printf("%d parent %4d %4d %4d\n", i, getppid(), getpid(), fpid);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
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    0xe94(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1222 <main+0xe2>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11de <main+0x9e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    %eax,%ecx
mov    -0x10(%rbp),%r8
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120f <main+0xcf>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
mov    -0x10(%rbp),%r8
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1214 <main+0xd4>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1188 <main+0x48>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe96(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xec9(%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     11b2 <main+0x62>
lea    0xe87(%rip),%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %eax,%ecx
mov    %rbp,%r8
jmp    11c2 <main+0x72>
lea    0xe5f(%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     11fd <main+0xad>
lea    0xe3f(%rip),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %eax,%ecx
mov    %rbx,%r8
jmp    1210 <main+0xc0>
lea    0xe14(%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"startup: %d, pid is %d\0A\00"
@.str.1 = [24 x i8] c"i son/pa ppid pid fpid\0A\00"
@.str.2 = [22 x i8] c"%d child %4d %4d %4d\0A\00"
@.str.3 = [23 x i8] c"%d parent %4d %4d %4d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
store i32 0, i32* %1
%4 = call i32 (...) @getpid()
%5 = call i32 (...) @getppid()
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %5)
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %8
8: ; preds = %28, %0
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %31
11: ; preds = %8
%12 = call i64 (...) @fork()
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i32, i32* %2
%17 = call i32 (...) @getppid()
%18 = call i32 (...) @getpid()
%19 = load i64, i64* %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %16, i32 %17, i32 %18, i64 %19)
br label %27
21: ; preds = %11
%22 = load i32, i32* %2
%23 = call i32 (...) @getppid()
%24 = call i32 (...) @getpid()
%25 = load i64, i64* %3
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %22, i32 %23, i32 %24, i64 %25)
br label %27
27: ; preds = %21, %15
br label %28
28: ; preds = %27
%29 = load i32, i32* %2
%30 = add nsw i32 %29, 1
store i32 %30, i32* %2
br label %8
31: ; preds = %8
ret i32 0
}

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