#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*/ uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int fread(int /*<<< orphan*/ *, int, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int const);
int /*<<< orphan*/ *realloc(int /*<<< orphan*/ *, int);
uint8_t *AllocAndReadFile(FILE *fh, uint32_t *size) {
  const int blocksize = 4096;
  uint8_t *buf = malloc(blocksize);
  uint32_t total = 0;
  for (;;) {
    uint32_t actual = fread(buf + total, 1, blocksize, fh);
    total += actual;
    if (actual < blocksize)
      break;
    buf = realloc(buf, total + blocksize);
  }
  *size = total;
  return buf;
}


x86 assembly (clang, -O0):
<AllocAndReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1000,-0x14(%rbp)
mov    $0x1000,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x1000,-0x28(%rbp)
jge    119c <AllocAndReadFile+0x6c>
jmp    11b7 <AllocAndReadFile+0x87>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1000,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    115c <AllocAndReadFile+0x2c>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AllocAndReadFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x1000,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %eax,%ebx
cmp    $0x1000,%eax
jl     11a3 <AllocAndReadFile+0x73>
nopl   0x0(%rax)
lea    0x1000(%rbx),%esi
mov    %r12,%rdi
call   1050 <realloc@plt>
mov    %rax,%r12
movslq %ebx,%rbx
lea    (%rax,%rbx,4),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
add    %eax,%ebx
cmp    $0xfff,%eax
jg     1170 <AllocAndReadFile+0x40>
mov    %ebx,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @AllocAndReadFile(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 4096, i32* %5
%9 = call i32* @malloc(i32 4096)
store i32* %9, i32** %6
store i32 0, i32* %7
br label %10
10: ; preds = %23, %2
%11 = load i32*, i32** %6
%12 = load i32, i32* %7
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %11, i64 %13
%15 = load i32*, i32** %3
%16 = call i32 @fread(i32* %14, i32 1, i32 4096, i32* %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = load i32, i32* %7
%19 = add nsw i32 %18, %17
store i32 %19, i32* %7
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 4096
br i1 %21, label %22, label %23
22: ; preds = %10
br label %28
23: ; preds = %10
%24 = load i32*, i32** %6
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 4096
%27 = call i32* @realloc(i32* %24, i32 %26)
store i32* %27, i32** %6
br label %10
28: ; preds = %22
%29 = load i32, i32* %7
%30 = load i32*, i32** %4
store i32 %29, i32* %30
%31 = load i32*, i32** %6
ret i32* %31
}

LLVM IR (-O2):
define i32* @AllocAndReadFile(i32* nocapture %0, i32* nocapture writeonly %1) {
%3 = tail call i32* @malloc(i32 4096)
%4 = tail call i32 @fread(i32* %3, i32 1, i32 4096, i32* %0)
%5 = icmp slt i32 %4, 4096
br i1 %5, label %16, label %6
6: ; preds = %2, %6
%7 = phi i32 [ %14, %6 ], [ %4, %2 ]
%8 = phi i32* [ %10, %6 ], [ %3, %2 ]
%9 = add nsw i32 %7, 4096
%10 = tail call i32* @realloc(i32* %8, i32 %9)
%11 = sext i32 %7 to i64
%12 = getelementptr inbounds i32, i32* %10, i64 %11
%13 = tail call i32 @fread(i32* %12, i32 1, i32 4096, i32* %0)
%14 = add nsw i32 %13, %7
%15 = icmp slt i32 %13, 4096
br i1 %15, label %16, label %6
16: ; preds = %6, %2
%17 = phi i32* [ %3, %2 ], [ %10, %6 ]
%18 = phi i32 [ %4, %2 ], [ %14, %6 ]
store i32 %18, i32* %1
ret i32* %17
}