#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ logInfo(char *, char *);
int /*<<< orphan*/ pexit(char *);
int vfork();
int main() {
  logInfo("Parent", "Calling vfork(2)");
  switch (vfork()) {
  case -1:
    pexit("vfork");
    break;
  case 0:
    logInfo("Child", "Closing stdout and finishing");
    if (close(STDOUT_FILENO) == -1) {
      pexit("close");
    }
    _exit(0);
    break;
  default:
    logInfo("Parent", "Back. If you can read this is because the file "
                      "descriptor was unaffected");
    break;
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1050 <logInfo@plt>
call   1060 <vfork@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
sub    $0xffffffff,%eax
je     11aa <main+0x4a>
jmp    119a <main+0x3a>
mov    -0x8(%rbp),%eax
test   %eax,%eax
je     11bb <main+0x5b>
jmp    11f8 <main+0x98>
lea    0xe67(%rip),%rdi
call   1080 <pexit@plt>
jmp    120b <main+0xab>
lea    0xe5c(%rip),%rdi
lea    0xe5b(%rip),%rsi
call   1050 <logInfo@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11f1 <main+0x91>
lea    0xe55(%rip),%rdi
call   1080 <pexit@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xe01(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1050 <logInfo@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xe98(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1050 <logInfo@plt>
call   1060 <vfork@plt>
test   %eax,%eax
je     1190 <main+0x30>
cmp    $0xffffffff,%eax
jne    11c9 <main+0x69>
lea    0xe8f(%rip),%rdi
call   1080 <pexit@plt>
jmp    11dc <main+0x7c>
lea    0xe87(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1050 <logInfo@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11c2 <main+0x62>
lea    0xe84(%rip),%rdi
call   1080 <pexit@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xe30(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1050 <logInfo@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"Parent\00"
@.str.1 = [17 x i8] c"Calling vfork(2)\00"
@.str.2 = [6 x i8] c"vfork\00"
@.str.3 = [6 x i8] c"Child\00"
@.str.4 = [29 x i8] c"Closing stdout and finishing\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [6 x i8] c"close\00"
@.str.6 = [73 x i8] c"Back. If you can read this is because the file descriptor was unaffected\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i32 @logInfo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%3 = call i32 @vfork()
switch i32 %3, label %15 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %17
6: ; preds = %0
%7 = call i32 @logInfo(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%8 = load i32, i32* @STDOUT_FILENO
%9 = call i32 @close(i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %13
13: ; preds = %11, %6
%14 = call i32 @_exit(i32 0)
unreachable
15: ; preds = %0
%16 = call i32 @logInfo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.6, i64 0, i64 0))
br label %17
17: ; preds = %15, %4
%18 = load i32, i32* @EXIT_SUCCESS
%19 = call i32 @exit(i32 %18)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"Parent\00"
@.str.1 = [17 x i8] c"Calling vfork(2)\00"
@.str.2 = [6 x i8] c"vfork\00"
@.str.3 = [6 x i8] c"Child\00"
@.str.4 = [29 x i8] c"Closing stdout and finishing\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [6 x i8] c"close\00"
@.str.6 = [73 x i8] c"Back. If you can read this is because the file descriptor was unaffected\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = call i32 @logInfo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%2 = call i32 @vfork()
switch i32 %2, label %14 [
i32 -1, label %3
i32 0, label %5
]
3: ; preds = %0
%4 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %16
5: ; preds = %0
%6 = call i32 @logInfo(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%7 = load i32, i32* @STDOUT_FILENO
%8 = call i32 @close(i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %12
12: ; preds = %10, %5
%13 = call i32 @_exit(i32 0)
unreachable
14: ; preds = %0
%15 = call i32 @logInfo(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.6, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i32, i32* @EXIT_SUCCESS
%18 = call i32 @exit(i32 %17)
unreachable
}