#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*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(void *, size_t, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
size_t fread_loop(void *bufp, size_t elemSize, size_t elems2Read,
                  FILE *theFile) {
  size_t totalRead = fread(bufp, elemSize, elems2Read, theFile);
  bufp = (void *)((char *)bufp + totalRead);
  while (elems2Read > totalRead) {
    size_t numread = fread(bufp, elemSize, elems2Read - totalRead, theFile);
    if (!numread) {
      return totalRead;
    }
    if (numread < 0) {
      fprintf(stderr, "Warning:  bad read in fread_loop, continuing...\n");
    }
    bufp = (void *)((char *)bufp + numread);
    totalRead += numread;
  }
  return totalRead;
}


x86 assembly (clang, -O0):
<fread_loop>:
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    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    11dd <fread_loop+0xbd>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a0 <fread_loop+0x80>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e5 <fread_loop+0xc5>
cmpq   $0x0,-0x38(%rbp)
jae    11c0 <fread_loop+0xa0>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    115d <fread_loop+0x3d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_loop>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r13
call   1030 <fread@plt>
add    %rax,%r13
nopl   (%rax)
mov    %rax,%rbx
cmp    %r12,%rax
jae    115a <fread_loop+0x4a>
mov    %r12,%rdx
sub    %rbx,%rdx
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %rax,%rcx
add    %rax,%r13
add    %rbx,%rax
test   %rcx,%rcx
jne    1130 <fread_loop+0x20>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [49 x i8] c"Warning: bad read in fread_loop, continuing...\0A\00"
define i64 @fread_loop(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = load i64, i64* %8
%15 = load i32*, i32** %9
%16 = call i64 @fread(i8* %12, i64 %13, i64 %14, i32* %15)
store i64 %16, i64* %10
%17 = load i8*, i8** %6
%18 = load i64, i64* %10
%19 = getelementptr inbounds i8, i8* %17, i64 %18
store i8* %19, i8** %6
br label %20
20: ; preds = %42, %4
%21 = load i64, i64* %8
%22 = load i64, i64* %10
%23 = icmp ugt i64 %21, %22
br i1 %23, label %24, label %49
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = load i64, i64* %8
%28 = load i64, i64* %10
%29 = sub i64 %27, %28
%30 = load i32*, i32** %9
%31 = call i64 @fread(i8* %25, i64 %26, i64 %29, i32* %30)
store i64 %31, i64* %11
%32 = load i64, i64* %11
%33 = icmp ne i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %24
%35 = load i64, i64* %10
store i64 %35, i64* %5
br label %51
36: ; preds = %24
%37 = load i64, i64* %11
%38 = icmp ult i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %39, %36
%43 = load i8*, i8** %6
%44 = load i64, i64* %11
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8* %45, i8** %6
%46 = load i64, i64* %11
%47 = load i64, i64* %10
%48 = add i64 %47, %46
store i64 %48, i64* %10
br label %20
49: ; preds = %20
%50 = load i64, i64* %10
store i64 %50, i64* %5
br label %51
51: ; preds = %49, %34
%52 = load i64, i64* %5
ret i64 %52
}

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