#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 */
unsigned int fread(char *, size_t const, unsigned int,
                   int /*<<< orphan*/ *const);
int /*<<< orphan*/ free(char *);
size_t get_remaining_stream_size(int /*<<< orphan*/ *const);
char *malloc(size_t const);
char *read_seekable_stream(FILE *const fp, size_t *read) {
  char *content;
  const size_t len = get_remaining_stream_size(fp);
  *read = 0UL;
  if ((content = malloc(len + 1U)) == NULL) {
    return NULL;
  }
  if (fread(content, len, 1U, fp) == 1U) {
    content[len] = '\0';
    *read = len;
  } else {
    free(content);
    content = NULL;
  }
  return content;
}


x86 assembly (clang, -O0):
<read_seekable_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <get_remaining_stream_size@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1190 <read_seekable_stream+0x50>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <read_seekable_stream+0xa4>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    11cb <read_seekable_stream+0x8b>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    11dc <read_seekable_stream+0x9c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_seekable_stream>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1060 <get_remaining_stream_size@plt>
mov    %rax,%r15
movq   $0x0,(%r14)
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1198 <read_seekable_stream+0x58>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1190 <read_seekable_stream+0x50>
movb   $0x0,(%rbx,%r15,1)
mov    %r15,(%r14)
jmp    119a <read_seekable_stream+0x5a>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @read_seekable_stream(i32* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
store i32* %0, i32** %4
store i64* %1, i64** %5
%8 = load i32*, i32** %4
%9 = call i64 @get_remaining_stream_size(i32* %8)
store i64 %9, i64* %7
%10 = load i64*, i64** %5
store i64 0, i64* %10
%11 = load i64, i64* %7
%12 = add i64 %11, 1
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i8* null, i8** %3
br label %33
16: ; preds = %2
%17 = load i8*, i8** %6
%18 = load i64, i64* %7
%19 = load i32*, i32** %4
%20 = call i32 @fread(i8* %17, i64 %18, i32 1, i32* %19)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = load i8*, i8** %6
%24 = load i64, i64* %7
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
%26 = load i64, i64* %7
%27 = load i64*, i64** %5
store i64 %26, i64* %27
br label %31
28: ; preds = %16
%29 = load i8*, i8** %6
%30 = call i32 @free(i8* %29)
store i8* null, i8** %6
br label %31
31: ; preds = %28, %22
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %15
%34 = load i8*, i8** %3
ret i8* %34
}

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