#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 *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
char *load_binfile_to_memory(const char *given_filename, int *file_size) {
  int my_filesize;
  char *my_buff;
  FILE *my_file;
  if ((my_file = fopen(given_filename, "rb")) != NULL) {
    fseek(my_file, 0, SEEK_END);
    my_filesize = ftell(my_file);
    fseek(my_file, 0, SEEK_SET);
    if ((my_buff = malloc(my_filesize)) != NULL) {
      fread(my_buff, my_filesize, 1, my_file);
      fclose(my_file);
      *file_size = my_filesize;
      return (my_buff);
    } else {
      fclose(my_file);
    }
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<load_binfile_to_memory>:
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,-0x30(%rbp)
cmp    $0x0,%rax
je     121a <load_binfile_to_memory+0xba>
mov    -0x30(%rbp),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     120c <load_binfile_to_memory+0xac>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1222 <load_binfile_to_memory+0xc2>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
jmp    121a <load_binfile_to_memory+0xba>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_binfile_to_memory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe8f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e7 <load_binfile_to_memory+0x87>
mov    %rax,%rbx
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %eax,%r15d
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r15d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11df <load_binfile_to_memory+0x7f>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15d,(%r14)
jmp    11e9 <load_binfile_to_memory+0x89>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
mov    %rbp,%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* @load_binfile_to_memory(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %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** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %38
12: ; preds = %2
%13 = load i32*, i32** %8
%14 = load i32, i32* @SEEK_END
%15 = call i32 @fseek(i32* %13, i32 0, i32 %14)
%16 = load i32*, i32** %8
%17 = call i32 @ftell(i32* %16)
store i32 %17, i32* %6
%18 = load i32*, i32** %8
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32, i32* %6
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %7
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %34
24: ; preds = %12
%25 = load i8*, i8** %7
%26 = load i32, i32* %6
%27 = load i32*, i32** %8
%28 = call i32 @fread(i8* %25, i32 %26, i32 1, i32* %27)
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* %6
%32 = load i32*, i32** %5
store i32 %31, i32* %32
%33 = load i8*, i8** %7
store i8* %33, i8** %3
br label %39
34: ; preds = %12
%35 = load i32*, i32** %8
%36 = call i32 @fclose(i32* %35)
br label %37
37: ; preds = %34
br label %38
38: ; preds = %37, %2
store i8* null, i8** %3
br label %39
39: ; preds = %38, %24
%40 = load i8*, i8** %3
ret i8* %40
}

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