#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*/ *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, int);
char *readFile(FILE *in) {
  char *data = malloc(65536);
  int cap = 65536;
  int size = 0;
  while (!feof(in)) {
    if (size == cap) {
      cap *= 2;
      data = realloc(data, cap);
    }
    size += fread(&data[size], 1, 65536, in);
  }
  data = realloc(data, size + 1);
  data[size] = '\0';
  return data;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x10000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x10000,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1186 <readFile+0x46>
jmp    11db <readFile+0x9b>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11ab <readFile+0x6b>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1030 <fread@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    1168 <readFile+0x28>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
mov    $0x10000,%r12d
mov    $0x10000,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <feof@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     1187 <readFile+0x47>
lea    0x1(%rbx),%esi
mov    %r14,%rdi
call   1060 <realloc@plt>
movslq %ebx,%rcx
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
jmp    11bb <readFile+0x7b>
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rdi
add    %r14,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %rax,%rbx
add    %ebp,%ebx
mov    %r15,%rdi
call   1040 <feof@plt>
mov    %ebx,%ebp
test   %eax,%eax
jne    116c <readFile+0x2c>
cmp    %r12d,%ebp
jne    1190 <readFile+0x50>
lea    0x0(%rbp,%rbp,1),%r12d
mov    %r14,%rdi
mov    %r12d,%esi
call   1060 <realloc@plt>
mov    %rax,%r14
jmp    1190 <readFile+0x50>

LLVM IR (-O0):
define i8* @readFile(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
%6 = call i8* @malloc(i32 65536)
store i8* %6, i8** %3
store i32 65536, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %22, %1
%8 = load i32*, i32** %2
%9 = call i32 @feof(i32* %8)
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %33
12: ; preds = %7
%13 = load i32, i32* %5
%14 = load i32, i32* %4
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* %4
%18 = mul nsw i32 %17, 2
store i32 %18, i32* %4
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = call i8* @realloc(i8* %19, i32 %20)
store i8* %21, i8** %3
br label %22
22: ; preds = %16, %12
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i32*, i32** %2
%28 = call i64 @fread(i8* %26, i32 1, i32 65536, i32* %27)
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = add nsw i64 %30, %28
%32 = trunc i64 %31 to i32
store i32 %32, i32* %5
br label %7
33: ; preds = %7
%34 = load i8*, i8** %3
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
%37 = call i8* @realloc(i8* %34, i32 %36)
store i8* %37, i8** %3
%38 = load i8*, i8** %3
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
define i8* @readFile(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(65536) i8* @malloc(i32 65536)
%3 = tail call i32 @feof(i32* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %23
5: ; preds = %1, %13
%6 = phi i32 [ %20, %13 ], [ 0, %1 ]
%7 = phi i32 [ %15, %13 ], [ 65536, %1 ]
%8 = phi i8* [ %14, %13 ], [ %2, %1 ]
%9 = icmp eq i32 %6, %7
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = shl nsw i32 %6, 1
%12 = tail call i8* @realloc(i8* %8, i32 %11)
br label %13
13: ; preds = %10, %5
%14 = phi i8* [ %12, %10 ], [ %8, %5 ]
%15 = phi i32 [ %11, %10 ], [ %7, %5 ]
%16 = sext i32 %6 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = tail call i64 @fread(i8* %17, i32 1, i32 65536, i32* %0)
%19 = trunc i64 %18 to i32
%20 = add i32 %6, %19
%21 = tail call i32 @feof(i32* %0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %5, label %23
23: ; preds = %13, %1
%24 = phi i8* [ %2, %1 ], [ %14, %13 ]
%25 = phi i32 [ 0, %1 ], [ %20, %13 ]
%26 = add nsw i32 %25, 1
%27 = tail call i8* @realloc(i8* %24, i32 %26)
%28 = sext i32 %25 to i64
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8 0, i8* %29
ret i8* %27
}