#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 */
scalar_t__ EXIT_FAILURE;
int STDERR_FILENO;
int STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ _exit(scalar_t__);
int /*<<< orphan*/ eiki_close(int);
int eiki_dup2(int, int /*<<< orphan*/);
int execve(char const *, char *const *, char **);
scalar_t__ fork();
int pipe(int *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int eiki_execv_fd(const char *file, char *const argv[]) {
  int pipe_fd[2] = {-1, -1};
  int rv;
  pid_t pid;
  rv = pipe(pipe_fd);
  if (rv) {
    goto fail;
  }
  pid = fork();
  if (pid < 0) {
    goto fail;
  } else if (!pid) {
    char *envp[] = {NULL};
    eiki_close(pipe_fd[0]);
    eiki_close(STDERR_FILENO);
    eiki_close(STDIN_FILENO);
    rv = eiki_dup2(pipe_fd[1], STDOUT_FILENO);
    if (rv < 0) {
      _exit(EXIT_FAILURE);
    }
    eiki_close(pipe_fd[1]);
    rv = execve(file, argv, envp);
    _exit(EXIT_FAILURE);
  } else {
    int status;
    eiki_close(pipe_fd[1]);
    pipe_fd[1] = -1;
    rv = (int)waitpid(pid, &status, 0);
    if (rv <= 0) {
      goto fail;
    }
    if (WEXITSTATUS(status) == EXIT_FAILURE) {
      goto fail;
    }
    return pipe_fd[0];
  }
fail:
  if (pipe_fd[0] >= 0) {
    eiki_close(pipe_fd[0]);
  }
  if (pipe_fd[1] >= 0) {
    eiki_close(pipe_fd[1]);
  }
  return -1;
}


x86 assembly (clang, -O0):
<eiki_execv_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xe59(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c6 <eiki_execv_fd+0x36>
jmp    12d0 <eiki_execv_fd+0x140>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11e1 <eiki_execv_fd+0x51>
jmp    12d0 <eiki_execv_fd+0x140>
cmpq   $0x0,-0x30(%rbp)
jne    1278 <eiki_execv_fd+0xe8>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%edi
call   1040 <eiki_close@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1040 <eiki_close@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1040 <eiki_close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
call   10b0 <eiki_dup2@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    124d <eiki_execv_fd+0xbd>
mov    0x2d8b(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
mov    -0x1c(%rbp),%edi
call   1040 <eiki_close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1070 <execve@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>
mov    -0x1c(%rbp),%edi
call   1040 <eiki_close@plt>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     12a8 <eiki_execv_fd+0x118>
jmp    12d0 <eiki_execv_fd+0x140>
mov    -0x3c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    0x2d19(%rip),%rcx
cmp    (%rcx),%rax
jne    12c5 <eiki_execv_fd+0x135>
jmp    12d0 <eiki_execv_fd+0x140>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12fb <eiki_execv_fd+0x16b>
cmpl   $0x0,-0x20(%rbp)
jl     12e2 <eiki_execv_fd+0x152>
mov    -0x20(%rbp),%edi
call   1040 <eiki_close@plt>
cmpl   $0x0,-0x1c(%rbp)
jl     12f4 <eiki_execv_fd+0x164>
mov    -0x1c(%rbp),%edi
call   1040 <eiki_close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eiki_execv_fd>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0xffffffffffffffff,(%rsp)
ff
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %eax,%eax
je     11cf <eiki_execv_fd+0x4f>
mov    (%rsp),%ebx
test   %ebx,%ebx
js     11b1 <eiki_execv_fd+0x31>
mov    %ebx,%edi
call   1040 <eiki_close@plt>
mov    0x4(%rsp),%edi
mov    $0xffffffff,%ebx
test   %edi,%edi
js     11c3 <eiki_execv_fd+0x43>
call   1040 <eiki_close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11a3 <eiki_execv_fd+0x23>
je     121e <eiki_execv_fd+0x9e>
mov    %rax,%rbx
mov    0x4(%rsp),%edi
call   1040 <eiki_close@plt>
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %eax,%eax
jle    11a3 <eiki_execv_fd+0x23>
mov    0x8(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    0x2dbc(%rip),%rcx
mov    (%rsp),%ebx
cmp    (%rcx),%rax
je     11a6 <eiki_execv_fd+0x26>
jmp    11c3 <eiki_execv_fd+0x43>
movq   $0x0,0x8(%rsp)
mov    (%rsp),%edi
call   1040 <eiki_close@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1040 <eiki_close@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1040 <eiki_close@plt>
mov    0x4(%rsp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
call   10a0 <eiki_dup2@plt>
test   %eax,%eax
js     127a <eiki_execv_fd+0xfa>
mov    0x4(%rsp),%edi
call   1040 <eiki_close@plt>
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <execve@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%rdi
call   1030 <_exit@plt>

LLVM IR (-O0):
@__const.eiki_execv_fd.pipe_fd = [2 x i32] [i32 -1, i32 -1]
@STDERR_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@EXIT_FAILURE = global i64 0
define i32 @eiki_execv_fd(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i64
%9 = alloca [1 x i8*]
%10 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%11 = bitcast [2 x i32]* %6 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
br label %73
17: ; preds = %2
%18 = call i64 (...) @fork()
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
br label %73
22: ; preds = %17
%23 = load i64, i64* %8
%24 = icmp ne i64 %23, 0
br i1 %24, label %53, label %25
25: ; preds = %22
%26 = bitcast [1 x i8*]* %9 to i8*
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i32 @eiki_close(i32 %28)
%30 = load i32, i32* @STDERR_FILENO
%31 = call i32 @eiki_close(i32 %30)
%32 = load i32, i32* @STDIN_FILENO
%33 = call i32 @eiki_close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%35 = load i32, i32* %34
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @eiki_dup2(i32 %35, i32 %36)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %25
%41 = load i64, i64* @EXIT_FAILURE
%42 = call i32 @_exit(i64 %41)
unreachable
43: ; preds = %25
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @eiki_close(i32 %45)
%47 = load i8*, i8** %4
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds [1 x i8*], [1 x i8*]* %9, i64 0, i64 0
%50 = call i32 @execve(i8* %47, i8** %48, i8** %49)
store i32 %50, i32* %7
%51 = load i64, i64* @EXIT_FAILURE
%52 = call i32 @_exit(i64 %51)
unreachable
53: ; preds = %22
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @eiki_close(i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
store i32 -1, i32* %57
%58 = load i64, i64* %8
%59 = call i64 @waitpid(i64 %58, i32* %10, i32 0)
%60 = trunc i64 %59 to i32
store i32 %60, i32* %7
%61 = load i32, i32* %7
%62 = icmp sle i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %53
br label %73
64: ; preds = %53
%65 = load i32, i32* %10
%66 = call i64 @WEXITSTATUS(i32 %65)
%67 = load i64, i64* @EXIT_FAILURE
%68 = icmp eq i64 %66, %67
br i1 %68, label %69, label %70
69: ; preds = %64
br label %73
70: ; preds = %64
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%72 = load i32, i32* %71
store i32 %72, i32* %3
br label %90
73: ; preds = %69, %63, %21, %16
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%75 = load i32, i32* %74
%76 = icmp sge i32 %75, 0
br i1 %76, label %77, label %81
77: ; preds = %73
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%79 = load i32, i32* %78
%80 = call i32 @eiki_close(i32 %79)
br label %81
81: ; preds = %77, %73
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%83 = load i32, i32* %82
%84 = icmp sge i32 %83, 0
br i1 %84, label %85, label %89
85: ; preds = %81
%86 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%87 = load i32, i32* %86
%88 = call i32 @eiki_close(i32 %87)
br label %89
89: ; preds = %85, %81
store i32 -1, i32* %3
br label %90
90: ; preds = %89, %70
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@STDERR_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@EXIT_FAILURE = global i64 0
define i32 @eiki_execv_fd(i8* %0, i8** %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to [2 x i32]*
%5 = alloca [1 x i8*]
%6 = alloca i32
%7 = bitcast i64* %3 to i8*
store i64 -1, i64* %3
%8 = bitcast i64* %3 to i32*
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %55
11: ; preds = %2
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %55, label %14
14: ; preds = %11
%15 = icmp eq i64 %12, 0
br i1 %15, label %16, label %40
16: ; preds = %14
%17 = bitcast [1 x i8*]* %5 to i8*
%18 = bitcast [1 x i8*]* %5 to i64*
store i64 0, i64* %18
%19 = load i32, i32* %8
%20 = call i32 @eiki_close(i32 %19)
%21 = load i32, i32* @STDERR_FILENO
%22 = call i32 @eiki_close(i32 %21)
%23 = load i32, i32* @STDIN_FILENO
%24 = call i32 @eiki_close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%26 = load i32, i32* %25
%27 = load i32, i32* @STDOUT_FILENO
%28 = call i32 @eiki_dup2(i32 %26, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %16
%31 = load i64, i64* @EXIT_FAILURE
%32 = call i32 @_exit(i64 %31)
unreachable
33: ; preds = %16
%34 = load i32, i32* %25
%35 = call i32 @eiki_close(i32 %34)
%36 = getelementptr inbounds [1 x i8*], [1 x i8*]* %5, i64 0, i64 0
%37 = call i32 @execve(i8* %0, i8** %1, i8** %36)
%38 = load i64, i64* @EXIT_FAILURE
%39 = call i32 @_exit(i64 %38)
unreachable
40: ; preds = %14
%41 = bitcast i32* %6 to i8*
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @eiki_close(i32 %43)
store i32 -1, i32* %42
%45 = call i64 @waitpid(i64 %12, i32* %6, i32 0)
%46 = trunc i64 %45 to i32
%47 = icmp slt i32 %46, 1
br i1 %47, label %48, label %49
48: ; preds = %40
br label %55
49: ; preds = %40
%50 = load i32, i32* %6
%51 = call i64 @WEXITSTATUS(i32 %50)
%52 = load i64, i64* @EXIT_FAILURE
%53 = icmp eq i64 %51, %52
%54 = load i32, i32* %8
br i1 %53, label %57, label %68
55: ; preds = %2, %11, %48
%56 = load i32, i32* %8
br label %57
57: ; preds = %55, %49
%58 = phi i32 [ %56, %55 ], [ %54, %49 ]
%59 = icmp sgt i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = call i32 @eiki_close(i32 %58)
br label %62
62: ; preds = %60, %57
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%64 = load i32, i32* %63
%65 = icmp sgt i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 @eiki_close(i32 %64)
br label %68
68: ; preds = %62, %66, %49
%69 = phi i32 [ %54, %49 ], [ -1, %66 ], [ -1, %62 ]
ret i32 %69
}