#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 WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ noop();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
scalar_t__ vfork();
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  int status;
  printf("Before vfork\n");
  fflush(stdout);
  pid = vfork();
  if (pid == 0) {
    noop();
    _exit(2);
  } else if (pid < 0)
    error(1, errno, "vfork");
  printf("After vfork (parent)\n");
  if (waitpid(0, &status, 0) != pid || !WIFEXITED(status) ||
      WEXITSTATUS(status) != 2)
    exit(1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x0,%al
call   10a0 <vfork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f0 <main+0x50>
mov    $0x0,%al
call   1040 <noop@plt>
mov    $0x2,%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x10(%rbp)
jge    1215 <main+0x75>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdfe(%rip),%rdx
call   1080 <error@plt>
jmp    121a <main+0x7a>
lea    0xdf3(%rip),%rdi
call   1050 <printf@plt>
xor    %edx,%edx
lea    -0x14(%rbp),%rsi
mov    %edx,%edi
call   1090 <waitpid@plt>
cmp    -0x10(%rbp),%rax
jne    125f <main+0xbf>
mov    -0x14(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%eax
je     125f <main+0xbf>
mov    -0x14(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x2,%eax
je     1269 <main+0xc9>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe54(%rip),%rdi
call   1050 <puts@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
call   10a0 <vfork@plt>
test   %rax,%rax
je     1236 <main+0x96>
mov    %rax,%rbx
jns    11ea <main+0x4a>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
lea    0xe1c(%rip),%rdi
call   1050 <puts@plt>
lea    0xc(%rsp),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    %rbx,%rax
jne    122c <main+0x8c>
mov    0xc(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
je     122c <main+0x8c>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x2,%eax
jne    122c <main+0x8c>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   1040 <noop@plt>
mov    $0x2,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Before vfork\0A\00"
@stdout = global i32 0
@errno = global i32 0
@.str.1 = [6 x i8] c"vfork\00"
@.str.2 = [22 x i8] c"After vfork (parent)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @stdout
%6 = call i32 @fflush(i32 %5)
%7 = call i64 (...) @vfork()
store i64 %7, i64* %2
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (...) @noop()
%12 = call i32 @_exit(i32 2)
unreachable
13: ; preds = %0
%14 = load i64, i64* %2
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = call i32 @error(i32 1, i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %16, %13
br label %20
20: ; preds = %19
%21 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%22 = call i64 @waitpid(i32 0, i32* %3, i32 0)
%23 = load i64, i64* %2
%24 = icmp ne i64 %22, %23
br i1 %24, label %33, label %25
25: ; preds = %20
%26 = load i32, i32* %3
%27 = call i32 @WIFEXITED(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* %3
%31 = call i32 @WEXITSTATUS(i32 %30)
%32 = icmp ne i32 %31, 2
br i1 %32, label %33, label %35
33: ; preds = %29, %25, %20
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@errno = global i32 0
@.str.1 = [6 x i8] c"vfork\00"
@str = [13 x i8] c"Before vfork\00"
@str.3 = [21 x i8] c"After vfork (parent)\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @stdout
%5 = tail call i32 @fflush(i32 %4)
%6 = tail call i64 (...) @vfork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 (...) @noop()
%10 = tail call i32 @_exit(i32 2)
unreachable
11: ; preds = %0
%12 = icmp slt i64 %6, 0
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = load i32, i32* @errno
%15 = tail call i32 @error(i32 1, i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %11, %13
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.3, i64 0, i64 0))
%18 = call i64 @waitpid(i32 0, i32* %1, i32 0)
%19 = icmp eq i64 %18, %6
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = load i32, i32* %1
%22 = call i32 @WIFEXITED(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i32, i32* %1
%26 = call i32 @WEXITSTATUS(i32 %25)
%27 = icmp eq i32 %26, 2
br i1 %27, label %30, label %28
28: ; preds = %24, %20, %16
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %24
ret i32 0
}