#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;
void 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    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <fread@plt>
cmp    -0x18(%rbp),%rax
je     11d3 <fread_err+0x83>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11a6 <fread_err+0x56>
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%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>
add    $0x20,%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     119a <fread_err+0x4a>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11a2 <fread_err+0x52>
mov    %r14,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     119a <fread_err+0x4a>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe57(%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 void @fread_err(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = load i64, i64* %7
%12 = load i32*, i32** %8
%13 = call i64 @fread(i8* %9, i64 %10, i64 %11, i32* %12)
%14 = load i64, i64* %7
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %30
16: ; preds = %4
%17 = load i32*, i32** %8
%18 = call i64 @ferror(i32* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i32*, i32** %8
%24 = call i64 @feof(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %26, %22
br label %30
30: ; preds = %29, %4
ret void
}

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 void @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 void
}