#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();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
main() {
  int i;
  for (i = 0; i < 3; i++) {
    if (fork() == 0) {
      printf("process created with pid = %d and its parent is = %d\n", getpid(),
             getppid());
    } else {
      if (i == 0)
        printf("Main parent with pid is = %d, and its parent is = %d\n",
               getpid(), getppid());
      break;
    }
  }
  while (1)
    ;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11dc <main+0x9c>
mov    $0x0,%al
call   1060 <fork@plt>
cmp    $0x0,%rax
jne    1198 <main+0x58>
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    0xe72(%rip),%rdi
call   1040 <printf@plt>
jmp    11c9 <main+0x89>
cmpl   $0x0,-0x8(%rbp)
jne    11c4 <main+0x84>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
jmp    11dc <main+0x9c>
jmp    11ce <main+0x8e>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1156 <main+0x16>
jmp    11e1 <main+0xa1>
jmp    11e1 <main+0xa1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getppid@plt>
test   %rbx,%rbx
je     1174 <main+0x34>
lea    0xecd(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
jmp    11c0 <main+0x80>
lea    0xe85(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jne    11c0 <main+0x80>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe59(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     11c2 <main+0x82>
nopl   0x0(%rax)
jmp    11c0 <main+0x80>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe27(%rip),%rdi
mov    %ebx,%esi
jmp    116b <main+0x2b>

LLVM IR (-O0):
@.str = [54 x i8] c"process created with pid = %d and its parent is = %d\0A\00"
@.str.1 = [54 x i8] c"Main parent with pid is = %d, and its parent is = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
br label %3
3: ; preds = %22, %0
%4 = load i32, i32* %2
%5 = icmp slt i32 %4, 3
br i1 %5, label %6, label %25
6: ; preds = %3
%7 = call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = call i32 (...) @getpid()
%11 = call i32 (...) @getppid()
%12 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
br label %21
13: ; preds = %6
%14 = load i32, i32* %2
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 (...) @getpid()
%18 = call i32 (...) @getppid()
%19 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
br label %20
20: ; preds = %16, %13
br label %25
21: ; preds = %9
br label %22
22: ; preds = %21
%23 = load i32, i32* %2
%24 = add nsw i32 %23, 1
store i32 %24, i32* %2
br label %3
25: ; preds = %20, %3
br label %26
26: ; preds = %25, %26
br label %26
}

LLVM IR (-O2):
@.str = [54 x i8] c"process created with pid = %d and its parent is = %d\0A\00"
@.str.1 = [54 x i8] c"Main parent with pid is = %d, and its parent is = %d\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (...) @getppid()
br i1 %2, label %5, label %19
5: ; preds = %0
%6 = tail call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %5
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (...) @getppid()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
%13 = tail call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %9
%16 = tail call i32 (...) @getpid()
%17 = tail call i32 (...) @getppid()
%18 = tail call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17)
br label %21
19: ; preds = %0
%20 = tail call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %4)
br label %21
21: ; preds = %5, %9, %15, %19
br label %22
22: ; preds = %21, %22
br label %22
}