#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 */
int EOF;
long INT_MAX;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int fgetc(int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
__attribute__((visibility("default"))) char *
read_stream_to_string(FILE *stream) {
  long lastPosition = ftell(stream);
  if (fseek(stream, 0, SEEK_END) != 0) {
    return NULL;
  }
  long size = ftell(stream);
  rewind(stream);
  if (size >= INT_MAX) {
    return NULL;
  }
  char *result = malloc(sizeof(char) * (size + 1));
  int ch = fgetc(stream);
  int pos = 0;
  while (ch != EOF) {
    result[pos] = (char)ch;
    pos++;
    ch = fgetc(stream);
  }
  result[size] = (char)0;
  fseek(stream, lastPosition, SEEK_SET);
  return result;
}


x86 assembly (clang, -O0):
<read_stream_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     1196 <read_stream_to_string+0x46>
movq   $0x0,-0x8(%rbp)
jmp    125d <read_stream_to_string+0x10d>
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <rewind@plt>
mov    -0x20(%rbp),%rax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
jl     11cd <read_stream_to_string+0x7d>
movq   $0x0,-0x8(%rbp)
jmp    125d <read_stream_to_string+0x10d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%eax
je     1233 <read_stream_to_string+0xe3>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
jmp    11f7 <read_stream_to_string+0xa7>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_stream_to_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <ftell@plt>
mov    %rax,%r14
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
jne    1203 <read_stream_to_string+0xb3>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <rewind@plt>
mov    0x2e41(%rip),%rax
cmp    (%rax),%r12
jge    1200 <read_stream_to_string+0xb0>
lea    0x1(%r12),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    0x2e18(%rip),%r13
cmp    0x0(%r13),%eax
je     11e5 <read_stream_to_string+0x95>
mov    %r15,%rbp
nopl   0x0(%rax)
mov    %al,0x0(%rbp)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
add    $0x1,%rbp
cmp    0x0(%r13),%eax
jne    11d0 <read_stream_to_string+0x80>
movb   $0x0,(%r15,%r12,1)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <fseek@plt>
jmp    1203 <read_stream_to_string+0xb3>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@SEEK_END = global i32 0
@INT_MAX = global i64 0
@EOF = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @read_stream_to_string(i32* nocapture %0) {
%2 = tail call i64 @ftell(i32* %0)
%3 = load i32, i32* @SEEK_END
%4 = tail call i64 @fseek(i32* %0, i64 0, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %31
6: ; preds = %1
%7 = tail call i64 @ftell(i32* %0)
%8 = tail call i32 @rewind(i32* %0)
%9 = load i64, i64* @INT_MAX
%10 = icmp slt i64 %7, %9
br i1 %10, label %11, label %31
11: ; preds = %6
%12 = trunc i64 %7 to i32
%13 = add i32 %12, 1
%14 = tail call i8* @malloc(i32 %13)
%15 = tail call i32 @fgetc(i32* %0)
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
br i1 %17, label %27, label %18
18: ; preds = %11, %18
%19 = phi i64 [ %23, %18 ], [ 0, %11 ]
%20 = phi i32 [ %24, %18 ], [ %15, %11 ]
%21 = trunc i32 %20 to i8
%22 = getelementptr inbounds i8, i8* %14, i64 %19
store i8 %21, i8* %22
%23 = add nuw i64 %19, 1
%24 = tail call i32 @fgetc(i32* %0)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %18
27: ; preds = %18, %11
%28 = getelementptr inbounds i8, i8* %14, i64 %7
store i8 0, i8* %28
%29 = load i32, i32* @SEEK_SET
%30 = tail call i64 @fseek(i32* %0, i64 %2, i32 %29)
br label %31
31: ; preds = %27, %6, %1
%32 = phi i8* [ null, %1 ], [ %14, %27 ], [ null, %6 ]
ret i8* %32
}