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

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int readlink(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
bool fd2path(int fd, char *path) {
  char fdpath[32];
  sprintf(fdpath, "/proc/self/fd/%i", fd);
  ssize_t len = readlink(fdpath, path, PATH_MAX);
  if (len == -1) {
    return false;
  }
  path[len] = '\0';
  return true;
}


x86 assembly (clang, -O0):
<fd2path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xec3(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e9f(%rip),%rax
mov    (%rax),%edx
call   1030 <readlink@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1171 <fd2path+0x51>
movl   $0x0,-0x4(%rbp)
jmp    1184 <fd2path+0x64>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd2path>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%edx
lea    0xecd(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <sprintf@plt>
mov    0x2eab(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <readlink@plt>
cmp    $0xffffffff,%eax
je     1164 <fd2path+0x44>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    $0x1,%eax
jmp    1166 <fd2path+0x46>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%i\00"
@PATH_MAX = global i32 0
define i32 @fd2path(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [32 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%12 = load i8*, i8** %5
%13 = load i32, i32* @PATH_MAX
%14 = call i32 @readlink(i8* %11, i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 0, i32* %3
br label %23
18: ; preds = %2
%19 = load i8*, i8** %5
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 0, i8* %22
store i32 1, i32* %3
br label %23
23: ; preds = %18, %17
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%i\00"
@PATH_MAX = global i32 0
define i32 @fd2path(i32 %0, i8* nocapture %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @PATH_MAX
%7 = call i32 @readlink(i8* %4, i8* %1, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = sext i32 %7 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %2, %9
%13 = phi i32 [ 1, %9 ], [ 0, %2 ]
ret i32 %13
}