#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 *, char *);
scalar_t__ 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 *);
int load_file(char *filename, char **file) {
  FILE *fd = fopen(filename, "rb");
  if (!fd) {
    printf("Error: Cannot open %s for reading\n", filename);
    return -1;
  }
  fseek(fd, 0, SEEK_END);
  long fsize = ftell(fd);
  fseek(fd, 0, SEEK_SET);
  *file = malloc(fsize + 1);
  if (0 == fread(*file, 1, fsize, fd)) {
    printf("Error: Read failed on file: %s\n", filename);
    return -1;
  }
  fclose(fd);
  (*file)[fsize] = '\0';
  return fsize;
}


x86 assembly (clang, -O0):
<load_file>:
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    11bb <load_file+0x4b>
mov    -0x10(%rbp),%rsi
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <load_file+0xf9>
mov    -0x20(%rbp),%rdi
mov    0x2e22(%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    0x2df1(%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),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    124a <load_file+0xda>
mov    -0x10(%rbp),%rsi
lea    0xded(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <load_file+0xf9>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11fa <load_file+0x8a>
mov    %rax,%rbx
mov    0x2e4f(%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    0x2e21(%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,(%r14)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1203 <load_file+0x93>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    (%r14),%rax
movb   $0x0,(%rax,%r12,1)
jmp    1218 <load_file+0xa8>
lea    0xe02(%rip),%rdi
jmp    120a <load_file+0x9a>
lea    0xe1c(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [35 x i8] c"Error: Cannot open %s for reading\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [32 x i8] c"Error: Read failed on file: %s\0A\00"
define i32 @load_file(i8* %0, i8** %1) {
%3 = alloca i32
%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 ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i32 -1, i32* %3
br label %46
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %6
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %7
%21 = load i32*, i32** %6
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i64, i64* %7
%25 = add nsw i64 %24, 1
%26 = call i8* @malloc(i64 %25)
%27 = load i8**, i8*** %5
store i8* %26, i8** %27
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = load i64, i64* %7
%31 = load i32*, i32** %6
%32 = call i64 @fread(i8* %29, i32 1, i64 %30, i32* %31)
%33 = icmp eq i64 0, %32
br i1 %33, label %34, label %37
34: ; preds = %15
%35 = load i8*, i8** %4
%36 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %35)
store i32 -1, i32* %3
br label %46
37: ; preds = %15
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
%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 i64, i64* %7
%45 = trunc i64 %44 to i32
store i32 %45, i32* %3
br label %46
46: ; preds = %37, %34, %12
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [35 x i8] c"Error: Cannot open %s for reading\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [32 x i8] c"Error: Read failed on file: %s\0A\00"
define i32 @load_file(i8* %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 %5, label %7
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %24
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 nsw i64 %10, 1
%14 = tail call i8* @malloc(i64 %13)
store i8* %14, i8** %1
%15 = tail call i64 @fread(i8* %14, i32 1, i64 %10, i32* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %24
19: ; preds = %7
%20 = tail call i32 @fclose(i32* %3)
%21 = load i8*, i8** %1
%22 = getelementptr inbounds i8, i8* %21, i64 %10
store i8 0, i8* %22
%23 = trunc i64 %10 to i32
br label %24
24: ; preds = %17, %19, %5
%25 = phi i32 [ -1, %5 ], [ -1, %17 ], [ %23, %19 ]
ret i32 %25
}