#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__ ssize_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ junk;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ eiki_close(int);
scalar_t__ eiki_read(int, char *, int);
int /*<<< orphan*/ eiki_write(int, char *, int);
int execve(char const *, char *const *, char **);
int fcntl(int, int /*<<< orphan*/, ...);
scalar_t__ fork();
int pipe(int *);
pid_t eiki_execv_async(const char *file, char *const argv[]) {
  int rv;
  pid_t pid;
  int success_pipe_fd[2] = {-1, -1};
  rv = pipe(success_pipe_fd);
  if (rv) {
    goto fail;
  }
  pid = fork();
  if (pid < 0) {
    goto fail;
  } else if (!pid) {
    char *envp[] = {NULL};
    char junk[1] = {0};
    int fflags;
    eiki_close(success_pipe_fd[0]);
    fflags = fcntl(success_pipe_fd[1], F_GETFD);
    if (fflags == -1) {
      goto child_fail;
    }
    rv = fcntl(success_pipe_fd[1], F_SETFD, fflags | FD_CLOEXEC);
    if (rv == -1) {
      goto child_fail;
    }
    rv = execve(file, argv, envp);
  child_fail:
    eiki_write(success_pipe_fd[1], junk, sizeof(junk));
    _exit(EXIT_FAILURE);
  } else {
    ssize_t len;
    char buf[1];
    eiki_close(success_pipe_fd[1]);
    success_pipe_fd[1] = -1;
    len = eiki_read(success_pipe_fd[0], buf, sizeof(buf));
    if (len) {
      goto fail;
    }
    eiki_close(success_pipe_fd[0]);
    return pid;
  }
fail:
  if (success_pipe_fd[0] >= 0) {
    eiki_close(success_pipe_fd[0]);
  }
  if (success_pipe_fd[1] >= 0) {
    eiki_close(success_pipe_fd[1]);
  }
  return -1;
}


x86 assembly (clang, -O0):
<eiki_execv_async>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xe59(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11c6 <eiki_execv_async+0x36>
jmp    12e6 <eiki_execv_async+0x156>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11e1 <eiki_execv_async+0x51>
jmp    12e6 <eiki_execv_async+0x156>
cmpq   $0x0,-0x28(%rbp)
jne    129d <eiki_execv_async+0x10d>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
lea    -0x39(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1070 <memset@plt>
mov    -0x30(%rbp),%edi
call   1060 <eiki_close@plt>
mov    -0x2c(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1239 <eiki_execv_async+0xa9>
jmp    127e <eiki_execv_async+0xee>
mov    -0x2c(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    -0x40(%rbp),%edx
mov    0x2d89(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    126a <eiki_execv_async+0xda>
jmp    127e <eiki_execv_async+0xee>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1090 <execve@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x39(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <eiki_write@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x2c(%rbp),%edi
call   1060 <eiki_close@plt>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x49(%rbp),%rsi
mov    $0x1,%edx
call   1050 <eiki_read@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12d1 <eiki_execv_async+0x141>
jmp    12e6 <eiki_execv_async+0x156>
mov    -0x30(%rbp),%edi
call   1060 <eiki_close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1312 <eiki_execv_async+0x182>
cmpl   $0x0,-0x30(%rbp)
jl     12f8 <eiki_execv_async+0x168>
mov    -0x30(%rbp),%edi
call   1060 <eiki_close@plt>
cmpl   $0x0,-0x2c(%rbp)
jl     130a <eiki_execv_async+0x17a>
mov    -0x2c(%rbp),%edi
call   1060 <eiki_close@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eiki_execv_async>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0xffffffffffffffff,0x10(%rsp)
ff ff
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
test   %eax,%eax
je     11d4 <eiki_execv_async+0x54>
mov    0x10(%rsp),%edi
test   %edi,%edi
js     11b3 <eiki_execv_async+0x33>
call   1060 <eiki_close@plt>
mov    0x14(%rsp),%edi
mov    $0xffffffffffffffff,%rbx
test   %edi,%edi
js     11c7 <eiki_execv_async+0x47>
call   1060 <eiki_close@plt>
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11a6 <eiki_execv_async+0x26>
je     1214 <eiki_execv_async+0x94>
mov    %rax,%rbx
mov    0x14(%rsp),%edi
call   1060 <eiki_close@plt>
movl   $0xffffffff,0x14(%rsp)
ff
mov    0x10(%rsp),%edi
lea    0x18(%rsp),%rsi
mov    $0x1,%edx
call   1050 <eiki_read@plt>
test   %rax,%rax
jne    11a6 <eiki_execv_async+0x26>
mov    0x10(%rsp),%edi
jmp    11c2 <eiki_execv_async+0x42>
movq   $0x0,0x18(%rsp)
movb   $0x0,0xf(%rsp)
mov    0x10(%rsp),%edi
call   1060 <eiki_close@plt>
mov    0x14(%rsp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1278 <eiki_execv_async+0xf8>
mov    0x14(%rsp),%edi
mov    0x2d91(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d80(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1278 <eiki_execv_async+0xf8>
lea    0x18(%rsp),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <execve@plt>
mov    0x14(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1090 <eiki_write@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

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

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