#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() {
  int fork_rv;
  printf("Before, my pid is %d \n", getpid());
  fork_rv = fork();
  if (fork_rv == -1) {
    perror("fork");
  } else if (fork_rv == 0) {
    printf("I am the child, my pid is %d\n", getpid());
  } else {
    printf("I am the parent, my child is %d\n", fork_rv);
  }
  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    0xea1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1189 <main+0x49>
lea    0xe98(%rip),%rdi
call   1050 <perror@plt>
jmp    11c1 <main+0x81>
cmpl   $0x0,-0x8(%rbp)
jne    11ad <main+0x6d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
jmp    11bc <main+0x7c>
mov    -0x8(%rbp),%esi
lea    0xe83(%rip),%rdi
call   1040 <printf@plt>
jmp    11c1 <main+0x81>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeb1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     116f <main+0x2f>
cmp    $0xffffffff,%eax
je     1188 <main+0x48>
lea    0xecd(%rip),%rdi
jmp    117d <main+0x3d>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9f(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
jmp    1184 <main+0x44>

LLVM IR (-O0):
@.str = [23 x i8] c"Before, my pid is %d \0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [30 x i8] c"I am the child, my pid is %d\0A\00"
@.str.3 = [33 x i8] c"I am the parent, my child is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @getpid()
%4 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 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 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %20
10: ; preds = %0
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %19
16: ; preds = %10
%17 = load i32, i32* %2
%18 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %13
br label %20
20: ; preds = %19, %8
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"Before, my pid is %d \0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [30 x i8] c"I am the child, my pid is %d\0A\00"
@.str.3 = [33 x i8] c"I am the parent, my child is %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 (...) @fork()
switch i32 %3, label %9 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %11
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %11
9: ; preds = %0
%10 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %3)
br label %11
11: ; preds = %6, %9, %4
ret i32 0
}