#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_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ fcntl(int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ mpush(char);
int /*<<< orphan*/ printf(char *, int);
int read(int /*<<< orphan*/, char *, int);
void keyq() {
  char c = 0;
  int r = 0;
  fcntl(0, F_SETFL, O_NONBLOCK);
  r = read(0, &c, 1);
  printf("%d ", r);
  if (r < 0)
    mpush(-1);
  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    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
xor    %edi,%edi
lea    -0x1(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jge    11a7 <keyq+0x67>
mov    $0xffffffff,%edi
call   1060 <mpush@plt>
jmp    11d2 <keyq+0x92>
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
jne    11b8 <keyq+0x78>
movb   $0xd,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0x7f,%eax
jne    11c9 <keyq+0x89>
movb   $0x8,-0x1(%rbp)
movsbl -0x1(%rbp),%edi
call   1060 <mpush@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keyq>:
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xf(%rsp)
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    0x2e8e(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
lea    0xf(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,%ebx
lea    0xe83(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebx,%ebx
js     11a4 <keyq+0x64>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
mov    $0xd,%ecx
cmovne %eax,%ecx
cmp    $0x7f,%cl
mov    $0x8,%eax
cmovne %ecx,%eax
jmp    11a6 <keyq+0x66>
mov    $0xff,%al
movsbl %al,%edi
add    $0x10,%rsp
pop    %rbx
jmp    1060 <mpush@plt>

LLVM IR (-O0):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [4 x i8] c"%d \00"
define void @keyq() {
%1 = alloca i8
%2 = alloca i32
store i8 0, i8* %1
store i32 0, i32* %2
%3 = load i32, i32* @F_SETFL
%4 = load i32, i32* @O_NONBLOCK
%5 = call i32 @fcntl(i32 0, i32 %3, i32 %4)
%6 = call i32 @read(i32 0, i8* %1, i32 1)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @mpush(i8 signext -1)
br label %26
13: ; preds = %0
%14 = load i8, i8* %1
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 10
br i1 %16, label %17, label %18
17: ; preds = %13
store i8 13, i8* %1
br label %18
18: ; preds = %17, %13
%19 = load i8, i8* %1
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 127
br i1 %21, label %22, label %23
22: ; preds = %18
store i8 8, i8* %1
br label %23
23: ; preds = %22, %18
%24 = load i8, i8* %1
%25 = call i32 @mpush(i8 signext %24)
br label %26
26: ; preds = %23, %11
ret void
}

LLVM IR (-O2):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [4 x i8] c"%d \00"
define void @keyq() {
%1 = alloca i8
store i8 0, i8* %1
%2 = load i32, i32* @F_SETFL
%3 = load i32, i32* @O_NONBLOCK
%4 = tail call i32 @fcntl(i32 0, i32 %2, i32 %3)
%5 = call i32 @read(i32 0, i8* %1, i32 1)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %5, 0
br i1 %7, label %14, label %8
8: ; preds = %0
%9 = load i8, i8* %1
%10 = icmp eq i8 %9, 10
%11 = select i1 %10, i8 13, i8 %9
%12 = icmp eq i8 %11, 127
%13 = select i1 %12, i8 8, i8 %11
br label %14
14: ; preds = %0, %8
%15 = phi i8 [ %13, %8 ], [ -1, %0 ]
%16 = tail call i32 @mpush(i8 signext %15)
ret void
}