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

/* Variables and functions */
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *realloc(char *, size_t);
const char *string_read_fp(FILE *fp) {
  char buffer[1024];
  size_t pos = 0;
  char *out = malloc(1);
  do {
    size_t count = fread(buffer, 1, sizeof(buffer), fp);
    out = realloc(out, pos + count + 1);
    memcpy(out + pos, buffer, count);
    pos += count;
  } while (!feof(fp));
  out[pos] = '\0';
  return out;
}


x86 assembly (clang, -O0):
<string_read_fp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x418(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %rax,-0x428(%rbp)
mov    -0x420(%rbp),%rdi
mov    -0x418(%rbp),%rsi
add    -0x428(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rdi
add    -0x418(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x428(%rbp),%rax
add    -0x418(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    117b <string_read_fp+0x2b>
mov    -0x420(%rbp),%rax
mov    -0x418(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x420(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_read_fp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
xor    %ebp,%ebp
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %rax,%r13
add    %rax,%rbp
lea    (%rax,%rbx,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   1070 <realloc@plt>
mov    %rax,%r12
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
mov    %r14,%rdi
call   1040 <feof@plt>
test   %eax,%eax
je     1180 <string_read_fp+0x30>
movb   $0x0,(%r12,%rbp,1)
mov    %r12,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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