#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*/ printf(char *, int, ...);
int fork1(void) {
  pid_t pid = getpid();
  pid_t cpid, mypid;
  printf("Parent PID: %i \n", pid);
  cpid = fork();
  if (cpid > 0) {
    mypid = getpid();
    printf("[%i] parent of [%i]\n", mypid, cpid);
  }
  if (cpid == 0) {
    mypid = getpid();
    printf("[%i] child\n", mypid);
  }
  printf(" PID= %i \n", cpid);
  return 0;
}


x86 assembly (clang, -O0):
<fork1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1185 <fork1+0x55>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11aa <fork1+0x7a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork1>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xec1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1170 <fork1+0x40>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeae(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1189 <fork1+0x59>
jne    1189 <fork1+0x59>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea2(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [17 x i8] c"Parent PID: %i \0A\00"
@.str.1 = [21 x i8] c"[%i] parent of [%i]\0A\00"
@.str.2 = [12 x i8] c"[%i] child\0A\00"
@.str.3 = [11 x i8] c" PID= %i \0A\00"
define i32 @fork1() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 (...) @fork()
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %3)
br label %13
8: ; preds = %0
%9 = icmp eq i32 %3, 0
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %5, %10, %8
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %3)
ret i32 0
}