#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*/ fclose(int /*<<< orphan*/ *);
long find_file_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
long fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(long);
char *read_file_to_memory(const char *file_name) {
  FILE *fp = fopen(file_name, "r");
  char *buffer = NULL;
  long file_size;
  if (fp == NULL) {
    goto clean_up;
  }
  file_size = find_file_size(fp);
  if (file_size < 0) {
    goto clean_up;
  }
  buffer = malloc(file_size + 1);
  if (buffer == NULL) {
    goto clean_up;
  }
  if (fread(buffer, 1, file_size, fp) != file_size) {
    goto clean_up;
  }
  buffer[file_size] = '\0';
  fclose(fp);
  return buffer;
clean_up:
  if (fp != NULL) {
    fclose(fp);
  }
  if (buffer != NULL) {
    free(buffer);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<read_file_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,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1198 <read_file_to_memory+0x38>
jmp    121d <read_file_to_memory+0xbd>
mov    -0x18(%rbp),%rdi
call   1060 <find_file_size@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11b5 <read_file_to_memory+0x55>
jmp    121d <read_file_to_memory+0xbd>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d6 <read_file_to_memory+0x76>
jmp    121d <read_file_to_memory+0xbd>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x28(%rbp),%rax
je     11fb <read_file_to_memory+0x9b>
jmp    121d <read_file_to_memory+0xbd>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124d <read_file_to_memory+0xed>
cmpq   $0x0,-0x18(%rbp)
je     1231 <read_file_to_memory+0xd1>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
je     1245 <read_file_to_memory+0xe5>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_to_memory>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11db <read_file_to_memory+0x7b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <find_file_size@plt>
test   %rax,%rax
js     11c1 <read_file_to_memory+0x61>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11c1 <read_file_to_memory+0x61>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
jne    11cb <read_file_to_memory+0x6b>
movb   $0x0,(%r14,%r15,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11de <read_file_to_memory+0x7e>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11db <read_file_to_memory+0x7b>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @read_file_to_memory(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
store i8* null, i8** %5
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
br label %40
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = call i64 @find_file_size(i32* %13)
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
br label %40
18: ; preds = %12
%19 = load i64, i64* %6
%20 = add nsw i64 %19, 1
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %18
br label %40
25: ; preds = %18
%26 = load i8*, i8** %5
%27 = load i64, i64* %6
%28 = load i32*, i32** %4
%29 = call i64 @fread(i8* %26, i32 1, i64 %27, i32* %28)
%30 = load i64, i64* %6
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %25
br label %40
33: ; preds = %25
%34 = load i8*, i8** %5
%35 = load i64, i64* %6
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 0, i8* %36
%37 = load i32*, i32** %4
%38 = call i32 @fclose(i32* %37)
%39 = load i8*, i8** %5
store i8* %39, i8** %2
br label %53
40: ; preds = %32, %24, %17, %11
%41 = load i32*, i32** %4
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32*, i32** %4
%45 = call i32 @fclose(i32* %44)
br label %46
46: ; preds = %43, %40
%47 = load i8*, i8** %5
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i8*, i8** %5
%51 = call i32 @free(i8* %50)
br label %52
52: ; preds = %49, %46
store i8* null, i8** %2
br label %53
53: ; preds = %52, %33
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @read_file_to_memory(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = tail call i64 @find_file_size(i32* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = add nuw nsw i64 %5, 1
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = tail call i64 @fread(i8* %9, i32 1, i64 %5, i32* %2)
%13 = icmp eq i64 %12, %5
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %9, i64 %5
store i8 0, i8* %15
%16 = tail call i32 @fclose(i32* %2)
br label %22
17: ; preds = %7, %4
%18 = tail call i32 @fclose(i32* %2)
br label %22
19: ; preds = %11
%20 = tail call i32 @fclose(i32* %2)
%21 = tail call i32 @free(i8* %9)
br label %22
22: ; preds = %1, %17, %19, %14
%23 = phi i8* [ %9, %14 ], [ null, %19 ], [ null, %17 ], [ null, %1 ]
ret i8* %23
}