#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_textfile_to_memory(const char *given_filename) {
  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 + 1)) != NULL) {
      fread(my_buff, my_filesize, 1, my_file);
      fclose(my_file);
      *(my_buff + my_filesize) = '\0';
      return (my_buff);
    } else {
      fclose(my_file);
    }
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<load_textfile_to_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     121c <load_textfile_to_memory+0xbc>
mov    -0x28(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     120e <load_textfile_to_memory+0xae>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1224 <load_textfile_to_memory+0xc4>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    121c <load_textfile_to_memory+0xbc>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_textfile_to_memory>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e8 <load_textfile_to_memory+0x88>
mov    %rax,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %eax,%r14d
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r14),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e0 <load_textfile_to_memory+0x80>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
jmp    11eb <load_textfile_to_memory+0x8b>
mov    %rbx,%rdi
call   1040 <fclose@plt>
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* @load_textfile_to_memory(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %39
10: ; preds = %1
%11 = load i32*, i32** %6
%12 = load i32, i32* @SEEK_END
%13 = call i32 @fseek(i32* %11, i32 0, i32 %12)
%14 = load i32*, i32** %6
%15 = call i32 @ftell(i32* %14)
store i32 %15, i32* %4
%16 = load i32*, i32** %6
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %35
23: ; preds = %10
%24 = load i8*, i8** %5
%25 = load i32, i32* %4
%26 = load i32*, i32** %6
%27 = call i32 @fread(i8* %24, i32 %25, i32 1, i32* %26)
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
%30 = load i8*, i8** %5
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %5
store i8* %34, i8** %2
br label %40
35: ; preds = %10
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
br label %38
38: ; preds = %35
br label %39
39: ; preds = %38, %1
store i8* null, i8** %2
br label %40
40: ; preds = %39, %23
%41 = load i8*, i8** %2
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* @load_textfile_to_memory(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %20, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %2, i32 0, i32 %5)
%7 = tail call i32 @ftell(i32* %2)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = add nsw i32 %7, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %18, label %13
13: ; preds = %4
%14 = tail call i32 @fread(i8* %11, i32 %7, i32 1, i32* %2)
%15 = tail call i32 @fclose(i32* %2)
%16 = sext i32 %7 to i64
%17 = getelementptr inbounds i8, i8* %11, i64 %16
store i8 0, i8* %17
br label %20
18: ; preds = %4
%19 = tail call i32 @fclose(i32* %2)
br label %20
20: ; preds = %1, %18, %13
%21 = phi i8* [ %11, %13 ], [ null, %18 ], [ null, %1 ]
ret i8* %21
}