#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char const *, char *const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int fork();
int pipe(int *);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, int /*<<< orphan*/ *, int);
int spawnv(const char *file, char *const argv[]) {
  int fd[2];
  if (pipe(fd) == -1)
    return -1;
  switch (fork()) {
  case 0:
    switch (fork()) {
    case 0:
      close(fd[0]);
      if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) == 0)
        execvp(file, argv);
    case -1:
      write(fd[1], &errno, sizeof errno);
      exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
  case -1:
    close(fd[0]);
    close(fd[1]);
    return -1;
  }
  wait(NULL);
  close(fd[1]);
  if (read(fd[0], &errno, sizeof errno)) {
    close(fd[0]);
    return -1;
  }
  close(fd[0]);
  return 0;
}


x86 assembly (clang, -O0):
<spawnv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11be <spawnv+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <spawnv+0x15d>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     127f <spawnv+0xef>
jmp    11d8 <spawnv+0x48>
mov    -0x24(%rbp),%eax
test   %eax,%eax
jne    129b <spawnv+0x10b>
jmp    11e8 <spawnv+0x58>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     124f <spawnv+0xbf>
jmp    1202 <spawnv+0x72>
mov    -0x28(%rbp),%eax
test   %eax,%eax
jne    1271 <spawnv+0xe1>
jmp    1212 <spawnv+0x82>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jne    124a <spawnv+0xba>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <execvp@plt>
jmp    124f <spawnv+0xbf>
mov    -0x1c(%rbp),%edi
mov    0x2d77(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <spawnv+0x15d>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
mov    0x2d1a(%rip),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
je     12de <spawnv+0x14e>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <spawnv+0x15d>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawnv>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     120c <spawnv+0x7c>
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     11fa <spawnv+0x6a>
test   %eax,%eax
je     1217 <spawnv+0x87>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2df3(%rip),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1050 <close@plt>
neg    %rbx
sbb    %ebp,%ebp
jmp    120c <spawnv+0x7c>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     125a <spawnv+0xca>
test   %eax,%eax
jne    127d <spawnv+0xed>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
test   %eax,%eax
jne    125a <spawnv+0xca>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <execvp@plt>
mov    0xc(%rsp),%edi
mov    0x2d6b(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @spawnv(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca [2 x i32]
store i8* %0, i8** %4
store i8** %1, i8*** %5
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %63
11: ; preds = %2
%12 = call i32 (...) @fork()
switch i32 %12, label %46 [
i32 0, label %13
i32 -1, label %39
]
13: ; preds = %11
%14 = call i32 (...) @fork()
switch i32 %14, label %36 [
i32 0, label %15
i32 -1, label %30
]
15: ; preds = %13
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%20 = load i32, i32* %19
%21 = load i32, i32* @F_SETFD
%22 = load i32, i32* @FD_CLOEXEC
%23 = call i32 @fcntl(i32 %20, i32 %21, i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %15
%26 = load i8*, i8** %4
%27 = load i8**, i8*** %5
%28 = call i32 @execvp(i8* %26, i8** %27)
br label %29
29: ; preds = %25, %15
br label %30
30: ; preds = %13, %29
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @write(i32 %32, i32* @errno, i32 4)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %13
%37 = load i32, i32* @EXIT_SUCCESS
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %11
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
store i32 -1, i32* %3
br label %63
46: ; preds = %11
%47 = call i32 @wait(i32* null)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%52 = load i32, i32* %51
%53 = call i64 @read(i32 %52, i32* @errno, i32 4)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %46
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %3
br label %63
59: ; preds = %46
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
store i32 0, i32* %3
br label %63
63: ; preds = %59, %55, %39, %10
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @spawnv(i8* %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %49, label %8
8: ; preds = %2
%9 = call i32 (...) @fork()
switch i32 %9, label %38 [
i32 0, label %10
i32 -1, label %32
]
10: ; preds = %8
%11 = call i32 (...) @fork()
switch i32 %11, label %29 [
i32 0, label %12
i32 -1, label %23
]
12: ; preds = %10
%13 = load i32, i32* %5
%14 = call i32 @close(i32 %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%16 = load i32, i32* %15
%17 = load i32, i32* @F_SETFD
%18 = load i32, i32* @FD_CLOEXEC
%19 = call i32 @fcntl(i32 %16, i32 %17, i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @execvp(i8* %0, i8** %1)
br label %23
23: ; preds = %12, %21, %10
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @write(i32 %25, i32* @errno, i32 4)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %10
%30 = load i32, i32* @EXIT_SUCCESS
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %8
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
br label %49
38: ; preds = %8
%39 = call i32 @wait(i32* null)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %5
%44 = call i64 @read(i32 %43, i32* @errno, i32 4)
%45 = icmp ne i64 %44, 0
%46 = load i32, i32* %5
%47 = call i32 @close(i32 %46)
%48 = sext i1 %45 to i32
br label %49
49: ; preds = %38, %2, %32
%50 = phi i32 [ -1, %32 ], [ -1, %2 ], [ %48, %38 ]
ret i32 %50
}