#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 fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  int pid = getpid(), i;
  for (i = 0; i < 3; i++) {
    if (pid > 0) {
      pid = fork();
    }
    if (pid > 0) {
      printf("I'm the parent! pid = %d\n", getpid());
    } else if (pid == 0) {
      printf("I'm the child! pid = %d\n", getpid());
    } else {
      perror("Error!");
    }
  }
  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,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    11ea <main+0xaa>
cmpl   $0x0,-0x8(%rbp)
jle    117e <main+0x3e>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11a2 <main+0x62>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
jmp    11d7 <main+0x97>
cmpl   $0x0,-0x8(%rbp)
jne    11c6 <main+0x86>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
jmp    11d2 <main+0x92>
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
jmp    11d7 <main+0x97>
jmp    11dc <main+0x9c>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1160 <main+0x20>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
test   %eax,%eax
jle    11a7 <main+0x67>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
jle    11a7 <main+0x67>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
jle    11ee <main+0xae>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
jle    11f2 <main+0xb2>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe5b(%rip),%rdi
jmp    11e3 <main+0xa3>
test   %eax,%eax
jne    11f6 <main+0xb6>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe61(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe4c(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe37(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
je     11c0 <main+0x80>
jmp    1202 <main+0xc2>
je     11d5 <main+0x95>
jmp    120e <main+0xce>
lea    0xe36(%rip),%rdi
call   1050 <perror@plt>
lea    0xe2a(%rip),%rdi
call   1050 <perror@plt>
lea    0xe1e(%rip),%rdi
call   1050 <perror@plt>
jmp    11ea <main+0xaa>

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

LLVM IR (-O2):
@.str = [26 x i8] c"I'm the parent! pid = %d\0A\00"
@.str.1 = [25 x i8] c"I'm the child! pid = %d\0A\00"
@.str.2 = [7 x i8] c"Error!\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = icmp sgt i32 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 (...) @fork()
%5 = icmp sgt i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %0, %3
%7 = phi i32 [ %4, %3 ], [ %1, %0 ]
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %23
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %21
14: ; preds = %3
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = tail call i32 (...) @fork()
%18 = icmp sgt i32 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %14
%20 = icmp eq i32 %17, 0
br i1 %20, label %23, label %21
21: ; preds = %19, %12
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %33
23: ; preds = %19, %9
%24 = tail call i32 (...) @getpid()
%25 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %35
26: ; preds = %14
%27 = tail call i32 (...) @getpid()
%28 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = tail call i32 (...) @fork()
%30 = icmp sgt i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %26
%32 = icmp eq i32 %29, 0
br i1 %32, label %35, label %33
33: ; preds = %21, %31
%34 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %41
35: ; preds = %23, %31
%36 = tail call i32 (...) @getpid()
%37 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %41
38: ; preds = %26
%39 = tail call i32 (...) @getpid()
%40 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %38, %35, %33
ret i32 0
}