#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 /*<<< orphan*/ _io_err(int /*<<< orphan*/, size_t, size_t,
                           int /*<<< orphan*/ *);
size_t fread(void *, size_t, size_t, int /*<<< orphan*/ *);
size_t check_fread(void *ptr, size_t size, size_t nitems, FILE *stream) {
  size_t res = 1, nread = 0;
  while (nread < nitems) {
    res = fread(ptr, size, nitems, stream);
    if (res <= 0)
      _io_err(0, size, nitems, stream);
    nread += res;
  }
  return nread;
}


x86 assembly (clang, -O0):
<check_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x1,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    119e <check_fread+0x7e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
ja     118d <check_fread+0x6d>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %edi,%edi
call   1040 <_io_err@plt>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1148 <check_fread+0x28>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_fread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1173 <check_fread+0x53>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
xor    %ebx,%ebx
jmp    1148 <check_fread+0x28>
add    %rbp,%rbx
cmp    %r15,%rbx
jae    1175 <check_fread+0x55>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1140 <check_fread+0x20>
xor    %edi,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <_io_err@plt>
jmp    1140 <check_fread+0x20>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @check_fread(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
store i64 1, i64* %9
store i64 0, i64* %10
br label %11
11: ; preds = %28, %4
%12 = load i64, i64* %10
%13 = load i64, i64* %7
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %32
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i64, i64* %6
%18 = load i64, i64* %7
%19 = load i32*, i32** %8
%20 = call i64 @fread(i8* %16, i64 %17, i64 %18, i32* %19)
store i64 %20, i64* %9
%21 = load i64, i64* %9
%22 = icmp ule i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %15
%24 = load i64, i64* %6
%25 = load i64, i64* %7
%26 = load i32*, i32** %8
%27 = call i32 @_io_err(i32 0, i64 %24, i64 %25, i32* %26)
br label %28
28: ; preds = %23, %15
%29 = load i64, i64* %9
%30 = load i64, i64* %10
%31 = add i64 %30, %29
store i64 %31, i64* %10
br label %11
32: ; preds = %11
%33 = load i64, i64* %10
ret i64 %33
}

LLVM IR (-O2):
define i64 @check_fread(i8* nocapture %0, i64 %1, i64 %2, i32* %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %15, label %6
6: ; preds = %4, %12
%7 = phi i64 [ %13, %12 ], [ 0, %4 ]
%8 = tail call i64 @fread(i8* %0, i64 %1, i64 %2, i32* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @_io_err(i32 0, i64 %1, i64 %2, i32* %3)
br label %12
12: ; preds = %10, %6
%13 = add i64 %8, %7
%14 = icmp ult i64 %13, %2
br i1 %14, label %6, label %15
15: ; preds = %12, %4
%16 = phi i64 [ 0, %4 ], [ %13, %12 ]
ret i64 %16
}