#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpush(char);
int read(int /*<<< orphan*/, char *, int);
void keyq() {
  char c = 0;
  int r = 0;
  int temp = fcntl(0, F_GETFL, NULL);
  fcntl(0, F_SETFL, O_NONBLOCK);
  r = read(0, &c, 1);
  fcntl(0, F_SETFL, temp);
  if (r < 1)
    mpush(0xffff);
  else {
    if (c == '\n')
      c = '\r';
    if (c == 0x7f)
      c = 0x8;
    mpush(c);
  }
}


x86 assembly (clang, -O0):
<keyq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movb   $0x0,-0x1(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    %edx,%edi
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
xor    %edi,%edi
lea    -0x1(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
cmpl   $0x1,-0x8(%rbp)
jge    11b0 <keyq+0x80>
mov    $0xffffffff,%edi
call   1050 <mpush@plt>
jmp    11db <keyq+0xab>
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
jne    11c1 <keyq+0x91>
movb   $0xd,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0x7f,%eax
jne    11d2 <keyq+0xa2>
movb   $0x8,-0x1(%rbp)
movsbl -0x1(%rbp),%edi
call   1050 <mpush@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keyq>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xf(%rsp)
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebx
mov    0x2e80(%rip),%r14
mov    (%r14),%esi
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
lea    0xf(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    (%r14),%esi
xor    %edi,%edi
mov    %ebx,%edx
call   1030 <fcntl@plt>
test   %ebp,%ebp
jle    11aa <keyq+0x7a>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
mov    $0xd,%ecx
cmovne %eax,%ecx
cmp    $0x7f,%cl
mov    $0x8,%eax
cmovne %ecx,%eax
jmp    11ac <keyq+0x7c>
mov    $0xff,%al
movsbl %al,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <mpush@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define void @keyq() {
%1 = alloca i8
%2 = alloca i32
%3 = alloca i32
store i8 0, i8* %1
store i32 0, i32* %2
%4 = load i32, i32* @F_GETFL
%5 = call i32 @fcntl(i32 0, i32 %4, i32 0)
store i32 %5, i32* %3
%6 = load i32, i32* @F_SETFL
%7 = load i32, i32* @O_NONBLOCK
%8 = call i32 @fcntl(i32 0, i32 %6, i32 %7)
%9 = call i32 @read(i32 0, i8* %1, i32 1)
store i32 %9, i32* %2
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* %3
%12 = call i32 @fcntl(i32 0, i32 %10, i32 %11)
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @mpush(i8 signext -1)
br label %30
17: ; preds = %0
%18 = load i8, i8* %1
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 10
br i1 %20, label %21, label %22
21: ; preds = %17
store i8 13, i8* %1
br label %22
22: ; preds = %21, %17
%23 = load i8, i8* %1
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 127
br i1 %25, label %26, label %27
26: ; preds = %22
store i8 8, i8* %1
br label %27
27: ; preds = %26, %22
%28 = load i8, i8* %1
%29 = call i32 @mpush(i8 signext %28)
br label %30
30: ; preds = %27, %15
ret void
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define void @keyq() {
%1 = alloca i8
store i8 0, i8* %1
%2 = load i32, i32* @F_GETFL
%3 = tail call i32 @fcntl(i32 0, i32 %2, i32 0)
%4 = load i32, i32* @F_SETFL
%5 = load i32, i32* @O_NONBLOCK
%6 = tail call i32 @fcntl(i32 0, i32 %4, i32 %5)
%7 = call i32 @read(i32 0, i8* %1, i32 1)
%8 = load i32, i32* @F_SETFL
%9 = tail call i32 @fcntl(i32 0, i32 %8, i32 %3)
%10 = icmp slt i32 %7, 1
br i1 %10, label %17, label %11
11: ; preds = %0
%12 = load i8, i8* %1
%13 = icmp eq i8 %12, 10
%14 = select i1 %13, i8 13, i8 %12
%15 = icmp eq i8 %14, 127
%16 = select i1 %15, i8 8, i8 %14
br label %17
17: ; preds = %0, %11
%18 = phi i8 [ %16, %11 ], [ -1, %0 ]
%19 = tail call i32 @mpush(i8 signext %18)
ret void
}