#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 newPid;
  void child_code(int);
  void parent_code(int);
  printf("before, mypid is %d\n", getpid());
  if ((newPid = fork()) == -1) {
    perror("fork");
  } else if (newPid == 0) {
    child_code(2);
  } else {
    parent_code(newPid);
  }
}


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    0xe81(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11a8 <main+0x48>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
jmp    11ce <main+0x6e>
cmpl   $0x0,-0x8(%rbp)
jne    11c1 <main+0x61>
mov    $0x2,%edi
call   1070 <child_code@plt>
jmp    11c9 <main+0x69>
mov    -0x8(%rbp),%edi
call   1060 <parent_code@plt>
jmp    11ce <main+0x6e>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
@.str = [21 x i8] c"before, mypid is %d\0A\00"
@.str.1 = [5 x i8] c"fork\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 ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %16
9: ; preds = %0
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
br label %15
13: ; preds = %9
%14 = load i32, i32* %2
br label %15
15: ; preds = %13, %12
br label %16
16: ; preds = %15, %7
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
@.str = [21 x i8] c"before, mypid is %d\0A\00"
@.str.1 = [5 x i8] c"fork\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 (...) @fork()
switch i32 %3, label %7 [
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 %8
6: ; preds = %0
tail call void @child_code(i32 2)
br label %8
7: ; preds = %0
tail call void @parent_code(i32 %3)
br label %8
8: ; preds = %6, %7, %4
ret i32 0
}