#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 EOF;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *realloc(char *, int);
char *read_file(const char *file) {
  int bufsize = (1024 * 100);
  int chars_read = 0;
  char *buffer = (char *)calloc(bufsize, sizeof(char));
  FILE *fin = fopen(file, "r");
  int cin = 0;
  if (!fin)
    return NULL;
  while (!feof(fin)) {
    cin = fgetc(fin);
    if (cin == EOF)
      break;
    buffer[chars_read] = cin;
    chars_read++;
    if (chars_read >= bufsize - 1) {
      bufsize *= 2;
      buffer = realloc(buffer, bufsize);
      memset(buffer + chars_read + 1, 0, bufsize / 2);
    }
  }
  return buffer;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x19000,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11be <read_file+0x5e>
movq   $0x0,-0x8(%rbp)
jmp    1277 <read_file+0x117>
jmp    11c3 <read_file+0x63>
mov    -0x28(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11e1 <read_file+0x81>
jmp    126f <read_file+0x10f>
mov    -0x28(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%eax
jne    1204 <read_file+0xa4>
jmp    126f <read_file+0x10f>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     126a <read_file+0x10a>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
jmp    11c3 <read_file+0x63>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x19000,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
lea    0xe79(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1213 <read_file+0xb3>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    1216 <read_file+0xb6>
mov    $0x19000,%r12d
mov    $0x2,%ebx
mov    0x2e33(%rip),%r13
jmp    11d0 <read_file+0x70>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1060 <feof@plt>
add    $0x1,%rbx
test   %eax,%eax
jne    1216 <read_file+0xb6>
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    0x0(%r13),%eax
je     1216 <read_file+0xb6>
mov    %al,-0x2(%r15,%rbx,1)
lea    -0x1(%r12),%eax
lea    -0x1(%rbx),%ecx
cmp    %ecx,%eax
jg     11c0 <read_file+0x60>
lea    (%r12,%r12,1),%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%r15
lea    (%rax,%rbx,1),%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1030 <memset@plt>
mov    %ebp,%r12d
jmp    11c0 <read_file+0x60>
xor    %r15d,%r15d
mov    %r15,%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"
@EOF = global i32 0
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %3
store i32 102400, i32* %4
store i32 0, i32* %5
%9 = load i32, i32* %4
%10 = call i64 @calloc(i32 %9, i32 1)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %6
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %7
store i32 0, i32* %8
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %1
store i8* null, i8** %2
br label %60
17: ; preds = %1
br label %18
18: ; preds = %57, %17
%19 = load i32*, i32** %7
%20 = call i32 @feof(i32* %19)
%21 = icmp ne i32 %20, 0
%22 = xor i1 %21, true
br i1 %22, label %23, label %58
23: ; preds = %18
%24 = load i32*, i32** %7
%25 = call i32 @fgetc(i32* %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = load i32, i32* @EOF
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %23
br label %58
30: ; preds = %23
%31 = load i32, i32* %8
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %6
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 %32, i8* %36
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = load i32, i32* %4
%41 = sub nsw i32 %40, 1
%42 = icmp sge i32 %39, %41
br i1 %42, label %43, label %57
43: ; preds = %30
%44 = load i32, i32* %4
%45 = mul nsw i32 %44, 2
store i32 %45, i32* %4
%46 = load i8*, i8** %6
%47 = load i32, i32* %4
%48 = call i8* @realloc(i8* %46, i32 %47)
store i8* %48, i8** %6
%49 = load i8*, i8** %6
%50 = load i32, i32* %5
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = load i32, i32* %4
%55 = sdiv i32 %54, 2
%56 = call i32 @memset(i8* %53, i32 0, i32 %55)
br label %57
57: ; preds = %43, %30
br label %18
58: ; preds = %29, %18
%59 = load i8*, i8** %6
store i8* %59, i8** %2
br label %60
60: ; preds = %58, %16
%61 = load i8*, i8** %2
ret i8* %61
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @read_file(i8* nocapture %0) {
%2 = tail call i64 @calloc(i32 102400, i32 1)
%3 = inttoptr i64 %2 to i8*
%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 %34, label %6
6: ; preds = %1
%7 = tail call i32 @feof(i32* %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %34
9: ; preds = %6, %29
%10 = phi i64 [ %19, %29 ], [ 0, %6 ]
%11 = phi i8* [ %31, %29 ], [ %3, %6 ]
%12 = phi i32 [ %30, %29 ], [ 102400, %6 ]
%13 = tail call i32 @fgetc(i32* %4)
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %13, %14
br i1 %15, label %34, label %16
16: ; preds = %9
%17 = trunc i32 %13 to i8
%18 = getelementptr inbounds i8, i8* %11, i64 %10
store i8 %17, i8* %18
%19 = add nuw i64 %10, 1
%20 = add nsw i32 %12, -1
%21 = trunc i64 %19 to i32
%22 = icmp sgt i32 %20, %21
br i1 %22, label %29, label %23
23: ; preds = %16
%24 = shl nsw i32 %12, 1
%25 = tail call i8* @realloc(i8* %11, i32 %24)
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = getelementptr inbounds i8, i8* %26, i64 %19
%28 = tail call i32 @memset(i8* %27, i32 0, i32 %12)
br label %29
29: ; preds = %23, %16
%30 = phi i32 [ %24, %23 ], [ %12, %16 ]
%31 = phi i8* [ %25, %23 ], [ %11, %16 ]
%32 = tail call i32 @feof(i32* %4)
%33 = icmp eq i32 %32, 0
br i1 %33, label %9, label %34
34: ; preds = %9, %29, %6, %1
%35 = phi i8* [ null, %1 ], [ %3, %6 ], [ %11, %9 ], [ %31, %29 ]
ret i8* %35
}