#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 ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *realloc(char *, int);
char *fp_to_buf(FILE *fp) {
  char *str = NULL;
  int nchunks = 1;
  while (1) {
    int res;
    char *s, *snext, buf[512];
    s = realloc(str, 1 + (512 * nchunks));
    if (!s) {
      fprintf(fp, "out of memory.\n");
      free(str);
      return NULL;
    }
    str = s;
    snext = str + (512 * (nchunks - 1));
    res = fread(buf, 1, 512, fp);
    if (res < 512) {
      if (res < 0) {
        fprintf(fp, "fread error %d\n", ferror(fp));
        free(str);
        return NULL;
      }
      memcpy(snext, buf, res);
      snext[res] = '\0';
      return str;
    }
    memcpy(snext, buf, 512);
    nchunks++;
  }
}


x86 assembly (clang, -O0):
<fp_to_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
shl    $0x9,%esi
add    $0x1,%esi
call   1080 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c7 <fp_to_buf+0x67>
mov    -0x10(%rbp),%rdi
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12aa <fp_to_buf+0x14a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
shl    $0x9,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1050 <fread@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x200,-0x20(%rbp)
jge    1287 <fp_to_buf+0x127>
cmpl   $0x0,-0x20(%rbp)
jge    125b <fp_to_buf+0xfb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
mov    -0x238(%rbp),%rdi
mov    %eax,%edx
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12aa <fp_to_buf+0x14a>
mov    -0x30(%rbp),%rdi
lea    -0x230(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12aa <fp_to_buf+0x14a>
mov    -0x30(%rbp),%rdi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1070 <memcpy@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117e <fp_to_buf+0x1e>
mov    -0x8(%rbp),%rax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fp_to_buf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%r14
xor    %r13d,%r13d
xor    %edi,%edi
mov    $0x201,%esi
call   1080 <realloc@plt>
mov    $0x0,%ebx
test   %rax,%rax
je     1203 <fp_to_buf+0x93>
mov    %rax,%r12
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %r13d,%rbx
add    %r12,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    $0x1ff,%eax
jle    1229 <fp_to_buf+0xb9>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x200,%edx
call   1070 <memcpy@plt>
lea    0x401(%r13),%esi
mov    %r12,%rdi
call   1080 <realloc@plt>
mov    %r12,%rbx
add    $0x200,%r13d
mov    %rax,%r12
test   %rax,%rax
jne    11b0 <fp_to_buf+0x40>
lea    0xdf6(%rip),%rdi
mov    $0xf,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
jmp    1242 <fp_to_buf+0xd2>
mov    %eax,%ebp
test   %eax,%eax
js     1257 <fp_to_buf+0xe7>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1070 <memcpy@plt>
mov    %ebp,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r12,%rax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1040 <ferror@plt>
lea    0xdaa(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r12,%rdi
jmp    121f <fp_to_buf+0xaf>

LLVM IR (-O0):
@.str = [16 x i8] c"out of memory.\0A\00"
@.str.1 = [16 x i8] c"fread error %d\0A\00"
define i8* @fp_to_buf(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [512 x i8]
store i32* %0, i32** %3
store i8* null, i8** %4
store i32 1, i32* %5
br label %10
10: ; preds = %1, %56
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = mul nsw i32 512, %12
%14 = add nsw i32 1, %13
%15 = call i8* @realloc(i8* %11, i32 %14)
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %23, label %18
18: ; preds = %10
%19 = load i32*, i32** %3
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%21 = load i8*, i8** %4
%22 = call i32 @free(i8* %21)
store i8* null, i8** %2
br label %62
23: ; preds = %10
%24 = load i8*, i8** %7
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = load i32, i32* %5
%27 = sub nsw i32 %26, 1
%28 = mul nsw i32 512, %27
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %25, i64 %29
store i8* %30, i8** %8
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%32 = load i32*, i32** %3
%33 = call i32 @fread(i8* %31, i32 1, i32 512, i32* %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 512
br i1 %35, label %36, label %56
36: ; preds = %23
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i32*, i32** %3
%41 = load i32*, i32** %3
%42 = call i32 @ferror(i32* %41)
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = load i8*, i8** %4
%45 = call i32 @free(i8* %44)
store i8* null, i8** %2
br label %62
46: ; preds = %36
%47 = load i8*, i8** %8
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%49 = load i32, i32* %6
%50 = call i32 @memcpy(i8* %47, i8* %48, i32 %49)
%51 = load i8*, i8** %8
%52 = load i32, i32* %6
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
%55 = load i8*, i8** %4
store i8* %55, i8** %2
br label %62
56: ; preds = %23
%57 = load i8*, i8** %8
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%59 = call i32 @memcpy(i8* %57, i8* %58, i32 512)
%60 = load i32, i32* %5
%61 = add nsw i32 %60, 1
store i32 %61, i32* %5
br label %10
62: ; preds = %46, %39, %18
%63 = load i8*, i8** %2
ret i8* %63
}

LLVM IR (-O2):
@.str = [16 x i8] c"out of memory.\0A\00"
@.str.1 = [16 x i8] c"fread error %d\0A\00"
define i8* @fp_to_buf(i32* nocapture %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = call i8* @realloc(i8* null, i32 513)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %29, %1
%7 = phi i8* [ null, %1 ], [ %12, %29 ]
%8 = call i64 @fwrite(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 15, i64 1, i32* %0)
%9 = call i32 @free(i8* %7)
br label %37
10: ; preds = %1, %29
%11 = phi i64 [ %31, %29 ], [ 1, %1 ]
%12 = phi i8* [ %35, %29 ], [ %4, %1 ]
%13 = phi i32 [ %33, %29 ], [ 512, %1 ]
%14 = add nsw i32 %13, -512
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
%17 = call i32 @fread(i8* %3, i32 1, i32 512, i32* %0)
%18 = icmp slt i32 %17, 512
br i1 %18, label %19, label %29
19: ; preds = %10
%20 = icmp slt i32 %17, 0
br i1 %20, label %21, label %25
21: ; preds = %19
%22 = call i32 @ferror(i32* %0)
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i32 @free(i8* %12)
br label %37
25: ; preds = %19
%26 = call i32 @memcpy(i8* %16, i8* %3, i32 %17)
%27 = zext i32 %17 to i64
%28 = getelementptr inbounds i8, i8* %16, i64 %27
store i8 0, i8* %28
br label %37
29: ; preds = %10
%30 = call i32 @memcpy(i8* %16, i8* %3, i32 512)
%31 = add nuw i64 %11, 1
%32 = trunc i64 %31 to i32
%33 = shl nsw i32 %32, 9
%34 = or i32 %33, 1
%35 = call i8* @realloc(i8* %12, i32 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %6, label %10
37: ; preds = %21, %25, %6
%38 = phi i8* [ null, %6 ], [ %12, %25 ], [ null, %21 ]
ret i8* %38
}