#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, size_t);
size_t read_text(char **t, FILE *stream) {
  size_t s = 0;
  *t = malloc(100);
  if (!*t) {
    perror("ERROR in read_text, malloc failed: ");
    exit(EXIT_FAILURE);
  }
  size_t cnt;
  size_t sum = 0;
  do {
    cnt = fread(*t + s, 1, 100, stream);
    sum += cnt;
    if (cnt == 100) {
      s += 100;
      *t = realloc(*t, s + 100);
      if (!*t) {
        perror("ERROR in read_text, realloc failed: ");
        exit(EXIT_FAILURE);
      }
    }
  } while (cnt == 100);
  *(*t + sum) = 0;
  return sum;
}


x86 assembly (clang, -O0):
<read_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x64,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a4 <read_text+0x54>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
add    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x64,%edx
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x64,-0x20(%rbp)
jne    123c <read_text+0xec>
mov    -0x18(%rbp),%rax
add    $0x64,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
add    $0x64,%rsi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1237 <read_text+0xe7>
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    123c <read_text+0xec>
jmp    1241 <read_text+0xf1>
cmpq   $0x64,-0x20(%rbp)
je     11ac <read_text+0x5c>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x64,%edi
call   1040 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11ee <read_text+0x9e>
mov    $0xc8,%ebx
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
lea    (%rax,%rbx,1),%rdi
add    $0xffffffffffffff38,%rdi
mov    $0x1,%esi
mov    $0x64,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
add    %rax,%r12
cmp    $0x64,%rax
jne    11d7 <read_text+0x87>
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
mov    %rax,(%r15)
add    $0x64,%rbx
test   %rax,%rax
jne    1180 <read_text+0x30>
lea    0xe60(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    (%r15),%rax
movb   $0x0,(%rax,%r12,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe0b(%rip),%rdi
jmp    11c4 <read_text+0x74>

LLVM IR (-O0):
@.str = [36 x i8] c"ERROR in read_text, malloc failed: \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [37 x i8] c"ERROR in read_text, realloc failed: \00"
define i64 @read_text(i8** %0, i32* %1) {
%3 = alloca i8**
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8** %0, i8*** %3
store i32* %1, i32** %4
store i64 0, i64* %5
%8 = call i8* @malloc(i32 100)
%9 = load i8**, i8*** %3
store i8* %8, i8** %9
%10 = load i8**, i8*** %3
%11 = load i8*, i8** %10
%12 = icmp ne i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
store i64 0, i64* %7
br label %18
18: ; preds = %48, %17
%19 = load i8**, i8*** %3
%20 = load i8*, i8** %19
%21 = load i64, i64* %5
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i32*, i32** %4
%24 = call i64 @fread(i8* %22, i32 1, i32 100, i32* %23)
store i64 %24, i64* %6
%25 = load i64, i64* %6
%26 = load i64, i64* %7
%27 = add i64 %26, %25
store i64 %27, i64* %7
%28 = load i64, i64* %6
%29 = icmp eq i64 %28, 100
br i1 %29, label %30, label %47
30: ; preds = %18
%31 = load i64, i64* %5
%32 = add i64 %31, 100
store i64 %32, i64* %5
%33 = load i8**, i8*** %3
%34 = load i8*, i8** %33
%35 = load i64, i64* %5
%36 = add i64 %35, 100
%37 = call i8* @realloc(i8* %34, i64 %36)
%38 = load i8**, i8*** %3
store i8* %37, i8** %38
%39 = load i8**, i8*** %3
%40 = load i8*, i8** %39
%41 = icmp ne i8* %40, null
br i1 %41, label %46, label %42
42: ; preds = %30
%43 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %30
br label %47
47: ; preds = %46, %18
br label %48
48: ; preds = %47
%49 = load i64, i64* %6
%50 = icmp eq i64 %49, 100
br i1 %50, label %18, label %51
51: ; preds = %48
%52 = load i8**, i8*** %3
%53 = load i8*, i8** %52
%54 = load i64, i64* %7
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8 0, i8* %55
%56 = load i64, i64* %7
ret i64 %56
}

LLVM IR (-O2):
@.str = [36 x i8] c"ERROR in read_text, malloc failed: \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [37 x i8] c"ERROR in read_text, realloc failed: \00"
define i64 @read_text(i8** nocapture %0, i32* nocapture %1) {
%3 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
store i8* %3, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2, %17
%10 = phi i8* [ %21, %17 ], [ %3, %2 ]
%11 = phi i64 [ %18, %17 ], [ 0, %2 ]
%12 = phi i64 [ %15, %17 ], [ 0, %2 ]
%13 = getelementptr inbounds i8, i8* %10, i64 %11
%14 = tail call i64 @fread(i8* %13, i32 1, i32 100, i32* %1)
%15 = add i64 %14, %12
%16 = icmp eq i64 %14, 100
br i1 %16, label %17, label %27
17: ; preds = %9
%18 = add i64 %11, 100
%19 = load i8*, i8** %0
%20 = add i64 %11, 200
%21 = tail call i8* @realloc(i8* %19, i64 %20)
store i8* %21, i8** %0
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %9
23: ; preds = %17
%24 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %9
%28 = load i8*, i8** %0
%29 = getelementptr inbounds i8, i8* %28, i64 %15
store i8 0, i8* %29
ret i64 %15
}