#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__ int64_t;

/* Variables and functions */
int EINVAL;
int ENOBUFS;
int PATH_MAX;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *realpath(char *, char *);
scalar_t__ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int get_path_by_fd(char *path, int64_t len, int fd) {
  int err = 0;
  char proc_path[PATH_MAX];
  int64_t count = 0;
  if (NULL == path || len <= 0 || len < PATH_MAX || fd < 0) {
    err = -EINVAL;
  } else if (0 != access("/proc/self/fd", X_OK)) {
    err = errno;
    perror("access:");
  } else if (0 >= (count = sprintf(proc_path, "/proc/self/fd/%i/", fd)) ||
             count > len) {
    err = -ENOBUFS;
    fprintf(stderr, "snprintf(): error\n");
    ;
  } else if (NULL == realpath(proc_path, path)) {
    err = errno;
    fprintf(stderr, "realpath(proc_path=%s)=>NULL\n", proc_path);
    ;
  }
  return err;
}


x86 assembly (clang, -O0):
<get_path_by_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     11d6 <get_path_by_fd+0x86>
cmpq   $0x0,-0x10(%rbp)
jle    11d6 <get_path_by_fd+0x86>
mov    -0x10(%rbp),%rax
mov    0x2e28(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jl     11d6 <get_path_by_fd+0x86>
cmpl   $0x0,-0x14(%rbp)
jge    11e9 <get_path_by_fd+0x99>
xor    %eax,%eax
mov    0x2dd9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x18(%rbp)
jmp    12d7 <get_path_by_fd+0x187>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   1050 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1229 <get_path_by_fd+0xd9>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xdef(%rip),%rdi
call   1060 <perror@plt>
jmp    12d2 <get_path_by_fd+0x182>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xddf(%rip),%rsi
call   1070 <sprintf@plt>
mov    %rax,%rcx
mov    %rcx,-0x30(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jge    125c <get_path_by_fd+0x10c>
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jle    1286 <get_path_by_fd+0x136>
xor    %eax,%eax
mov    0x2d93(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    12cd <get_path_by_fd+0x17d>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <realpath@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12c8 <get_path_by_fd+0x178>
mov    -0x38(%rbp),%rdx
mov    0x2d14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    12cd <get_path_by_fd+0x17d>
jmp    12d2 <get_path_by_fd+0x182>
jmp    12d7 <get_path_by_fd+0x187>
mov    -0x18(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_path_by_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
lea    0xf(%rax),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r13
mov    %r13,%rsp
test   %rdi,%rdi
je     120f <get_path_by_fd+0xbf>
mov    %rsi,%r15
test   %rsi,%rsi
jle    120f <get_path_by_fd+0xbf>
mov    %edx,%r12d
test   %edx,%edx
js     120f <get_path_by_fd+0xbf>
cltq
cmp    %r15,%rax
jg     120f <get_path_by_fd+0xbf>
mov    %rdi,%r14
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1050 <access@plt>
test   %rax,%rax
jne    124f <get_path_by_fd+0xff>
lea    0xe53(%rip),%rsi
mov    %r13,%rdi
mov    %r12d,%edx
call   1070 <sprintf@plt>
test   %rax,%rax
jle    122b <get_path_by_fd+0xdb>
cmp    %r15,%rax
jg     122b <get_path_by_fd+0xdb>
mov    %r13,%rdi
mov    %r14,%rsi
call   1040 <realpath@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    121a <get_path_by_fd+0xca>
mov    0x2dcf(%rip),%rax
mov    (%rax),%ebx
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    121a <get_path_by_fd+0xca>
mov    0x2da2(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc6(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    121a <get_path_by_fd+0xca>
mov    0x2d6a(%rip),%rax
mov    (%rax),%ebx
lea    0xdaf(%rip),%rdi
call   1060 <perror@plt>
jmp    121a <get_path_by_fd+0xca>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@.str = [14 x i8] c"/proc/self/fd\00"
@X_OK = global i32 0
@errno = global i32 0
@.str.1 = [8 x i8] c"access:\00"
@.str.2 = [18 x i8] c"/proc/self/fd/%i/\00"
@ENOBUFS = global i32 0
@stderr = global i32 0
@.str.3 = [19 x i8] c"snprintf(): error\0A\00"
@.str.4 = [30 x i8] c"realpath(proc_path=%s)=>NULL\0A\00"
define i32 @get_path_by_fd(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
store i64 0, i64* %10
%15 = load i8*, i8** %4
%16 = icmp eq i8* null, %15
br i1 %16, label %28, label %17
17: ; preds = %3
%18 = load i64, i64* %5
%19 = icmp sle i64 %18, 0
br i1 %19, label %28, label %20
20: ; preds = %17
%21 = load i64, i64* %5
%22 = load i32, i32* @PATH_MAX
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %21, %23
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25, %20, %17, %3
%29 = load i32, i32* @EINVAL
%30 = sub nsw i32 0, %29
store i32 %30, i32* %7
br label %62
31: ; preds = %25
%32 = load i32, i32* @X_OK
%33 = call i64 @access(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = icmp ne i64 0, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @errno
store i32 %36, i32* %7
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %61
38: ; preds = %31
%39 = load i32, i32* %6
%40 = call i64 @sprintf(i8* %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %39)
store i64 %40, i64* %10
%41 = icmp sge i64 0, %40
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = load i64, i64* %10
%44 = load i64, i64* %5
%45 = icmp sgt i64 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %42, %38
%47 = load i32, i32* @ENOBUFS
%48 = sub nsw i32 0, %47
store i32 %48, i32* %7
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %60
51: ; preds = %42
%52 = load i8*, i8** %4
%53 = call i32* @realpath(i8* %14, i8* %52)
%54 = icmp eq i32* null, %53
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i32, i32* @errno
store i32 %56, i32* %7
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %14)
br label %59
59: ; preds = %55, %51
br label %60
60: ; preds = %59, %46
br label %61
61: ; preds = %60, %35
br label %62
62: ; preds = %61, %28
%63 = load i32, i32* %7
%64 = load i8*, i8** %8
ret i32 %63
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@.str = [14 x i8] c"/proc/self/fd\00"
@X_OK = global i32 0
@errno = global i32 0
@.str.1 = [8 x i8] c"access:\00"
@.str.2 = [18 x i8] c"/proc/self/fd/%i/\00"
@ENOBUFS = global i32 0
@stderr = global i32 0
@.str.3 = [19 x i8] c"snprintf(): error\0A\00"
@.str.4 = [30 x i8] c"realpath(proc_path=%s)=>NULL\0A\00"
define i32 @get_path_by_fd(i8* %0, i64 %1, i32 %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = icmp eq i8* %0, null
%8 = icmp slt i64 %1, 1
%9 = or i1 %7, %8
br i1 %9, label %15, label %10
10: ; preds = %3
%11 = sext i32 %4 to i64
%12 = icmp sgt i64 %11, %1
%13 = icmp slt i32 %2, 0
%14 = or i1 %13, %12
br i1 %14, label %15, label %18
15: ; preds = %10, %3
%16 = load i32, i32* @EINVAL
%17 = sub nsw i32 0, %16
br label %42
18: ; preds = %10
%19 = load i32, i32* @X_OK
%20 = tail call i64 @access(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = load i32, i32* @errno
%24 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %42
25: ; preds = %18
%26 = call i64 @sprintf(i8* %6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%27 = icmp slt i64 %26, 1
%28 = icmp sgt i64 %26, %1
%29 = or i1 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i32, i32* @ENOBUFS
%32 = sub nsw i32 0, %31
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %42
35: ; preds = %25
%36 = call i32* @realpath(i8* %6, i8* %0)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %42
42: ; preds = %22, %35, %38, %30, %15
%43 = phi i32 [ %17, %15 ], [ %23, %22 ], [ %32, %30 ], [ %39, %38 ], [ 0, %35 ]
ret i32 %43
}