#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*/ 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) {
  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;
    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;
    close(err_pipe[1]);
    rc = read_all(err_pipe[0], &child_errno, sizeof(int));
    errno_save = errno;
    close(err_pipe[0]);
    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    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11de <spawn+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <spawn+0x1d0>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11fe <spawn+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <spawn+0x1d0>
cmpl   $0x0,-0x1c(%rbp)
jne    12cb <spawn+0x11b>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    0xdc6(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     1266 <spawn+0xb6>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x28(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
xor    %edi,%edi
call   1050 <close@plt>
movl   $0x3,-0x30(%rbp)
cmpl   $0x1e,-0x30(%rbp)
jge    1294 <spawn+0xe4>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1274 <spawn+0xc4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <execvp@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1070 <write_all@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
call   10d0 <read_all@plt>
mov    %eax,-0x34(%rbp)
mov    0x2cda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmpl   $0xffffffff,-0x34(%rbp)
jne    132b <spawn+0x17b>
mov    -0x38(%rbp),%ecx
mov    0x2cab(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <spawn+0x1d0>
movslq -0x34(%rbp),%rax
cmp    $0x4,%rax
jne    1351 <spawn+0x1a1>
mov    -0x3c(%rbp),%ecx
mov    0x2c85(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <spawn+0x1d0>
cmpl   $0x0,-0x34(%rbp)
je     1379 <spawn+0x1c9>
mov    0x2c86(%rip),%rax
mov    (%rax),%ecx
mov    0x2c5d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <spawn+0x1d0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1246 <spawn+0x96>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     124b <spawn+0x9b>
test   %ebp,%ebp
je     125a <spawn+0xaa>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   10d0 <read_all@plt>
mov    %eax,%ebx
mov    0x2dc0(%rip),%r14
mov    (%r14),%r12d
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    %ebp,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0xffffffff,%ebx
je     1243 <spawn+0x93>
test   %ebx,%ebx
je     1235 <spawn+0x85>
cmp    $0x4,%ebx
jne    1239 <spawn+0x89>
mov    0x4(%rsp),%r12d
jmp    1243 <spawn+0x93>
mov    %ebx,%ebp
jmp    124b <spawn+0x9b>
mov    0x2da8(%rip),%rax
mov    (%rax),%r12d
mov    %r12d,(%r14)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
lea    0xd72(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12b2 <spawn+0x102>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x3,%edi
call   1050 <close@plt>
mov    $0x4,%edi
call   1050 <close@plt>
mov    $0x5,%edi
call   1050 <close@plt>
mov    $0x6,%edi
call   1050 <close@plt>
mov    $0x7,%edi
call   1050 <close@plt>
mov    $0x8,%edi
call   1050 <close@plt>
mov    $0x9,%edi
call   1050 <close@plt>
mov    $0xa,%edi
call   1050 <close@plt>
mov    $0xb,%edi
call   1050 <close@plt>
mov    $0xc,%edi
call   1050 <close@plt>
mov    $0xd,%edi
call   1050 <close@plt>
mov    $0xe,%edi
call   1050 <close@plt>
mov    $0xf,%edi
call   1050 <close@plt>
mov    $0x10,%edi
call   1050 <close@plt>
mov    $0x11,%edi
call   1050 <close@plt>
mov    $0x12,%edi
call   1050 <close@plt>
mov    $0x13,%edi
call   1050 <close@plt>
mov    $0x14,%edi
call   1050 <close@plt>
mov    $0x15,%edi
call   1050 <close@plt>
mov    $0x16,%edi
call   1050 <close@plt>
mov    $0x17,%edi
call   1050 <close@plt>
mov    $0x18,%edi
call   1050 <close@plt>
mov    $0x19,%edi
call   1050 <close@plt>
mov    $0x1a,%edi
call   1050 <close@plt>
mov    $0x1b,%edi
call   1050 <close@plt>
mov    $0x1c,%edi
call   1050 <close@plt>
mov    $0x1d,%edi
call   1050 <close@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   10a0 <execvp@plt>
mov    0x2bef(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0xc(%rsp),%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1070 <write_all@plt>
mov    $0x1,%edi
call   10b0 <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) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8** %0, i8*** %4
store i32* %1, i32** %5
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %96
18: ; preds = %2
%19 = call i32 (...) @fork()
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %3
br label %96
23: ; preds = %18
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %66
26: ; preds = %23
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%31 = load i32, i32* %30
%32 = load i32, i32* @F_SETFD
%33 = load i32, i32* @FD_CLOEXEC
%34 = call i32 @fcntl(i32 %31, i32 %32, i32 %33)
%35 = load i32, i32* @O_WRONLY
%36 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %26
%40 = load i32, i32* %8
%41 = call i32 @dup2(i32 %40, i32 1)
%42 = load i32, i32* %8
%43 = call i32 @dup2(i32 %42, i32 2)
br label %44
44: ; preds = %39, %26
%45 = call i32 @close(i32 0)
store i32 3, i32* %10
br label %46
46: ; preds = %52, %44
%47 = load i32, i32* %10
%48 = icmp slt i32 %47, 30
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = load i32, i32* %10
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %46
55: ; preds = %46
%56 = load i8**, i8*** %4
%57 = getelementptr inbounds i8*, i8** %56, i64 0
%58 = load i8*, i8** %57
%59 = load i8**, i8*** %4
%60 = call i32 @execvp(i8* %58, i8** %59)
%61 = load i32, i32* @errno
store i32 %61, i32* %9
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @write_all(i32 %63, i32* %9, i32 4)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %23
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%71 = load i32, i32* %70
%72 = call i32 @read_all(i32 %71, i32* %13, i32 4)
store i32 %72, i32* %11
%73 = load i32, i32* @errno
store i32 %73, i32* %12
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%75 = load i32, i32* %74
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* %6
%78 = load i32*, i32** %5
%79 = call i32 @waitpid(i32 %77, i32* %78, i32 0)
%80 = load i32, i32* %11
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %84
82: ; preds = %66
%83 = load i32, i32* %12
store i32 %83, i32* @errno
store i32 -1, i32* %3
br label %96
84: ; preds = %66
%85 = load i32, i32* %11
%86 = sext i32 %85 to i64
%87 = icmp eq i64 %86, 4
br i1 %87, label %88, label %90
88: ; preds = %84
%89 = load i32, i32* %13
store i32 %89, i32* @errno
store i32 -1, i32* %3
br label %96
90: ; preds = %84
%91 = load i32, i32* %11
%92 = icmp ne i32 %91, 0
br i1 %92, label %93, label %95
93: ; preds = %90
%94 = load i32, i32* @EMSGSIZE
store i32 %94, i32* @errno
store i32 -1, i32* %3
br label %96
95: ; preds = %90
store i32 0, i32* %3
br label %96
96: ; preds = %95, %93, %88, %82, %22, %17
%97 = load i32, i32* %3
ret i32 %97
}

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) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %81, label %10
10: ; preds = %2
%11 = call i32 (...) @fork()
switch i32 %11, label %62 [
i32 -1, label %81
i32 0, label %12
]
12: ; preds = %10
%13 = bitcast i32* %4 to i8*
%14 = load i32, i32* %7
%15 = call i32 @close(i32 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%17 = load i32, i32* %16
%18 = load i32, i32* @F_SETFD
%19 = load i32, i32* @FD_CLOEXEC
%20 = call i32 @fcntl(i32 %17, i32 %18, i32 %19)
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %27, label %24
24: ; preds = %12
%25 = call i32 @dup2(i32 %22, i32 1)
%26 = call i32 @dup2(i32 %22, i32 2)
br label %27
27: ; preds = %24, %12
%28 = call i32 @close(i32 0)
%29 = call i32 @close(i32 3)
%30 = call i32 @close(i32 4)
%31 = call i32 @close(i32 5)
%32 = call i32 @close(i32 6)
%33 = call i32 @close(i32 7)
%34 = call i32 @close(i32 8)
%35 = call i32 @close(i32 9)
%36 = call i32 @close(i32 10)
%37 = call i32 @close(i32 11)
%38 = call i32 @close(i32 12)
%39 = call i32 @close(i32 13)
%40 = call i32 @close(i32 14)
%41 = call i32 @close(i32 15)
%42 = call i32 @close(i32 16)
%43 = call i32 @close(i32 17)
%44 = call i32 @close(i32 18)
%45 = call i32 @close(i32 19)
%46 = call i32 @close(i32 20)
%47 = call i32 @close(i32 21)
%48 = call i32 @close(i32 22)
%49 = call i32 @close(i32 23)
%50 = call i32 @close(i32 24)
%51 = call i32 @close(i32 25)
%52 = call i32 @close(i32 26)
%53 = call i32 @close(i32 27)
%54 = call i32 @close(i32 28)
%55 = call i32 @close(i32 29)
%56 = load i8*, i8** %0
%57 = call i32 @execvp(i8* %56, i8** %0)
%58 = load i32, i32* @errno
store i32 %58, i32* %4
%59 = load i32, i32* %16
%60 = call i32 @write_all(i32 %59, i32* %4, i32 4)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %10
%63 = bitcast i32* %5 to i8*
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %7
%68 = call i32 @read_all(i32 %67, i32* %5, i32 4)
%69 = load i32, i32* @errno
%70 = load i32, i32* %7
%71 = call i32 @close(i32 %70)
%72 = call i32 @waitpid(i32 %11, i32* %1, i32 0)
switch i32 %68, label %75 [
i32 -1, label %77
i32 4, label %73
i32 0, label %79
]
73: ; preds = %62
%74 = load i32, i32* %5
br label %77
75: ; preds = %62
%76 = load i32, i32* @EMSGSIZE
br label %77
77: ; preds = %62, %73, %75
%78 = phi i32 [ %76, %75 ], [ %74, %73 ], [ %69, %62 ]
store i32 %78, i32* @errno
br label %79
79: ; preds = %77, %62
%80 = phi i32 [ %68, %62 ], [ -1, %77 ]
br label %81
81: ; preds = %10, %2, %79
%82 = phi i32 [ %80, %79 ], [ -1, %2 ], [ %11, %10 ]
ret i32 %82
}