#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 scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int main() {
  pid_t val;
  printf("PID before fork(): %d\n", getpid());
  val = fork();
  if (val > 0) {
    printf("Parent PID: %d\n", getpid());
  } else if (val == 0) {
    printf("Child PID: %d\n", (int)getpid());
  } else {
    printf("Fork failed!");
    return 1;
  }
  puts("good bye");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    1198 <main+0x58>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11de <main+0x9e>
cmpq   $0x0,-0x10(%rbp)
jne    11bf <main+0x7f>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11d9 <main+0x99>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <main+0xb1>
jmp    11de <main+0x9e>
lea    0xe5e(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xead(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jle    1190 <main+0x50>
lea    0xea8(%rip),%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xeba(%rip),%rdi
call   1030 <puts@plt>
jmp    11a5 <main+0x65>
je     11ae <main+0x6e>
lea    0xe9d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe72(%rip),%rbx
jmp    116f <main+0x2f>

LLVM IR (-O0):
@.str = [23 x i8] c"PID before fork(): %d\0A\00"
@.str.1 = [16 x i8] c"Parent PID: %d\0A\00"
@.str.2 = [15 x i8] c"Child PID: %d\0A\00"
@.str.3 = [13 x i8] c"Fork failed!\00"
@.str.4 = [9 x i8] c"good bye\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i32 (...) @getpid()
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (...) @getpid()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %20
11: ; preds = %0
%12 = load i64, i64* %2
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 (...) @getpid()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %15)
br label %19
17: ; preds = %11
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %22
19: ; preds = %14
br label %20
20: ; preds = %19, %8
%21 = call i32 @puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [23 x i8] c"PID before fork(): %d\0A\00"
@.str.1 = [16 x i8] c"Parent PID: %d\0A\00"
@.str.2 = [15 x i8] c"Child PID: %d\0A\00"
@.str.3 = [13 x i8] c"Fork failed!\00"
@.str.4 = [9 x i8] c"good bye\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i64 (...) @fork()
%4 = icmp sgt i64 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %0
%6 = icmp eq i64 %3, 0
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %14
9: ; preds = %5, %0
%10 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %5 ]
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %10, i32 %11)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %14
14: ; preds = %9, %7
%15 = phi i32 [ 0, %9 ], [ 1, %7 ]
ret i32 %15
}