#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 fprintf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *, int, char *,
            char const *);
int /*<<< orphan*/ printf(char *);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int print_filename(FILE *out, FILE *fp, const char *msg) {
  int fno = 0;
  ssize_t r = 0;
  int MAXSIZE = 0xFFF;
  char proclnk[0xFFF] = {};
  char filename[0xFFF] = {};
  int res = 0;
  if (fp != NULL) {
    fno = fileno(fp);
    sprintf(proclnk, "/proc/self/fd/%d", fno);
    r = readlink(proclnk, filename, MAXSIZE);
    if (r < 0) {
      printf("failed to readlink\n");
      exit(1);
    }
    filename[r] = '\0';
    res = fprintf(out, "fp -> fno -> filename: %p -> %d -> %s : %s\n", fp, fno,
                  filename, msg);
  }
  return res;
}


x86 assembly (clang, -O0):
<print_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0xfff,-0x2c(%rbp)
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0xfff,%edx
call   1050 <memset@plt>
lea    -0x2030(%rbp),%rdi
xor    %esi,%esi
mov    $0xfff,%edx
call   1050 <memset@plt>
movl   $0x0,-0x2034(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1269 <print_filename+0xf9>
mov    -0x10(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x1030(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe0b(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x1030(%rbp),%rdi
lea    -0x2030(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <readlink@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jae    1235 <print_filename+0xc5>
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rax
movb   $0x0,-0x2030(%rbp,%rax,1)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    -0x2030(%rbp),%r8
mov    -0x18(%rbp),%r9
lea    0xdc7(%rip),%rsi
call   1060 <fprintf@plt>
mov    %eax,-0x2034(%rbp)
mov    -0x2034(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_filename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x1000(%rsp),%rdi
xor    %ebp,%ebp
mov    $0xfff,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    %rsp,%rdi
mov    $0xfff,%edx
xor    %esi,%esi
call   1040 <memset@plt>
test   %rbx,%rbx
je     11ea <print_filename+0x9a>
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    %eax,%ebp
lea    0xe5b(%rip),%rsi
lea    0x1000(%rsp),%r12
mov    %r12,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    %rsp,%r13
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0xfff,%edx
call   1030 <readlink@plt>
movb   $0x0,(%rsp,%rax,1)
lea    0xe3c(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
mov    %ebp,%ecx
mov    %r13,%r8
mov    %r14,%r9
call   1050 <fprintf@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [20 x i8] c"failed to readlink\0A\00"
@.str.2 = [44 x i8] c"fp -> fno -> filename: %p -> %d -> %s : %s\0A\00"
define i32 @print_filename(i32* %0, i32* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = alloca [4095 x i8]
%11 = alloca [4095 x i8]
%12 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
store i32 0, i32* %7
store i64 0, i64* %8
store i32 4095, i32* %9
%13 = bitcast [4095 x i8]* %10 to i8*
%14 = bitcast [4095 x i8]* %11 to i8*
store i32 0, i32* %12
%15 = load i32*, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %41
17: ; preds = %3
%18 = load i32*, i32** %5
%19 = call i32 @fileno(i32* %18)
store i32 %19, i32* %7
%20 = getelementptr inbounds [4095 x i8], [4095 x i8]* %10, i64 0, i64 0
%21 = load i32, i32* %7
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [4095 x i8], [4095 x i8]* %10, i64 0, i64 0
%24 = getelementptr inbounds [4095 x i8], [4095 x i8]* %11, i64 0, i64 0
%25 = load i32, i32* %9
%26 = call i64 @readlink(i8* %23, i8* %24, i32 %25)
store i64 %26, i64* %8
%27 = load i64, i64* %8
%28 = icmp ult i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %17
%33 = load i64, i64* %8
%34 = getelementptr inbounds [4095 x i8], [4095 x i8]* %11, i64 0, i64 %33
store i8 0, i8* %34
%35 = load i32*, i32** %4
%36 = load i32*, i32** %5
%37 = load i32, i32* %7
%38 = getelementptr inbounds [4095 x i8], [4095 x i8]* %11, i64 0, i64 0
%39 = load i8*, i8** %6
%40 = call i32 @fprintf(i32* %35, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32* %36, i32 %37, i8* %38, i8* %39)
store i32 %40, i32* %12
br label %41
41: ; preds = %32, %3
%42 = load i32, i32* %12
ret i32 %42
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.2 = [44 x i8] c"fp -> fno -> filename: %p -> %d -> %s : %s\0A\00"
define i32 @print_filename(i32* nocapture %0, i32* %1, i8* %2) {
%4 = alloca [4095 x i8]
%5 = alloca [4095 x i8]
%6 = getelementptr inbounds [4095 x i8], [4095 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [4095 x i8], [4095 x i8]* %5, i64 0, i64 0
%8 = icmp eq i32* %1, null
br i1 %8, label %15, label %9
9: ; preds = %3
%10 = tail call i32 @fileno(i32* %1)
%11 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i64 @readlink(i8* %6, i8* %7, i32 4095)
%13 = getelementptr inbounds [4095 x i8], [4095 x i8]* %5, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32* %1, i32 %10, i8* %7, i8* %2)
br label %15
15: ; preds = %9, %3
%16 = phi i32 [ %14, %9 ], [ 0, %3 ]
ret i32 %16
}