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

/* Variables and functions */
int /*<<< orphan*/ errno;
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *, char *);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
ssize_t try_fread(FILE *in, void *buf, size_t len, const char *func,
                  const char *fname) {
  size_t got = fread(buf, 1, len, in);
  if (got == 0 && ferror(in)) {
    fprintf(stderr, "%s : Error reading from %s : %s\n", func, fname,
            strerror(errno));
    return -1;
  }
  return got;
}


x86 assembly (clang, -O0):
<try_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11e4 <try_fread+0xa4>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11e4 <try_fread+0xa4>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    %rax,%r8
lea    0xe2d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <try_fread+0xab>
mov    -0x38(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_fread>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a9 <try_fread+0x69>
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11a9 <try_fread+0x69>
mov    0x2e63(%rip),%rax
mov    (%rax),%ebx
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe6c(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rax,%r8
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"%s : Error reading from %s : %s\0A\00"
@errno = global i32 0
define i32 @try_fread(i32* %0, i8* %1, i64 %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
store i32* %0, i32** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%13 = load i8*, i8** %8
%14 = load i64, i64* %9
%15 = load i32*, i32** %7
%16 = call i64 @fread(i8* %13, i32 1, i64 %14, i32* %15)
store i64 %16, i64* %12
%17 = load i64, i64* %12
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %5
%20 = load i32*, i32** %7
%21 = call i64 @ferror(i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %10
%26 = load i8*, i8** %11
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %26, i8* %28)
store i32 -1, i32* %6
br label %33
30: ; preds = %19, %5
%31 = load i64, i64* %12
%32 = trunc i64 %31 to i32
store i32 %32, i32* %6
br label %33
33: ; preds = %30, %23
%34 = load i32, i32* %6
ret i32 %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"%s : Error reading from %s : %s\0A\00"
@errno = global i32 0
define i32 @try_fread(i32* nocapture %0, i8* nocapture %1, i64 %2, i8* %3, i8* %4) {
%6 = tail call i64 @fread(i8* %1, i32 1, i64 %2, i32* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = tail call i64 @ferror(i32* %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %4, i8* %14)
br label %18
16: ; preds = %8, %5
%17 = trunc i64 %6 to i32
br label %18
18: ; preds = %16, %11
%19 = phi i32 [ -1, %11 ], [ %17, %16 ]
ret i32 %19
}