#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 EXIT_FAILURE;
int WEXITSTATUS(int);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ vfork();
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, ...);
int spawn(int argc, char *argv[]) {
  pid_t pid;
  int status;
  status = 0;
  pid = vfork();
  if (pid < 0) {
    warn("vfork failed");
    return (EXIT_FAILURE);
  }
  if (pid == 0) {
    execvp(argv[0], argv);
    warn("%s failed", argv[0]);
    _exit(EXIT_FAILURE);
  }
  if (waitpid(pid, &status, 0) < 0)
    warn("waitpid failed");
  return (WEXITSTATUS(status));
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <vfork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11ab <spawn+0x4b>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <warn@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    121b <spawn+0xbb>
cmpq   $0x0,-0x18(%rbp)
jne    11e9 <spawn+0x89>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <execvp@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <warn@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jge    1210 <spawn+0xb0>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <warn@plt>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   1070 <vfork@plt>
test   %rax,%rax
js     11ab <spawn+0x4b>
je     11c8 <spawn+0x68>
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
jns    11a0 <spawn+0x40>
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
call   1040 <warn@plt>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
jmp    11c2 <spawn+0x62>
lea    0xe4e(%rip),%rdi
xor    %eax,%eax
call   1040 <warn@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1080 <execvp@plt>
mov    (%rbx),%rsi
lea    0xe30(%rip),%rdi
xor    %eax,%eax
call   1040 <warn@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"vfork failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"%s failed\00"
@.str.2 = [15 x i8] c"waitpid failed\00"
define i32 @spawn(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, 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 %14
11: ; preds = %2
%12 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
store i32 %13, i32* %3
br label %38
14: ; preds = %2
%15 = load i64, i64* %6
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = load i8**, i8*** %5
%22 = call i32 @execvp(i8* %20, i8** %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @_exit(i32 %27)
unreachable
29: ; preds = %14
%30 = load i64, i64* %6
%31 = call i64 @waitpid(i64 %30, i32* %7, i32 0)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* %7
%37 = call i32 @WEXITSTATUS(i32 %36)
store i32 %37, i32* %3
br label %38
38: ; preds = %35, %11
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [13 x i8] c"vfork failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"%s failed\00"
@.str.2 = [15 x i8] c"waitpid failed\00"
define i32 @spawn(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i64 (...) @vfork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
br label %27
10: ; preds = %2
%11 = icmp eq i64 %5, 0
br i1 %11, label %12, label %19
12: ; preds = %10
%13 = load i8*, i8** %1
%14 = tail call i32 @execvp(i8* %13, i8** %1)
%15 = load i8*, i8** %1
%16 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @_exit(i32 %17)
unreachable
19: ; preds = %10
%20 = call i64 @waitpid(i64 %5, i32* %3, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = load i32, i32* %3
%26 = call i32 @WEXITSTATUS(i32 %25)
br label %27
27: ; preds = %24, %7
%28 = phi i32 [ %9, %7 ], [ %26, %24 ]
ret i32 %28
}