#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
char *load_file(const char *filename) {
  char *b = NULL;
  FILE *f = fopen(filename, "r");
  long s;
  size_t real_len;
  if (f != NULL) {
    if (fseek(f, 0L, SEEK_END) == 0) {
      s = ftell(f);
      if (s == -1) {
        fprintf(stderr, "Error, not able to calculate the size of the file \n");
        exit(EXIT_FAILURE);
      }
      b = (char *)malloc(sizeof(char) * s + 1);
      if (fseek(f, 0L, SEEK_SET) != 0) {
        fprintf(stderr, "Error, not got back to the beginning of the file\n");
        exit(EXIT_FAILURE);
      }
      real_len = fread(b, sizeof(char), s, f);
      if (real_len == 0) {
        fprintf(stderr, "Error, reading file %s\n", filename);
        exit(EXIT_FAILURE);
      }
      b[real_len] = '\0';
    }
    fclose(f);
  }
  return b;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12cf <load_file+0x14f>
mov    -0x18(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
jne    12c6 <load_file+0x146>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1210 <load_file+0x90>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     126c <load_file+0xec>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12ba <load_file+0x13a>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe6e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1215 <load_file+0x95>
mov    %rax,%rbx
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    120b <load_file+0x8b>
mov    %rbx,%rdi
call   1060 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1227 <load_file+0xa7>
mov    %rax,%r12
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    1239 <load_file+0xb9>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1252 <load_file+0xd2>
movb   $0x0,(%r15,%rax,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1218 <load_file+0x98>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
jmp    1249 <load_file+0xc9>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    126c <load_file+0xec>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@stderr = global i32 0
@.str.1 = [52 x i8] c"Error, not able to calculate the size of the file \0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [50 x i8] c"Error, not got back to the beginning of the file\0A\00"
@.str.3 = [24 x i8] c"Error, reading file %s\0A\00"
define i8* @load_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %2
store i8* null, i8** %3
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %62
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = load i32, i32* @SEEK_END
%14 = call i64 @fseek(i32* %12, i64 0, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %59
16: ; preds = %11
%17 = load i32*, i32** %4
%18 = call i64 @ftell(i32* %17)
store i64 %18, i64* %5
%19 = load i64, i64* %5
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %16
%27 = load i64, i64* %5
%28 = mul i64 1, %27
%29 = add i64 %28, 1
%30 = trunc i64 %29 to i32
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %3
%33 = load i32*, i32** %4
%34 = load i32, i32* @SEEK_SET
%35 = call i64 @fseek(i32* %33, i64 0, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %26
%43 = load i8*, i8** %3
%44 = load i64, i64* %5
%45 = load i32*, i32** %4
%46 = call i64 @fread(i8* %43, i32 1, i64 %44, i32* %45)
store i64 %46, i64* %6
%47 = load i64, i64* %6
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %42
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %2
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %42
%56 = load i8*, i8** %3
%57 = load i64, i64* %6
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8 0, i8* %58
br label %59
59: ; preds = %55, %11
%60 = load i32*, i32** %4
%61 = call i32 @fclose(i32* %60)
br label %62
62: ; preds = %59, %1
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@stderr = global i32 0
@.str.1 = [52 x i8] c"Error, not able to calculate the size of the file \0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [50 x i8] c"Error, not got back to the beginning of the file\0A\00"
@.str.3 = [24 x i8] c"Error, reading file %s\0A\00"
define i8* @load_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %42, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %2, i64 0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %39
8: ; preds = %4
%9 = tail call i64 @ftell(i32* %2)
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %8
%17 = trunc i64 %9 to i32
%18 = add i32 %17, 1
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = load i32, i32* @SEEK_SET
%22 = tail call i64 @fseek(i32* %2, i64 0, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %16
%30 = tail call i64 @fread(i8* %20, i32 1, i64 %9, i32* %2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %29
%38 = getelementptr inbounds i8, i8* %20, i64 %30
store i8 0, i8* %38
br label %39
39: ; preds = %37, %4
%40 = phi i8* [ %20, %37 ], [ null, %4 ]
%41 = tail call i32 @fclose(i32* %2)
br label %42
42: ; preds = %39, %1
%43 = phi i8* [ %40, %39 ], [ null, %1 ]
ret i8* %43
}