#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 */
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
size_t file_read(void *buf, size_t bufsz, FILE *stream) {
  size_t r;
  r = fread(buf, 1, bufsz, stream);
  if (r < bufsz && ferror(stream)) {
    fprintf(stderr, "Error reading file.\n");
  }
  if (r < bufsz && feof(stream)) {
    fprintf(stderr, "EOF\n");
  }
  return r;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a4 <file_read+0x64>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11a4 <file_read+0x64>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11da <file_read+0x9a>
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     11da <file_read+0x9a>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%r14
cmp    %rbx,%rax
jae    11a7 <file_read+0x67>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1185 <file_read+0x45>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r15,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     11a7 <file_read+0x67>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"Error reading file.\0A\00"
@.str.1 = [5 x i8] c"EOF\0A\00"
define i64 @file_read(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 = load i64, i64* %5
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %22
15: ; preds = %3
%16 = load i32*, i32** %6
%17 = call i64 @ferror(i32* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %15, %3
%23 = load i64, i64* %7
%24 = load i64, i64* %5
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i32*, i32** %6
%28 = call i64 @feof(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %30, %26, %22
%34 = load i64, i64* %7
ret i64 %34
}

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