#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 /*<<< orphan*/ child_code(int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ parent_code(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  pid_t pid;
  printf("Before: pid is %d\n", getpid());
  if ((pid = fork()) == -1) {
    perror("fork() failed");
    exit(1);
  } else if (pid == 0) {
    child_code(2);
  } else {
    parent_code(pid);
  }
  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   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11c4 <main+0x54>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11dd <main+0x6d>
mov    $0x2,%edi
call   1080 <child_code@plt>
jmp    11e5 <main+0x75>
mov    -0x14(%rbp),%edi
call   1070 <parent_code@plt>
jmp    11ea <main+0x7a>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe81(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     119f <main+0x2f>
cmp    $0xffffffff,%eax
je     11ad <main+0x3d>
mov    %eax,%edi
call   1070 <parent_code@plt>
jmp    11a9 <main+0x39>
mov    $0x2,%edi
call   1080 <child_code@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Before: pid is %d\0A\00"
@.str.1 = [14 x i8] c"fork() failed\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 (...) @getpid()
%8 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @child_code(i32 2)
br label %22
19: ; preds = %14
%20 = load i32, i32* %6
%21 = call i32 @parent_code(i32 %20)
br label %22
22: ; preds = %19, %17
br label %23
23: ; preds = %22
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"Before: pid is %d\0A\00"
@.str.1 = [14 x i8] c"fork() failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 (...) @fork()
switch i32 %5, label %11 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = tail call i32 @child_code(i32 2)
br label %13
11: ; preds = %2
%12 = tail call i32 @parent_code(i32 %5)
br label %13
13: ; preds = %9, %11
ret i32 0
}