#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 EXIT_SUCCESS;
int atoi(char *);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int main(int argc, char *argv[]) {
  int i;
  int n = atoi(argv[1]);
  printf("Start pid: %d\n", getpid());
  for (i = 0; i < n; i++) {
    if (fork()) {
      if (fork()) {
        break;
      }
    }
    if (i == (n - 1)) {
      printf("child pid: %d.(ppid %d)\n", getpid(), getppid());
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1214 <main+0xc4>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     11cc <main+0x7c>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     11c7 <main+0x77>
jmp    1214 <main+0xc4>
jmp    11cc <main+0x7c>
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1201 <main+0xb1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1206 <main+0xb6>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1194 <main+0x44>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x8(%rsi),%rdi
call   1050 <atoi@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe93(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jle    11d2 <main+0x82>
lea    0xe8e(%rip),%r14
jmp    1195 <main+0x45>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffff,%ebx
je     11d2 <main+0x82>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11ad <main+0x5d>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11d2 <main+0x82>
cmp    $0x1,%ebx
jne    1190 <main+0x40>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1190 <main+0x40>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Start pid: %d\0A\00"
@.str.1 = [25 x i8] c"child pid: %d.(ppid %d)\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @atoi(i8* %10)
store i32 %11, i32* %7
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 0, i32* %6
br label %14
14: ; preds = %36, %2
%15 = load i32, i32* %6
%16 = load i32, i32* %7
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %39
18: ; preds = %14
%19 = call i64 (...) @fork()
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = call i64 (...) @fork()
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
br label %39
25: ; preds = %21
br label %26
26: ; preds = %25, %18
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = sub nsw i32 %28, 1
%30 = icmp eq i32 %27, %29
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = call i32 (...) @getpid()
%33 = call i32 (...) @getppid()
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %33)
br label %35
35: ; preds = %31, %26
br label %36
36: ; preds = %35
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
store i32 %38, i32* %6
br label %14
39: ; preds = %24, %14
%40 = load i32, i32* @EXIT_SUCCESS
ret i32 %40
}

LLVM IR (-O2):
@.str = [15 x i8] c"Start pid: %d\0A\00"
@.str.1 = [25 x i8] c"child pid: %d.(ppid %d)\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32 @atoi(i8* %4)
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp sgt i32 %5, 0
br i1 %8, label %9, label %27
9: ; preds = %2
%10 = add nsw i32 %5, -1
br label %11
11: ; preds = %9, %24
%12 = phi i32 [ 0, %9 ], [ %25, %24 ]
%13 = tail call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = tail call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15, %11
%19 = icmp eq i32 %12, %10
br i1 %19, label %20, label %24
20: ; preds = %18
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (...) @getppid()
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %22)
br label %24
24: ; preds = %18, %20
%25 = add nuw nsw i32 %12, 1
%26 = icmp eq i32 %25, %5
br i1 %26, label %27, label %11
27: ; preds = %24, %15, %2
%28 = load i32, i32* @EXIT_SUCCESS
ret i32 %28
}