#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*/ errno;
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t safe_fread(void *buf, size_t len, FILE *file) {
  size_t rd = fread(buf, 1, len, file);
  if (!rd) {
    if (ferror(file))
      fprintf(stderr, "Error reading file: %s\n", strerror(errno));
    else
      fprintf(stderr, "Unexpected end of file\n");
  }
  return rd;
}


x86 assembly (clang, -O0):
<safe_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11db <safe_fread+0x9b>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11bf <safe_fread+0x7f>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11d6 <safe_fread+0x96>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11db <safe_fread+0x9b>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fread>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%rdx
mov    $0x1,%esi
mov    %rbp,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a7 <safe_fread+0x67>
mov    %rbp,%rdi
call   1030 <ferror@plt>
mov    0x2e73(%rip),%rcx
mov    (%rcx),%ebp
test   %rax,%rax
je     1197 <safe_fread+0x57>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe77(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11a7 <safe_fread+0x67>
lea    0xe7a(%rip),%rsi
mov    %ebp,%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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