#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_FAILURE;
int PATH_MAX;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void get_file_desc(FILE *stream, char *fbuf) {
  int fd = fileno(stream);
  if (fd == -1) {
    memcpy(fbuf, "<invalid>", 9 * sizeof(char));
    return;
  }
  char pbuf[PATH_MAX];
  snprintf(pbuf, PATH_MAX, "/proc/self/fd/%d", fd);
  ssize_t t = readlink(pbuf, fbuf, PATH_MAX);
  fbuf[t] = '\0';
  do {
    if (errno) {
      fprintf(stderr,
              "error while reading from stream"
              " '%s': %s\n",
              fbuf, strerror(errno));
      exit(EXIT_FAILURE);
    }
  } while (false);
}


x86 assembly (clang, -O0):
<get_file_desc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b0 <get_file_desc+0x40>
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x9,%edx
call   1060 <memcpy@plt>
jmp    1283 <get_file_desc+0x113>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%ecx
lea    0xe1b(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
call   1030 <readlink@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2daf(%rip),%rax
cmpq   $0x0,(%rax)
je     1272 <get_file_desc+0x102>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdbc(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1277 <get_file_desc+0x107>
jmp    127c <get_file_desc+0x10c>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_desc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
call   1070 <fileno@plt>
cmp    $0xffffffff,%eax
je     11ef <get_file_desc+0x7f>
mov    %rsp,%r15
mov    0x2e5e(%rip),%r12
mov    (%r12),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%r12),%esi
lea    0xe58(%rip),%rdx
mov    %rbx,%rdi
mov    %eax,%ecx
call   1040 <snprintf@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <readlink@plt>
movb   $0x0,(%r14,%rax,1)
mov    0x2df9(%rip),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
jne    120f <get_file_desc+0x9f>
mov    %r15,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0a(%rip),%rsi
mov    %r14,%rdi
mov    $0x9,%edx
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <memcpy@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%ebx
call   1090 <strerror@plt>
lea    0xdf7(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"<invalid>\00"
@PATH_MAX = global i32 0
@.str.1 = [17 x i8] c"/proc/self/fd/%d\00"
@errno = global i64 0
@stderr = global i32 0
@.str.2 = [42 x i8] c"error while reading from stream '%s': %s\0A\00"
@EXIT_FAILURE = global i32 0
define void @get_file_desc(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32* %0, i32** %3
store i8* %1, i8** %4
%9 = load i32*, i32** %3
%10 = call i32 @fileno(i32* %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @memcpy(i8* %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
br label %45
16: ; preds = %2
%17 = load i32, i32* @PATH_MAX
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %6
%20 = alloca i8, i64 %18
store i64 %18, i64* %7
%21 = load i32, i32* @PATH_MAX
%22 = load i32, i32* %5
%23 = call i32 @snprintf(i8* %20, i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i8*, i8** %4
%25 = load i32, i32* @PATH_MAX
%26 = call i64 @readlink(i8* %20, i8* %24, i32 %25)
store i64 %26, i64* %8
%27 = load i8*, i8** %4
%28 = load i64, i64* %8
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %16
%31 = load i64, i64* @errno
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %4
%36 = load i64, i64* @errno
%37 = call i8* @strerror(i64 %36)
%38 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %35, i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %30
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
%44 = load i8*, i8** %6
br label %45
45: ; preds = %43, %13
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"<invalid>\00"
@PATH_MAX = global i32 0
@.str.1 = [17 x i8] c"/proc/self/fd/%d\00"
@errno = global i64 0
@stderr = global i32 0
@.str.2 = [42 x i8] c"error while reading from stream '%s': %s\0A\00"
@EXIT_FAILURE = global i32 0
define void @get_file_desc(i32* nocapture %0, i8* %1) {
%3 = tail call i32 @fileno(i32* %0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @memcpy(i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
br label %26
7: ; preds = %2
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = load i32, i32* @PATH_MAX
%13 = call i32 @snprintf(i8* %11, i32 %12, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%14 = load i32, i32* @PATH_MAX
%15 = call i64 @readlink(i8* %11, i8* %1, i32 %14)
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %16
%17 = load i64, i64* @errno
%18 = icmp eq i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %7
%20 = load i32, i32* @stderr
%21 = call i8* @strerror(i64 %17)
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %7
br label %26
26: ; preds = %25, %5
ret void
}