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

/* Variables and functions */
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *realloc(char *, size_t);
char *read_from_fp(FILE *fp, size_t *len_p) {
  char *data = NULL;
  char buf[65536];
  size_t len = 0, size = 0, ret;
  while ((ret = fread(buf, 1, sizeof(buf), fp)) > 0) {
    len += ret;
    if (len > size) {
      size = (len + 16) * 3 / 2;
      data = realloc(data, size);
    }
    memcpy(data + len - ret, buf, ret);
  }
  *len_p = len;
  return data;
}


x86 assembly (clang, -O0):
<read_from_fp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x10028(%rbp)
movq   $0x0,-0x10030(%rbp)
lea    -0x10020(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1030 <fread@plt>
mov    %rax,-0x10038(%rbp)
cmp    $0x0,%rax
jbe    1212 <read_from_fp+0xe2>
mov    -0x10038(%rbp),%rax
add    -0x10028(%rbp),%rax
mov    %rax,-0x10028(%rbp)
mov    -0x10028(%rbp),%rax
cmp    -0x10030(%rbp),%rax
jbe    11e3 <read_from_fp+0xb3>
mov    -0x10028(%rbp),%rax
add    $0x10,%rax
imul   $0x3,%rax,%rax
shr    $0x1,%rax
mov    %rax,-0x10030(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10030(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x10028(%rbp),%rdi
xor    %eax,%eax
sub    -0x10038(%rbp),%rax
add    %rax,%rdi
lea    -0x10020(%rbp),%rsi
mov    -0x10038(%rbp),%rdx
call   1040 <memcpy@plt>
jmp    1161 <read_from_fp+0x31>
mov    -0x10028(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_fp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %rsi,(%rsp)
mov    %rdi,%rcx
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %rcx,0x8(%rsp)
call   1030 <fread@plt>
test   %rax,%rax
je     11d7 <read_from_fp+0xa7>
mov    %rax,%rbx
xor    %r13d,%r13d
lea    0x10(%rsp),%r12
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    11b3 <read_from_fp+0x83>
nopl   0x0(%rax,%rax,1)
add    %r13,%r14
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    0x8(%rsp),%rcx
call   1030 <fread@plt>
mov    %rax,%rbx
mov    %r15,%r14
test   %rax,%rax
je     11dd <read_from_fp+0xad>
lea    (%rbx,%r14,1),%r15
cmp    %rbp,%r15
jbe    1180 <read_from_fp+0x50>
lea    (%r15,%r15,2),%rbp
add    $0x30,%rbp
shr    %rbp
mov    %r13,%rdi
mov    %rbp,%rsi
call   1050 <realloc@plt>
mov    %rax,%r13
jmp    1180 <read_from_fp+0x50>
xor    %r15d,%r15d
xor    %r13d,%r13d
mov    (%rsp),%rax
mov    %r15,(%rax)
mov    %r13,%rax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @read_from_fp(i32* nocapture %0, i64* nocapture writeonly %1) {
%3 = alloca [65536 x i8]
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%5 = call i64 @fread(i8* %4, i32 1, i32 65536, i32* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %26, label %7
7: ; preds = %2, %19
%8 = phi i64 [ %24, %19 ], [ %5, %2 ]
%9 = phi i8* [ %21, %19 ], [ null, %2 ]
%10 = phi i64 [ %20, %19 ], [ 0, %2 ]
%11 = phi i64 [ %12, %19 ], [ 0, %2 ]
%12 = add i64 %8, %11
%13 = icmp ugt i64 %12, %10
br i1 %13, label %14, label %19
14: ; preds = %7
%15 = mul i64 %12, 3
%16 = add i64 %15, 48
%17 = lshr i64 %16, 1
%18 = call i8* @realloc(i8* %9, i64 %17)
br label %19
19: ; preds = %14, %7
%20 = phi i64 [ %17, %14 ], [ %10, %7 ]
%21 = phi i8* [ %18, %14 ], [ %9, %7 ]
%22 = getelementptr inbounds i8, i8* %21, i64 %11
%23 = call i32 @memcpy(i8* %22, i8* %4, i64 %8)
%24 = call i64 @fread(i8* %4, i32 1, i32 65536, i32* %0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %7
26: ; preds = %19, %2
%27 = phi i64 [ 0, %2 ], [ %12, %19 ]
%28 = phi i8* [ null, %2 ], [ %21, %19 ]
store i64 %27, i64* %1
ret i8* %28
}