#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 ENODATA;
int errno;
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
int img_stdio_fread(void *obj, void *buf, size_t num_bytes,
                    size_t *bytes_read) {
  FILE *file = (FILE *)obj;
  *bytes_read = fread(buf, 1, num_bytes, file);
  if (ferror(file))
    return errno;
  if (feof(file))
    return ENODATA;
  return 0;
}


x86 assembly (clang, -O0):
<img_stdio_fread>:
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),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1194 <img_stdio_fread+0x64>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <img_stdio_fread+0x8f>
mov    -0x30(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11b8 <img_stdio_fread+0x88>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <img_stdio_fread+0x8f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<img_stdio_fread>:
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1163 <img_stdio_fread+0x33>
mov    0x2e77(%rip),%rax
jmp    117c <img_stdio_fread+0x4c>
mov    %rbx,%rdi
call   1050 <feof@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     117e <img_stdio_fread+0x4e>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i32 0
@ENODATA = global i32 0
define i32 @img_stdio_fread(i8* %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64* %3, i64** %9
%11 = load i8*, i8** %6
%12 = bitcast i8* %11 to i32*
store i32* %12, i32** %10
%13 = load i8*, i8** %7
%14 = load i64, i64* %8
%15 = load i32*, i32** %10
%16 = call i64 @fread(i8* %13, i32 1, i64 %14, i32* %15)
%17 = load i64*, i64** %9
store i64 %16, i64* %17
%18 = load i32*, i32** %10
%19 = call i64 @ferror(i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = load i32, i32* @errno
store i32 %22, i32* %5
br label %30
23: ; preds = %4
%24 = load i32*, i32** %10
%25 = call i64 @feof(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i32, i32* @ENODATA
store i32 %28, i32* %5
br label %30
29: ; preds = %23
store i32 0, i32* %5
br label %30
30: ; preds = %29, %27, %21
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
@errno = global i32 0
@ENODATA = global i32 0
define i32 @img_stdio_fread(i8* nocapture %0, i8* nocapture %1, i64 %2, i64* nocapture writeonly %3) {
%5 = bitcast i8* %0 to i32*
%6 = tail call i64 @fread(i8* %1, i32 1, i64 %2, i32* %5)
store i64 %6, i64* %3
%7 = tail call i64 @ferror(i32* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = load i32, i32* @errno
br label %16
11: ; preds = %4
%12 = tail call i64 @feof(i32* %5)
%13 = icmp eq i64 %12, 0
%14 = load i32, i32* @ENODATA
%15 = select i1 %13, i32 0, i32 %14
br label %16
16: ; preds = %11, %9
%17 = phi i32 [ %10, %9 ], [ %15, %11 ]
ret i32 %17
}