#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*/ free(unsigned char *);
scalar_t__ getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
unsigned char *realloc(unsigned char *, size_t);
unsigned char *read_text_file(FILE *file) {
  unsigned char *message = NULL;
  unsigned char *aux_pointer = NULL;
  size_t string_size = 0;
  size_t position = 0;
  unsigned int read_char = 1;
  while (read_char) {
    read_char = (unsigned)getc(file);
    if (read_char == '\n' || read_char == '\0') {
      message[position] = '\0';
      return message;
    }
    if (string_size <= position) {
      string_size++;
      aux_pointer = realloc(message, string_size);
      if (!aux_pointer) {
        printf("Error on realloc!\n");
        free(message);
        return NULL;
      }
      message = aux_pointer;
    }
    message[position] = (unsigned char)read_char;
    position++;
  }
  return message;
}


x86 assembly (clang, -O0):
<read_text_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1237 <read_text_file+0xf7>
mov    -0x10(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xa,-0x34(%rbp)
je     119d <read_text_file+0x5d>
cmpl   $0x0,-0x34(%rbp)
jne    11b6 <read_text_file+0x76>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123f <read_text_file+0xff>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
ja     1216 <read_text_file+0xd6>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120e <read_text_file+0xce>
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123f <read_text_file+0xff>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x34(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1173 <read_text_file+0x33>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
xor    %r15d,%r15d
xor    %r12d,%r12d
xor    %r13d,%r13d
jmp    116e <read_text_file+0x2e>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rax
mov    %bl,(%rax,%r13,1)
add    $0x1,%r13
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <getc@plt>
mov    %rax,%rbx
cmp    $0xa,%ebx
je     11b4 <read_text_file+0x74>
test   %ebx,%ebx
je     11b4 <read_text_file+0x74>
cmp    %r13,%r12
ja     1160 <read_text_file+0x20>
add    $0x1,%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
jne    1163 <read_text_file+0x23>
lea    0xe5e(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    11b9 <read_text_file+0x79>
movb   $0x0,(%r15,%r13,1)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@str = [18 x i8] c"Error on realloc!\00"
define noalias i8* @read_text_file(i32* nocapture %0) {
br label %2
2: ; preds = %1, %19
%3 = phi i8* [ null, %1 ], [ %20, %19 ]
%4 = phi i64 [ 0, %1 ], [ %21, %19 ]
%5 = phi i64 [ 0, %1 ], [ %24, %19 ]
%6 = tail call i64 @getc(i32* %0)
%7 = trunc i64 %6 to i32
switch i32 %7, label %10 [
i32 10, label %8
i32 0, label %8
]
8: ; preds = %2, %2
%9 = getelementptr inbounds i8, i8* %3, i64 %5
store i8 0, i8* %9
br label %25
10: ; preds = %2
%11 = icmp ugt i64 %4, %5
br i1 %11, label %19, label %12
12: ; preds = %10
%13 = add i64 %4, 1
%14 = tail call i8* @realloc(i8* %3, i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @free(i8* %3)
br label %25
19: ; preds = %12, %10
%20 = phi i8* [ %3, %10 ], [ %14, %12 ]
%21 = phi i64 [ %4, %10 ], [ %13, %12 ]
%22 = trunc i64 %6 to i8
%23 = getelementptr inbounds i8, i8* %20, i64 %5
store i8 %22, i8* %23
%24 = add i64 %5, 1
br label %2
25: ; preds = %16, %8
%26 = phi i8* [ %3, %8 ], [ null, %16 ]
ret i8* %26
}