#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 getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
void test_fork() {
  pid_t p = fork();
  if (p == -1) {
    perror("fork");
    return;
  }
  if (p == 0) {
    sleep(1);
    printf("I am new process\n");
    printf("pid %d\n", getpid());
    printf("ppid %d\n", getppid());
  } else {
    printf("I am old process\n");
    printf("pid %d\n", getpid());
    printf("ppid %d\n", getppid());
  }
  printf("parent and child all run here\n");
  wait(&p);
}


x86 assembly (clang, -O0):
<test_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    119d <test_fork+0x2d>
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
jmp    1245 <test_fork+0xd5>
cmpl   $0x0,-0x4(%rbp)
jne    11f2 <test_fork+0x82>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122e <test_fork+0xbe>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,%esi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x4(%rbp),%rdi
call   1080 <wait@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fork>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
je     119e <test_fork+0x1e>
cmp    $0xffffffff,%eax
je     11fa <test_fork+0x7a>
lea    0xe72(%rip),%rdi
jmp    11af <test_fork+0x2f>
mov    $0x1,%edi
call   1080 <sleep@plt>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe44(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe2c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
pop    %rax
ret
lea    0xdff(%rip),%rdi
pop    %rax
jmp    1060 <perror@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [18 x i8] c"I am new process\0A\00"
@.str.2 = [8 x i8] c"pid %d\0A\00"
@.str.3 = [9 x i8] c"ppid %d\0A\00"
@.str.4 = [18 x i8] c"I am old process\0A\00"
@.str.5 = [31 x i8] c"parent and child all run here\0A\00"
define void @test_fork() {
%1 = alloca i32
%2 = call i32 (...) @fork()
store i32 %2, i32* %1
%3 = load i32, i32* %1
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %26
7: ; preds = %0
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %7
%11 = call i32 @sleep(i32 1)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = call i32 (...) @getppid()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %15)
br label %23
17: ; preds = %7
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = call i32 (...) @getppid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %17, %10
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 @wait(i32* %1)
br label %26
26: ; preds = %23, %5
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.2 = [8 x i8] c"pid %d\0A\00"
@.str.3 = [9 x i8] c"ppid %d\0A\00"
@str = [17 x i8] c"I am old process\00"
@str.6 = [30 x i8] c"parent and child all run here\00"
@str.7 = [17 x i8] c"I am new process\00"
define void @test_fork() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
store i32 %3, i32* %1
switch i32 %3, label %8 [
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, i64 0, i64 0))
br label %17
6: ; preds = %0
%7 = tail call i32 @sleep(i32 1)
br label %8
8: ; preds = %0, %6
%9 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0), %6 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0), %0 ]
%10 = tail call i32 @puts(i8* dereferenceable(1) %9)
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0))
%16 = call i32 @wait(i32* %1)
br label %17
17: ; preds = %8, %4
ret void
}