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


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c0 <read_file+0x50>
jmp    128e <read_file+0x11e>
mov    -0x10(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     11e3 <read_file+0x73>
jmp    128e <read_file+0x11e>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1200 <read_file+0x90>
jmp    128e <read_file+0x11e>
mov    -0x10(%rbp),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     1223 <read_file+0xb3>
jmp    128e <read_file+0x11e>
mov    -0x18(%rbp),%rdi
add    $0x4,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1244 <read_file+0xd4>
jmp    128e <read_file+0x11e>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1282 <read_file+0x112>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
jmp    128e <read_file+0x11e>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x10(%rbp)
je     12a2 <read_file+0x132>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11ff <read_file+0x8f>
mov    %rax,%rbx
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    1207 <read_file+0x97>
mov    %rbx,%rdi
call   1060 <ftell@plt>
test   %rax,%rax
js     1204 <read_file+0x94>
mov    %rax,%r15
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    1207 <read_file+0x97>
lea    0x4(%r15),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1204 <read_file+0x94>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
jne    1218 <read_file+0xa8>
movb   $0x0,(%r14,%r15,1)
jmp    1207 <read_file+0x97>
xor    %r14d,%r14d
jmp    120f <read_file+0x9f>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    1207 <read_file+0x97>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %2
store i32* null, i32** %3
store i64 0, i64* %4
store i8* null, i8** %5
store i64 0, i64* %6
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
br label %53
12: ; preds = %1
%13 = load i32*, i32** %3
%14 = load i32, i32* @SEEK_END
%15 = call i64 @fseek(i32* %13, i32 0, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
br label %53
18: ; preds = %12
%19 = load i32*, i32** %3
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %4
%21 = load i64, i64* %4
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %53
24: ; preds = %18
%25 = load i32*, i32** %3
%26 = load i32, i32* @SEEK_SET
%27 = call i64 @fseek(i32* %25, i32 0, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %53
30: ; preds = %24
%31 = load i64, i64* %4
%32 = add i64 %31, 4
%33 = call i64 @malloc(i64 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %5
%35 = load i8*, i8** %5
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %38
37: ; preds = %30
br label %53
38: ; preds = %30
%39 = load i8*, i8** %5
%40 = load i64, i64* %4
%41 = load i32*, i32** %3
%42 = call i64 @fread(i8* %39, i32 1, i64 %40, i32* %41)
store i64 %42, i64* %6
%43 = load i64, i64* %6
%44 = load i64, i64* %4
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
store i8* null, i8** %5
br label %53
49: ; preds = %38
%50 = load i8*, i8** %5
%51 = load i64, i64* %6
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8 0, i8* %52
br label %53
53: ; preds = %49, %46, %37, %29, %23, %17, %11
%54 = load i32*, i32** %3
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32*, i32** %3
%58 = call i32 @fclose(i32* %57)
br label %59
59: ; preds = %56, %53
%60 = load i8*, i8** %5
ret i8* %60
}

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