#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 */
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
unsigned char *malloc(size_t);
unsigned char *realloc(unsigned char *, size_t);
void read_file(unsigned char **buf_ptr, size_t *len_ptr, FILE *fp) {
  size_t len = 0;
  size_t size = 1024;
  size_t chunk;
  unsigned char *buf = malloc(size);
  while ((chunk = fread(buf + len, 1, size - len, fp)) == size - len) {
    len = size;
    size *= 2;
    buf = realloc(buf, size);
  }
  len += chunk;
  *buf_ptr = buf;
  *len_ptr = len;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x400,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
sub    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rcx
sub    -0x20(%rbp),%rcx
cmp    %rcx,%rax
jne    11be <read_file+0x8e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x38(%rbp)
jmp    1161 <read_file+0x31>
mov    -0x30(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
add    $0x40,%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    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x400,%ebx
mov    $0x400,%edi
call   1040 <malloc@plt>
mov    %rax,%r13
mov    $0x400,%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x400,%rax
jne    11af <read_file+0x7f>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rbp
add    %rbx,%rbx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
mov    %rax,%r13
lea    (%rax,%rbp,1),%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    %rbp,%rax
je     1180 <read_file+0x50>
jmp    11b1 <read_file+0x81>
xor    %ebp,%ebp
add    %rax,%rbp
mov    %r13,(%r15)
mov    %rbp,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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