#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 errno;
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ vfork();
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t f, rv;
  int status;
  f = vfork();
  if (f == 0) {
    printf("Child (%d)...\n", (int)getpid());
    _exit(123);
  } else if (f > 0) {
    int err = 0;
    printf("Parent (child = %d)\n", (int)f);
    rv = waitpid(f, &status, 0);
    if (rv != f) {
      printf("waitpid returned %d, errno = %d\n", (int)rv, errno);
      err++;
    }
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 123) {
      printf("Child process existed with wrong status %d\n", status);
      err++;
    }
    return err;
  } else {
    printf("vfork returned %d, errno = %d\n", (int)f, errno);
    return 127;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <vfork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bd <main+0x4d>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x7b,%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1271 <main+0x101>
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     122a <main+0xba>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%eax
je     124c <main+0xdc>
mov    -0x24(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x7b,%eax
je     1266 <main+0xf6>
mov    -0x24(%rbp),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1295 <main+0x125>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x7f,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <vfork@plt>
test   %rax,%rax
je     1225 <main+0xb5>
mov    %rax,%rbx
jle    11fe <main+0x8e>
lea    0xe80(%rip),%rdi
xor    %ebp,%ebp
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    %rbx,%rax
je     11cc <main+0x5c>
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%edx
lea    0xe66(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
je     11e7 <main+0x77>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x7b,%eax
je     121c <main+0xac>
mov    0x4(%rsp),%esi
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
jmp    121c <main+0xac>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xe63(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x7f,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdcd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x7b,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Child (%d)...\0A\00"
@.str.1 = [21 x i8] c"Parent (child = %d)\0A\00"
@.str.2 = [33 x i8] c"waitpid returned %d, errno = %d\0A\00"
@errno = global i32 0
@.str.3 = [44 x i8] c"Child process existed with wrong status %d\0A\00"
@.str.4 = [31 x i8] c"vfork returned %d, errno = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i64 (...) @vfork()
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = call i64 (...) @getpid()
%15 = trunc i64 %14 to i32
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = call i32 @_exit(i32 123)
unreachable
18: ; preds = %2
%19 = load i64, i64* %6
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %52
21: ; preds = %18
store i32 0, i32* %9
%22 = load i64, i64* %6
%23 = trunc i64 %22 to i32
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i64, i64* %6
%26 = call i64 @waitpid(i64 %25, i32* %8, i32 0)
store i64 %26, i64* %7
%27 = load i64, i64* %7
%28 = load i64, i64* %6
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %37
30: ; preds = %21
%31 = load i64, i64* %7
%32 = trunc i64 %31 to i32
%33 = load i32, i32* @errno
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33)
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
store i32 %36, i32* %9
br label %37
37: ; preds = %30, %21
%38 = load i32, i32* %8
%39 = call i32 @WIFEXITED(i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* %8
%43 = call i32 @WEXITSTATUS(i32 %42)
%44 = icmp ne i32 %43, 123
br i1 %44, label %45, label %50
45: ; preds = %41, %37
%46 = load i32, i32* %8
%47 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %50
50: ; preds = %45, %41
%51 = load i32, i32* %9
store i32 %51, i32* %3
br label %57
52: ; preds = %18
%53 = load i64, i64* %6
%54 = trunc i64 %53 to i32
%55 = load i32, i32* @errno
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %54, i32 %55)
store i32 127, i32* %3
br label %57
57: ; preds = %52, %50
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [15 x i8] c"Child (%d)...\0A\00"
@.str.1 = [21 x i8] c"Parent (child = %d)\0A\00"
@.str.2 = [33 x i8] c"waitpid returned %d, errno = %d\0A\00"
@errno = global i32 0
@.str.3 = [44 x i8] c"Child process existed with wrong status %d\0A\00"
@.str.4 = [31 x i8] c"vfork returned %d, errno = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @vfork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = tail call i64 (...) @getpid()
%9 = trunc i64 %8 to i32
%10 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 @_exit(i32 123)
unreachable
12: ; preds = %2
%13 = icmp sgt i64 %5, 0
%14 = trunc i64 %5 to i32
br i1 %13, label %15, label %36
15: ; preds = %12
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%17 = call i64 @waitpid(i64 %5, i32* %3, i32 0)
%18 = icmp eq i64 %17, %5
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = trunc i64 %17 to i32
%21 = load i32, i32* @errno
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21)
br label %23
23: ; preds = %19, %15
%24 = phi i32 [ 1, %19 ], [ 0, %15 ]
%25 = load i32, i32* %3
%26 = call i32 @WIFEXITED(i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %23
%29 = load i32, i32* %3
%30 = call i32 @WEXITSTATUS(i32 %29)
%31 = icmp eq i32 %30, 123
br i1 %31, label %39, label %32
32: ; preds = %28, %23
%33 = load i32, i32* %3
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = add nuw nsw i32 %24, 1
br label %39
36: ; preds = %12
%37 = load i32, i32* @errno
%38 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %14, i32 %37)
br label %39
39: ; preds = %28, %32, %36
%40 = phi i32 [ 127, %36 ], [ %35, %32 ], [ %24, %28 ]
ret i32 %40
}