#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 */

/* Variables and functions */
char EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char getchar();
char *malloc(size_t);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, size_t);
char *read_input(void) {
  unsigned int input_cnt = 0;
  size_t input_buf_size = 64;
  char c = '\0';
  char *input = malloc(input_buf_size * sizeof(char));
  if (input == NULL) {
    printf("ERROR: Input buffer allocation failed! \n");
    exit(EXIT_FAILURE);
  }
  while (1) {
    c = getchar();
    if (c == EOF || c == '\n') {
      input[input_cnt] = '\0';
      return input;
    }
    input[input_cnt] = c;
    input_cnt++;
    if (input_cnt == input_buf_size + 1) {
      input_buf_size = input_buf_size * 2;
      if (input_buf_size > 32768) {
        printf("ERROR: Input size too big! \n");
        free(input);
        exit(EXIT_FAILURE);
      }
      input = realloc(input, input_buf_size * sizeof(char));
      if (input == NULL) {
        printf("ERROR: Input buffer re-allocation failed! \n");
        exit(EXIT_FAILURE);
      }
    }
  }
}


x86 assembly (clang, -O0):
<read_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x40,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b1 <read_input+0x51>
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11b6 <read_input+0x56>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
mov    0x2e1d(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11e3 <read_input+0x83>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    11f8 <read_input+0x98>
mov    -0x20(%rbp),%rax
mov    -0x4(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x11(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x4(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x10(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jne    129e <read_input+0x13e>
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x8000,-0x10(%rbp)
jbe    125f <read_input+0xff>
lea    0xde6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
shl    $0x0,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1299 <read_input+0x139>
lea    0xdc0(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    129e <read_input+0x13e>
jmp    11b6 <read_input+0x56>

x86 assembly (clang, -O2):
<read_input>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x40,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     122a <read_input+0xca>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
je     11e6 <read_input+0x86>
mov    0x2e56(%rip),%r15
cmp    (%r15),%al
je     11e6 <read_input+0x86>
mov    $0x40,%ebx
xor    %r12d,%r12d
nop
mov    %r12d,%ecx
mov    %al,(%r14,%rcx,1)
add    $0x1,%r12d
lea    0x1(%rbx),%rax
cmp    %r12,%rax
jne    11d3 <read_input+0x73>
add    %rbx,%rbx
cmp    $0x8001,%rbx
jae    11fa <read_input+0x9a>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1210 <read_input+0xb0>
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
je     11e3 <read_input+0x83>
cmp    (%r15),%al
jne    11a0 <read_input+0x40>
mov    %r12d,%ebx
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe2a(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    121c <read_input+0xbc>
lea    0xde9(%rip),%rdi
call   1040 <puts@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe16(%rip),%rdi
jmp    1217 <read_input+0xb7>

LLVM IR (-O0):
@.str = [41 x i8] c"ERROR: Input buffer allocation failed! \0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
@.str.1 = [29 x i8] c"ERROR: Input size too big! \0A\00"
@.str.2 = [44 x i8] c"ERROR: Input buffer re-allocation failed! \0A\00"
define i8* @read_input() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8
%4 = alloca i8*
store i32 0, i32* %1
store i64 64, i64* %2
store i8 0, i8* %3
%5 = load i64, i64* %2
%6 = mul i64 %5, 1
%7 = call i8* @malloc(i64 %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
br label %15
15: ; preds = %14, %68
%16 = call signext i8 (...) @getchar()
store i8 %16, i8* %3
%17 = load i8, i8* %3
%18 = sext i8 %17 to i32
%19 = load i8, i8* @EOF
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %18, %20
br i1 %21, label %26, label %22
22: ; preds = %15
%23 = load i8, i8* %3
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %26, label %32
26: ; preds = %22, %15
%27 = load i8*, i8** %4
%28 = load i32, i32* %1
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
%31 = load i8*, i8** %4
ret i8* %31
32: ; preds = %22
%33 = load i8, i8* %3
%34 = load i8*, i8** %4
%35 = load i32, i32* %1
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 %33, i8* %37
%38 = load i32, i32* %1
%39 = add i32 %38, 1
store i32 %39, i32* %1
%40 = load i32, i32* %1
%41 = zext i32 %40 to i64
%42 = load i64, i64* %2
%43 = add i64 %42, 1
%44 = icmp eq i64 %41, %43
br i1 %44, label %45, label %68
45: ; preds = %32
%46 = load i64, i64* %2
%47 = mul i64 %46, 2
store i64 %47, i64* %2
%48 = load i64, i64* %2
%49 = icmp ugt i64 %48, 32768
br i1 %49, label %50, label %56
50: ; preds = %45
%51 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%52 = load i8*, i8** %4
%53 = call i32 @free(i8* %52)
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %45
%57 = load i8*, i8** %4
%58 = load i64, i64* %2
%59 = mul i64 %58, 1
%60 = call i8* @realloc(i8* %57, i64 %59)
store i8* %60, i8** %4
%61 = load i8*, i8** %4
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %67
63: ; preds = %56
%64 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %56
br label %68
68: ; preds = %67, %32
br label %15
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
@str = [43 x i8] c"ERROR: Input buffer re-allocation failed! \00"
@str.3 = [28 x i8] c"ERROR: Input size too big! \00"
@str.4 = [40 x i8] c"ERROR: Input buffer allocation failed! \00"
define noalias i8* @read_input() {
%1 = tail call dereferenceable_or_null(64) i8* @malloc(i64 64)
%2 = icmp eq i8* %1, null
br i1 %2, label %9, label %3
3: ; preds = %0
%4 = tail call signext i8 (...) @getchar()
%5 = load i8, i8* @EOF
%6 = icmp eq i8 %4, %5
%7 = icmp eq i8 %4, 10
%8 = or i1 %7, %6
br i1 %8, label %15, label %19
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.4, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %45
%14 = zext i32 %26 to i64
br label %15
15: ; preds = %3, %13
%16 = phi i64 [ %14, %13 ], [ 0, %3 ]
%17 = phi i8* [ %47, %13 ], [ %1, %3 ]
%18 = getelementptr inbounds i8, i8* %17, i64 %16
store i8 0, i8* %18
ret i8* %17
19: ; preds = %3, %45
%20 = phi i8 [ %48, %45 ], [ %4, %3 ]
%21 = phi i8* [ %47, %45 ], [ %1, %3 ]
%22 = phi i64 [ %46, %45 ], [ 64, %3 ]
%23 = phi i32 [ %26, %45 ], [ 0, %3 ]
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 %20, i8* %25
%26 = add i32 %23, 1
%27 = zext i32 %26 to i64
%28 = add i64 %22, 1
%29 = icmp eq i64 %28, %27
br i1 %29, label %30, label %45
30: ; preds = %19
%31 = shl i64 %22, 1
%32 = icmp ugt i64 %31, 32768
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.3, i64 0, i64 0))
%35 = tail call i32 @free(i8* %21)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %30
%39 = tail call i8* @realloc(i8* %21, i64 %31)
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %38, %19
%46 = phi i64 [ %31, %38 ], [ %22, %19 ]
%47 = phi i8* [ %39, %38 ], [ %21, %19 ]
%48 = tail call signext i8 (...) @getchar()
%49 = load i8, i8* @EOF
%50 = icmp eq i8 %48, %49
%51 = icmp eq i8 %48, 10
%52 = or i1 %51, %50
br i1 %52, label %13, label %19
}