#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_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*/ stderr;
char *load_file(char *filename) {
  FILE *f = fopen(filename, "rb");
  if (!f) {
    fprintf(stderr, "[core\tfail] Error opening file %s", filename);
    return NULL;
  }
  fseek(f, 0, SEEK_END);
  long fsize = ftell(f);
  fseek(f, 0, SEEK_SET);
  char *str = malloc(fsize + 1);
  size_t read_bytes = fread(str, 1, fsize, f);
  if (read_bytes != fsize) {
    fprintf(stderr, "[core\tfail] failed to read all bytes from file %s",
            filename);
    fprintf(stderr, "             read %lu bytes out of %ld", read_bytes,
            fsize);
    return NULL;
  }
  fclose(f);
  str[fsize] = 0;
  return str;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c3 <load_file+0x53>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1295 <load_file+0x125>
mov    -0x18(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1278 <load_file+0x108>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1295 <load_file+0x125>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
lea    0xe7d(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11f4 <load_file+0x84>
mov    %rax,%rbx
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x1(%r15),%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r15,%rax
jne    1213 <load_file+0xa3>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movb   $0x0,(%r12,%r15,1)
jmp    1249 <load_file+0xd9>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
xor    %r12d,%r12d
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1249 <load_file+0xd9>
mov    %rax,%r13
mov    0x2dbb(%rip),%rbx
mov    (%rbx),%edi
lea    0xdff(%rip),%rsi
xor    %r12d,%r12d
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe1b(%rip),%rsi
mov    %r13,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"[core\09fail] Error opening file %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [50 x i8] c"[core\09fail] failed to read all bytes from file %s\00"
@.str.3 = [39 x i8] c" read %lu bytes out of %ld\00"
define i8* @load_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i8* null, i8** %2
br label %50
16: ; preds = %1
%17 = load i32*, i32** %4
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %4
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %5
%22 = load i32*, i32** %4
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i64, i64* %5
%26 = add nsw i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = load i64, i64* %5
%30 = load i32*, i32** %4
%31 = call i64 @fread(i8* %28, i32 1, i64 %29, i32* %30)
store i64 %31, i64* %7
%32 = load i64, i64* %7
%33 = load i64, i64* %5
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %43
35: ; preds = %16
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %3
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @stderr
%40 = load i64, i64* %7
%41 = load i64, i64* %5
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i64 %40, i64 %41)
store i8* null, i8** %2
br label %50
43: ; preds = %16
%44 = load i32*, i32** %4
%45 = call i32 @fclose(i32* %44)
%46 = load i8*, i8** %6
%47 = load i64, i64* %5
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %6
store i8* %49, i8** %2
br label %50
50: ; preds = %43, %35, %12
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"[core\09fail] Error opening file %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [50 x i8] c"[core\09fail] failed to read all bytes from file %s\00"
@.str.3 = [39 x i8] c" read %lu bytes out of %ld\00"
define noalias i8* @load_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
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 i8* @malloc(i64 %13)
%15 = tail call i64 @fread(i8* %14, i32 1, i64 %10, i32* %2)
%16 = icmp eq i64 %15, %10
br i1 %16, label %22, label %17
17: ; preds = %7
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i64 %15, i64 %10)
br label %25
22: ; preds = %7
%23 = tail call i32 @fclose(i32* %2)
%24 = getelementptr inbounds i8, i8* %14, i64 %10
store i8 0, i8* %24
br label %25
25: ; preds = %17, %22, %4
%26 = phi i8* [ null, %4 ], [ null, %17 ], [ %14, %22 ]
ret i8* %26
}