#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*/ printf(char *, ...);
int main() {
  int child;
  printf("Parent is starting (pid %d)\n", getpid());
  child = fork();
  if (0 == child) {
    printf("I am the child (pid %d)\n", getpid());
  } else if (child < 0) {
    printf("Fork failed!\n");
  } else {
    printf("I am the parent (pid %d).  My child is pid %d\n", getpid(), child);
  }
  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,%esi
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jne    1187 <main+0x57>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11c3 <main+0x93>
cmpl   $0x0,-0x8(%rbp)
jge    11a4 <main+0x74>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11be <main+0x8e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11c3 <main+0x93>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xeb1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     1182 <main+0x42>
js     119b <main+0x5b>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xec1(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11a7 <main+0x67>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11a7 <main+0x67>
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Parent is starting (pid %d)\0A\00"
@.str.1 = [25 x i8] c"I am the child (pid %d)\0A\00"
@.str.2 = [14 x i8] c"Fork failed!\0A\00"
@.str.3 = [47 x i8] c"I am the parent (pid %d). My child is pid %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @getpid()
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 0, %6
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (...) @getpid()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %21
11: ; preds = %0
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %20
16: ; preds = %11
%17 = call i32 (...) @getpid()
%18 = load i32, i32* %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %18)
br label %20
20: ; preds = %16, %14
br label %21
21: ; preds = %20, %8
ret i32 0
}

LLVM IR (-O2):
@.str = [29 x i8] c"Parent is starting (pid %d)\0A\00"
@.str.1 = [25 x i8] c"I am the child (pid %d)\0A\00"
@.str.3 = [47 x i8] c"I am the parent (pid %d). My child is pid %d\0A\00"
@str = [13 x i8] c"Fork failed!\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %15
8: ; preds = %0
%9 = icmp slt i32 %3, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %15
12: ; preds = %8
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %13, i32 %3)
br label %15
15: ; preds = %10, %12, %5
ret i32 0
}