#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*/ dbgprintf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *realloc(char *, size_t);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *load_file(const char *filename) {
  FILE *fp;
  size_t n;
  enum { chunksize = 16 };
  char chunk[chunksize];
  char *buf = NULL;
  size_t bufsize = 0;
  char *p = NULL;
  if ((fp = fopen(filename, "r")) == NULL)
    return NULL;
  while ((n = fread(chunk, 1, chunksize, fp)) > 0) {
    if ((p = realloc(buf, bufsize + n + 1)) == NULL) {
      fclose(fp);
      dbgprintf("realloc: %s\n", strerror(errno));
      return buf;
    }
    buf = p;
    memcpy(buf + bufsize, chunk, n);
    bufsize += n;
    buf[bufsize] = '\0';
  }
  fclose(fp);
  return buf;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11bf <load_file+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    1289 <load_file+0x119>
jmp    11c4 <load_file+0x54>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jbe    1278 <load_file+0x108>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
add    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    123e <load_file+0xce>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xdd6(%rip),%rdi
call   1060 <dbgprintf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1289 <load_file+0x119>
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11c4 <load_file+0x54>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe7b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     121c <load_file+0xac>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1221 <load_file+0xb1>
mov    %rax,%r13
xor    %ebp,%ebp
mov    %rsp,%r14
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
lea    0x1(,%rbp,1),%rsi
add    %r13,%rsi
mov    %r12,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     1230 <load_file+0xc0>
mov    %rax,%rbx
mov    %r13,%r12
add    %rbp,%r12
mov    %rax,%rdi
add    %rbp,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%rbx,%r12,1)
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %rax,%r13
mov    %r12,%rbp
mov    %rbx,%r12
test   %rax,%rax
jne    11c0 <load_file+0x50>
jmp    1223 <load_file+0xb3>
xor    %r12d,%r12d
jmp    1254 <load_file+0xe4>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%r12
jmp    1254 <load_file+0xe4>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdb5(%rip),%rdi
mov    %eax,%esi
call   1060 <dbgprintf@plt>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [13 x i8] c"realloc: %s\0A\00"
@errno = global i32 0
define i8* @load_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca [16 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %7
store i64 0, i64* %8
store i8* null, i8** %9
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %53
14: ; preds = %1
br label %15
15: ; preds = %35, %14
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call i64 @fread(i8* %16, i32 1, i32 16, i32* %17)
store i64 %18, i64* %5
%19 = icmp ugt i64 %18, 0
br i1 %19, label %20, label %49
20: ; preds = %15
%21 = load i8*, i8** %7
%22 = load i64, i64* %8
%23 = load i64, i64* %5
%24 = add i64 %22, %23
%25 = add i64 %24, 1
%26 = call i8* @realloc(i8* %21, i64 %25)
store i8* %26, i8** %9
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %35
28: ; preds = %20
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @dbgprintf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i8*, i8** %7
store i8* %34, i8** %2
br label %53
35: ; preds = %20
%36 = load i8*, i8** %9
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = load i64, i64* %8
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%41 = load i64, i64* %5
%42 = call i32 @memcpy(i8* %39, i8* %40, i64 %41)
%43 = load i64, i64* %5
%44 = load i64, i64* %8
%45 = add i64 %44, %43
store i64 %45, i64* %8
%46 = load i8*, i8** %7
%47 = load i64, i64* %8
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 0, i8* %48
br label %15
49: ; preds = %15
%50 = load i32*, i32** %4
%51 = call i32 @fclose(i32* %50)
%52 = load i8*, i8** %7
store i8* %52, i8** %2
br label %53
53: ; preds = %49, %28, %13
%54 = load i8*, i8** %2
ret i8* %54
}

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