#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;
char *calloc(size_t, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *_read_file(const char *filename, size_t *contents_len) {
  FILE *f = fopen(filename, "r");
  char *contents = NULL;
  size_t read;
  if (!f)
    goto error;
  if (fseek(f, 0, SEEK_END))
    goto error;
  *contents_len = ftell(f);
  if (fseek(f, 0, SEEK_SET))
    goto error;
  contents = calloc(*contents_len + 1, sizeof(char));
  if (!contents)
    goto error;
  read = fread(contents, *contents_len, 1, f);
  if (read < 1)
    goto error;
  fclose(f);
  contents[*contents_len] = 0;
  *contents_len += 1;
  return contents;
error:
  if (contents)
    free(contents);
  if (f)
    fclose(f);
  return NULL;
}


x86 assembly (clang, -O0):
<_read_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)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <_read_file+0x3c>
jmp    128e <_read_file+0x11e>
mov    -0x20(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     11cf <_read_file+0x5f>
jmp    128e <_read_file+0x11e>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     1205 <_read_file+0x95>
jmp    128e <_read_file+0x11e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122e <_read_file+0xbe>
jmp    128e <_read_file+0x11e>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
jae    125b <_read_file+0xeb>
jmp    128e <_read_file+0x11e>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12be <_read_file+0x14e>
cmpq   $0x0,-0x28(%rbp)
je     12a2 <_read_file+0x132>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x20(%rbp)
je     12b6 <_read_file+0x146>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe81(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11cf <_read_file+0x5f>
mov    %rax,%rbx
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    11c7 <_read_file+0x57>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,(%r14)
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     11db <_read_file+0x6b>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%r14),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     11c7 <_read_file+0x57>
mov    %rax,%r15
mov    (%r14),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1222 <_read_file+0xb2>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    (%r14),%rax
movb   $0x0,(%r15,%rax,1)
addq   $0x1,(%r14)
jmp    11d2 <_read_file+0x62>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11c7 <_read_file+0x57>

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

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