#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 fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  printf("my pid is %d\n", getpid());
  int fork_rv = fork();
  if (fork_rv == -1)
    perror("fork");
  else if (fork_rv == 0)
    printf("I am the child, my pid = %d\n", getpid());
  else
    printf("I am the parent, my pid = %d\n", getpid());
  return EXIT_SUCCESS;
}


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    0xe8f(%rip),%rdi
call   1050 <perror@plt>
jmp    11c7 <main+0x87>
cmpl   $0x0,-0x8(%rbp)
jne    11ad <main+0x6d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe70(%rip),%rdi
call   1040 <printf@plt>
jmp    11c2 <main+0x82>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe73(%rip),%rdi
call   1040 <printf@plt>
jmp    11c7 <main+0x87>
mov    0x2e12(%rip),%rax
mov    (%rax),%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     1176 <main+0x36>
cmp    $0xffffffff,%eax
je     1196 <main+0x56>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xebc(%rip),%rdi
jmp    1184 <main+0x44>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8f(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
jmp    118b <main+0x4b>

LLVM IR (-O0):
@.str = [14 x i8] c"my pid is %d\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [29 x i8] c"I am the child, my pid = %d\0A\00"
@.str.3 = [30 x i8] c"I am the parent, my pid = %d\0A\00"
@EXIT_SUCCESS = global i32 0
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 ([14 x i8], [14 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 ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %19
16: ; preds = %10
%17 = call i32 (...) @getpid()
%18 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %13
br label %20
20: ; preds = %19, %8
%21 = load i32, i32* @EXIT_SUCCESS
ret i32 %21
}

LLVM IR (-O2):
@.str = [14 x i8] c"my pid is %d\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [29 x i8] c"I am the child, my pid = %d\0A\00"
@.str.3 = [30 x i8] c"I am the parent, my pid = %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 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 %12
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %12
9: ; preds = %0
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %6, %9, %4
%13 = load i32, i32* @EXIT_SUCCESS
ret i32 %13
}