#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 readchr();
int readescseq(char *);
int strlen(char *);
int kb_readkey(unsigned char *dest) {
  int c = readchr();
  if (!c)
    return 0;
  if (c == '\033')
    switch (c = readescseq((char *)dest + 1)) {
    case -1:
      return 0;
    case 0:
      dest[0] = '\033';
      return strlen((char *)dest);
    }
  else if (c == 0x7F)
    c = '\b';
  *dest = c;
  return 1;
}


x86 assembly (clang, -O0):
<kb_readkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <readchr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    115c <kb_readkey+0x2c>
movl   $0x0,-0x4(%rbp)
jmp    11ea <kb_readkey+0xba>
cmpl   $0x1b,-0x14(%rbp)
jne    11c2 <kb_readkey+0x92>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1050 <readescseq@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
sub    $0xffffffff,%eax
je     1199 <kb_readkey+0x69>
jmp    1189 <kb_readkey+0x59>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11a5 <kb_readkey+0x75>
jmp    11bd <kb_readkey+0x8d>
movl   $0x0,-0x4(%rbp)
jmp    11ea <kb_readkey+0xba>
mov    -0x10(%rbp),%rax
movb   $0x1b,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x4(%rbp)
jmp    11ea <kb_readkey+0xba>
jmp    11d8 <kb_readkey+0xa8>
cmpl   $0x7f,-0x14(%rbp)
jne    11d3 <kb_readkey+0xa3>
movl   $0x8,-0x14(%rbp)
jmp    11d8 <kb_readkey+0xa8>
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kb_readkey>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <readchr@plt>
test   %eax,%eax
je     1176 <kb_readkey+0x46>
cmp    $0x1b,%eax
jne    1164 <kb_readkey+0x34>
lea    0x1(%rbx),%rdi
call   1050 <readescseq@plt>
cmp    $0xffffffff,%eax
je     1178 <kb_readkey+0x48>
mov    %eax,%ecx
test   %eax,%eax
jne    116f <kb_readkey+0x3f>
movb   $0x1b,(%rbx)
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <strlen@plt>
cmp    $0x7f,%eax
mov    $0x8,%ecx
cmovne %eax,%ecx
mov    %cl,(%rbx)
mov    $0x1,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @kb_readkey(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = call i32 (...) @readchr()
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp ne i32 %6, 0
br i1 %7, label %9, label %8
8: ; preds = %1
store i32 0, i32* %2
br label %32
9: ; preds = %1
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 27
br i1 %11, label %12, label %23
12: ; preds = %9
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = call i32 @readescseq(i8* %14)
store i32 %15, i32* %4
switch i32 %15, label %22 [
i32 -1, label %16
i32 0, label %17
]
16: ; preds = %12
store i32 0, i32* %2
br label %32
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 27, i8* %19
%20 = load i8*, i8** %3
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %2
br label %32
22: ; preds = %12
br label %28
23: ; preds = %9
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 127
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 8, i32* %4
br label %27
27: ; preds = %26, %23
br label %28
28: ; preds = %27, %22
%29 = load i32, i32* %4
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %3
store i8 %30, i8* %31
store i32 1, i32* %2
br label %32
32: ; preds = %28, %17, %16, %8
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
define i32 @kb_readkey(i8* %0) {
%2 = tail call i32 (...) @readchr()
switch i32 %2, label %8 [
i32 0, label %14
i32 27, label %3
]
3: ; preds = %1
%4 = getelementptr inbounds i8, i8* %0, i64 1
%5 = tail call i32 @readescseq(i8* %4)
switch i32 %5, label %11 [
i32 -1, label %14
i32 0, label %6
]
6: ; preds = %3
store i8 27, i8* %0
%7 = tail call i32 @strlen(i8* %0)
br label %14
8: ; preds = %1
%9 = icmp eq i32 %2, 127
%10 = select i1 %9, i32 8, i32 %2
br label %11
11: ; preds = %3, %8
%12 = phi i32 [ %5, %3 ], [ %10, %8 ]
%13 = trunc i32 %12 to i8
store i8 %13, i8* %0
br label %14
14: ; preds = %3, %1, %11, %6
%15 = phi i32 [ 1, %11 ], [ %7, %6 ], [ %2, %1 ], [ 0, %3 ]
ret i32 %15
}