#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 getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int vfork();
void test_vfork() {
  pid_t p = vfork();
  if (p == -1) {
    perror("vfork");
    return;
  }
  if (p == 0) {
    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());
  }
}


x86 assembly (clang, -O0):
<test_vfork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
call   1060 <vfork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    117b <test_vfork+0x2b>
lea    0xe8f(%rip),%rdi
call   1050 <perror@plt>
jmp    1202 <test_vfork+0xb2>
cmpl   $0x0,-0x4(%rbp)
jne    11c6 <test_vfork+0x76>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1202 <test_vfork+0xb2>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_vfork>:
push   %rbx
call   1070 <vfork@plt>
test   %eax,%eax
je     1178 <test_vfork+0x18>
cmp    $0xffffffff,%eax
je     11b6 <test_vfork+0x56>
lea    0xe99(%rip),%rdi
jmp    117f <test_vfork+0x1f>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
mov    %eax,%ebx
call   1040 <getpid@plt>
lea    0xe73(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
call   1080 <getppid@plt>
lea    0xe5b(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%eax
call   1050 <printf@plt>
pop    %rbx
ret
lea    0xe43(%rip),%rdi
call   1060 <perror@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"vfork\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"
define void @test_vfork() {
%1 = alloca i32
%2 = call i32 @vfork()
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 ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %22
7: ; preds = %0
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = call i32 (...) @getppid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %22
16: ; preds = %7
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = call i32 (...) @getppid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %5, %16, %10
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"vfork\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.5 = [17 x i8] c"I am new process\00"
define void @test_vfork() {
%1 = call i32 @vfork()
switch i32 %1, label %10 [
i32 -1, label %2
i32 0, label %4
]
2: ; preds = %0
%3 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %16
4: ; preds = %0
%5 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.5, i64 0, i64 0))
%6 = call i32 (...) @getpid()
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%8 = call i32 (...) @getppid()
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %8)
br label %16
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = call i32 (...) @getppid()
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %4, %10, %2
ret void
}