#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*/ clearerr(int /*<<< orphan*/ *);
int errno;
int ferror(int /*<<< orphan*/ *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int freadn(void *dst, FILE *src, size_t len) {
  int rc;
  size_t n, rem = len;
  unsigned char *ptr = dst;
  clearerr(src);
  while (rem > 0) {
    if ((n = fread(ptr, 1, rem, src)) != rem) {
      if ((rc = ferror(src))) {
        goto finally;
      }
      if (!n) {
        rc = errno ? errno : -1;
        goto finally;
      }
    }
    rem -= n;
    ptr += n;
  }
  rc = 0;
finally:
  return rc;
}


x86 assembly (clang, -O0):
<freadn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <clearerr@plt>
cmpq   $0x0,-0x30(%rbp)
jbe    120f <freadn+0xdf>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
cmp    -0x30(%rbp),%rax
je     11ef <freadn+0xbf>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     11a6 <freadn+0x76>
jmp    1216 <freadn+0xe6>
cmpq   $0x0,-0x28(%rbp)
jne    11ea <freadn+0xba>
mov    0x2e28(%rip),%rax
cmpl   $0x0,(%rax)
je     11d2 <freadn+0xa2>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    11df <freadn+0xaf>
mov    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    11df <freadn+0xaf>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1216 <freadn+0xe6>
jmp    11ef <freadn+0xbf>
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    115d <freadn+0x2d>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freadn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
call   1050 <clearerr@plt>
xor    %r14d,%r14d
test   %rbx,%rbx
jne    1168 <freadn+0x38>
jmp    11ab <freadn+0x7b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %rbp,%r12
sub    %rbp,%rbx
je     11ab <freadn+0x7b>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%rbp
cmp    %rax,%rbx
je     1160 <freadn+0x30>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %eax,%eax
jne    11a8 <freadn+0x78>
test   %rbp,%rbp
jne    1160 <freadn+0x30>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
cmp    $0x1,%eax
sbb    %r14d,%r14d
or     %eax,%r14d
jmp    11ab <freadn+0x7b>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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