#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;
char *build_path(char const *);
int /*<<< orphan*/ cache_exists(char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
char *cache_get(const char *name, int *size) {
  char *path;
  FILE *file;
  char *data = NULL;
  if (!cache_exists(name)) {
    return NULL;
  }
  path = build_path(name);
  file = fopen(path, "rb");
  free(path);
  if (!file) {
    return NULL;
  }
  fseek(file, 0, SEEK_END);
  *size = ftell(file);
  fseek(file, 0, SEEK_SET);
  if (*size > 0) {
    data = malloc(*size);
    *size = fread(data, 1, *size, file);
  }
  fclose(file);
  return data;
}


x86 assembly (clang, -O0):
<cache_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <cache_exists@plt>
cmp    $0x0,%eax
jne    11c7 <cache_get+0x37>
movq   $0x0,-0x8(%rbp)
jmp    128f <cache_get+0xff>
mov    -0x10(%rbp),%rdi
call   1040 <build_path@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1209 <cache_get+0x79>
movq   $0x0,-0x8(%rbp)
jmp    128f <cache_get+0xff>
mov    -0x28(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jle    127e <cache_get+0xee>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cache_get>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1070 <cache_exists@plt>
test   %eax,%eax
je     1238 <cache_get+0xa8>
mov    %rbx,%rdi
call   1040 <build_path@plt>
mov    %rax,%rbp
lea    0xe44(%rip),%rsi
mov    %rax,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     1238 <cache_get+0xa8>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,(%r14)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    (%r14),%ebp
test   %ebp,%ebp
jle    122e <cache_get+0x9e>
mov    %ebp,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    123b <cache_get+0xab>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @cache_get(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8* null, i8** %8
%9 = load i8*, i8** %4
%10 = call i32 @cache_exists(i8* %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %2
store i8* null, i8** %3
br label %50
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i8* @build_path(i8* %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %7
%18 = load i8*, i8** %6
%19 = call i32 @free(i8* %18)
%20 = load i32*, i32** %7
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %13
store i8* null, i8** %3
br label %50
23: ; preds = %13
%24 = load i32*, i32** %7
%25 = load i32, i32* @SEEK_END
%26 = call i32 @fseek(i32* %24, i32 0, i32 %25)
%27 = load i32*, i32** %7
%28 = call i32 @ftell(i32* %27)
%29 = load i32*, i32** %5
store i32 %28, i32* %29
%30 = load i32*, i32** %7
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @fseek(i32* %30, i32 0, i32 %31)
%33 = load i32*, i32** %5
%34 = load i32, i32* %33
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %23
%37 = load i32*, i32** %5
%38 = load i32, i32* %37
%39 = call i8* @malloc(i32 %38)
store i8* %39, i8** %8
%40 = load i8*, i8** %8
%41 = load i32*, i32** %5
%42 = load i32, i32* %41
%43 = load i32*, i32** %7
%44 = call i32 @fread(i8* %40, i32 1, i32 %42, i32* %43)
%45 = load i32*, i32** %5
store i32 %44, i32* %45
br label %46
46: ; preds = %36, %23
%47 = load i32*, i32** %7
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %8
store i8* %49, i8** %3
br label %50
50: ; preds = %46, %22, %12
%51 = load i8*, i8** %3
ret i8* %51
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @cache_get(i8* %0, i32* nocapture %1) {
%3 = tail call i32 @cache_exists(i8* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %24, label %5
5: ; preds = %2
%6 = tail call i8* @build_path(i8* %0)
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @free(i8* %6)
%9 = icmp eq i32* %7, null
br i1 %9, label %24, label %10
10: ; preds = %5
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %7, i32 0, i32 %11)
%13 = tail call i32 @ftell(i32* %7)
store i32 %13, i32* %1
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %7, i32 0, i32 %14)
%16 = load i32, i32* %1
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = tail call i8* @malloc(i32 %16)
%20 = tail call i32 @fread(i8* %19, i32 1, i32 %16, i32* %7)
store i32 %20, i32* %1
br label %21
21: ; preds = %18, %10
%22 = phi i8* [ %19, %18 ], [ null, %10 ]
%23 = tail call i32 @fclose(i32* %7)
br label %24
24: ; preds = %5, %2, %21
%25 = phi i8* [ %22, %21 ], [ null, %2 ], [ null, %5 ]
ret i8* %25
}