#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
void get_filename(FILE *fp, char *filename) {
  int fno;
  char proclnk[0xFFF];
  ssize_t r = 0;
  fno = fileno(fp);
  sprintf(proclnk, "/proc/self/fd/%d", fno);
  r = readlink(proclnk, filename, 0xFFF);
  if (r < 0) {
    printf("Error with filename.txt and surname.txt, failed to readlink.\n");
    exit(1);
  }
  filename[r] = '\0';
}


x86 assembly (clang, -O0):
<get_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x1028(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x14(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe75(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xfff,%edx
call   1030 <readlink@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jae    11d0 <get_filename+0x80>
lea    0xe50(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x1028(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filename>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbx
call   1040 <fileno@plt>
lea    0xeb7(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%edx
call   1050 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0xfff,%edx
call   1030 <readlink@plt>
movb   $0x0,(%rbx,%rax,1)
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [62 x i8] c"Error with filename.txt and surname.txt, failed to readlink.\0A\00"
define void @get_filename(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [4095 x i8]
%7 = alloca i64
store i32* %0, i32** %3
store i8* %1, i8** %4
store i64 0, i64* %7
%8 = load i32*, i32** %3
%9 = call i32 @fileno(i32* %8)
store i32 %9, i32* %5
%10 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 0
%14 = load i8*, i8** %4
%15 = call i64 @readlink(i8* %13, i8* %14, i32 4095)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp ult i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = load i64, i64* %7
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8 0, i8* %24
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
define void @get_filename(i32* nocapture %0, i8* nocapture %1) {
%3 = alloca [4095 x i8]
%4 = getelementptr inbounds [4095 x i8], [4095 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @fileno(i32* %0)
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i64 @readlink(i8* %4, i8* %1, i32 4095)
%8 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 0, i8* %8
ret void
}