#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 int pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int atoi(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int i, j;
  int n = atoi(argv[1]);
  pid_t pidGeneration[100];
  printf("Start pid: %d\n", getpid());
  for (i = 0; i < n; i++) {
    pidGeneration[i] = getpid();
    if (fork()) {
      if (fork()) {
        exit(EXIT_SUCCESS);
      }
    }
    if (i == (n - 1)) {
      printf("Process tree : ");
      for (j = 0; j < n; j++) {
        printf("%d ", pidGeneration[j]);
      }
      printf("%d", getpid());
      printf("\n");
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    127d <main+0x12d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
movslq -0x14(%rbp),%rax
mov    %ecx,-0x1b0(%rbp,%rax,4)
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     11ec <main+0x9c>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     11e7 <main+0x97>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11ec <main+0x9c>
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    126a <main+0x11a>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1245 <main+0xf5>
movslq -0x18(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1212 <main+0xc2>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    126f <main+0x11f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1197 <main+0x47>
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x198,%rsp
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe76(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
jle    1249 <main+0xe9>
lea    -0x1(%rbx),%r12d
mov    %ebx,%ebx
lea    0xe77(%rip),%r13
lea    0xe74(%rip),%r15
xor    %ebp,%ebp
jmp    11cd <main+0x6d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbp
cmp    %rbx,%rbp
je     1249 <main+0xe9>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,(%rsp,%rbp,4)
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11ef <main+0x8f>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    1264 <main+0x104>
cmp    %r12,%rbp
jne    11c0 <main+0x60>
lea    0xe14(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rsp,%r14,4),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r14
cmp    %r14,%rbx
jne    1210 <main+0xb0>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11c0 <main+0x60>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
add    $0x198,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Start pid: %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [16 x i8] c"Process tree : \00"
@.str.2 = [4 x i8] c"%d \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [100 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @atoi(i8* %12)
store i32 %13, i32* %8
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 0, i32* %6
br label %16
16: ; preds = %59, %2
%17 = load i32, i32* %6
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %62
20: ; preds = %16
%21 = call i32 (...) @getpid()
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [100 x i32], [100 x i32]* %9, i64 0, i64 %23
store i32 %21, i32* %24
%25 = call i64 (...) @fork()
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %20
%28 = call i64 (...) @fork()
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @EXIT_SUCCESS
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %27
br label %34
34: ; preds = %33, %20
%35 = load i32, i32* %6
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, 1
%38 = icmp eq i32 %35, %37
br i1 %38, label %39, label %58
39: ; preds = %34
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %7
br label %41
41: ; preds = %51, %39
%42 = load i32, i32* %7
%43 = load i32, i32* %8
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %54
45: ; preds = %41
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [100 x i32], [100 x i32]* %9, i64 0, i64 %47
%49 = load i32, i32* %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %45
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %41
54: ; preds = %41
%55 = call i32 (...) @getpid()
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %54, %34
br label %59
59: ; preds = %58
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
store i32 %61, i32* %6
br label %16
62: ; preds = %16
%63 = load i32, i32* @EXIT_SUCCESS
ret i32 %63
}

LLVM IR (-O2):
@.str = [15 x i8] c"Start pid: %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [16 x i8] c"Process tree : \00"
@.str.2 = [4 x i8] c"%d \00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i32]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = tail call i32 @atoi(i8* %5)
%7 = bitcast [100 x i32]* %3 to i8*
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp sgt i32 %6, 0
br i1 %10, label %11, label %45
11: ; preds = %2
%12 = add nsw i32 %6, -1
%13 = zext i32 %12 to i64
%14 = zext i32 %6 to i64
br label %15
15: ; preds = %11, %42
%16 = phi i64 [ 0, %11 ], [ %43, %42 ]
%17 = tail call i32 (...) @getpid()
%18 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 %16
store i32 %17, i32* %18
%19 = tail call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %15
%22 = tail call i64 (...) @fork()
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* @EXIT_SUCCESS
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %21, %15
%28 = icmp eq i64 %16, %13
br i1 %28, label %29, label %42
29: ; preds = %27
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ 0, %29 ], [ %36, %31 ]
%33 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = add nuw nsw i64 %32, 1
%37 = icmp eq i64 %36, %14
br i1 %37, label %38, label %31
38: ; preds = %31
%39 = tail call i32 (...) @getpid()
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = tail call i32 @putchar(i32 10)
br label %42
42: ; preds = %27, %38
%43 = add nuw nsw i64 %16, 1
%44 = icmp eq i64 %43, %14
br i1 %44, label %45, label %15
45: ; preds = %42, %2
%46 = load i32, i32* @EXIT_SUCCESS
ret i32 %46
}