#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 fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
size_t load_source(const char *filename, char **source) {
  FILE *file;
  size_t length;
  file = fopen(filename, "rb");
  if (file == 0)
    return -1;
  fseek(file, 0, SEEK_END);
  length = ftell(file);
  fseek(file, 0, SEEK_SET);
  (*source) = (char *)malloc(length + 1);
  if (fread((*source), length, 1, file) != 1) {
    fclose(file);
    free((*source));
    return -1;
  }
  (*source)[length] = '\0';
  fclose(file);
  return length;
}


x86 assembly (clang, -O0):
<load_source>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <load_source+0x3c>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    125c <load_source+0xec>
mov    -0x20(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     123c <load_source+0xcc>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    125c <load_source+0xec>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_source>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
lea    0xe7e(%rip),%rsi
call   1090 <fopen@plt>
mov    $0xffffffffffffffff,%r14
test   %rax,%rax
je     1211 <load_source+0xa1>
mov    %rax,%rbx
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x1(%r12),%rdi
call   1070 <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    1201 <load_source+0x91>
mov    (%r15),%rax
movb   $0x0,(%rax,%r12,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12,%r14
jmp    1211 <load_source+0xa1>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
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 i64 @load_source(i8* %0, i8** %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i64 -1, i64* %3
br label %47
13: ; preds = %2
%14 = load i32*, i32** %6
%15 = load i32, i32* @SEEK_END
%16 = call i32 @fseek(i32* %14, i32 0, i32 %15)
%17 = load i32*, i32** %6
%18 = call i64 @ftell(i32* %17)
store i64 %18, i64* %7
%19 = load i32*, i32** %6
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i64, i64* %7
%23 = add i64 %22, 1
%24 = call i64 @malloc(i64 %23)
%25 = inttoptr i64 %24 to i8*
%26 = load i8**, i8*** %5
store i8* %25, i8** %26
%27 = load i8**, i8*** %5
%28 = load i8*, i8** %27
%29 = load i64, i64* %7
%30 = load i32*, i32** %6
%31 = call i32 @fread(i8* %28, i64 %29, i32 1, i32* %30)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %39
33: ; preds = %13
%34 = load i32*, i32** %6
%35 = call i32 @fclose(i32* %34)
%36 = load i8**, i8*** %5
%37 = load i8*, i8** %36
%38 = call i32 @free(i8* %37)
store i64 -1, i64* %3
br label %47
39: ; preds = %13
%40 = load i8**, i8*** %5
%41 = load i8*, i8** %40
%42 = load i64, i64* %7
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8 0, i8* %43
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = load i64, i64* %7
store i64 %46, i64* %3
br label %47
47: ; preds = %39, %33, %12
%48 = load i64, i64* %3
ret i64 %48
}

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