#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *realloc(char *, size_t);
char *read_file(char *fname) {
  FILE *fp;
  char *buf = NULL, *buf_tmp;
  size_t size, ret;
  fp = fopen(fname, "r");
  if (!fp)
    return NULL;
  size = 0;
  while (!feof(fp)) {
    buf_tmp = realloc(buf, size + 4097);
    if (!buf_tmp) {
      free(buf);
      fclose(fp);
      return NULL;
    }
    buf = buf_tmp;
    ret = fread(buf + size, 1, 4096, fp);
    size += ret;
  }
  fclose(fp);
  if (buf)
    buf[size] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <read_file+0x40>
movq   $0x0,-0x8(%rbp)
jmp    1268 <read_file+0x108>
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11c6 <read_file+0x66>
jmp    1240 <read_file+0xe0>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x1001,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1208 <read_file+0xa8>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1268 <read_file+0x108>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11a8 <read_file+0x48>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
je     1260 <read_file+0x100>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%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   %r12
push   %rbx
push   %rax
lea    0xe91(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f0 <read_file+0x90>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11e8 <read_file+0x88>
xor    %ebx,%ebx
xor    %r12d,%r12d
nopl   (%rax)
lea    0x1001(%rbx),%rsi
mov    %r12,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     11e0 <read_file+0x80>
mov    %rax,%r15
mov    %rax,%rdi
add    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
add    %rax,%rbx
mov    %r14,%rdi
call   1060 <feof@plt>
mov    %r15,%r12
test   %eax,%eax
je     1190 <read_file+0x30>
mov    %r14,%rdi
call   1050 <fclose@plt>
movb   $0x0,(%r15,%rbx,1)
jmp    11f3 <read_file+0x93>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %5
%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
%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 %53
14: ; preds = %1
store i64 0, i64* %7
br label %15
15: ; preds = %32, %14
%16 = load i32*, i32** %4
%17 = call i32 @feof(i32* %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %42
20: ; preds = %15
%21 = load i8*, i8** %5
%22 = load i64, i64* %7
%23 = add i64 %22, 4097
%24 = call i8* @realloc(i8* %21, i64 %23)
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %32, label %27
27: ; preds = %20
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
store i8* null, i8** %2
br label %53
32: ; preds = %20
%33 = load i8*, i8** %6
store i8* %33, i8** %5
%34 = load i8*, i8** %5
%35 = load i64, i64* %7
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = load i32*, i32** %4
%38 = call i64 @fread(i8* %36, i32 1, i32 4096, i32* %37)
store i64 %38, i64* %8
%39 = load i64, i64* %8
%40 = load i64, i64* %7
%41 = add i64 %40, %39
store i64 %41, i64* %7
br label %15
42: ; preds = %15
%43 = load i32*, i32** %4
%44 = call i32 @fclose(i32* %43)
%45 = load i8*, i8** %5
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i8*, i8** %5
%49 = load i64, i64* %7
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 0, i8* %50
br label %51
51: ; preds = %47, %42
%52 = load i8*, i8** %5
store i8* %52, i8** %2
br label %53
53: ; preds = %51, %27, %13
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
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 %27, label %4
4: ; preds = %1
%5 = tail call i32 @feof(i32* %2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = tail call i32 @fclose(i32* %2)
br label %27
9: ; preds = %4, %18
%10 = phi i64 [ %21, %18 ], [ 0, %4 ]
%11 = phi i8* [ %13, %18 ], [ null, %4 ]
%12 = add i64 %10, 4097
%13 = tail call i8* @realloc(i8* %11, i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @free(i8* %11)
%17 = tail call i32 @fclose(i32* %2)
br label %27
18: ; preds = %9
%19 = getelementptr inbounds i8, i8* %13, i64 %10
%20 = tail call i64 @fread(i8* %19, i32 1, i32 4096, i32* %2)
%21 = add i64 %20, %10
%22 = tail call i32 @feof(i32* %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %9, label %24
24: ; preds = %18
%25 = tail call i32 @fclose(i32* %2)
%26 = getelementptr inbounds i8, i8* %13, i64 %21
store i8 0, i8* %26
br label %27
27: ; preds = %7, %24, %1, %15
%28 = phi i8* [ null, %15 ], [ null, %1 ], [ %13, %24 ], [ null, %7 ]
ret i8* %28
}