#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*/ *);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
size_t fread_bytes(FILE *stream, void *ptr, size_t byte_count, bool *eof,
                   bool *error) {
  size_t total = 0;
  *eof = false;
  *error = false;
  while (byte_count) {
    size_t count;
    count = fread(ptr, 1, byte_count, stream);
    ptr += count;
    total += count;
    byte_count -= count;
    if (byte_count) {
      if (ferror(stream)) {
        *error = true;
        return total;
      }
      if (feof(stream)) {
        *eof = true;
        return total;
      }
    }
  }
  return total;
}


x86 assembly (clang, -O0):
<fread_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x30(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     121d <fread_bytes+0xed>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1218 <fread_bytes+0xe8>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11e9 <fread_bytes+0xb9>
mov    -0x30(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1225 <fread_bytes+0xf5>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1213 <fread_bytes+0xe3>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1225 <fread_bytes+0xf5>
jmp    1218 <fread_bytes+0xe8>
jmp    1168 <fread_bytes+0x38>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,(%rcx)
movl   $0x0,(%r8)
test   %rdx,%rdx
je     11a8 <fread_bytes+0x78>
mov    %r8,%r14
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %rdi,%rbp
mov    %rcx,(%rsp)
xor    %r12d,%r12d
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %rbp,%rcx
call   1040 <fread@plt>
add    %rax,%r12
sub    %rax,%rbx
je     11ab <fread_bytes+0x7b>
mov    %rax,%r15
mov    %rbp,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    119f <fread_bytes+0x6f>
add    %r15,%r13
mov    %rbp,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     1160 <fread_bytes+0x30>
mov    (%rsp),%r14
movl   $0x1,(%r14)
jmp    11ab <fread_bytes+0x7b>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @fread_bytes(i32* %0, i8* %1, i64 %2, i32* %3, i32* %4) {
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i64
store i32* %0, i32** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32* %3, i32** %10
store i32* %4, i32** %11
store i64 0, i64* %12
%14 = load i32*, i32** %10
store i32 0, i32* %14
%15 = load i32*, i32** %11
store i32 0, i32* %15
br label %16
16: ; preds = %50, %5
%17 = load i64, i64* %9
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %51
19: ; preds = %16
%20 = load i8*, i8** %8
%21 = load i64, i64* %9
%22 = load i32*, i32** %7
%23 = call i64 @fread(i8* %20, i32 1, i64 %21, i32* %22)
store i64 %23, i64* %13
%24 = load i64, i64* %13
%25 = load i8*, i8** %8
%26 = getelementptr i8, i8* %25, i64 %24
store i8* %26, i8** %8
%27 = load i64, i64* %13
%28 = load i64, i64* %12
%29 = add i64 %28, %27
store i64 %29, i64* %12
%30 = load i64, i64* %13
%31 = load i64, i64* %9
%32 = sub i64 %31, %30
store i64 %32, i64* %9
%33 = load i64, i64* %9
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %50
35: ; preds = %19
%36 = load i32*, i32** %7
%37 = call i64 @ferror(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32*, i32** %11
store i32 1, i32* %40
%41 = load i64, i64* %12
store i64 %41, i64* %6
br label %53
42: ; preds = %35
%43 = load i32*, i32** %7
%44 = call i64 @feof(i32* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32*, i32** %10
store i32 1, i32* %47
%48 = load i64, i64* %12
store i64 %48, i64* %6
br label %53
49: ; preds = %42
br label %50
50: ; preds = %49, %19
br label %16
51: ; preds = %16
%52 = load i64, i64* %12
store i64 %52, i64* %6
br label %53
53: ; preds = %51, %46, %39
%54 = load i64, i64* %6
ret i64 %54
}

LLVM IR (-O2):
define i64 @fread_bytes(i32* nocapture %0, i8* nocapture %1, i64 %2, i32* nocapture writeonly %3, i32* nocapture writeonly %4) {
store i32 0, i32* %3
store i32 0, i32* %4
%6 = icmp eq i64 %2, 0
br i1 %6, label %24, label %7
7: ; preds = %5, %19
%8 = phi i64 [ %13, %19 ], [ 0, %5 ]
%9 = phi i8* [ %12, %19 ], [ %1, %5 ]
%10 = phi i64 [ %14, %19 ], [ %2, %5 ]
%11 = tail call i64 @fread(i8* %9, i32 1, i64 %10, i32* %0)
%12 = getelementptr i8, i8* %9, i64 %11
%13 = add i64 %11, %8
%14 = sub i64 %10, %11
%15 = icmp eq i64 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %7
%17 = tail call i64 @ferror(i32* %0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i64 @feof(i32* %0)
%21 = icmp eq i64 %20, 0
br i1 %21, label %7, label %22
22: ; preds = %19, %16
%23 = phi i32* [ %4, %16 ], [ %3, %19 ]
store i32 1, i32* %23
br label %24
24: ; preds = %7, %22, %5
%25 = phi i64 [ 0, %5 ], [ %13, %22 ], [ %13, %7 ]
ret i64 %25
}