#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ perror(char *);
char *read_source_file(const char *filename) {
  FILE *fp = fopen(filename, "r");
  if (fp == NULL) {
    perror("Failed to read file");
    exit(-1);
  }
  fseek(fp, 0, SEEK_END);
  long file_size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  char *code = (char *)malloc(file_size + 1);
  fread(code, file_size, 1, fp);
  fclose(fp);
  code[file_size] = 0;
  return code;
}


x86 assembly (clang, -O0):
<read_source_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c1 <read_source_file+0x41>
lea    0xe50(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_source_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ff <read_source_file+0x7f>
mov    %rax,%rbx
mov    0x2e48(%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    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r14),%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdfc(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"Failed to read file\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_source_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %1
%14 = load i32*, i32** %3
%15 = load i32, i32* @SEEK_END
%16 = call i32 @fseek(i32* %14, i32 0, i32 %15)
%17 = load i32*, i32** %3
%18 = call i64 @ftell(i32* %17)
store i64 %18, i64* %4
%19 = load i32*, i32** %3
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i64, i64* %4
%23 = add nsw i64 %22, 1
%24 = call i64 @malloc(i64 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = load i64, i64* %4
%28 = load i32*, i32** %3
%29 = call i32 @fread(i8* %26, i64 %27, i32 1, i32* %28)
%30 = load i32*, i32** %3
%31 = call i32 @fclose(i32* %30)
%32 = load i8*, i8** %5
%33 = load i64, i64* %4
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %5
ret i8* %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"Failed to read file\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_source_file(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 %4, label %7
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %2)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %2, i32 0, i32 %11)
%13 = add nsw i64 %10, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @fread(i8* %15, i64 %10, i32 1, i32* %2)
%17 = tail call i32 @fclose(i32* %2)
%18 = getelementptr inbounds i8, i8* %15, i64 %10
store i8 0, i8* %18
ret i8* %15
}