#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 *alloc(char *, size_t);
int getch();
int /*<<< orphan*/ printf(char *);
char *readline(void) {
  char *line;
  size_t i, z;
  int c;
  i = 0;
  z = 20;
  line = alloc(0, z * sizeof(char));
  do {
    c = getch();
    if (c < ' ' && c != '\n') {
      printf("\7Don't use control characters!\n");
      while ((c = getch()) != '\n') {
      }
      i = 0;
    } else {
      line[i++] = c;
      if (i == z) {
        z *= 2;
        line = alloc(line, z * sizeof(char));
      }
    }
  } while (c != '\n');
  line[i] = 0;
  return line;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x14,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
shl    $0x0,%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <alloc@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x20,-0x1c(%rbp)
jge    11ac <readline+0x7c>
cmpl   $0xa,-0x1c(%rbp)
je     11ac <readline+0x7c>
lea    0xe7e(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <getch@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xa,%eax
je     119f <readline+0x6f>
jmp    1187 <readline+0x57>
movq   $0x0,-0x10(%rbp)
jmp    11fb <readline+0xcb>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x10(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11f6 <readline+0xc6>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x0,%rsi
call   1040 <alloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11fb <readline+0xcb>
jmp    1200 <readline+0xd0>
cmpl   $0xa,-0x1c(%rbp)
jne    115d <readline+0x2d>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x14,%r15d
mov    $0x14,%esi
xor    %edi,%edi
call   1040 <alloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
jmp    1155 <readline+0x25>
cmp    $0xa,%ebp
je     11ac <readline+0x7c>
xor    %eax,%eax
call   1050 <getch@plt>
mov    %eax,%ebp
cmp    $0x1f,%eax
jg     1168 <readline+0x38>
cmp    $0xa,%ebp
jne    1188 <readline+0x58>
mov    %bpl,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %r15,%rbx
jne    1150 <readline+0x20>
add    %r15,%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <alloc@plt>
mov    %rax,%r14
jmp    1150 <readline+0x20>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <getch@plt>
cmp    $0xa,%eax
jne    11a0 <readline+0x70>
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"\07Don't use control characters!\0A\00"
define i8* @readline() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
store i64 0, i64* %2
store i64 20, i64* %3
%5 = load i64, i64* %3
%6 = mul i64 %5, 1
%7 = call i8* @alloc(i8* null, i64 %6)
store i8* %7, i8** %1
br label %8
8: ; preds = %41, %0
%9 = call i32 (...) @getch()
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 32
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 10
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %20, %15
%18 = call i32 (...) @getch()
store i32 %18, i32* %4
%19 = icmp ne i32 %18, 10
br i1 %19, label %20, label %21
20: ; preds = %17
br label %17
21: ; preds = %17
store i64 0, i64* %2
br label %40
22: ; preds = %12, %8
%23 = load i32, i32* %4
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %1
%26 = load i64, i64* %2
%27 = add i64 %26, 1
store i64 %27, i64* %2
%28 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 %24, i8* %28
%29 = load i64, i64* %2
%30 = load i64, i64* %3
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %39
32: ; preds = %22
%33 = load i64, i64* %3
%34 = mul i64 %33, 2
store i64 %34, i64* %3
%35 = load i8*, i8** %1
%36 = load i64, i64* %3
%37 = mul i64 %36, 1
%38 = call i8* @alloc(i8* %35, i64 %37)
store i8* %38, i8** %1
br label %39
39: ; preds = %32, %22
br label %40
40: ; preds = %39, %21
br label %41
41: ; preds = %40
%42 = load i32, i32* %4
%43 = icmp ne i32 %42, 10
br i1 %43, label %8, label %44
44: ; preds = %41
%45 = load i8*, i8** %1
%46 = load i64, i64* %2
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %1
ret i8* %48
}

LLVM IR (-O2):
@str = [31 x i8] c"\07Don't use control characters!\00"
define i8* @readline() {
%1 = tail call i8* @alloc(i8* null, i64 20)
br label %2
2: ; preds = %23, %0
%3 = phi i8* [ %1, %0 ], [ %24, %23 ]
%4 = phi i64 [ 0, %0 ], [ %17, %23 ]
%5 = phi i64 [ 20, %0 ], [ %25, %23 ]
%6 = tail call i32 (...) @getch()
%7 = icmp slt i32 %6, 32
%8 = icmp ne i32 %6, 10
%9 = and i1 %7, %8
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %12
12: ; preds = %12, %10
%13 = tail call i32 (...) @getch()
%14 = icmp eq i32 %13, 10
br i1 %14, label %27, label %12
15: ; preds = %2
%16 = trunc i32 %6 to i8
%17 = add i64 %4, 1
%18 = getelementptr inbounds i8, i8* %3, i64 %4
store i8 %16, i8* %18
%19 = icmp eq i64 %17, %5
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = shl i64 %5, 1
%22 = tail call i8* @alloc(i8* %3, i64 %21)
br label %23
23: ; preds = %20, %15
%24 = phi i8* [ %22, %20 ], [ %3, %15 ]
%25 = phi i64 [ %21, %20 ], [ %5, %15 ]
%26 = icmp eq i32 %6, 10
br i1 %26, label %27, label %2
27: ; preds = %23, %12
%28 = phi i64 [ 0, %12 ], [ %17, %23 ]
%29 = phi i8* [ %3, %12 ], [ %24, %23 ]
%30 = getelementptr inbounds i8, i8* %29, i64 %28
store i8 0, i8* %30
ret i8* %29
}