#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;
      if ((last_allocated_block = realloc(content, new_size)) == 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     122e <read_nonseekable_stream+0xee>
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     11f1 <read_nonseekable_stream+0xb1>
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)
cmp    $0x0,%rax
jne    11d8 <read_nonseekable_stream+0x98>
jmp    11f1 <read_nonseekable_stream+0xb1>
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    121e <read_nonseekable_stream+0xde>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    1229 <read_nonseekable_stream+0xe9>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    122e <read_nonseekable_stream+0xee>
cmpq   $0x0,-0x18(%rbp)
jne    1244 <read_nonseekable_stream+0x104>
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 %51
13: ; preds = %2
%14 = load i8*, i8** %5
store i8* %14, i8** %6
store i64 0, i64* %7
br label %15
15: ; preds = %33, %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 %38
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 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %22
br label %38
33: ; preds = %22
%34 = load i8*, i8** %6
store i8* %34, i8** %5
%35 = load i64, i64* %8
%36 = load i64, i64* %7
%37 = add i64 %36, %35
store i64 %37, i64* %7
br label %15
38: ; preds = %32, %15
%39 = load i8*, i8** %5
%40 = load i64, i64* %7
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %6
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = load i8*, i8** %5
%46 = call i32 @free(i8* %45)
store i8* null, i8** %5
br label %50
47: ; preds = %38
%48 = load i64, i64* %7
%49 = load i64*, i64** %4
store i64 %48, i64* %49
br label %50
50: ; preds = %47, %44
br label %51
51: ; preds = %50, %2
%52 = load i8*, i8** %5
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load i64*, i64** %4
store i64 0, i64* %55
br label %56
56: ; preds = %54, %51
%57 = load i8*, i8** %5
ret i8* %57
}

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
}