#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 *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ nez_PrintErrorInfo(char *);
char *loadFile(const char *filename, size_t *length) {
  size_t len = 0;
  FILE *fp = fopen(filename, "rb");
  char *source;
  if (!fp) {
    nez_PrintErrorInfo("fopen error: cannot open file");
    return NULL;
  }
  fseek(fp, 0, SEEK_END);
  len = (size_t)ftell(fp);
  fseek(fp, 0, SEEK_SET);
  source = (char *)malloc(len + 1);
  if (len != fread(source, 1, len, fp)) {
    nez_PrintErrorInfo("fread error: cannot read file collectly");
  }
  source[len] = '\0';
  fclose(fp);
  *length = len;
  return source;
}


x86 assembly (clang, -O0):
<loadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c0 <loadFile+0x50>
lea    0xe55(%rip),%rdi
call   1080 <nez_PrintErrorInfo@plt>
movq   $0x0,-0x8(%rbp)
jmp    1268 <loadFile+0xf8>
mov    -0x28(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
je     1240 <loadFile+0xd0>
lea    0xde6(%rip),%rdi
call   1080 <nez_PrintErrorInfo@plt>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe7e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1202 <loadFile+0x92>
mov    %rax,%rbx
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r15
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r15),%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %rax,%r15
je     11f0 <loadFile+0x80>
lea    0xe36(%rip),%rdi
call   1080 <nez_PrintErrorInfo@plt>
movb   $0x0,(%r12,%r15,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,(%r14)
jmp    1211 <loadFile+0xa1>
lea    0xdfa(%rip),%rdi
call   1080 <nez_PrintErrorInfo@plt>
xor    %r12d,%r12d
mov    %r12,%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 = [30 x i8] c"fopen error: cannot open file\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [40 x i8] c"fread error: cannot read file collectly\00"
define i8* @loadFile(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
store i64 0, i64* %6
%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** %7
%11 = load i32*, i32** %7
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 @nez_PrintErrorInfo(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %45
15: ; preds = %2
%16 = load i32*, i32** %7
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %7
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %6
%21 = load i32*, i32** %7
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i64, i64* %6
%25 = add i64 %24, 1
%26 = call i64 @malloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %8
%28 = load i64, i64* %6
%29 = load i8*, i8** %8
%30 = load i64, i64* %6
%31 = load i32*, i32** %7
%32 = call i64 @fread(i8* %29, i32 1, i64 %30, i32* %31)
%33 = icmp ne i64 %28, %32
br i1 %33, label %34, label %36
34: ; preds = %15
%35 = call i32 @nez_PrintErrorInfo(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %15
%37 = load i8*, i8** %8
%38 = load i64, i64* %6
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 0, i8* %39
%40 = load i32*, i32** %7
%41 = call i32 @fclose(i32* %40)
%42 = load i64, i64* %6
%43 = load i64*, i64** %5
store i64 %42, i64* %43
%44 = load i8*, i8** %8
store i8* %44, i8** %3
br label %45
45: ; preds = %36, %13
%46 = load i8*, i8** %3
ret i8* %46
}

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