#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 /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
char *realloc(char *, size_t);
char *slurp(FILE *stream) {
  size_t capacity = 4096;
  char *buffer = malloc(capacity + 1);
  size_t length = 0;
  char *nbuffer;
  while (!feof(stream)) {
    if (length == capacity) {
      capacity += 4096;
      nbuffer = realloc(buffer, capacity + 1);
      if (nbuffer == NULL) {
        free(buffer);
        return NULL;
      }
      buffer = nbuffer;
      buffer[capacity] = '\0';
    }
    length += fread(buffer + length, 1, capacity - length, stream);
  }
  buffer[length] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<slurp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x1000,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    119b <slurp+0x4b>
jmp    122c <slurp+0xdc>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1201 <slurp+0xb1>
mov    -0x18(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ed <slurp+0x9d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1240 <slurp+0xf0>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    117d <slurp+0x2d>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x1001,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     118a <slurp+0x3a>
xor    %ebx,%ebx
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    $0x1000,%r12d
xor    %ebx,%ebx
jmp    11c6 <slurp+0x76>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%r14,%rbx,1),%rdi
mov    %r12,%rdx
sub    %rbx,%rdx
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
add    %rax,%rbx
mov    %r15,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    1176 <slurp+0x26>
cmp    %r12,%rbx
jne    11a0 <slurp+0x50>
lea    0x1001(%rbx),%rsi
mov    %r14,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     11f0 <slurp+0xa0>
lea    0x1000(%rbx),%r12
movb   $0x0,(%rax,%r12,1)
mov    %rax,%r14
jmp    11a0 <slurp+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    117b <slurp+0x2b>

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

LLVM IR (-O2):
define noalias i8* @slurp(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(4097) i8* @malloc(i64 4097)
%3 = tail call i32 @feof(i32* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %28
5: ; preds = %1, %19
%6 = phi i64 [ %25, %19 ], [ 0, %1 ]
%7 = phi i8* [ %21, %19 ], [ %2, %1 ]
%8 = phi i64 [ %20, %19 ], [ 4096, %1 ]
%9 = icmp eq i64 %6, %8
br i1 %9, label %10, label %19
10: ; preds = %5
%11 = add i64 %6, 4096
%12 = add i64 %6, 4097
%13 = tail call i8* @realloc(i8* %7, i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @free(i8* %7)
br label %32
17: ; preds = %10
%18 = getelementptr inbounds i8, i8* %13, i64 %11
store i8 0, i8* %18
br label %19
19: ; preds = %17, %5
%20 = phi i64 [ %11, %17 ], [ %8, %5 ]
%21 = phi i8* [ %13, %17 ], [ %7, %5 ]
%22 = getelementptr inbounds i8, i8* %21, i64 %6
%23 = sub i64 %20, %6
%24 = tail call i64 @fread(i8* %22, i32 1, i64 %23, i32* %0)
%25 = add i64 %24, %6
%26 = tail call i32 @feof(i32* %0)
%27 = icmp eq i32 %26, 0
br i1 %27, label %5, label %28
28: ; preds = %19, %1
%29 = phi i8* [ %2, %1 ], [ %21, %19 ]
%30 = phi i64 [ 0, %1 ], [ %25, %19 ]
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %28, %15
%33 = phi i8* [ null, %15 ], [ %29, %28 ]
ret i8* %33
}