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

/* Variables and functions */
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int bs_fread(u_int8_t *pos, size_t size, size_t nitems, FILE *fp) {
  int rsize = 0, n;
  size *= nitems;
  while (rsize < size) {
    n = size - rsize;
    if (n > 4096)
      n = 4096;
    if (fread(pos + rsize, 1, n, fp) < n) {
      return -1;
    }
    rsize += n;
  }
  return size;
}


x86 assembly (clang, -O0):
<bs_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)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11af <bs_fread+0x9f>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x30(%rbp)
cmpl   $0x1000,-0x30(%rbp)
jle    116c <bs_fread+0x5c>
movl   $0x1000,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x30(%rbp),%eax
jge    11a1 <bs_fread+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <bs_fread+0xa6>
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    113c <bs_fread+0x2c>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bs_fread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
imul   %rdx,%r13
test   %r13,%r13
je     1172 <bs_fread+0x62>
mov    %rcx,%r14
mov    %rdi,%r15
xor    %eax,%eax
mov    $0x1000,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r13d,%ebp
sub    %ebx,%ebp
cmp    $0x1000,%ebp
cmovge %r12d,%ebp
lea    (%r15,%rax,4),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    %ebp,%eax
jl     1177 <bs_fread+0x67>
add    %ebp,%ebx
movslq %ebx,%rax
cmp    %rax,%r13
ja     1140 <bs_fread+0x30>
jmp    117d <bs_fread+0x6d>
xor    %r13d,%r13d
jmp    117d <bs_fread+0x6d>
mov    $0xffffffff,%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 @bs_fread(i32* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i32 0, i32* %10
%12 = load i64, i64* %8
%13 = load i64, i64* %7
%14 = mul i64 %13, %12
store i64 %14, i64* %7
br label %15
15: ; preds = %40, %4
%16 = load i32, i32* %10
%17 = sext i32 %16 to i64
%18 = load i64, i64* %7
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %44
20: ; preds = %15
%21 = load i64, i64* %7
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = sub i64 %21, %23
%25 = trunc i64 %24 to i32
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp sgt i32 %26, 4096
br i1 %27, label %28, label %29
28: ; preds = %20
store i32 4096, i32* %11
br label %29
29: ; preds = %28, %20
%30 = load i32*, i32** %6
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %11
%35 = load i32*, i32** %9
%36 = call i32 @fread(i32* %33, i32 1, i32 %34, i32* %35)
%37 = load i32, i32* %11
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %29
store i32 -1, i32* %5
br label %47
40: ; preds = %29
%41 = load i32, i32* %11
%42 = load i32, i32* %10
%43 = add nsw i32 %42, %41
store i32 %43, i32* %10
br label %15
44: ; preds = %15
%45 = load i64, i64* %7
%46 = trunc i64 %45 to i32
store i32 %46, i32* %5
br label %47
47: ; preds = %44, %39
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
define i32 @bs_fread(i32* nocapture %0, i64 %1, i64 %2, i32* nocapture %3) {
%5 = mul i64 %2, %1
%6 = icmp eq i64 %5, 0
br i1 %6, label %22, label %7
7: ; preds = %4
%8 = trunc i64 %5 to i32
br label %12
9: ; preds = %12
%10 = sext i32 %21 to i64
%11 = icmp ugt i64 %5, %10
br i1 %11, label %12, label %22
12: ; preds = %7, %9
%13 = phi i64 [ 0, %7 ], [ %10, %9 ]
%14 = phi i32 [ 0, %7 ], [ %21, %9 ]
%15 = sub i32 %8, %14
%16 = icmp slt i32 %15, 4096
%17 = select i1 %16, i32 %15, i32 4096
%18 = getelementptr inbounds i32, i32* %0, i64 %13
%19 = tail call i32 @fread(i32* %18, i32 1, i32 %17, i32* %3)
%20 = icmp slt i32 %19, %17
%21 = add nsw i32 %17, %14
br i1 %20, label %22, label %9
22: ; preds = %12, %9, %4
%23 = phi i32 [ 0, %4 ], [ -1, %12 ], [ %8, %9 ]
ret i32 %23
}