#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 int ssize_t;
typedef int /*<<< orphan*/ filePath;
typedef int /*<<< orphan*/ fdPath;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int const);
int /*<<< orphan*/ strncpy(char *, char *, int);
void fileByFD(const int fd, char *dst, ssize_t len) {
  char fdPath[PATH_MAX], filePath[PATH_MAX];
  ssize_t filePathLen;
  memset(fdPath, 0, sizeof(fdPath));
  memset(filePath, 0, sizeof(filePath));
  memset(dst, 0, len);
  sprintf(fdPath, "/proc/self/fd/%d", fd);
  filePathLen = readlink(fdPath, filePath, sizeof(filePath) - 1);
  if (filePathLen == -1) {
    strncpy(dst, "(unknown)", len - 1);
  } else {
    strncpy(dst, filePath, len - 1);
  }
}


x86 assembly (clang, -O0):
<fileByFD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e97(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    (%rcx),%ecx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe2a(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <readlink@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    121a <fileByFD+0xda>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
lea    0xe01(%rip),%rsi
call   1030 <strncpy@plt>
jmp    122d <fileByFD+0xed>
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileByFD>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,-0x2c(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r13
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r13,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1050 <memset@plt>
lea    0xe54(%rip),%rsi
mov    %r13,%rdi
mov    -0x2c(%rbp),%edx
call   1060 <sprintf@plt>
lea    -0x1(%r12),%edx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <readlink@plt>
add    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11d8 <fileByFD+0x98>
mov    %r14,%rdi
mov    %rbx,%rsi
jmp    11e2 <fileByFD+0xa2>
lea    0xe32(%rip),%rsi
mov    %r14,%rdi
mov    %r15d,%edx
call   1030 <strncpy@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [10 x i8] c"(unknown)\00"
define void @fileByFD(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = alloca i8, i64 %16
store i64 %16, i64* %9
%18 = trunc i64 %12 to i32
%19 = call i32 @memset(i8* %14, i32 0, i32 %18)
%20 = trunc i64 %16 to i32
%21 = call i32 @memset(i8* %17, i32 0, i32 %20)
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = call i32 @memset(i8* %22, i32 0, i32 %23)
%25 = load i32, i32* %4
%26 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = sub i64 %16, 1
%28 = trunc i64 %27 to i32
%29 = call i32 @readlink(i8* %14, i8* %17, i32 %28)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %3
%33 = load i8*, i8** %5
%34 = load i32, i32* %6
%35 = sub nsw i32 %34, 1
%36 = call i32 @strncpy(i8* %33, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %42
37: ; preds = %3
%38 = load i8*, i8** %5
%39 = load i32, i32* %6
%40 = sub nsw i32 %39, 1
%41 = call i32 @strncpy(i8* %38, i8* %17, i32 %40)
br label %42
42: ; preds = %37, %32
%43 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [10 x i8] c"(unknown)\00"
define void @fileByFD(i32 %0, i8* %1, i32 %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = alloca i8, i64 %5
%8 = call i32 @memset(i8* %6, i32 0, i32 %4)
%9 = call i32 @memset(i8* %7, i32 0, i32 %4)
%10 = call i32 @memset(i8* %1, i32 0, i32 %2)
%11 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%12 = add i32 %4, -1
%13 = call i32 @readlink(i8* %6, i8* %7, i32 %12)
%14 = icmp eq i32 %13, -1
%15 = add nsw i32 %2, -1
br i1 %14, label %16, label %18
16: ; preds = %3
%17 = call i32 @strncpy(i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %20
18: ; preds = %3
%19 = call i32 @strncpy(i8* %1, i8* %7, i32 %15)
br label %20
20: ; preds = %18, %16
ret void
}