#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int readcachefile(char *cachefile, unsigned char *buffer) {
  FILE *cacheptr = fopen(cachefile, "rb");
  unsigned char cachebuff[512];
  int buflen = 256, nread = 0, maxbytes = (131072), nbytes = 0;
  if (cacheptr == (FILE *)NULL)
    goto end_of_job;
  if (buffer == (unsigned char *)NULL)
    goto end_of_job;
  while (1) {
    nread = fread(cachebuff, sizeof(unsigned char), buflen, cacheptr);
    if (nbytes + nread > maxbytes)
      nread = maxbytes - nbytes;
    if (nread < 1)
      break;
    memcpy(buffer + nbytes, cachebuff, nread);
    nbytes += nread;
    if (nread < buflen)
      break;
    if (nbytes >= maxbytes)
      break;
  }
end_of_job:
  if (cacheptr != NULL)
    fclose(cacheptr);
  return (nbytes);
}


x86 assembly (clang, -O0):
<readcachefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x100,-0x224(%rbp)
movl   $0x0,-0x228(%rbp)
movl   $0x20000,-0x22c(%rbp)
movl   $0x0,-0x230(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119f <readcachefile+0x5f>
jmp    127b <readcachefile+0x13b>
cmpq   $0x0,-0x10(%rbp)
jne    11af <readcachefile+0x6f>
jmp    127b <readcachefile+0x13b>
jmp    11b4 <readcachefile+0x74>
lea    -0x220(%rbp),%rdi
mov    -0x224(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x228(%rbp)
mov    -0x230(%rbp),%eax
add    -0x228(%rbp),%eax
cmp    -0x22c(%rbp),%eax
jle    11ff <readcachefile+0xbf>
mov    -0x22c(%rbp),%eax
sub    -0x230(%rbp),%eax
mov    %eax,-0x228(%rbp)
cmpl   $0x1,-0x228(%rbp)
jge    1211 <readcachefile+0xd1>
jmp    1276 <readcachefile+0x136>
mov    -0x10(%rbp),%rdi
movslq -0x230(%rbp),%rax
add    %rax,%rdi
lea    -0x220(%rbp),%rsi
mov    -0x228(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x228(%rbp),%eax
add    -0x230(%rbp),%eax
mov    %eax,-0x230(%rbp)
mov    -0x228(%rbp),%eax
cmp    -0x224(%rbp),%eax
jge    125a <readcachefile+0x11a>
jmp    1276 <readcachefile+0x136>
mov    -0x230(%rbp),%eax
cmp    -0x22c(%rbp),%eax
jl     1271 <readcachefile+0x131>
jmp    1276 <readcachefile+0x136>
jmp    11b4 <readcachefile+0x74>
jmp    127b <readcachefile+0x13b>
cmpq   $0x0,-0x18(%rbp)
je     128f <readcachefile+0x14f>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x230(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readcachefile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%r15
lea    0xea7(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
xor    %ebx,%ebx
test   %r15,%r15
je     11ce <readcachefile+0x8e>
test   %r14,%r14
je     11ce <readcachefile+0x8e>
xor    %ebx,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
lea    (%rax,%rbx,1),%ecx
mov    $0x20000,%ebp
sub    %ebx,%ebp
cmp    $0x20001,%ecx
cmovl  %eax,%ebp
test   %ebp,%ebp
jle    11ce <readcachefile+0x8e>
movslq %ebx,%rdi
add    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
add    %ebp,%ebx
cmp    $0x100,%ebp
jb     11ce <readcachefile+0x8e>
cmp    $0x20000,%ebx
jl     1180 <readcachefile+0x40>
test   %r14,%r14
je     11db <readcachefile+0x9b>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i32 @readcachefile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [512 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %5
store i32 256, i32* %7
store i32 0, i32* %8
store i32 131072, i32* %9
store i32 0, i32* %10
%13 = load i32*, i32** %5
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
br label %61
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %16
br label %61
20: ; preds = %16
br label %21
21: ; preds = %20, %59
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%23 = load i32, i32* %7
%24 = load i32*, i32** %5
%25 = call i32 @fread(i8* %22, i32 1, i32 %23, i32* %24)
store i32 %25, i32* %8
%26 = load i32, i32* %10
%27 = load i32, i32* %8
%28 = add nsw i32 %26, %27
%29 = load i32, i32* %9
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = load i32, i32* %9
%33 = load i32, i32* %10
%34 = sub nsw i32 %32, %33
store i32 %34, i32* %8
br label %35
35: ; preds = %31, %21
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 1
br i1 %37, label %38, label %39
38: ; preds = %35
br label %60
39: ; preds = %35
%40 = load i8*, i8** %4
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%45 = load i32, i32* %8
%46 = call i32 @memcpy(i8* %43, i8* %44, i32 %45)
%47 = load i32, i32* %8
%48 = load i32, i32* %10
%49 = add nsw i32 %48, %47
store i32 %49, i32* %10
%50 = load i32, i32* %8
%51 = load i32, i32* %7
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %39
br label %60
54: ; preds = %39
%55 = load i32, i32* %10
%56 = load i32, i32* %9
%57 = icmp sge i32 %55, %56
br i1 %57, label %58, label %59
58: ; preds = %54
br label %60
59: ; preds = %54
br label %21
60: ; preds = %58, %53, %38
br label %61
61: ; preds = %60, %19, %15
%62 = load i32*, i32** %5
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i32*, i32** %5
%66 = call i32 @fclose(i32* %65)
br label %67
67: ; preds = %64, %61
%68 = load i32, i32* %10
ret i32 %68
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @readcachefile(i8* nocapture %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = icmp eq i32* %4, null
%7 = icmp eq i8* %1, null
%8 = or i1 %7, %6
br i1 %8, label %25, label %9
9: ; preds = %2, %17
%10 = phi i32 [ %21, %17 ], [ 0, %2 ]
%11 = call i32 @fread(i8* %5, i32 1, i32 256, i32* %4)
%12 = add nsw i32 %11, %10
%13 = icmp sgt i32 %12, 131072
%14 = sub nsw i32 131072, %10
%15 = select i1 %13, i32 %14, i32 %11
%16 = icmp slt i32 %15, 1
br i1 %16, label %25, label %17
17: ; preds = %9
%18 = sext i32 %10 to i64
%19 = getelementptr inbounds i8, i8* %1, i64 %18
%20 = call i32 @memcpy(i8* %19, i8* %5, i32 %15)
%21 = add nsw i32 %15, %10
%22 = icmp ult i32 %15, 256
%23 = icmp sgt i32 %21, 131071
%24 = select i1 %22, i1 true, i1 %23
br i1 %24, label %25, label %9
25: ; preds = %9, %17, %2
%26 = phi i32 [ 0, %2 ], [ %21, %17 ], [ %10, %9 ]
br i1 %6, label %29, label %27
27: ; preds = %25
%28 = call i32 @fclose(i32* %4)
br label %29
29: ; preds = %27, %25
ret i32 %26
}