#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 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 *, ...);
int /*<<< orphan*/ readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void check_stream(FILE *stream) {
  int fd = fileno(stream);
  if (fd == -1) {
    fprintf(stderr, "error while reading from stream: invalid stream (%p)",
            stream);
    exit(EXIT_FAILURE);
  }
  if (errno != 0) {
    char fbuf[PATH_MAX];
    char pbuf[PATH_MAX];
    snprintf(pbuf, PATH_MAX, "/proc/self/fd/%d", fd);
    readlink(pbuf, fbuf, PATH_MAX);
    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):
<check_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ab <check_stream+0x4b>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2e1e(%rip),%rax
cmpq   $0x0,(%rax)
je     1297 <check_stream+0x137>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x30(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x20(%rbp)
mov    (%rax),%eax
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    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%ecx
lea    0xe1c(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
call   1030 <readlink@plt>
mov    0x2d95(%rip),%rax
cmpq   $0x0,(%rax)
je     1286 <check_stream+0x126>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    128b <check_stream+0x12b>
jmp    1290 <check_stream+0x130>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_stream>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1060 <fileno@plt>
cmp    $0xffffffff,%eax
je     11f9 <check_stream+0x99>
mov    0x2e4e(%rip),%r15
cmpq   $0x0,(%r15)
je     11ea <check_stream+0x8a>
mov    %rsp,%r12
mov    0x2e5e(%rip),%r13
mov    0x0(%r13),%ecx
mov    %rsp,%r14
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r14
mov    %r14,%rsp
mov    0x0(%r13),%esi
mov    %rsp,%rbx
mov    %rsi,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
lea    0xe6f(%rip),%rdx
mov    %rbx,%rdi
mov    %eax,%ecx
call   1040 <snprintf@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <readlink@plt>
mov    (%r15),%rdi
test   %rdi,%rdi
jne    1215 <check_stream+0xb5>
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1239 <check_stream+0xd9>
mov    0x2dbc(%rip),%rax
mov    (%rax),%ebx
call   1080 <strerror@plt>
lea    0xe1c(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

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