#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int EINVAL;
int ENOENT;
int ENOSYS;
int /*<<< orphan*/ __set_errno(int);
int errno;
int /*<<< orphan*/ execve(char *, char *const *, char *const *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ stat(char *, struct stat *);
int fexecve(int fd, char *const argv[], char *const envp[]) {
  if (fd < 0 || argv == NULL || envp == NULL) {
    __set_errno(EINVAL);
    return -1;
  }
  char buf[sizeof "/proc/self/fd/" + sizeof(int) * 3];
  snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
  execve(buf, argv, envp);
  int save = errno;
  struct stat st;
  if (stat("/proc/self/fd", &st) != 0 && errno == ENOENT)
    save = ENOSYS;
  __set_errno(save);
  return -1;
}


x86 assembly (clang, -O0):
<fexecve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1173 <fexecve+0x33>
cmpq   $0x0,-0x10(%rbp)
je     1173 <fexecve+0x33>
cmpq   $0x0,-0x18(%rbp)
jne    118d <fexecve+0x4d>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <fexecve+0xcf>
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1b,%esi
lea    0xe60(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <execve@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
lea    0xe48(%rip),%rdi
lea    -0x48(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1200 <fexecve+0xc0>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%eax
jne    1200 <fexecve+0xc0>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fexecve>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
test   %edi,%edi
js     11c1 <fexecve+0x81>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11c1 <fexecve+0x81>
mov    %rdx,%r14
test   %rdx,%rdx
je     11c1 <fexecve+0x81>
mov    %edi,%ecx
lea    0xe9a(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x1b,%esi
call   1040 <snprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <execve@plt>
mov    0x2e4b(%rip),%r14
mov    (%r14),%ebx
lea    0xe7a(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     11bd <fexecve+0x7d>
mov    (%r14),%eax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%eax
jne    11bd <fexecve+0x7d>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
jmp    11ca <fexecve+0x8a>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@EINVAL = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@errno = global i32 0
@.str.1 = [14 x i8] c"/proc/self/fd\00"
@ENOENT = global i32 0
@ENOSYS = global i32 0
define i32 @fexecve(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [27 x i8]
%9 = alloca i32
%10 = alloca %struct.stat
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %3
%14 = load i8**, i8*** %6
%15 = icmp eq i8** %14, null
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i8**, i8*** %7
%18 = icmp eq i8** %17, null
br i1 %18, label %19, label %22
19: ; preds = %16, %13, %3
%20 = load i32, i32* @EINVAL
%21 = call i32 @__set_errno(i32 %20)
store i32 -1, i32* %4
br label %42
22: ; preds = %16
%23 = getelementptr inbounds [27 x i8], [27 x i8]* %8, i64 0, i64 0
%24 = load i32, i32* %5
%25 = call i32 @snprintf(i8* %23, i32 27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [27 x i8], [27 x i8]* %8, i64 0, i64 0
%27 = load i8**, i8*** %6
%28 = load i8**, i8*** %7
%29 = call i32 @execve(i8* %26, i8** %27, i8** %28)
%30 = load i32, i32* @errno
store i32 %30, i32* %9
%31 = call i64 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %10)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %22
%34 = load i32, i32* @errno
%35 = load i32, i32* @ENOENT
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = load i32, i32* @ENOSYS
store i32 %38, i32* %9
br label %39
39: ; preds = %37, %33, %22
%40 = load i32, i32* %9
%41 = call i32 @__set_errno(i32 %40)
store i32 -1, i32* %4
br label %42
42: ; preds = %39, %19
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EINVAL = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@errno = global i32 0
@.str.1 = [14 x i8] c"/proc/self/fd\00"
@ENOENT = global i32 0
@ENOSYS = global i32 0
define i32 @fexecve(i32 %0, i8** %1, i8** %2) {
%4 = alloca [27 x i8]
%5 = alloca %struct.stat
%6 = icmp slt i32 %0, 0
%7 = icmp eq i8** %1, null
%8 = or i1 %6, %7
%9 = icmp eq i8** %2, null
%10 = or i1 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @EINVAL
%13 = tail call i32 @__set_errno(i32 %12)
br label %31
14: ; preds = %3
%15 = getelementptr inbounds [27 x i8], [27 x i8]* %4, i64 0, i64 0
%16 = call i32 @snprintf(i8* %15, i32 27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = call i32 @execve(i8* %15, i8** %1, i8** %2)
%18 = load i32, i32* @errno
%19 = bitcast %struct.stat* %5 to i8*
%20 = call i64 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %14
%23 = load i32, i32* @errno
%24 = load i32, i32* @ENOENT
%25 = icmp eq i32 %23, %24
%26 = load i32, i32* @ENOSYS
%27 = select i1 %25, i32 %26, i32 %18
br label %28
28: ; preds = %22, %14
%29 = phi i32 [ %18, %14 ], [ %27, %22 ]
%30 = call i32 @__set_errno(i32 %29)
br label %31
31: ; preds = %28, %11
ret i32 -1
}