#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 *);
int fread(char *, size_t, int, 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 file_size;
  long pos;
  char *file_contents;
  if (!fp)
    return NULL;
  fseek(fp, 0L, SEEK_END);
  pos = ftell(fp);
  if (pos < 0) {
    fclose(fp);
    return NULL;
  }
  file_size = pos;
  rewind(fp);
  file_contents = (char *)parson_malloc(sizeof(char) * (file_size + 1));
  if (!file_contents) {
    fclose(fp);
    return NULL;
  }
  if (fread(file_contents, file_size, 1, fp) < 1) {
    if (ferror(fp)) {
      fclose(fp);
      parson_free(file_contents);
      return NULL;
    }
  }
  fclose(fp);
  file_contents[file_size] = '\0';
  return file_contents;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <read_file+0x38>
movq   $0x0,-0x8(%rbp)
jmp    12c8 <read_file+0x138>
mov    -0x18(%rbp),%rdi
mov    0x2e15(%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,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    120c <read_file+0x7c>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c8 <read_file+0x138>
mov    -0x28(%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,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1255 <read_file+0xc5>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c8 <read_file+0x138>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
jge    12ab <read_file+0x11b>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12a6 <read_file+0x116>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <parson_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c8 <read_file+0x138>
jmp    12ab <read_file+0x11b>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%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     1213 <read_file+0x83>
mov    %rax,%rbx
mov    0x2e38(%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     120b <read_file+0x7b>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <rewind@plt>
lea    0x1(%r15),%edi
call   1060 <parson_malloc@plt>
test   %rax,%rax
je     120b <read_file+0x7b>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    121f <read_file+0x8f>
mov    %rbx,%rdi
call   1050 <fclose@plt>
movb   $0x0,(%r14,%r15,1)
jmp    1216 <read_file+0x86>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11fc <read_file+0x6c>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   10a0 <parson_free@plt>
jmp    1213 <read_file+0x83>

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

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 i32 @fread(i8* %16, i64 %7, i32 1, i32* %2)
%22 = icmp slt i32 %21, 1
br i1 %22, label %23, label %29
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
%27 = tail call i32 @fclose(i32* %2)
%28 = tail call i32 @parson_free(i8* %16)
br label %32
29: ; preds = %23, %20
%30 = tail call i32 @fclose(i32* %2)
%31 = getelementptr inbounds i8, i8* %16, i64 %7
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
}