#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;
char getchar();
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ terminate(char *, int /*<<< orphan*/);
void read_input(char **str) {
  int i = 0, max_len = 80;
  char c, *in = malloc(max_len + 1);
  if (in == NULL)
    terminate("Memory allocation error\n", EXIT_FAILURE);
  while ((c = getchar()) != '\n' && c != EOF) {
    if (i > max_len) {
      in = realloc(in, i + max_len);
      if (in == NULL)
        terminate("Input too long! Memory error!\n", EXIT_FAILURE);
    }
    in[i++] = c;
  }
  in[i] = '\0';
  *str = in;
}


x86 assembly (clang, -O0):
<read_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x50,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1189 <read_input+0x49>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1050 <terminate@plt>
jmp    118e <read_input+0x4e>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x21(%rbp)
je     11bf <read_input+0x7f>
movsbl -0x11(%rbp),%eax
mov    0x2e34(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11cf <read_input+0x8f>
jmp    1230 <read_input+0xf0>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    1213 <read_input+0xd3>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    -0x10(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120e <read_input+0xce>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    0xe10(%rip),%rdi
call   1050 <terminate@plt>
jmp    1213 <read_input+0xd3>
mov    -0x11(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0xc(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    118e <read_input+0x4e>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_input>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x51,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1175 <read_input+0x35>
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1050 <terminate@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     1201 <read_input+0xc1>
mov    %eax,%ebx
mov    0x2e5d(%rip),%r13
cmp    0x0(%r13),%al
je     1201 <read_input+0xc1>
mov    %r14,(%rsp)
mov    0x2e3c(%rip),%r14
lea    0xe76(%rip),%r12
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x51,%rbp
jb     11e0 <read_input+0xa0>
lea    0x50(%rbp),%esi
mov    %r15,%rdi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11e0 <read_input+0xa0>
mov    (%r14),%esi
mov    %r12,%rdi
call   1050 <terminate@plt>
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %bl,(%r15,%rbp,1)
add    $0x1,%rbp
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     11fb <read_input+0xbb>
mov    %eax,%ebx
cmp    0x0(%r13),%al
jne    11b0 <read_input+0x70>
mov    %ebp,%ebp
mov    (%rsp),%r14
movb   $0x0,(%r15,%rbp,1)
mov    %r15,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Memory allocation error\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
@.str.1 = [31 x i8] c"Input too long! Memory error!\0A\00"
define void @read_input(i8** %0) {
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
store i8** %0, i8*** %2
store i32 0, i32* %3
store i32 80, i32* %4
%7 = load i32, i32* %4
%8 = add nsw i32 %7, 1
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @terminate(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %1
br label %16
16: ; preds = %44, %15
%17 = call signext i8 (...) @getchar()
store i8 %17, i8* %5
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 10
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i8, i8* %5
%22 = sext i8 %21 to i32
%23 = load i8, i8* @EOF
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %22, %24
br label %26
26: ; preds = %20, %16
%27 = phi i1 [ false, %16 ], [ %25, %20 ]
br i1 %27, label %28, label %51
28: ; preds = %26
%29 = load i32, i32* %3
%30 = load i32, i32* %4
%31 = icmp sgt i32 %29, %30
br i1 %31, label %32, label %44
32: ; preds = %28
%33 = load i8*, i8** %6
%34 = load i32, i32* %3
%35 = load i32, i32* %4
%36 = add nsw i32 %34, %35
%37 = call i8* @realloc(i8* %33, i32 %36)
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @terminate(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %40, %32
br label %44
44: ; preds = %43, %28
%45 = load i8, i8* %5
%46 = load i8*, i8** %6
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 %45, i8* %50
br label %16
51: ; preds = %26
%52 = load i8*, i8** %6
%53 = load i32, i32* %3
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
%56 = load i8*, i8** %6
%57 = load i8**, i8*** %2
store i8* %56, i8** %57
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"Memory allocation error\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
@.str.1 = [31 x i8] c"Input too long! Memory error!\0A\00"
define void @read_input(i8** nocapture writeonly %0) {
%2 = tail call dereferenceable_or_null(81) i8* @malloc(i32 81)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @terminate(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %7
7: ; preds = %4, %1
%8 = tail call signext i8 (...) @getchar()
%9 = icmp ne i8 %8, 10
%10 = load i8, i8* @EOF
%11 = icmp ne i8 %8, %10
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %37
13: ; preds = %7, %26
%14 = phi i64 [ %28, %26 ], [ 0, %7 ]
%15 = phi i8 [ %30, %26 ], [ %8, %7 ]
%16 = phi i8* [ %27, %26 ], [ %2, %7 ]
%17 = icmp ugt i64 %14, 80
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = trunc i64 %14 to i32
%20 = add i32 %19, 80
%21 = tail call i8* @realloc(i8* %16, i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @terminate(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %18, %23, %13
%27 = phi i8* [ null, %23 ], [ %21, %18 ], [ %16, %13 ]
%28 = add nuw i64 %14, 1
%29 = getelementptr inbounds i8, i8* %27, i64 %14
store i8 %15, i8* %29
%30 = tail call signext i8 (...) @getchar()
%31 = icmp ne i8 %30, 10
%32 = load i8, i8* @EOF
%33 = icmp ne i8 %30, %32
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %13, label %35
35: ; preds = %26
%36 = and i64 %28, 4294967295
br label %37
37: ; preds = %35, %7
%38 = phi i64 [ 0, %7 ], [ %36, %35 ]
%39 = phi i8* [ %2, %7 ], [ %27, %35 ]
%40 = getelementptr inbounds i8, i8* %39, i64 %38
store i8 0, i8* %40
store i8* %39, i8** %0
ret void
}