#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;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
char *cw_load_file(const char *filename, size_t *size) {
  char *buf;
  FILE *infile;
  infile = fopen(filename, "rb");
  if (!infile)
    return NULL;
  fseek(infile, 0, SEEK_END);
  *size = ftell(infile);
  buf = malloc(*size);
  if (!buf)
    goto errX;
  fseek(infile, 0, SEEK_SET);
  *size = fread(buf, 1, *size, infile);
errX:
  fclose(infile);
  return buf;
}


x86 assembly (clang, -O0):
<cw_load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119c <cw_load_file+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    122b <cw_load_file+0xcb>
mov    -0x28(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e3 <cw_load_file+0x83>
jmp    121a <cw_load_file+0xba>
mov    -0x28(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cw_load_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe91(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11dd <cw_load_file+0x7d>
mov    %rax,%rbx
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11d3 <cw_load_file+0x73>
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    (%r14),%rdx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11e0 <cw_load_file+0x80>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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