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

/* Variables and functions */
size_t readlink(char *, char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *get_file_path_from_fd(int fd, char *buffer, size_t buffer_size) {
  ssize_t len;
  if (fd <= 0)
    return NULL;
  sprintf(buffer, "/proc/self/fd/%d", fd);
  if ((len = readlink(buffer, buffer, buffer_size - 1)) < 0)
    return NULL;
  buffer[len] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<get_file_path_from_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     114a <get_file_path_from_fd+0x2a>
movq   $0x0,-0x8(%rbp)
jmp    11a1 <get_file_path_from_fd+0x81>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xea8(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <readlink@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jae    118d <get_file_path_from_fd+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    11a1 <get_file_path_from_fd+0x81>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_path_from_fd>:
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
jle    1157 <get_file_path_from_fd+0x37>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%edx
lea    0xec9(%rip),%rsi
mov    %rbx,%rdi
call   1040 <sprintf@plt>
add    $0xffffffffffffffff,%r14
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <readlink@plt>
movb   $0x0,(%rbx,%rax,1)
jmp    1159 <get_file_path_from_fd+0x39>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
define i8* @get_file_path_from_fd(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i32, i32* %5
%10 = icmp sle i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
store i8* null, i8** %4
br label %28
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = load i32, i32* %5
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %6
%17 = load i8*, i8** %6
%18 = load i64, i64* %7
%19 = sub i64 %18, 1
%20 = call i64 @readlink(i8* %16, i8* %17, i64 %19)
store i64 %20, i64* %8
%21 = icmp ult i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %12
store i8* null, i8** %4
br label %28
23: ; preds = %12
%24 = load i8*, i8** %6
%25 = load i64, i64* %8
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %6
store i8* %27, i8** %4
br label %28
28: ; preds = %23, %22, %11
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
define i8* @get_file_path_from_fd(i32 %0, i8* %1, i64 %2) {
%4 = icmp slt i32 %0, 1
br i1 %4, label %10, label %5
5: ; preds = %3
%6 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = add i64 %2, -1
%8 = tail call i64 @readlink(i8* %1, i8* %1, i64 %7)
%9 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 0, i8* %9
br label %10
10: ; preds = %3, %5
%11 = phi i8* [ %1, %5 ], [ null, %3 ]
ret i8* %11
}