#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*/ O_WRONLY;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int errno;
int /*<<< orphan*/ execv(char *const, char *const *);
int /*<<< orphan*/ message(char *const *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sayn(int);
int /*<<< orphan*/ says(char *);
scalar_t__ vfork();
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
void vexec(int failcode, char *const path[]) {
  pid_t pid;
  int status, save_errno;
  int devnull = 0;
  if (failcode < 0) {
    devnull = 1;
    failcode = -failcode;
  }
  pid = vfork();
  if (pid == 0) {
    int fd;
    if (devnull && (fd = open("/dev/null", O_WRONLY)) >= 0) {
      dup2(fd, 1);
      dup2(fd, 2);
      close(fd);
    }
    execv(path[0], path + 1);
    save_errno = errno;
    message(path);
    says(" exec failed with errno ");
    sayn(save_errno);
    says("\n");
    _exit(failcode);
  } else if (pid < 0) {
    save_errno = errno;
    message(path);
    says(" fork failed with errno ");
    sayn(save_errno);
    says("\n");
    _exit(failcode + 1);
  }
  if (waitpid(0, &status, 0) != pid || !WIFEXITED(status)) {
    message(path);
    says(" child terminated abnormally\n");
    _exit(failcode + 2);
  }
  if (WEXITSTATUS(status)) {
    message(path);
    says(" child exited with exit code ");
    sayn(WEXITSTATUS(status));
    says("\n");
    _exit(WEXITSTATUS(status));
  }
}


x86 assembly (clang, -O0):
<vexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11ef <vexec+0x2f>
movl   $0x1,-0x24(%rbp)
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   10c0 <vfork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12a3 <vexec+0xe3>
cmpl   $0x0,-0x24(%rbp)
je     1252 <vexec+0x92>
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    0xde1(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jl     1252 <vexec+0x92>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    -0x28(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10e0 <execv@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <message@plt>
lea    0xd88(%rip),%rdi
call   1050 <says@plt>
mov    -0x20(%rbp),%edi
call   1080 <sayn@plt>
lea    0xdc2(%rip),%rdi
call   1050 <says@plt>
mov    -0x4(%rbp),%edi
call   1040 <_exit@plt>
cmpq   $0x0,-0x18(%rbp)
jge    12ee <vexec+0x12e>
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <message@plt>
lea    0xd59(%rip),%rdi
call   1050 <says@plt>
mov    -0x20(%rbp),%edi
call   1080 <sayn@plt>
lea    0xd7a(%rip),%rdi
call   1050 <says@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1040 <_exit@plt>
jmp    12f3 <vexec+0x133>
xor    %edx,%edx
lea    -0x1c(%rbp),%rsi
mov    %edx,%edi
call   10a0 <waitpid@plt>
cmp    -0x18(%rbp),%rax
jne    131b <vexec+0x15b>
mov    -0x1c(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    133b <vexec+0x17b>
mov    -0x10(%rbp),%rdi
call   1030 <message@plt>
lea    0xd11(%rip),%rdi
call   1050 <says@plt>
mov    -0x4(%rbp),%edi
add    $0x2,%edi
call   1040 <_exit@plt>
mov    -0x1c(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     138b <vexec+0x1cb>
mov    -0x10(%rbp),%rdi
call   1030 <message@plt>
lea    0xcfe(%rip),%rdi
call   1050 <says@plt>
mov    -0x1c(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%edi
call   1080 <sayn@plt>
lea    0xce1(%rip),%rdi
call   1050 <says@plt>
mov    -0x1c(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%edi
call   1040 <_exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vexec>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
mov    %edi,%r15d
neg    %r15d
cmovs  %edi,%r15d
xor    %eax,%eax
call   10c0 <vfork@plt>
test   %rax,%rax
je     1247 <vexec+0x87>
js     12c9 <vexec+0x109>
mov    %rax,%rbp
lea    0x4(%rsp),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmp    %rbp,%rax
jne    1227 <vexec+0x67>
mov    0x4(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %eax,%eax
je     1227 <vexec+0x67>
mov    0x4(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
test   %eax,%eax
jne    1305 <vexec+0x145>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1030 <message@plt>
lea    0xe06(%rip),%rdi
call   1050 <says@plt>
add    $0x2,%r15d
mov    %r15d,%edi
call   1040 <_exit@plt>
test   %ebx,%ebx
jns    1285 <vexec+0xc5>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
lea    0xda5(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1285 <vexec+0xc5>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    (%r14),%rdi
lea    0x8(%r14),%rsi
call   10e0 <execv@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%ebx
mov    %r14,%rdi
call   1030 <message@plt>
lea    0xd61(%rip),%rdi
call   1050 <says@plt>
mov    %ebx,%edi
call   1080 <sayn@plt>
lea    0xd9c(%rip),%rdi
call   1050 <says@plt>
mov    %r15d,%edi
call   1040 <_exit@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%ebx
mov    %r14,%rdi
call   1030 <message@plt>
lea    0xd42(%rip),%rdi
call   1050 <says@plt>
mov    %ebx,%edi
call   1080 <sayn@plt>
lea    0xd64(%rip),%rdi
call   1050 <says@plt>
add    $0x1,%r15d
mov    %r15d,%edi
call   1040 <_exit@plt>
mov    %r14,%rdi
call   1030 <message@plt>
lea    0xd46(%rip),%rdi
call   1050 <says@plt>
mov    0x4(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%edi
call   1080 <sayn@plt>
lea    0xd28(%rip),%rdi
call   1050 <says@plt>
mov    0x4(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@.str.1 = [25 x i8] c" exec failed with errno \00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [25 x i8] c" fork failed with errno \00"
@.str.4 = [30 x i8] c" child terminated abnormally\0A\00"
@.str.5 = [30 x i8] c" child exited with exit code \00"
define void @vexec(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %8
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
store i32 1, i32* %8
%13 = load i32, i32* %3
%14 = sub nsw i32 0, %13
store i32 %14, i32* %3
br label %15
15: ; preds = %12, %2
%16 = call i64 (...) @vfork()
store i64 %16, i64* %5
%17 = load i64, i64* %5
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %49
19: ; preds = %15
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %19
%23 = load i32, i32* @O_WRONLY
%24 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %23)
store i32 %24, i32* %9
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i32, i32* %9
%28 = call i32 @dup2(i32 %27, i32 1)
%29 = load i32, i32* %9
%30 = call i32 @dup2(i32 %29, i32 2)
%31 = load i32, i32* %9
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %26, %22, %19
%34 = load i8**, i8*** %4
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = load i8**, i8*** %4
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = call i32 @execv(i8* %36, i8** %38)
%40 = load i32, i32* @errno
store i32 %40, i32* %7
%41 = load i8**, i8*** %4
%42 = call i32 @message(i8** %41)
%43 = call i32 @says(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%44 = load i32, i32* %7
%45 = call i32 @sayn(i32 %44)
%46 = call i32 @says(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* %3
%48 = call i32 @_exit(i32 %47)
unreachable
49: ; preds = %15
%50 = load i64, i64* %5
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %63
52: ; preds = %49
%53 = load i32, i32* @errno
store i32 %53, i32* %7
%54 = load i8**, i8*** %4
%55 = call i32 @message(i8** %54)
%56 = call i32 @says(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* %7
%58 = call i32 @sayn(i32 %57)
%59 = call i32 @says(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%60 = load i32, i32* %3
%61 = add nsw i32 %60, 1
%62 = call i32 @_exit(i32 %61)
unreachable
63: ; preds = %49
br label %64
64: ; preds = %63
%65 = call i64 @waitpid(i32 0, i32* %6, i32 0)
%66 = load i64, i64* %5
%67 = icmp ne i64 %65, %66
br i1 %67, label %72, label %68
68: ; preds = %64
%69 = load i32, i32* %6
%70 = call i32 @WIFEXITED(i32 %69)
%71 = icmp ne i32 %70, 0
br i1 %71, label %79, label %72
72: ; preds = %68, %64
%73 = load i8**, i8*** %4
%74 = call i32 @message(i8** %73)
%75 = call i32 @says(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%76 = load i32, i32* %3
%77 = add nsw i32 %76, 2
%78 = call i32 @_exit(i32 %77)
unreachable
79: ; preds = %68
%80 = load i32, i32* %6
%81 = call i32 @WEXITSTATUS(i32 %80)
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %94
83: ; preds = %79
%84 = load i8**, i8*** %4
%85 = call i32 @message(i8** %84)
%86 = call i32 @says(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%87 = load i32, i32* %6
%88 = call i32 @WEXITSTATUS(i32 %87)
%89 = call i32 @sayn(i32 %88)
%90 = call i32 @says(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%91 = load i32, i32* %6
%92 = call i32 @WEXITSTATUS(i32 %91)
%93 = call i32 @_exit(i32 %92)
unreachable
94: ; preds = %79
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@.str.1 = [25 x i8] c" exec failed with errno \00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [25 x i8] c" fork failed with errno \00"
@.str.4 = [30 x i8] c" child terminated abnormally\0A\00"
@.str.5 = [30 x i8] c" child exited with exit code \00"
define void @vexec(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 0
%6 = sub nsw i32 0, %0
%7 = select i1 %5, i32 %6, i32 %0
%8 = tail call i64 (...) @vfork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %29
10: ; preds = %2
br i1 %5, label %11, label %19
11: ; preds = %10
%12 = load i32, i32* @O_WRONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = tail call i32 @dup2(i32 %13, i32 1)
%17 = tail call i32 @dup2(i32 %13, i32 2)
%18 = tail call i32 @close(i32 %13)
br label %19
19: ; preds = %15, %11, %10
%20 = load i8*, i8** %1
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = tail call i32 @execv(i8* %20, i8** %21)
%23 = load i32, i32* @errno
%24 = tail call i32 @message(i8** %1)
%25 = tail call i32 @says(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%26 = tail call i32 @sayn(i32 %23)
%27 = tail call i32 @says(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @_exit(i32 %7)
unreachable
29: ; preds = %2
%30 = icmp slt i64 %8, 0
br i1 %30, label %31, label %39
31: ; preds = %29
%32 = load i32, i32* @errno
%33 = tail call i32 @message(i8** %1)
%34 = tail call i32 @says(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 @sayn(i32 %32)
%36 = tail call i32 @says(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = add nuw nsw i32 %7, 1
%38 = tail call i32 @_exit(i32 %37)
unreachable
39: ; preds = %29
%40 = call i64 @waitpid(i32 0, i32* %3, i32 0)
%41 = icmp eq i64 %40, %8
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i32, i32* %3
%44 = call i32 @WIFEXITED(i32 %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %42, %39
%47 = call i32 @message(i8** %1)
%48 = call i32 @says(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%49 = add nuw nsw i32 %7, 2
%50 = call i32 @_exit(i32 %49)
unreachable
51: ; preds = %42
%52 = load i32, i32* %3
%53 = call i32 @WEXITSTATUS(i32 %52)
%54 = icmp eq i32 %53, 0
br i1 %54, label %65, label %55
55: ; preds = %51
%56 = call i32 @message(i8** %1)
%57 = call i32 @says(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%58 = load i32, i32* %3
%59 = call i32 @WEXITSTATUS(i32 %58)
%60 = call i32 @sayn(i32 %59)
%61 = call i32 @says(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%62 = load i32, i32* %3
%63 = call i32 @WEXITSTATUS(i32 %62)
%64 = call i32 @_exit(i32 %63)
unreachable
65: ; preds = %51
ret void
}