#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*/ *fopen(char *, char *);
size_t fread(unsigned char *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
unsigned char *loadfile2(char *filename, unsigned long *filesize) {
  FILE *filepointer;
  unsigned char *filebuffer;
  size_t bytesread;
  filepointer = fopen(filename, "rb");
  if (filepointer == NULL) {
    perror("Could not open file, Error");
    return NULL;
  }
  fseek(filepointer, 0L, SEEK_END);
  *filesize = ftell(filepointer);
  rewind(filepointer);
  filebuffer = (unsigned char *)malloc(sizeof(unsigned char) * *filesize);
  if (filebuffer == NULL) {
    perror("Could not allocate memory, Error");
    return NULL;
  }
  bytesread = fread(filebuffer, 1, *filesize, filepointer);
  if (bytesread != *filesize) {
    perror("Could not read file, Error");
    return NULL;
  }
  return filebuffer;
}


x86 assembly (clang, -O0):
<loadfile2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b8 <loadfile2+0x48>
lea    0xe5d(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1273 <loadfile2+0x103>
mov    -0x20(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1224 <loadfile2+0xb4>
lea    0xe0c(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1273 <loadfile2+0x103>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     126b <loadfile2+0xfb>
lea    0xde6(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1273 <loadfile2+0x103>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadfile2>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e3 <loadfile2+0x73>
mov    %rax,%rbx
mov    0x2e55(%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    %rbx,%rdi
call   1040 <rewind@plt>
mov    (%r14),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ec <loadfile2+0x7c>
mov    %rax,%r15
mov    (%r14),%rdx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    (%r14),%rax
jne    11f5 <loadfile2+0x85>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe19(%rip),%rdi
jmp    11fc <loadfile2+0x8c>
lea    0xe2b(%rip),%rdi
jmp    11fc <loadfile2+0x8c>
lea    0xe43(%rip),%rdi
call   1090 <perror@plt>
xor    %r15d,%r15d
jmp    11da <loadfile2+0x6a>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [27 x i8] c"Could not open file, Error\00"
@SEEK_END = global i32 0
@.str.2 = [33 x i8] c"Could not allocate memory, Error\00"
@.str.3 = [27 x i8] c"Could not read file, Error\00"
define i8* @loadfile2(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 %20, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i64 0, i32 %6)
%8 = tail call i64 @ftell(i32* %3)
store i64 %8, i64* %1
%9 = tail call i32 @rewind(i32* %3)
%10 = load i64, i64* %1
%11 = trunc i64 %10 to i32
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %20, label %15
15: ; preds = %5
%16 = load i64, i64* %1
%17 = tail call i64 @fread(i8* %13, i32 1, i64 %16, i32* %3)
%18 = load i64, i64* %1
%19 = icmp eq i64 %17, %18
br i1 %19, label %23, label %20
20: ; preds = %15, %5, %2
%21 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), %5 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), %15 ]
%22 = tail call i32 @perror(i8* %21)
br label %23
23: ; preds = %20, %15
%24 = phi i8* [ %13, %15 ], [ null, %20 ]
ret i8* %24
}