#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 */
char EOF;
int /*<<< orphan*/ EXIT_FAILURE;
char fgetc(int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ terminate(char *, int /*<<< orphan*/);
char *read_input(FILE *stream) {
  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 = fgetc(stream)) != '\n' && c != EOF) {
    if (i >= max_len) {
      in = realloc(in, i + max_len + 1);
      if (in == NULL)
        terminate("Input too long! Memory error!\n", EXIT_FAILURE);
    }
    in[i++] = c;
  }
  in[i] = '\0';
  return 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    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x21(%rbp)
je     11c1 <read_input+0x81>
movsbl -0x11(%rbp),%eax
mov    0x2e32(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11d1 <read_input+0x91>
jmp    1235 <read_input+0xf5>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jl     1218 <read_input+0xd8>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    -0x10(%rbp),%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1213 <read_input+0xd3>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    0xe0b(%rip),%rdi
call   1050 <terminate@plt>
jmp    1218 <read_input+0xd8>
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),%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>
mov    %r14,%rdi
call   1030 <fgetc@plt>
mov    %eax,%ebx
xor    %eax,%eax
cmp    $0xa,%bl
je     11ee <read_input+0xae>
mov    0x2e5b(%rip),%r13
cmp    0x0(%r13),%bl
je     11ee <read_input+0xae>
xor    %ebp,%ebp
lea    0xe7d(%rip),%r12
nopl   0x0(%rax)
cmp    $0x50,%rbp
jb     11d0 <read_input+0x90>
lea    0x51(%rbp),%esi
mov    %r15,%rdi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11d0 <read_input+0x90>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1050 <terminate@plt>
xor    %r15d,%r15d
nopl   (%rax)
mov    %bl,(%r15,%rbp,1)
add    $0x1,%rbp
mov    %r14,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%al
je     11ec <read_input+0xac>
mov    %eax,%ebx
cmp    0x0(%r13),%al
jne    11a0 <read_input+0x60>
mov    %ebp,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
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 i8* @read_input(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
store i32* %0, i32** %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 = %46, %15
%17 = load i32*, i32** %2
%18 = call signext i8 @fgetc(i32* %17)
store i8 %18, i8* %5
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 10
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i8, i8* %5
%23 = sext i8 %22 to i32
%24 = load i8, i8* @EOF
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %23, %25
br label %27
27: ; preds = %21, %16
%28 = phi i1 [ false, %16 ], [ %26, %21 ]
br i1 %28, label %29, label %53
29: ; preds = %27
%30 = load i32, i32* %3
%31 = load i32, i32* %4
%32 = icmp sge i32 %30, %31
br i1 %32, label %33, label %46
33: ; preds = %29
%34 = load i8*, i8** %6
%35 = load i32, i32* %3
%36 = load i32, i32* %4
%37 = add nsw i32 %35, %36
%38 = add nsw i32 %37, 1
%39 = call i8* @realloc(i8* %34, i32 %38)
store i8* %39, i8** %6
%40 = load i8*, i8** %6
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %45
42: ; preds = %33
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @terminate(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %42, %33
br label %46
46: ; preds = %45, %29
%47 = load i8, i8* %5
%48 = load i8*, i8** %6
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8 %47, i8* %52
br label %16
53: ; preds = %27
%54 = load i8*, i8** %6
%55 = load i32, i32* %3
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 0, i8* %57
%58 = load i8*, i8** %6
ret i8* %58
}

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 i8* @read_input(i32* nocapture %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 @fgetc(i32* %0)
%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, 79
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = trunc i64 %14 to i32
%20 = add i32 %19, 81
%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 @fgetc(i32* %0)
%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
ret i8* %39
}