#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *realloc(char *, size_t);
char *read_file(char *filename) {
  FILE *file;
  char *new_buffer;
  char *buffer = NULL;
  size_t buffer_length = 0;
  size_t buffer_capacity = 0;
  const size_t chunk_len = 2048;
  file = fopen(filename, "r");
  if (!file)
    return NULL;
  while (1) {
    size_t fread_length;
    if (buffer_capacity - buffer_length < chunk_len) {
      new_buffer = realloc(buffer, buffer_capacity + chunk_len);
      if (!new_buffer) {
        int __errno = errno;
        (void)fclose(file);
        free(buffer);
        errno = __errno;
        return NULL;
      }
      buffer = new_buffer;
      buffer_capacity += chunk_len;
    }
    fread_length =
        fread(buffer + buffer_length, 1, buffer_capacity - buffer_length, file);
    if (fread_length < buffer_capacity - buffer_length) {
      if (ferror(file)) {
        int __errno = errno;
        (void)fclose(file);
        free(buffer);
        errno = __errno;
        return NULL;
      }
      if (feof(file))
        break;
    }
    buffer_length += fread_length;
  }
  (void)fclose(file);
  return buffer;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x800,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <read_file+0x58>
movq   $0x0,-0x8(%rbp)
jmp    1311 <read_file+0x1a1>
jmp    11cd <read_file+0x5d>
mov    -0x38(%rbp),%rax
sub    -0x30(%rbp),%rax
cmp    $0x800,%rax
jae    1251 <read_file+0xe1>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
add    $0x800,%rsi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123b <read_file+0xcb>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4c(%rbp),%ecx
mov    0x2db4(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1311 <read_file+0x1a1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
sub    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    -0x30(%rbp),%rcx
cmp    %rcx,%rax
jae    12ef <read_file+0x17f>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12d2 <read_file+0x162>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%ecx
mov    0x2d1d(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1311 <read_file+0x1a1>
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     12ea <read_file+0x17a>
jmp    1300 <read_file+0x190>
jmp    12ef <read_file+0x17f>
mov    -0x48(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11cd <read_file+0x5d>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     122d <read_file+0xbd>
mov    %rax,%r15
xor    %r12d,%r12d
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    11a3 <read_file+0x33>
nopl   (%rax)
add    %r13,%rbp
mov    %r12,%rbx
sub    %rbp,%rbx
cmp    $0x7ff,%rbx
ja     11d2 <read_file+0x62>
add    $0x800,%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
je     1212 <read_file+0xa2>
mov    %r12,%rbx
sub    %rbp,%rbx
mov    %rax,%r14
lea    (%r14,%rbp,1),%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1050 <fread@plt>
mov    %rax,%r13
cmp    %rbx,%rax
jae    11a0 <read_file+0x30>
mov    %r15,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1212 <read_file+0xa2>
mov    %r15,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     11a0 <read_file+0x30>
mov    %r15,%rdi
call   1060 <fclose@plt>
jmp    1230 <read_file+0xc0>
mov    0x2dc7(%rip),%rbx
mov    (%rbx),%ebp
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,(%rbx)
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
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
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %6
store i64 0, i64* %7
store i64 0, i64* %8
store i64 2048, i64* %9
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %1
store i8* null, i8** %2
br label %81
18: ; preds = %1
br label %19
19: ; preds = %18, %73
%20 = load i64, i64* %8
%21 = load i64, i64* %7
%22 = sub i64 %20, %21
%23 = icmp ult i64 %22, 2048
br i1 %23, label %24, label %42
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = load i64, i64* %8
%27 = add i64 %26, 2048
%28 = call i8* @realloc(i8* %25, i64 %27)
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = icmp ne i8* %29, null
br i1 %30, label %38, label %31
31: ; preds = %24
%32 = load i32, i32* @errno
store i32 %32, i32* %11
%33 = load i32*, i32** %4
%34 = call i32 @fclose(i32* %33)
%35 = load i8*, i8** %6
%36 = call i32 @free(i8* %35)
%37 = load i32, i32* %11
store i32 %37, i32* @errno
store i8* null, i8** %2
br label %81
38: ; preds = %24
%39 = load i8*, i8** %5
store i8* %39, i8** %6
%40 = load i64, i64* %8
%41 = add i64 %40, 2048
store i64 %41, i64* %8
br label %42
42: ; preds = %38, %19
%43 = load i8*, i8** %6
%44 = load i64, i64* %7
%45 = getelementptr inbounds i8, i8* %43, i64 %44
%46 = load i64, i64* %8
%47 = load i64, i64* %7
%48 = sub i64 %46, %47
%49 = load i32*, i32** %4
%50 = call i64 @fread(i8* %45, i32 1, i64 %48, i32* %49)
store i64 %50, i64* %10
%51 = load i64, i64* %10
%52 = load i64, i64* %8
%53 = load i64, i64* %7
%54 = sub i64 %52, %53
%55 = icmp ult i64 %51, %54
br i1 %55, label %56, label %73
56: ; preds = %42
%57 = load i32*, i32** %4
%58 = call i64 @ferror(i32* %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %67
60: ; preds = %56
%61 = load i32, i32* @errno
store i32 %61, i32* %12
%62 = load i32*, i32** %4
%63 = call i32 @fclose(i32* %62)
%64 = load i8*, i8** %6
%65 = call i32 @free(i8* %64)
%66 = load i32, i32* %12
store i32 %66, i32* @errno
store i8* null, i8** %2
br label %81
67: ; preds = %56
%68 = load i32*, i32** %4
%69 = call i64 @feof(i32* %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %67
br label %77
72: ; preds = %67
br label %73
73: ; preds = %72, %42
%74 = load i64, i64* %10
%75 = load i64, i64* %7
%76 = add i64 %75, %74
store i64 %76, i64* %7
br label %19
77: ; preds = %71
%78 = load i32*, i32** %4
%79 = call i32 @fclose(i32* %78)
%80 = load i8*, i8** %6
store i8* %80, i8** %2
br label %81
81: ; preds = %77, %60, %31, %17
%82 = load i8*, i8** %2
ret i8* %82
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define noalias 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 %41, label %4
4: ; preds = %1, %37
%5 = phi i64 [ %22, %37 ], [ 0, %1 ]
%6 = phi i64 [ %38, %37 ], [ 0, %1 ]
%7 = phi i8* [ %23, %37 ], [ null, %1 ]
%8 = sub i64 %5, %6
%9 = icmp ult i64 %8, 2048
br i1 %9, label %10, label %20
10: ; preds = %4
%11 = add i64 %5, 2048
%12 = tail call i8* @realloc(i8* %7, i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = sub i64 %11, %6
br label %20
16: ; preds = %10
%17 = load i32, i32* @errno
%18 = tail call i32 @fclose(i32* %2)
%19 = tail call i32 @free(i8* %7)
store i32 %17, i32* @errno
br label %41
20: ; preds = %14, %4
%21 = phi i64 [ %15, %14 ], [ %8, %4 ]
%22 = phi i64 [ %11, %14 ], [ %5, %4 ]
%23 = phi i8* [ %12, %14 ], [ %7, %4 ]
%24 = getelementptr inbounds i8, i8* %23, i64 %6
%25 = tail call i64 @fread(i8* %24, i32 1, i64 %21, i32* %2)
%26 = icmp ult i64 %25, %21
br i1 %26, label %27, label %37
27: ; preds = %20
%28 = tail call i64 @ferror(i32* %2)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = tail call i32 @fclose(i32* %2)
%33 = tail call i32 @free(i8* %23)
store i32 %31, i32* @errno
br label %41
34: ; preds = %27
%35 = tail call i64 @feof(i32* %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %20, %34
%38 = add i64 %25, %6
br label %4
39: ; preds = %34
%40 = tail call i32 @fclose(i32* %2)
br label %41
41: ; preds = %16, %30, %1, %39
%42 = phi i8* [ %23, %39 ], [ null, %1 ], [ null, %30 ], [ null, %16 ]
ret i8* %42
}