#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, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ printf(char *);
char *file_to_string(const char *filename) {
  char *buffer = 0;
  long length;
  FILE *f = fopen(filename, "rb");
  if (f) {
    fseek(f, 0, SEEK_END);
    length = ftell(f);
    fseek(f, 0, SEEK_SET);
    buffer = malloc(length);
    if (buffer) {
      fread(buffer, 1, length, f);
    }
    fclose(f);
  } else {
    printf("That file does not exist.\n");
  }
  if (buffer) {
    return buffer;
  } else {
    printf(
        "There is something wrong with your program (perhaps it's empty). \n");
    return "";
  }
}


x86 assembly (clang, -O0):
<file_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1214 <file_to_string+0xa4>
mov    -0x28(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1206 <file_to_string+0x96>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1220 <file_to_string+0xb0>
lea    0xde8(%rip),%rdi
call   1050 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
je     1238 <file_to_string+0xc8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124f <file_to_string+0xdf>
lea    0xddf(%rip),%rdi
call   1050 <printf@plt>
lea    0xe15(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_to_string>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11e7 <file_to_string+0x77>
mov    %rax,%rbx
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f5 <file_to_string+0x85>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1210 <file_to_string+0xa0>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
jmp    11fd <file_to_string+0x8d>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4e(%rip),%r14
mov    %r14,%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
@.str.1 = [27 x i8] c"That file does not exist.\0A\00"
@.str.2 = [67 x i8] c"There is something wrong with your program (perhaps it's empty). \0A\00"
@.str.3 = [1 x i8] zeroinitializer
define i8* @file_to_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
store i8* %0, i8** %3
store i8* null, i8** %4
%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 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %32
11: ; preds = %1
%12 = load i32*, i32** %6
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %12, i32 0, i32 %13)
%15 = load i32*, i32** %6
%16 = call i64 @ftell(i32* %15)
store i64 %16, i64* %5
%17 = load i32*, i32** %6
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i64, i64* %5
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %11
%25 = load i8*, i8** %4
%26 = load i64, i64* %5
%27 = load i32*, i32** %6
%28 = call i32 @fread(i8* %25, i32 1, i64 %26, i32* %27)
br label %29
29: ; preds = %24, %11
%30 = load i32*, i32** %6
%31 = call i32 @fclose(i32* %30)
br label %34
32: ; preds = %1
%33 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = load i8*, i8** %4
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = load i8*, i8** %4
store i8* %38, i8** %2
br label %41
39: ; preds = %34
%40 = call i32 @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0))
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %41
41: ; preds = %39, %37
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [1 x i8] zeroinitializer
@str = [26 x i8] c"That file does not exist.\00"
@str.4 = [66 x i8] c"There is something wrong with your program (perhaps it's empty). \00"
define i8* @file_to_string(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 %14, 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 i64 @ftell(i32* %2)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = tail call i8* @malloc(i64 %7)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %4
%13 = tail call i32 @fclose(i32* %2)
br label %19
14: ; preds = %1
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %19
16: ; preds = %4
%17 = tail call i32 @fread(i8* %10, i32 1, i64 %7, i32* %2)
%18 = tail call i32 @fclose(i32* %2)
br label %21
19: ; preds = %12, %14
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @str.4, i64 0, i64 0))
br label %21
21: ; preds = %16, %19
%22 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %19 ], [ %10, %16 ]
ret i8* %22
}