#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*/ clu_Exit(char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *clu_LoadProgSource(const char *source_file, size_t *source_len_ret) {
  FILE *fp_source = fopen(source_file, "rb");
  if (fp_source == NULL) {
    clu_Exit("ERROR: Failed to open %s\n", source_file);
    return NULL;
  }
  fseek(fp_source, 0, SEEK_END);
  size_t length = (size_t)ftell(fp_source);
  *source_len_ret = length;
  rewind(fp_source);
  char *source_code = (char *)malloc(length + 1);
  if (fread(source_code, length, 1, fp_source) != 1) {
    fclose(fp_source);
    free(source_code);
    clu_Exit("ERROR: Failed to read %s\n", source_file);
    return NULL;
  }
  source_code[length] = '\0';
  fclose(fp_source);
  return source_code;
}


x86 assembly (clang, -O0):
<clu_LoadProgSource>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dc <clu_LoadProgSource+0x4c>
mov    -0x10(%rbp),%rsi
lea    0xe39(%rip),%rdi
call   1070 <clu_Exit@plt>
movq   $0x0,-0x8(%rbp)
jmp    128f <clu_LoadProgSource+0xff>
mov    -0x20(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1272 <clu_LoadProgSource+0xe2>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rsi
lea    0xdbd(%rip),%rdi
call   1070 <clu_Exit@plt>
movq   $0x0,-0x8(%rbp)
jmp    128f <clu_LoadProgSource+0xff>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clu_LoadProgSource>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe5b(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     120f <clu_LoadProgSource+0x7f>
mov    %rax,%rbx
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
movslq %eax,%r12
mov    %r12,(%r15)
mov    %rbx,%rdi
call   1060 <rewind@plt>
lea    0x1(%r12),%rdi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1218 <clu_LoadProgSource+0x88>
movb   $0x0,(%r15,%r12,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    123a <clu_LoadProgSource+0xaa>
lea    0xded(%rip),%rdi
jmp    122f <clu_LoadProgSource+0x9f>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xdee(%rip),%rdi
mov    %r14,%rsi
call   1070 <clu_Exit@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"ERROR: Failed to open %s\0A\00"
@SEEK_END = global i32 0
@.str.2 = [26 x i8] c"ERROR: Failed to read %s\0A\00"
define i8* @clu_LoadProgSource(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %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** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @clu_Exit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i8* null, i8** %3
br label %50
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %6
%21 = call i32 @ftell(i32* %20)
%22 = sext i32 %21 to i64
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = load i64*, i64** %5
store i64 %23, i64* %24
%25 = load i32*, i32** %6
%26 = call i32 @rewind(i32* %25)
%27 = load i64, i64* %7
%28 = add i64 %27, 1
%29 = call i64 @malloc(i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = load i64, i64* %7
%33 = load i32*, i32** %6
%34 = call i32 @fread(i8* %31, i64 %32, i32 1, i32* %33)
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %43
36: ; preds = %16
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
%39 = load i8*, i8** %8
%40 = call i32 @free(i8* %39)
%41 = load i8*, i8** %4
%42 = call i32 @clu_Exit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %41)
store i8* null, i8** %3
br label %50
43: ; preds = %16
%44 = load i8*, i8** %8
%45 = load i64, i64* %7
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8 0, i8* %46
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %8
store i8* %49, i8** %3
br label %50
50: ; preds = %43, %36, %13
%51 = load i8*, i8** %3
ret i8* %51
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"ERROR: Failed to open %s\0A\00"
@SEEK_END = global i32 0
@.str.2 = [26 x i8] c"ERROR: Failed to read %s\0A\00"
define i8* @clu_LoadProgSource(i8* %0, i64* 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 %5, label %7
5: ; preds = %2
%6 = tail call i32 @clu_Exit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i32 @ftell(i32* %3)
%11 = sext i32 %10 to i64
store i64 %11, i64* %1
%12 = tail call i32 @rewind(i32* %3)
%13 = add nsw i64 %11, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @fread(i8* %15, i64 %11, i32 1, i32* %3)
%17 = icmp eq i32 %16, 1
br i1 %17, label %22, label %18
18: ; preds = %7
%19 = tail call i32 @fclose(i32* %3)
%20 = tail call i32 @free(i8* %15)
%21 = tail call i32 @clu_Exit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %25
22: ; preds = %7
%23 = getelementptr inbounds i8, i8* %15, i64 %11
store i8 0, i8* %23
%24 = tail call i32 @fclose(i32* %3)
br label %25
25: ; preds = %18, %22, %5
%26 = phi i8* [ null, %5 ], [ null, %18 ], [ %15, %22 ]
ret i8* %26
}