#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 */
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ stderr;
scalar_t__ wait(int /*<<< orphan*/);
int main(void) {
  fprintf(stderr, "Start %d\n", (int)getpid());
  if (fork() < 0 || fork() < 0 || fork() < 0 || fork() < 0) {
    fprintf(stderr, "Fork failure in pid %d\n", (int)getpid());
    exit(1);
  }
  fprintf(stderr, "i am %d, my parent is %d\n", (int)getpid(), (int)getppid());
  while (wait(0) > 0)
    ;
  fprintf(stderr, "Exit %d\n", (int)getpid());
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x8(%rbp),%edi
mov    %eax,%edx
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jl     11d9 <main+0x79>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jl     11d9 <main+0x79>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jl     11d9 <main+0x79>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jge    1209 <main+0xa9>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%edx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%edx
mov    %eax,%ecx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1070 <wait@plt>
cmp    $0x0,%rax
jle    1252 <main+0xf2>
jmp    123c <main+0xdc>
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e75(%rip),%r14
mov    (%r14),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe84(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     120f <main+0xaf>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     120f <main+0xaf>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     120f <main+0xaf>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     120f <main+0xaf>
mov    (%r14),%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe50(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
nop
xor    %edi,%edi
call   1070 <wait@plt>
test   %rax,%rax
jg     11e0 <main+0x80>
mov    (%r14),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe3f(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%r14),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdea(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [10 x i8] c"Start %d\0A\00"
@.str.1 = [24 x i8] c"Fork failure in pid %d\0A\00"
@.str.2 = [26 x i8] c"i am %d, my parent is %d\0A\00"
@.str.3 = [9 x i8] c"Exit %d\0A\00"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = load i32, i32* @stderr
%3 = call i64 (...) @getpid()
%4 = trunc i64 %3 to i32
%5 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %0
%9 = call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = call i64 (...) @fork()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14, %11, %8, %0
%18 = load i32, i32* @stderr
%19 = call i64 (...) @getpid()
%20 = trunc i64 %19 to i32
%21 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = call i64 (...) @getpid()
%26 = trunc i64 %25 to i32
%27 = call i64 (...) @getppid()
%28 = trunc i64 %27 to i32
%29 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %28)
br label %30
30: ; preds = %33, %23
%31 = call i64 @wait(i32 0)
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %30
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i64 (...) @getpid()
%37 = trunc i64 %36 to i32
%38 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %37)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [10 x i8] c"Start %d\0A\00"
@.str.1 = [24 x i8] c"Fork failure in pid %d\0A\00"
@.str.2 = [26 x i8] c"i am %d, my parent is %d\0A\00"
@.str.3 = [9 x i8] c"Exit %d\0A\00"
define i32 @main() {
%1 = load i32, i32* @stderr
%2 = tail call i64 (...) @getpid()
%3 = trunc i64 %2 to i32
%4 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %0
%8 = tail call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = tail call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13, %10, %7, %0
%17 = load i32, i32* @stderr
%18 = tail call i64 (...) @getpid()
%19 = trunc i64 %18 to i32
%20 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = tail call i64 (...) @getpid()
%25 = trunc i64 %24 to i32
%26 = tail call i64 (...) @getppid()
%27 = trunc i64 %26 to i32
%28 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %27)
br label %29
29: ; preds = %29, %22
%30 = tail call i64 @wait(i32 0)
%31 = icmp sgt i64 %30, 0
br i1 %31, label %29, label %32
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = tail call i64 (...) @getpid()
%35 = trunc i64 %34 to i32
%36 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %35)
ret i32 0
}