#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 */
int EOF;
int /*<<< orphan*/ exit(int);
int getchar();
scalar_t__ malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
char *readline() {
  int ch;
  int i;
  char *str;
  int SIZE = 32;
  str = (char *)malloc(SIZE);
  if (str == NULL) {
    char error[] = "Malloc failed\n";
    write(1, error, strlen(error));
    exit(1);
  }
  ch = getchar();
  i = 0;
  while (ch != '\n' && ch != EOF) {
    str[i++] = ch;
    if (i == SIZE - 1) {
      SIZE *= 2;
      str = realloc(str, SIZE);
    }
    ch = getchar();
  }
  str[i] = '\0';
  return str;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x20,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d9 <readline+0x79>
mov    0xe73(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xe71(%rip),%eax
mov    %eax,-0x1b(%rbp)
mov    0xe6b(%rip),%ax
mov    %ax,-0x17(%rbp)
mov    0xe63(%rip),%al
mov    %al,-0x15(%rbp)
lea    -0x23(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x23(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0xa,-0x4(%rbp)
mov    %al,-0x31(%rbp)
je     120b <readline+0xab>
mov    -0x4(%rbp),%eax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    121b <readline+0xbb>
jmp    126e <readline+0x10e>
mov    -0x4(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x8(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    125f <readline+0xff>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x4(%rbp)
jmp    11ea <readline+0x8a>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x20,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ea <readline+0x8a>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
je     11d6 <readline+0x76>
mov    0x2e56(%rip),%r15
cmp    (%r15),%eax
je     11d6 <readline+0x76>
mov    $0x20,%r12d
xor    %ebx,%ebx
nop
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
lea    -0x1(%r12),%eax
cmp    %rax,%rbx
jne    11c3 <readline+0x63>
add    %r12d,%r12d
mov    %r14,%rdi
mov    %r12d,%esi
call   1070 <realloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
je     11d4 <readline+0x74>
cmp    (%r15),%eax
jne    11a0 <readline+0x40>
mov    %ebx,%ebx
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
movabs $0xa64656c696166,%rax
mov    %rax,0xf(%rsp)
movabs $0x6620636f6c6c614d,%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.readline.error = [15 x i8] c"Malloc failed\0A\00"
@EOF = global i32 0
define i8* @readline() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [15 x i8]
store i32 32, i32* %4
%6 = load i32, i32* %4
%7 = call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %18
11: ; preds = %0
%12 = bitcast [15 x i8]* %5 to i8*
%13 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%14 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
%16 = call i32 @write(i32 1, i8* %13, i32 %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = call i32 (...) @getchar()
store i32 %19, i32* %1
store i32 0, i32* %2
br label %20
20: ; preds = %47, %18
%21 = load i32, i32* %1
%22 = icmp ne i32 %21, 10
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* %1
%25 = load i32, i32* @EOF
%26 = icmp ne i32 %24, %25
br label %27
27: ; preds = %23, %20
%28 = phi i1 [ false, %20 ], [ %26, %23 ]
br i1 %28, label %29, label %49
29: ; preds = %27
%30 = load i32, i32* %1
%31 = trunc i32 %30 to i8
%32 = load i8*, i8** %3
%33 = load i32, i32* %2
%34 = add nsw i32 %33, 1
store i32 %34, i32* %2
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8 %31, i8* %36
%37 = load i32, i32* %2
%38 = load i32, i32* %4
%39 = sub nsw i32 %38, 1
%40 = icmp eq i32 %37, %39
br i1 %40, label %41, label %47
41: ; preds = %29
%42 = load i32, i32* %4
%43 = mul nsw i32 %42, 2
store i32 %43, i32* %4
%44 = load i8*, i8** %3
%45 = load i32, i32* %4
%46 = call i8* @realloc(i8* %44, i32 %45)
store i8* %46, i8** %3
br label %47
47: ; preds = %41, %29
%48 = call i32 (...) @getchar()
store i32 %48, i32* %1
br label %20
49: ; preds = %27
%50 = load i8*, i8** %3
%51 = load i32, i32* %2
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
@__const.readline.error = [15 x i8] c"Malloc failed\0A\00"
@EOF = global i32 0
define i8* @readline() {
%1 = alloca [15 x i8]
%2 = tail call i64 @malloc(i32 32)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %0
%5 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%6 = call i32 @strlen(i8* %5)
%7 = call i32 @write(i32 1, i8* %5, i32 %6)
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = inttoptr i64 %2 to i8*
%11 = tail call i32 (...) @getchar()
%12 = icmp ne i32 %11, 10
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %11, %13
%15 = select i1 %12, i1 %14, i1 false
br i1 %15, label %16, label %40
16: ; preds = %9, %30
%17 = phi i64 [ %22, %30 ], [ 0, %9 ]
%18 = phi i32 [ %32, %30 ], [ 32, %9 ]
%19 = phi i8* [ %31, %30 ], [ %10, %9 ]
%20 = phi i32 [ %33, %30 ], [ %11, %9 ]
%21 = trunc i32 %20 to i8
%22 = add nuw i64 %17, 1
%23 = getelementptr inbounds i8, i8* %19, i64 %17
store i8 %21, i8* %23
%24 = add nsw i32 %18, -1
%25 = zext i32 %24 to i64
%26 = icmp eq i64 %22, %25
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = shl nsw i32 %18, 1
%29 = tail call i8* @realloc(i8* %19, i32 %28)
br label %30
30: ; preds = %27, %16
%31 = phi i8* [ %29, %27 ], [ %19, %16 ]
%32 = phi i32 [ %28, %27 ], [ %18, %16 ]
%33 = tail call i32 (...) @getchar()
%34 = icmp ne i32 %33, 10
%35 = load i32, i32* @EOF
%36 = icmp ne i32 %33, %35
%37 = select i1 %34, i1 %36, i1 false
br i1 %37, label %16, label %38
38: ; preds = %30
%39 = and i64 %22, 4294967295
br label %40
40: ; preds = %38, %9
%41 = phi i64 [ 0, %9 ], [ %39, %38 ]
%42 = phi i8* [ %10, %9 ], [ %31, %38 ]
%43 = getelementptr inbounds i8, i8* %42, i64 %41
store i8 0, i8* %43
ret i8* %42
}