#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 int pid_t;

/* Variables and functions */
int EMSGSIZE;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
int pipe(int *);
int read_all(int, int *, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ write_all(int, int *, int);
int spawn(char *argv[], int *status, int do_wait) {
  pid_t pid;
  int err_pipe[2];
  if (pipe(err_pipe) == -1)
    return -1;
  pid = fork();
  if (pid == -1) {
    return -1;
  } else if (pid == 0) {
    int dev_null, err, i;
    if (!do_wait) {
      switch (fork()) {
      case 0:
        break;
      case -1:
        _exit(127);
      default:
        _exit(0);
      }
    }
    close(err_pipe[0]);
    fcntl(err_pipe[1], F_SETFD, FD_CLOEXEC);
    dev_null = open("/dev/null", O_WRONLY);
    if (dev_null != -1) {
      dup2(dev_null, 1);
      dup2(dev_null, 2);
    }
    close(0);
    for (i = 3; i < 30; i++)
      close(i);
    execvp(argv[0], argv);
    err = errno;
    write_all(err_pipe[1], &err, sizeof(int));
    exit(1);
  } else {
    int rc, errno_save, child_errno, tmp;
    close(err_pipe[1]);
    rc = read_all(err_pipe[0], &child_errno, sizeof(int));
    errno_save = errno;
    close(err_pipe[0]);
    if (!do_wait)
      status = &tmp;
    waitpid(pid, status, 0);
    if (rc == -1) {
      errno = errno_save;
      return -1;
    }
    if (rc == sizeof(int)) {
      errno = child_errno;
      return -1;
    }
    if (rc != 0) {
      errno = EMSGSIZE;
      return -1;
    }
    return 0;
  }
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11f1 <spawn+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <spawn+0x234>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1211 <spawn+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <spawn+0x234>
cmpl   $0x0,-0x20(%rbp)
jne    132d <spawn+0x16d>
cmpl   $0x0,-0x1c(%rbp)
jne    126a <spawn+0xaa>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x48(%rbp)
sub    $0xffffffff,%eax
je     1254 <spawn+0x94>
jmp    123f <spawn+0x7f>
mov    -0x48(%rbp),%eax
test   %eax,%eax
jne    125e <spawn+0x9e>
jmp    124f <spawn+0x8f>
jmp    1265 <spawn+0xa5>
mov    $0x7f,%edi
call   1030 <_exit@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    126a <spawn+0xaa>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    0x2d4b(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
lea    0xd64(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     12c8 <spawn+0x108>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
call   1060 <close@plt>
movl   $0x3,-0x34(%rbp)
cmpl   $0x1e,-0x34(%rbp)
jge    12f6 <spawn+0x136>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12d6 <spawn+0x116>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <execvp@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1080 <write_all@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   10e0 <read_all@plt>
mov    %eax,-0x38(%rbp)
mov    0x2c78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    136f <spawn+0x1af>
lea    -0x44(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmpl   $0xffffffff,-0x38(%rbp)
jne    139f <spawn+0x1df>
mov    -0x3c(%rbp),%ecx
mov    0x2c37(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <spawn+0x234>
movslq -0x38(%rbp),%rax
cmp    $0x4,%rax
jne    13c5 <spawn+0x205>
mov    -0x40(%rbp),%ecx
mov    0x2c11(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <spawn+0x234>
cmpl   $0x0,-0x38(%rbp)
je     13ed <spawn+0x22d>
mov    0x2c12(%rip),%rax
mov    (%rax),%ecx
mov    0x2be9(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <spawn+0x234>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xc(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     127a <spawn+0xba>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     127f <spawn+0xbf>
test   %ebp,%ebp
je     1290 <spawn+0xd0>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   10e0 <read_all@plt>
mov    %eax,%r14d
mov    0x2d9e(%rip),%r12
mov    (%r12),%r13d
mov    0xc(%rsp),%edi
call   1060 <close@plt>
test   %r15d,%r15d
lea    0x14(%rsp),%rax
cmove  %rax,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0xffffffff,%r14d
je     1276 <spawn+0xb6>
test   %r14d,%r14d
je     1267 <spawn+0xa7>
cmp    $0x4,%r14d
jne    126c <spawn+0xac>
mov    0x8(%rsp),%r13d
jmp    1276 <spawn+0xb6>
mov    %r14d,%ebp
jmp    127f <spawn+0xbf>
mov    0x2d75(%rip),%rax
mov    (%rax),%r13d
mov    %r13d,(%r12)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r15d,%r15d
jne    12b3 <spawn+0xf3>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12b3 <spawn+0xf3>
cmp    $0xffffffff,%eax
jne    1455 <spawn+0x295>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
mov    0x2d00(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
lea    0xd19(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     130b <spawn+0x14b>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x3,%edi
call   1060 <close@plt>
mov    $0x4,%edi
call   1060 <close@plt>
mov    $0x5,%edi
call   1060 <close@plt>
mov    $0x6,%edi
call   1060 <close@plt>
mov    $0x7,%edi
call   1060 <close@plt>
mov    $0x8,%edi
call   1060 <close@plt>
mov    $0x9,%edi
call   1060 <close@plt>
mov    $0xa,%edi
call   1060 <close@plt>
mov    $0xb,%edi
call   1060 <close@plt>
mov    $0xc,%edi
call   1060 <close@plt>
mov    $0xd,%edi
call   1060 <close@plt>
mov    $0xe,%edi
call   1060 <close@plt>
mov    $0xf,%edi
call   1060 <close@plt>
mov    $0x10,%edi
call   1060 <close@plt>
mov    $0x11,%edi
call   1060 <close@plt>
mov    $0x12,%edi
call   1060 <close@plt>
mov    $0x13,%edi
call   1060 <close@plt>
mov    $0x14,%edi
call   1060 <close@plt>
mov    $0x15,%edi
call   1060 <close@plt>
mov    $0x16,%edi
call   1060 <close@plt>
mov    $0x17,%edi
call   1060 <close@plt>
mov    $0x18,%edi
call   1060 <close@plt>
mov    $0x19,%edi
call   1060 <close@plt>
mov    $0x1a,%edi
call   1060 <close@plt>
mov    $0x1b,%edi
call   1060 <close@plt>
mov    $0x1c,%edi
call   1060 <close@plt>
mov    $0x1d,%edi
call   1060 <close@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   10b0 <execvp@plt>
mov    0x2b96(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x10(%rsp),%edi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1080 <write_all@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@EMSGSIZE = global i32 0
define i32 @spawn(i8** %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i32]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %114
21: ; preds = %3
%22 = call i32 (...) @fork()
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %4
br label %114
26: ; preds = %21
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %80
29: ; preds = %26
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %40, label %32
32: ; preds = %29
%33 = call i32 (...) @fork()
switch i32 %33, label %37 [
i32 0, label %34
i32 -1, label %35
]
34: ; preds = %32
br label %39
35: ; preds = %32
%36 = call i32 @_exit(i32 127)
unreachable
37: ; preds = %32
%38 = call i32 @_exit(i32 0)
unreachable
39: ; preds = %34
br label %40
40: ; preds = %39, %29
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%45 = load i32, i32* %44
%46 = load i32, i32* @F_SETFD
%47 = load i32, i32* @FD_CLOEXEC
%48 = call i32 @fcntl(i32 %45, i32 %46, i32 %47)
%49 = load i32, i32* @O_WRONLY
%50 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %49)
store i32 %50, i32* %10
%51 = load i32, i32* %10
%52 = icmp ne i32 %51, -1
br i1 %52, label %53, label %58
53: ; preds = %40
%54 = load i32, i32* %10
%55 = call i32 @dup2(i32 %54, i32 1)
%56 = load i32, i32* %10
%57 = call i32 @dup2(i32 %56, i32 2)
br label %58
58: ; preds = %53, %40
%59 = call i32 @close(i32 0)
store i32 3, i32* %12
br label %60
60: ; preds = %66, %58
%61 = load i32, i32* %12
%62 = icmp slt i32 %61, 30
br i1 %62, label %63, label %69
63: ; preds = %60
%64 = load i32, i32* %12
%65 = call i32 @close(i32 %64)
br label %66
66: ; preds = %63
%67 = load i32, i32* %12
%68 = add nsw i32 %67, 1
store i32 %68, i32* %12
br label %60
69: ; preds = %60
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 0
%72 = load i8*, i8** %71
%73 = load i8**, i8*** %5
%74 = call i32 @execvp(i8* %72, i8** %73)
%75 = load i32, i32* @errno
store i32 %75, i32* %11
%76 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%77 = load i32, i32* %76
%78 = call i32 @write_all(i32 %77, i32* %11, i32 4)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %26
%81 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%82 = load i32, i32* %81
%83 = call i32 @close(i32 %82)
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%85 = load i32, i32* %84
%86 = call i32 @read_all(i32 %85, i32* %15, i32 4)
store i32 %86, i32* %13
%87 = load i32, i32* @errno
store i32 %87, i32* %14
%88 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%89 = load i32, i32* %88
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* %7
%92 = icmp ne i32 %91, 0
br i1 %92, label %94, label %93
93: ; preds = %80
store i32* %16, i32** %6
br label %94
94: ; preds = %93, %80
%95 = load i32, i32* %8
%96 = load i32*, i32** %6
%97 = call i32 @waitpid(i32 %95, i32* %96, i32 0)
%98 = load i32, i32* %13
%99 = icmp eq i32 %98, -1
br i1 %99, label %100, label %102
100: ; preds = %94
%101 = load i32, i32* %14
store i32 %101, i32* @errno
store i32 -1, i32* %4
br label %114
102: ; preds = %94
%103 = load i32, i32* %13
%104 = sext i32 %103 to i64
%105 = icmp eq i64 %104, 4
br i1 %105, label %106, label %108
106: ; preds = %102
%107 = load i32, i32* %15
store i32 %107, i32* @errno
store i32 -1, i32* %4
br label %114
108: ; preds = %102
%109 = load i32, i32* %13
%110 = icmp ne i32 %109, 0
br i1 %110, label %111, label %113
111: ; preds = %108
%112 = load i32, i32* @EMSGSIZE
store i32 %112, i32* @errno
store i32 -1, i32* %4
br label %114
113: ; preds = %108
store i32 0, i32* %4
br label %114
114: ; preds = %113, %111, %106, %100, %25, %20
%115 = load i32, i32* %4
ret i32 %115
}

LLVM IR (-O2):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@EMSGSIZE = global i32 0
define i32 @spawn(i8** %0, i32* %1, i32 %2) {
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast [2 x i32]* %4 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %94, label %12
12: ; preds = %3
%13 = call i32 (...) @fork()
switch i32 %13, label %72 [
i32 -1, label %94
i32 0, label %14
]
14: ; preds = %12
%15 = bitcast i32* %5 to i8*
%16 = icmp eq i32 %2, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = call i32 (...) @fork()
switch i32 %18, label %21 [
i32 0, label %23
i32 -1, label %19
]
19: ; preds = %17
%20 = call i32 @_exit(i32 127)
unreachable
21: ; preds = %17
%22 = call i32 @_exit(i32 0)
unreachable
23: ; preds = %17, %14
%24 = load i32, i32* %9
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%27 = load i32, i32* %26
%28 = load i32, i32* @F_SETFD
%29 = load i32, i32* @FD_CLOEXEC
%30 = call i32 @fcntl(i32 %27, i32 %28, i32 %29)
%31 = load i32, i32* @O_WRONLY
%32 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %37, label %34
34: ; preds = %23
%35 = call i32 @dup2(i32 %32, i32 1)
%36 = call i32 @dup2(i32 %32, i32 2)
br label %37
37: ; preds = %34, %23
%38 = call i32 @close(i32 0)
%39 = call i32 @close(i32 3)
%40 = call i32 @close(i32 4)
%41 = call i32 @close(i32 5)
%42 = call i32 @close(i32 6)
%43 = call i32 @close(i32 7)
%44 = call i32 @close(i32 8)
%45 = call i32 @close(i32 9)
%46 = call i32 @close(i32 10)
%47 = call i32 @close(i32 11)
%48 = call i32 @close(i32 12)
%49 = call i32 @close(i32 13)
%50 = call i32 @close(i32 14)
%51 = call i32 @close(i32 15)
%52 = call i32 @close(i32 16)
%53 = call i32 @close(i32 17)
%54 = call i32 @close(i32 18)
%55 = call i32 @close(i32 19)
%56 = call i32 @close(i32 20)
%57 = call i32 @close(i32 21)
%58 = call i32 @close(i32 22)
%59 = call i32 @close(i32 23)
%60 = call i32 @close(i32 24)
%61 = call i32 @close(i32 25)
%62 = call i32 @close(i32 26)
%63 = call i32 @close(i32 27)
%64 = call i32 @close(i32 28)
%65 = call i32 @close(i32 29)
%66 = load i8*, i8** %0
%67 = call i32 @execvp(i8* %66, i8** %0)
%68 = load i32, i32* @errno
store i32 %68, i32* %5
%69 = load i32, i32* %26
%70 = call i32 @write_all(i32 %69, i32* %5, i32 4)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %12
%73 = bitcast i32* %6 to i8*
%74 = bitcast i32* %7 to i8*
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%76 = load i32, i32* %75
%77 = call i32 @close(i32 %76)
%78 = load i32, i32* %9
%79 = call i32 @read_all(i32 %78, i32* %6, i32 4)
%80 = load i32, i32* @errno
%81 = load i32, i32* %9
%82 = call i32 @close(i32 %81)
%83 = icmp eq i32 %2, 0
%84 = select i1 %83, i32* %7, i32* %1
%85 = call i32 @waitpid(i32 %13, i32* %84, i32 0)
switch i32 %79, label %88 [
i32 -1, label %90
i32 4, label %86
i32 0, label %92
]
86: ; preds = %72
%87 = load i32, i32* %6
br label %90
88: ; preds = %72
%89 = load i32, i32* @EMSGSIZE
br label %90
90: ; preds = %72, %86, %88
%91 = phi i32 [ %89, %88 ], [ %87, %86 ], [ %80, %72 ]
store i32 %91, i32* @errno
br label %92
92: ; preds = %90, %72
%93 = phi i32 [ %79, %72 ], [ -1, %90 ]
br label %94
94: ; preds = %12, %3, %92
%95 = phi i32 [ %93, %92 ], [ -1, %3 ], [ %13, %12 ]
ret i32 %95
}