#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fread(int /*<<< orphan*/ *, int, size_t, int /*<<< orphan*/ *);
int safe_fread(uint8_t *buffer, size_t len, FILE *f) {
  size_t i, amt;
  for (i = 0; i < len; i += amt) {
    amt = fread(buffer + i, 1, len - i, f);
    if (ferror(f) || feof(f))
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<safe_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11cb <safe_fread+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    11a9 <safe_fread+0x79>
mov    -0x20(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11b5 <safe_fread+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <safe_fread+0xa2>
jmp    11ba <safe_fread+0x8a>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    114c <safe_fread+0x1c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     119c <safe_fread+0x6c>
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebp,%ebp
mov    $0xffffffff,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbp,4),%rdi
mov    %r14,%rdx
sub    %rbp,%rdx
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    119f <safe_fread+0x6f>
mov    %r12,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    119f <safe_fread+0x6f>
add    %rbx,%rbp
cmp    %r14,%rbp
jb     1160 <safe_fread+0x30>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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