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

/* Variables and functions */
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ realloc(char *, int);
char *read_file(FILE *fp) {
  char *prod = NULL;
  uint32_t len = 0, prod_s = 0;
  do
    prod = (char *)realloc(prod, prod_s += 1024);
  while ((len = fread(&prod[prod_s - 1024], 1, 1024, fp)) == 1024);
  prod[prod_s - 1024 + len] = '\0';
  return prod;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x400,%esi
mov    %esi,-0x18(%rbp)
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%eax
sub    $0x400,%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x400,%eax
je     1142 <read_file+0x22>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x400,%ecx
add    -0x14(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %r15d,%r15d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rbx
add    $0x400,%r15
mov    %r12,%rdi
mov    %r15d,%esi
call   1040 <realloc@plt>
mov    %rax,%r12
add    %rax,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x400,%eax
je     1140 <read_file+0x20>
add    %r15d,%eax
add    $0xfffffc00,%eax
cltq
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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