#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 */
int /*<<< orphan*/ printf(char *, int);
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;
  printf("/proc/self/fd/%d\n", fd);
  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     115a <get_file_path_from_fd+0x2a>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <get_file_path_from_fd+0x90>
mov    -0xc(%rbp),%esi
lea    0xe9c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe9b(%rip),%rsi
call   1050 <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    11ac <get_file_path_from_fd+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <get_file_path_from_fd+0x90>
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   %rbp
push   %r14
push   %rbx
test   %edi,%edi
jle    1177 <get_file_path_from_fd+0x47>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xeb9(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
lea    0xebd(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1050 <sprintf@plt>
add    $0xffffffffffffffff,%r14
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <readlink@plt>
movb   $0x0,(%rbx,%rax,1)
jmp    1179 <get_file_path_from_fd+0x49>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [18 x i8] c"/proc/self/fd/%d\0A\00"
@.str.1 = [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 %11, label %5
5: ; preds = %3
%6 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%8 = add i64 %2, -1
%9 = tail call i64 @readlink(i8* %1, i8* %1, i64 %8)
%10 = getelementptr inbounds i8, i8* %1, i64 %9
store i8 0, i8* %10
br label %11
11: ; preds = %3, %5
%12 = phi i8* [ %1, %5 ], [ null, %3 ]
ret i8* %12
}