#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(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *realloc(char *, size_t const);
char *read_nonseekable_stream(FILE *fp, size_t *read) {
  enum { PIECE_LEN = 4096 };
  char *content = malloc(PIECE_LEN + 1);
  if (content != NULL) {
    char *last_allocated_block = content;
    size_t len = 0U, piece_len;
    while ((piece_len = fread(content + len, 1, PIECE_LEN, fp)) != 0U) {
      const size_t new_size = len + piece_len + PIECE_LEN + 1U;
      last_allocated_block = realloc(content, new_size);
      if (last_allocated_block == NULL) {
        break;
      }
      content = last_allocated_block;
      len += piece_len;
    }
    content[len] = '\0';
    if (last_allocated_block == NULL) {
      free(content);
      content = NULL;
    } else {
      *read = len;
    }
  }
  if (content == NULL) {
    *read = 0U;
  }
  return content;
}


x86 assembly (clang, -O0):
<read_nonseekable_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1001,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     122f <read_nonseekable_stream+0xef>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11f2 <read_nonseekable_stream+0xb2>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
add    $0x1000,%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d9 <read_nonseekable_stream+0x99>
jmp    11f2 <read_nonseekable_stream+0xb2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1179 <read_nonseekable_stream+0x39>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x20(%rbp)
jne    121f <read_nonseekable_stream+0xdf>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    122a <read_nonseekable_stream+0xea>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    122f <read_nonseekable_stream+0xef>
cmpq   $0x0,-0x18(%rbp)
jne    1245 <read_nonseekable_stream+0x105>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_nonseekable_stream>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x1001,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11bd <read_nonseekable_stream+0x7d>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%r12
lea    (%rax,%rbx,1),%r13
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11c4 <read_nonseekable_stream+0x84>
lea    (%rax,%rbx,1),%rbp
lea    (%rax,%rbx,1),%rsi
add    $0x1001,%rsi
mov    %r12,%rdi
call   1060 <realloc@plt>
mov    %rbp,%rbx
test   %rax,%rax
jne    1170 <read_nonseekable_stream+0x30>
movb   $0x0,0x0(%r13)
mov    %r12,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
xor    %r12d,%r12d
jmp    11c9 <read_nonseekable_stream+0x89>
movb   $0x0,0x0(%r13)
mov    %rbx,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @read_nonseekable_stream(i32* %0, i64* %1) {
%3 = alloca i32*
%4 = alloca i64*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %3
store i64* %1, i64** %4
%10 = call i8* @malloc(i32 4097)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %52
13: ; preds = %2
%14 = load i8*, i8** %5
store i8* %14, i8** %6
store i64 0, i64* %7
br label %15
15: ; preds = %34, %13
%16 = load i8*, i8** %5
%17 = load i64, i64* %7
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i32*, i32** %3
%20 = call i64 @fread(i8* %18, i32 1, i32 4096, i32* %19)
store i64 %20, i64* %8
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %39
22: ; preds = %15
%23 = load i64, i64* %7
%24 = load i64, i64* %8
%25 = add i64 %23, %24
%26 = add i64 %25, 4096
%27 = add i64 %26, 1
store i64 %27, i64* %9
%28 = load i8*, i8** %5
%29 = load i64, i64* %9
%30 = call i8* @realloc(i8* %28, i64 %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %22
br label %39
34: ; preds = %22
%35 = load i8*, i8** %6
store i8* %35, i8** %5
%36 = load i64, i64* %8
%37 = load i64, i64* %7
%38 = add i64 %37, %36
store i64 %38, i64* %7
br label %15
39: ; preds = %33, %15
%40 = load i8*, i8** %5
%41 = load i64, i64* %7
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8 0, i8* %42
%43 = load i8*, i8** %6
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = load i8*, i8** %5
%47 = call i32 @free(i8* %46)
store i8* null, i8** %5
br label %51
48: ; preds = %39
%49 = load i64, i64* %7
%50 = load i64*, i64** %4
store i64 %49, i64* %50
br label %51
51: ; preds = %48, %45
br label %52
52: ; preds = %51, %2
%53 = load i8*, i8** %5
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = load i64*, i64** %4
store i64 0, i64* %56
br label %57
57: ; preds = %55, %52
%58 = load i8*, i8** %5
ret i8* %58
}

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