#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 /*<<< orphan*/ backspace();
char getcode();
int /*<<< orphan*/ nextline();
int /*<<< orphan*/ terminal_putchar(char);
char *kscan() {
  int i = 0;
  const int max_limit = 1024 * 1024;
  char c;
  static unsigned char buffer[1024 * 1024];
  do {
    c = getcode();
    if (c == '\b') {
      backspace();
      i = i > 0 ? (i - 1) : 0;
    } else if (c == '\n') {
      nextline();
      break;
    } else {
      terminal_putchar(c);
      if (i < max_limit)
        buffer[i++] = c;
      else
        break;
    }
  } while (1);
  buffer[i] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<kscan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x100000,-0x8(%rbp)
mov    $0x0,%al
call   1060 <getcode@plt>
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x8,%eax
jne    11a1 <kscan+0x61>
mov    $0x0,%al
call   1030 <backspace@plt>
cmpl   $0x0,-0x4(%rbp)
jle    118c <kscan+0x4c>
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1196 <kscan+0x56>
xor    %eax,%eax
mov    %eax,-0x10(%rbp)
jmp    1196 <kscan+0x56>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <kscan+0xbf>
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
jne    11ba <kscan+0x7a>
mov    $0x0,%al
call   1050 <nextline@plt>
jmp    1213 <kscan+0xd3>
movsbl -0x9(%rbp),%edi
call   1040 <terminal_putchar@plt>
cmpl   $0x100000,-0x4(%rbp)
jge    11f0 <kscan+0xb0>
mov    -0x9(%rbp),%dl
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x4(%rbp)
movslq %eax,%rcx
lea    0x2e68(%rip),%rax
mov    %dl,(%rax,%rcx,1)
jmp    11f5 <kscan+0xb5>
jmp    1213 <kscan+0xd3>
jmp    11fa <kscan+0xba>
jmp    11ff <kscan+0xbf>
jmp    1204 <kscan+0xc4>
mov    $0x1,%al
test   $0x1,%al
jne    1156 <kscan+0x16>
jmp    1213 <kscan+0xd3>
movslq -0x4(%rbp),%rcx
lea    0x2e32(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e27(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kscan>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebp,%ebp
lea    0x2f00(%rip),%r14
xor    %r15d,%r15d
jmp    1174 <kscan+0x34>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <backspace@plt>
test   %r15d,%r15d
lea    -0x1(%r15),%eax
cmovle %ebp,%eax
mov    %eax,%r15d
xor    %eax,%eax
call   1060 <getcode@plt>
cmp    $0x8,%al
je     1160 <kscan+0x20>
mov    %eax,%ebx
cmp    $0xa,%al
je     11a3 <kscan+0x63>
movsbl %bl,%edi
call   1040 <terminal_putchar@plt>
cmp    $0xfffff,%r15d
jg     11aa <kscan+0x6a>
movslq %r15d,%rax
add    $0x1,%r15d
mov    %bl,(%rax,%r14,1)
jmp    1174 <kscan+0x34>
xor    %eax,%eax
call   1050 <nextline@plt>
movslq %r15d,%rax
movb   $0x0,(%rax,%r14,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@kscan.buffer = internal global [1048576 x i8] zeroinitializer
define i8* @kscan() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
store i32 0, i32* %1
store i32 1048576, i32* %2
br label %4
4: ; preds = %40, %0
%5 = call signext i8 (...) @getcode()
store i8 %5, i8* %3
%6 = load i8, i8* %3
%7 = sext i8 %6 to i32
%8 = icmp eq i32 %7, 8
br i1 %8, label %9, label %19
9: ; preds = %4
%10 = call i32 (...) @backspace()
%11 = load i32, i32* %1
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* %1
%15 = sub nsw i32 %14, 1
br label %17
16: ; preds = %9
br label %17
17: ; preds = %16, %13
%18 = phi i32 [ %15, %13 ], [ 0, %16 ]
store i32 %18, i32* %1
br label %39
19: ; preds = %4
%20 = load i8, i8* %3
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 (...) @nextline()
br label %41
25: ; preds = %19
%26 = load i8, i8* %3
%27 = call i32 @terminal_putchar(i8 signext %26)
%28 = load i32, i32* %1
%29 = icmp slt i32 %28, 1048576
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = load i8, i8* %3
%32 = load i32, i32* %1
%33 = add nsw i32 %32, 1
store i32 %33, i32* %1
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* @kscan.buffer, i64 0, i64 %34
store i8 %31, i8* %35
br label %37
36: ; preds = %25
br label %41
37: ; preds = %30
br label %38
38: ; preds = %37
br label %39
39: ; preds = %38, %17
br label %40
40: ; preds = %39
br i1 true, label %4, label %41
41: ; preds = %40, %36, %23
%42 = load i32, i32* %1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* @kscan.buffer, i64 0, i64 %43
store i8 0, i8* %44
ret i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @kscan.buffer, i64 0, i64 0)
}

LLVM IR (-O2):
@kscan.buffer = internal global [1048576 x i8] zeroinitializer
define i8* @kscan() {
br label %1
1: ; preds = %9, %0
%2 = phi i32 [ 0, %0 ], [ %10, %9 ]
%3 = tail call signext i8 (...) @getcode()
switch i8 %3, label %13 [
i8 8, label %4
i8 10, label %11
]
4: ; preds = %1
%5 = tail call i32 (...) @backspace()
%6 = icmp sgt i32 %2, 0
%7 = add nsw i32 %2, -1
%8 = select i1 %6, i32 %7, i32 0
br label %9
9: ; preds = %4, %16
%10 = phi i32 [ %8, %4 ], [ %17, %16 ]
br label %1
11: ; preds = %1
%12 = tail call i32 (...) @nextline()
br label %20
13: ; preds = %1
%14 = tail call i32 @terminal_putchar(i8 signext %3)
%15 = icmp slt i32 %2, 1048576
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = add nsw i32 %2, 1
%18 = sext i32 %2 to i64
%19 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* @kscan.buffer, i64 0, i64 %18
store i8 %3, i8* %19
br label %9
20: ; preds = %13, %11
%21 = sext i32 %2 to i64
%22 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* @kscan.buffer, i64 0, i64 %21
store i8 0, i8* %22
ret i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @kscan.buffer, i64 0, i64 0)
}