#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 */
typedef int pid_t;

/* Variables and functions */
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid = fork();
  if (pid > 0)
    printf("I am the parent of pid=%d!\n", getpid());
  else if (pid == 0)
    printf("I am the baby pid=%d\n", getpid());
  else if (pid == -1)
    perror("fork");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1184 <main+0x44>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
jmp    11c8 <main+0x88>
cmpl   $0x0,-0x14(%rbp)
jne    11a8 <main+0x68>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7e(%rip),%rdi
call   1040 <printf@plt>
jmp    11c3 <main+0x83>
cmpl   $0xffffffff,-0x14(%rbp)
jne    11be <main+0x7e>
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
jmp    11c3 <main+0x83>
jmp    11c8 <main+0x88>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
jle    115c <main+0x1c>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea6(%rip),%rdi
jmp    117f <main+0x3f>
je     1171 <main+0x31>
cmp    $0xffffffff,%eax
jne    1186 <main+0x46>
lea    0xec8(%rip),%rdi
call   1050 <perror@plt>
jmp    1186 <main+0x46>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
@.str = [28 x i8] c"I am the parent of pid=%d!\0A\00"
@.str.1 = [22 x i8] c"I am the baby pid=%d\0A\00"
@.str.2 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %14
8: ; preds = %2
switch i32 %3, label %14 [
i32 0, label %9
i32 -1, label %12
]
9: ; preds = %8
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %14
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %8, %9, %12, %5
ret i32 0
}