#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 EBADF;
int ENOENT;
int ENOSYS;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ execve(char *, char *const *, char *const *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int fexecve(int fd, char *const argv[], char *const envp[]) {
  char pathname[sizeof("/dev/fd"
                       "//") +
                3 * sizeof(int)];
  int saved_errno;
  sprintf(pathname,
          "/dev/fd"
          "/%i/",
          fd);
  execve(pathname, argv, envp);
  saved_errno = errno;
  if (access("/dev/fd"
             "/",
             X_OK))
    saved_errno = ENOSYS;
  errno = saved_errno == ENOENT ? EBADF : saved_errno;
  return -1;
}


x86 assembly (clang, -O0):
<fexecve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xeaf(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <execve@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1040 <access@plt>
cmp    $0x0,%rax
je     119e <fexecve+0x6e>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%eax
jne    11c1 <fexecve+0x91>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
jmp    11c7 <fexecve+0x97>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%ecx
mov    0x2dff(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffff,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fexecve>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%edx
lea    0xeb7(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
call   1050 <sprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <execve@plt>
mov    0x2e67(%rip),%rbx
mov    (%rbx),%ebp
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1040 <access@plt>
test   %rax,%rax
je     118e <fexecve+0x5e>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ebp
mov    0x2e23(%rip),%rax
cmp    (%rax),%ebp
jne    11a2 <fexecve+0x72>
mov    0x2e28(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,(%rbx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/fd/%i/\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"/dev/fd/\00"
@X_OK = global i32 0
@ENOSYS = global i32 0
@ENOENT = global i32 0
@EBADF = global i32 0
define i32 @fexecve(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca [22 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
%9 = getelementptr inbounds [22 x i8], [22 x i8]* %7, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [22 x i8], [22 x i8]* %7, i64 0, i64 0
%13 = load i8**, i8*** %5
%14 = load i8**, i8*** %6
%15 = call i32 @execve(i8* %12, i8** %13, i8** %14)
%16 = load i32, i32* @errno
store i32 %16, i32* %8
%17 = load i32, i32* @X_OK
%18 = call i64 @access(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = load i32, i32* @ENOSYS
store i32 %21, i32* %8
br label %22
22: ; preds = %20, %3
%23 = load i32, i32* %8
%24 = load i32, i32* @ENOENT
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = load i32, i32* @EBADF
br label %30
28: ; preds = %22
%29 = load i32, i32* %8
br label %30
30: ; preds = %28, %26
%31 = phi i32 [ %27, %26 ], [ %29, %28 ]
store i32 %31, i32* @errno
ret i32 -1
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/fd/%i/\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"/dev/fd/\00"
@X_OK = global i32 0
@ENOSYS = global i32 0
@ENOENT = global i32 0
@EBADF = global i32 0
define i32 @fexecve(i32 %0, i8** %1, i8** %2) {
%4 = alloca [22 x i8]
%5 = getelementptr inbounds [22 x i8], [22 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32 @execve(i8* %5, i8** %1, i8** %2)
%8 = load i32, i32* @errno
%9 = load i32, i32* @X_OK
%10 = call i64 @access(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp eq i64 %10, 0
%12 = load i32, i32* @ENOSYS
%13 = select i1 %11, i32 %8, i32 %12
%14 = load i32, i32* @ENOENT
%15 = icmp eq i32 %13, %14
%16 = load i32, i32* @EBADF
%17 = select i1 %15, i32 %16, i32 %13
store i32 %17, i32* @errno
ret i32 -1
}