#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 */
scalar_t__ EBADF;
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ execve(char *, char *const *, char *const *);
int /*<<< orphan*/ snprintf(char *, int, char const *, int);
int fexecve(int fd, char *const argv[], char *const envp[]) {
  static const char proc[] = "/proc/self/fd/%d";
  char buf[sizeof proc + 3 * sizeof(int)];
  snprintf(buf, sizeof buf, proc, fd);
  execve(buf, argv, envp);
  if (errno == ENOENT)
    errno = EBADF;
  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    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x1d,%esi
lea    0xeba(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <execve@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <fexecve+0x6a>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e59(%rip),%rax
mov    %rcx,(%rax)
mov    $0xffffffff,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fexecve>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ecx
lea    0xec8(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x1d,%esi
call   1030 <snprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <execve@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%rcx
mov    0x2e61(%rip),%rdx
cmp    (%rdx),%rcx
jne    1179 <fexecve+0x59>
mov    0x2e65(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rax)
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@fexecve.proc = internal constant [17 x i8] c"/proc/self/fd/%d\00"
@errno = global i64 0
@ENOENT = global i64 0
@EBADF = global i64 0
define i32 @fexecve(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca [29 x i8]
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
%8 = getelementptr inbounds [29 x i8], [29 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @fexecve.proc, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [29 x i8], [29 x i8]* %7, i64 0, i64 0
%12 = load i8**, i8*** %5
%13 = load i8**, i8*** %6
%14 = call i32 @execve(i8* %11, i8** %12, i8** %13)
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i64, i64* @EBADF
store i64 %19, i64* @errno
br label %20
20: ; preds = %18, %3
ret i32 -1
}

LLVM IR (-O2):
@fexecve.proc = internal constant [17 x i8] c"/proc/self/fd/%d\00"
@errno = global i64 0
@ENOENT = global i64 0
@EBADF = global i64 0
define i32 @fexecve(i32 %0, i8** %1, i8** %2) {
%4 = alloca [29 x i8]
%5 = getelementptr inbounds [29 x i8], [29 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @fexecve.proc, i64 0, i64 0), i32 %0)
%7 = call i32 @execve(i8* %5, i8** %1, i8** %2)
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i64, i64* @EBADF
store i64 %12, i64* @errno
br label %13
13: ; preds = %11, %3
ret i32 -1
}