#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 uint;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ferror(int /*<<< orphan*/);
int fread(char *, int, int, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, int);
int /*<<< orphan*/ stdin;
uint freadall(char **target, FILE *fh) {
  uint bufsize = 1024;
  char *buf = malloc(bufsize);
  if (buf == NULL)
    return 0;
  uint len = 0;
  while (1) {
    if ((len = len + fread(buf + len, 1, bufsize - len, stdin)) == bufsize) {
      bufsize *= 2;
      buf = realloc(buf, bufsize);
    } else if (ferror(stdin)) {
      perror("Reading everything there is from a handle");
    } else
      break;
  }
  *target = buf;
  return len;
}


x86 assembly (clang, -O0):
<freadall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x400,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118a <freadall+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    1235 <freadall+0xe5>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
sub    -0x2c(%rbp),%edx
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
cmp    -0x1c(%rbp),%eax
jne    11ec <freadall+0x9c>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
jmp    121f <freadall+0xcf>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1215 <freadall+0xc5>
lea    0xdf5(%rip),%rdi
call   1070 <perror@plt>
jmp    121a <freadall+0xca>
jmp    1224 <freadall+0xd4>
jmp    121f <freadall+0xcf>
jmp    1191 <freadall+0x41>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freadall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x400,%r12d
mov    $0x400,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d8 <freadall+0x88>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    0x2e69(%rip),%r13
movslq %ebp,%r15
lea    (%rbx,%r15,1),%rdi
mov    %r12d,%edx
sub    %r15d,%edx
mov    0x0(%r13),%ecx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,%ebp
add    %r15d,%ebp
cmp    %r12d,%ebp
je     11c0 <freadall+0x70>
mov    0x0(%r13),%edi
call   1030 <ferror@plt>
test   %rax,%rax
je     11d3 <freadall+0x83>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
jmp    117f <freadall+0x2f>
add    %r12d,%r12d
mov    %rbx,%rdi
mov    %r12d,%esi
call   1060 <realloc@plt>
mov    %rax,%rbx
jmp    117f <freadall+0x2f>
mov    %rbx,(%r14)
jmp    11da <freadall+0x8a>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [42 x i8] c"Reading everything there is from a handle\00"
define i32 @freadall(i8** %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i32 1024, i32* %6
%9 = load i32, i32* %6
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* %3
br label %48
14: ; preds = %2
store i32 0, i32* %8
br label %15
15: ; preds = %14, %43
%16 = load i32, i32* %8
%17 = load i8*, i8** %7
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %6
%22 = load i32, i32* %8
%23 = sub nsw i32 %21, %22
%24 = load i32, i32* @stdin
%25 = call i32 @fread(i8* %20, i32 1, i32 %23, i32 %24)
%26 = add nsw i32 %16, %25
store i32 %26, i32* %8
%27 = load i32, i32* %6
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %35
29: ; preds = %15
%30 = load i32, i32* %6
%31 = mul nsw i32 %30, 2
store i32 %31, i32* %6
%32 = load i8*, i8** %7
%33 = load i32, i32* %6
%34 = call i8* @realloc(i8* %32, i32 %33)
store i8* %34, i8** %7
br label %43
35: ; preds = %15
%36 = load i32, i32* @stdin
%37 = call i64 @ferror(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %42
41: ; preds = %35
br label %44
42: ; preds = %39
br label %43
43: ; preds = %42, %29
br label %15
44: ; preds = %41
%45 = load i8*, i8** %7
%46 = load i8**, i8*** %4
store i8* %45, i8** %46
%47 = load i32, i32* %8
store i32 %47, i32* %3
br label %48
48: ; preds = %44, %13
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [42 x i8] c"Reading everything there is from a handle\00"
define i32 @freadall(i8** nocapture writeonly %0, i32* nocapture readnone %1) {
%3 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%4 = icmp eq i8* %3, null
br i1 %4, label %28, label %5
5: ; preds = %2, %18
%6 = phi i32 [ %19, %18 ], [ 1024, %2 ]
%7 = phi i8* [ %20, %18 ], [ %3, %2 ]
%8 = phi i32 [ %16, %18 ], [ 0, %2 ]
br label %9
9: ; preds = %5, %25
%10 = phi i32 [ %16, %25 ], [ %8, %5 ]
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %7, i64 %11
%13 = sub nsw i32 %6, %10
%14 = load i32, i32* @stdin
%15 = tail call i32 @fread(i8* %12, i32 1, i32 %13, i32 %14)
%16 = add nsw i32 %15, %10
%17 = icmp eq i32 %16, %6
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = shl nsw i32 %6, 1
%20 = tail call i8* @realloc(i8* %7, i32 %19)
br label %5
21: ; preds = %9
%22 = load i32, i32* @stdin
%23 = tail call i64 @ferror(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %9
27: ; preds = %21
store i8* %7, i8** %0
br label %28
28: ; preds = %2, %27
%29 = phi i32 [ %16, %27 ], [ 0, %2 ]
ret i32 %29
}