#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 *, size_t, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int fread_err(void *ptr, size_t size, size_t count, FILE *stream) {
  if (fread(ptr, size, count, stream) != count) {
    if (ferror(stream)) {
      perror("toasim: read error");
    }
    if (feof(stream)) {
      fprintf(stderr, "toasim: EOF reached!\n");
    }
  }
}


x86 assembly (clang, -O0):
<fread_err>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <fread@plt>
cmp    -0x20(%rbp),%rax
je     11d3 <fread_err+0x83>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11a6 <fread_err+0x56>
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     11ce <fread_err+0x7e>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11d3 <fread_err+0x83>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_err>:
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
call   1040 <fread@plt>
cmp    %rbx,%rax
je     1193 <fread_err+0x43>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    119b <fread_err+0x4b>
mov    %r14,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     1193 <fread_err+0x43>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1050 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe5e(%rip),%rdi
call   1070 <perror@plt>
jmp    1171 <fread_err+0x21>

LLVM IR (-O0):
@.str = [19 x i8] c"toasim: read error\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"toasim: EOF reached!\0A\00"
define i32 @fread_err(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%10 = load i8*, i8** %6
%11 = load i64, i64* %7
%12 = load i64, i64* %8
%13 = load i32*, i32** %9
%14 = call i64 @fread(i8* %10, i64 %11, i64 %12, i32* %13)
%15 = load i64, i64* %8
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %31
17: ; preds = %4
%18 = load i32*, i32** %9
%19 = call i64 @ferror(i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = load i32*, i32** %9
%25 = call i64 @feof(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %27, %23
br label %31
31: ; preds = %30, %4
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
@.str = [19 x i8] c"toasim: read error\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"toasim: EOF reached!\0A\00"
define i32 @fread_err(i8* nocapture %0, i64 %1, i64 %2, i32* nocapture %3) {
%5 = tail call i64 @fread(i8* %0, i64 %1, i64 %2, i32* %3)
%6 = icmp eq i64 %5, %2
br i1 %6, label %18, label %7
7: ; preds = %4
%8 = tail call i64 @ferror(i32* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = tail call i64 @feof(i32* %3)
%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 ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %12, %15, %4
ret i32 undef
}