#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ stderr;
char *read_text_file(const char *filename) {
  char *buffer = NULL;
  size_t size = 0;
  size_t free = 0;
  FILE *fp = fopen(filename, "rt");
  while (fp && !feof(fp)) {
    if (free == 0) {
      char *new_buffer = (char *)realloc(buffer, size + 1024);
      if (new_buffer) {
        free += 1024;
        size += 1024;
        buffer = new_buffer;
      } else {
        fprintf(stderr, "read_text_file realloc fail\n");
        return buffer;
      }
    }
    if (free == 0)
      break;
    size_t bytes_read = fread(&buffer[size - 1024], 1, free, fp);
    free -= bytes_read;
  }
  if (buffer != NULL && size > 0) {
    buffer[size] = '\0';
  }
  return buffer;
}


x86 assembly (clang, -O0):
<read_text_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x41(%rbp)
je     11ac <read_text_file+0x5c>
mov    -0x30(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11bc <read_text_file+0x6c>
jmp    1285 <read_text_file+0x135>
cmpq   $0x0,-0x28(%rbp)
jne    123a <read_text_file+0xea>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x400,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1213 <read_text_file+0xc3>
mov    -0x28(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1235 <read_text_file+0xe5>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12af <read_text_file+0x15f>
jmp    123a <read_text_file+0xea>
cmpq   $0x0,-0x28(%rbp)
jne    124a <read_text_file+0xfa>
jmp    1285 <read_text_file+0x135>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
sub    $0x400,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1188 <read_text_file+0x38>
cmpq   $0x0,-0x18(%rbp)
je     12a7 <read_text_file+0x157>
cmpq   $0x0,-0x20(%rbp)
jbe    12a7 <read_text_file+0x157>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xea1(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1192 <read_text_file+0x42>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <feof@plt>
xor    %r14d,%r14d
mov    $0x0,%r12d
test   %eax,%eax
je     11a4 <read_text_file+0x54>
test   %r14,%r14
je     1195 <read_text_file+0x45>
test   %r12,%r12
je     1195 <read_text_file+0x45>
movb   $0x0,(%r14,%r12,1)
jmp    1195 <read_text_file+0x45>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %ebx,%ebx
xor    %r12d,%r12d
xor    %r14d,%r14d
jmp    11da <read_text_file+0x8a>
xchg   %ax,%ax
lea    (%r12,%r14,1),%rdi
add    $0xfffffffffffffc00,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
sub    %rax,%rbx
mov    %r15,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    1181 <read_text_file+0x31>
test   %rbx,%rbx
jne    11b0 <read_text_file+0x60>
add    $0x400,%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     1200 <read_text_file+0xb0>
mov    $0x400,%ebx
mov    %rax,%r14
jmp    11b0 <read_text_file+0x60>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1195 <read_text_file+0x45>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rt\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"read_text_file realloc fail\0A\00"
define i8* @read_text_file(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %39, label %4
4: ; preds = %1
%5 = tail call i32 @feof(i32* %2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %31
7: ; preds = %4, %21
%8 = phi i64 [ %28, %21 ], [ 0, %4 ]
%9 = phi i64 [ %23, %21 ], [ 0, %4 ]
%10 = phi i8* [ %24, %21 ], [ null, %4 ]
%11 = icmp eq i64 %8, 0
br i1 %11, label %12, label %21
12: ; preds = %7
%13 = add i64 %9, 1024
%14 = tail call i64 @realloc(i8* %10, i64 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %39
19: ; preds = %12
%20 = inttoptr i64 %14 to i8*
br label %21
21: ; preds = %7, %19
%22 = phi i64 [ 1024, %19 ], [ %8, %7 ]
%23 = phi i64 [ %13, %19 ], [ %9, %7 ]
%24 = phi i8* [ %20, %19 ], [ %10, %7 ]
%25 = add i64 %23, -1024
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = tail call i64 @fread(i8* %26, i32 1, i64 %22, i32* %2)
%28 = sub i64 %22, %27
%29 = tail call i32 @feof(i32* %2)
%30 = icmp eq i32 %29, 0
br i1 %30, label %7, label %31
31: ; preds = %21, %4
%32 = phi i8* [ null, %4 ], [ %24, %21 ]
%33 = phi i64 [ 0, %4 ], [ %23, %21 ]
%34 = icmp ne i8* %32, null
%35 = icmp ne i64 %33, 0
%36 = select i1 %34, i1 %35, i1 false
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 0, i8* %38
br label %39
39: ; preds = %1, %16, %31, %37
%40 = phi i8* [ %10, %16 ], [ %32, %31 ], [ %32, %37 ], [ null, %1 ]
ret i8* %40
}