#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ parson_free(char *);
scalar_t__ parson_malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *read_file(const char *filename) {
  FILE *fp = fopen(filename, "r");
  size_t size_to_read = 0;
  size_t size_read = 0;
  long pos;
  char *file_contents;
  if (!fp) {
    return NULL;
  }
  fseek(fp, 0L, SEEK_END);
  pos = ftell(fp);
  if (pos < 0) {
    fclose(fp);
    return NULL;
  }
  size_to_read = pos;
  rewind(fp);
  file_contents = (char *)parson_malloc(sizeof(char) * (size_to_read + 1));
  if (!file_contents) {
    fclose(fp);
    return NULL;
  }
  size_read = fread(file_contents, 1, size_to_read, fp);
  if (size_read == 0 || ferror(fp)) {
    fclose(fp);
    parson_free(file_contents);
    return NULL;
  }
  fclose(fp);
  file_contents[size_read] = '\0';
  return file_contents;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d8 <read_file+0x48>
movq   $0x0,-0x8(%rbp)
jmp    12d9 <read_file+0x149>
mov    -0x18(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    121c <read_file+0x8c>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d9 <read_file+0x149>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <parson_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1265 <read_file+0xd5>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d9 <read_file+0x149>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     129d <read_file+0x10d>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12bc <read_file+0x12c>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   10a0 <parson_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d9 <read_file+0x149>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe64(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     122b <read_file+0x9b>
mov    %rax,%rbx
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
test   %rax,%rax
js     1223 <read_file+0x93>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <rewind@plt>
lea    0x1(%r15),%edi
call   1060 <parson_malloc@plt>
test   %rax,%rax
je     1223 <read_file+0x93>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1211 <read_file+0x81>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1237 <read_file+0xa7>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   10a0 <parson_free@plt>
jmp    122b <read_file+0x9b>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1050 <fclose@plt>
movb   $0x0,(%r14,%r15,1)
jmp    122e <read_file+0x9e>

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

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