#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 */

/* Variables and functions */
scalar_t__ fork();
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ wait(int /*<<< orphan*/);
int main() {
  int level;
  printf("Start %d\n", (int)getpid());
  for (level = 0; level < 2; level++) {
    if (fork() == 0 || fork() == 0)
      continue;
    break;
  }
  printf("I am %d, my parent is %d\n", (int)getpid(), (int)getppid());
  while (wait(0) > 0)
    ;
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c1 <main+0x71>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     11a9 <main+0x59>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
jne    11ae <main+0x5e>
jmp    11b3 <main+0x63>
jmp    11c1 <main+0x71>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    117d <main+0x2d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <wait@plt>
cmp    $0x0,%rax
jle    11fb <main+0xab>
jmp    11e5 <main+0x95>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%bl
jmp    117a <main+0x2a>
nopl   0x0(%rax)
test   $0x1,%bl
mov    $0x0,%ebx
je     119c <main+0x4c>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     1170 <main+0x20>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
sete   %al
test   %bl,%al
mov    $0x0,%ebx
jne    117a <main+0x2a>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe56(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
nop
xor    %edi,%edi
call   1060 <wait@plt>
test   %rax,%rax
jg     11c0 <main+0x70>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [10 x i8] c"Start %d\0A\00"
@.str.1 = [26 x i8] c"I am %d, my parent is %d\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @getpid()
%2 = trunc i64 %1 to i32
%3 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %4
4: ; preds = %13, %0
%5 = phi i1 [ true, %0 ], [ false, %13 ]
%6 = tail call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = tail call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
%11 = and i1 %10, %5
br i1 %11, label %13, label %14
12: ; preds = %4
br i1 %5, label %13, label %14
13: ; preds = %12, %8
br label %4
14: ; preds = %8, %12
%15 = tail call i64 (...) @getpid()
%16 = trunc i64 %15 to i32
%17 = tail call i64 (...) @getppid()
%18 = trunc i64 %17 to i32
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %18)
br label %20
20: ; preds = %20, %14
%21 = tail call i64 @wait(i32 0)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %20, label %23
23: ; preds = %20
ret i32 0
}