#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
unsigned char *alloc_mem(size_t);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(unsigned char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int cache_file(FILE **fp, unsigned char **buf) {
  size_t size = 0;
  fseek(*fp, 0, SEEK_END);
  size = ftell(*fp);
  fseek(*fp, 0, SEEK_SET);
  *buf = alloc_mem(size + 1);
  if (!*buf)
    return -2;
  while (!feof(*fp))
    fread(*buf, size, 1, *fp);
  return size;
}


x86 assembly (clang, -O0):
<cache_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1030 <alloc_mem@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d7 <cache_file+0x87>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1225 <cache_file+0xd5>
jmp    11dc <cache_file+0x8c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11fd <cache_file+0xad>
jmp    121e <cache_file+0xce>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
jmp    11dc <cache_file+0x8c>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cache_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    (%rbx),%rdi
call   1050 <ftell@plt>
mov    %rax,%r15
mov    (%rbx),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r15),%rdi
call   1030 <alloc_mem@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11c1 <cache_file+0x71>
nopl   (%rax)
mov    (%rbx),%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11c7 <cache_file+0x77>
mov    (%r14),%rdi
mov    (%rbx),%rcx
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <fread@plt>
jmp    11a0 <cache_file+0x50>
mov    $0xfffffffe,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @cache_file(i32** %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32**
%5 = alloca i8**
%6 = alloca i64
store i32** %0, i32*** %4
store i8** %1, i8*** %5
store i64 0, i64* %6
%7 = load i32**, i32*** %4
%8 = load i32*, i32** %7
%9 = load i32, i32* @SEEK_END
%10 = call i32 @fseek(i32* %8, i32 0, i32 %9)
%11 = load i32**, i32*** %4
%12 = load i32*, i32** %11
%13 = call i64 @ftell(i32* %12)
store i64 %13, i64* %6
%14 = load i32**, i32*** %4
%15 = load i32*, i32** %14
%16 = load i32, i32* @SEEK_SET
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i64, i64* %6
%19 = add i64 %18, 1
%20 = call i8* @alloc_mem(i64 %19)
%21 = load i8**, i8*** %5
store i8* %20, i8** %21
%22 = load i8**, i8*** %5
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %2
store i32 -2, i32* %3
br label %43
26: ; preds = %2
br label %27
27: ; preds = %33, %26
%28 = load i32**, i32*** %4
%29 = load i32*, i32** %28
%30 = call i32 @feof(i32* %29)
%31 = icmp ne i32 %30, 0
%32 = xor i1 %31, true
br i1 %32, label %33, label %40
33: ; preds = %27
%34 = load i8**, i8*** %5
%35 = load i8*, i8** %34
%36 = load i64, i64* %6
%37 = load i32**, i32*** %4
%38 = load i32*, i32** %37
%39 = call i32 @fread(i8* %35, i64 %36, i32 1, i32* %38)
br label %27
40: ; preds = %27
%41 = load i64, i64* %6
%42 = trunc i64 %41 to i32
store i32 %42, i32* %3
br label %43
43: ; preds = %40, %25
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @cache_file(i32** nocapture %0, i8** nocapture %1) {
%3 = load i32*, i32** %0
%4 = load i32, i32* @SEEK_END
%5 = tail call i32 @fseek(i32* %3, i32 0, i32 %4)
%6 = load i32*, i32** %0
%7 = tail call i64 @ftell(i32* %6)
%8 = load i32*, i32** %0
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %8, i32 0, i32 %9)
%11 = add i64 %7, 1
%12 = tail call i8* @alloc_mem(i64 %11)
store i8* %12, i8** %1
%13 = icmp eq i8* %12, null
br i1 %13, label %27, label %14
14: ; preds = %2
%15 = load i32*, i32** %0
%16 = tail call i32 @feof(i32* %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %14, %18
%19 = load i8*, i8** %1
%20 = load i32*, i32** %0
%21 = tail call i32 @fread(i8* %19, i64 %7, i32 1, i32* %20)
%22 = load i32*, i32** %0
%23 = tail call i32 @feof(i32* %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %18, label %25
25: ; preds = %18, %14
%26 = trunc i64 %7 to i32
br label %27
27: ; preds = %2, %25
%28 = phi i32 [ %26, %25 ], [ -2, %2 ]
ret i32 %28
}