#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ vfork();
int main(int argc, char **argv) {
  pid_t pid;
  int val = 10;
  pid = vfork();
  if (pid < 0) {
    perror("vfork() ");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    printf("This is child with pid %u and parent pid %u.\n", getpid(),
           getppid());
    printf("Sleep 3s.\n");
    sleep(3);
    val = 100;
    _exit(EXIT_SUCCESS);
  } else {
    printf("This is parent with pid %u.\n", getpid());
  }
  printf("val: %d\n", val);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xa,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <vfork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11cd <main+0x4d>
lea    0xe46(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1229 <main+0xa9>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
movl   $0x64,-0x1c(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1245 <main+0xc5>
mov    -0x1c(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <vfork@plt>
test   %rax,%rax
jns    11b9 <main+0x29>
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebp
test   %rbx,%rbx
jne    1207 <main+0x77>
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xe31(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe73(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
lea    0xe29(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe36(%rip),%rdi
mov    $0xa,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"vfork() \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [46 x i8] c"This is child with pid %u and parent pid %u.\0A\00"
@.str.2 = [11 x i8] c"Sleep 3s.\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [29 x i8] c"This is parent with pid %u.\0A\00"
@.str.4 = [9 x i8] c"val: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 10, i32* %7
%8 = call i64 (...) @vfork()
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i64, i64* %6
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getppid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @sleep(i32 3)
store i32 100, i32* %7
%24 = load i32, i32* @EXIT_SUCCESS
%25 = call i32 @_exit(i32 %24)
unreachable
26: ; preds = %15
%27 = call i32 (...) @getpid()
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %26
br label %30
30: ; preds = %29
%31 = load i32, i32* %7
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"vfork() \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [46 x i8] c"This is child with pid %u and parent pid %u.\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [29 x i8] c"This is parent with pid %u.\0A\00"
@.str.4 = [9 x i8] c"val: %d\0A\00"
@str = [10 x i8] c"Sleep 3s.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @vfork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = icmp eq i64 %3, 0
%11 = tail call i32 (...) @getpid()
br i1 %10, label %12, label %19
12: ; preds = %9
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %13)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @sleep(i32 3)
%17 = load i32, i32* @EXIT_SUCCESS
%18 = tail call i32 @_exit(i32 %17)
unreachable
19: ; preds = %9
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 10)
%22 = load i32, i32* @EXIT_SUCCESS
%23 = tail call i32 @exit(i32 %22)
unreachable
}