#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(unsigned char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
unsigned char *malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
unsigned char *readFiletoBuffer(size_t *size, unsigned char *fileName) {
  FILE *fp;
  unsigned char *buffer;
  fp = fopen(fileName, "rb");
  if (fp == NULL)
    exit(0);
  fseek(fp, 0, SEEK_END);
  *size = ftell(fp);
  rewind(fp);
  buffer = malloc(*size);
  fread(buffer, 1, *size, fp);
  fclose(fp);
  return buffer;
}


x86 assembly (clang, -O0):
<readFiletoBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b6 <readFiletoBuffer+0x36>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFiletoBuffer>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xe6e(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11fa <readFiletoBuffer+0x7a>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    (%r14),%r14
mov    %r14,%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %edi,%edi
call   10a0 <exit@plt>

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

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