#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 getch();
int /*<<< orphan*/ stdscr;
int translate_key(int);
int /*<<< orphan*/ wtimeout(int /*<<< orphan*/, int);
size_t term_xgetkey(int mode, size_t timeout) {
  size_t key;
  for (;;) {
    int c;
    if (mode & 0001)
      wtimeout(stdscr, (int)timeout * 100);
    c = getch();
    if (mode & 0001)
      wtimeout(stdscr, -1);
    if (mode & 0002)
      key = (size_t)c;
    else {
      key = translate_key(c);
      while (key == 02000)
        key = translate_key(getch()) | 02000;
    }
    break;
  }
  return key;
}


x86 assembly (clang, -O0):
<term_xgetkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1163 <term_xgetkey+0x33>
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
imul   $0x64,%eax,%esi
call   1030 <wtimeout@plt>
mov    $0x0,%al
call   1050 <getch@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     118f <term_xgetkey+0x5f>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    $0xffffffff,%esi
call   1030 <wtimeout@plt>
mov    -0x4(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11ab <term_xgetkey+0x7b>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11ea <term_xgetkey+0xba>
mov    -0x1c(%rbp),%edi
call   1040 <translate_key@plt>
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0x400,-0x18(%rbp)
jne    11e5 <term_xgetkey+0xb5>
mov    $0x0,%al
call   1050 <getch@plt>
mov    %eax,%edi
call   1040 <translate_key@plt>
or     $0x400,%eax
cltq
mov    %rax,-0x18(%rbp)
jmp    11b9 <term_xgetkey+0x89>
jmp    11ea <term_xgetkey+0xba>
jmp    11ef <term_xgetkey+0xbf>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_xgetkey>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
test   $0x1,%bl
jne    114b <term_xgetkey+0x1b>
xor    %eax,%eax
call   1050 <getch@plt>
mov    %eax,%ebp
test   $0x2,%bl
je     1178 <term_xgetkey+0x48>
jmp    11ae <term_xgetkey+0x7e>
mov    0x2e96(%rip),%r14
mov    (%r14),%edi
imul   $0x64,%esi,%esi
call   1030 <wtimeout@plt>
xor    %eax,%eax
call   1050 <getch@plt>
mov    %eax,%ebp
mov    (%r14),%edi
mov    $0xffffffff,%esi
call   1030 <wtimeout@plt>
test   $0x2,%bl
jne    11ae <term_xgetkey+0x7e>
mov    %ebp,%edi
call   1040 <translate_key@plt>
mov    %eax,%ebp
cmp    $0x400,%eax
jne    11ae <term_xgetkey+0x7e>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <getch@plt>
mov    %eax,%edi
call   1040 <translate_key@plt>
mov    %eax,%ebp
or     $0x400,%ebp
cmp    $0x400,%ebp
je     1190 <term_xgetkey+0x60>
movslq %ebp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdscr = global i32 0
define i64 @term_xgetkey(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
br label %7
7: ; preds = %2
%8 = load i32, i32* %3
%9 = and i32 %8, 1
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = load i32, i32* @stdscr
%13 = load i64, i64* %4
%14 = trunc i64 %13 to i32
%15 = mul nsw i32 %14, 100
%16 = call i32 @wtimeout(i32 %12, i32 %15)
br label %17
17: ; preds = %11, %7
%18 = call i32 (...) @getch()
store i32 %18, i32* %6
%19 = load i32, i32* %3
%20 = and i32 %19, 1
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @stdscr
%24 = call i32 @wtimeout(i32 %23, i32 -1)
br label %25
25: ; preds = %22, %17
%26 = load i32, i32* %3
%27 = and i32 %26, 2
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
store i64 %31, i64* %5
br label %45
32: ; preds = %25
%33 = load i32, i32* %6
%34 = call i32 @translate_key(i32 %33)
%35 = sext i32 %34 to i64
store i64 %35, i64* %5
br label %36
36: ; preds = %39, %32
%37 = load i64, i64* %5
%38 = icmp eq i64 %37, 1024
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = call i32 (...) @getch()
%41 = call i32 @translate_key(i32 %40)
%42 = or i32 %41, 1024
%43 = sext i32 %42 to i64
store i64 %43, i64* %5
br label %36
44: ; preds = %36
br label %45
45: ; preds = %44, %29
br label %46
46: ; preds = %45
%47 = load i64, i64* %5
ret i64 %47
}

LLVM IR (-O2):
@stdscr = global i32 0
define i64 @term_xgetkey(i32 %0, i64 %1) {
%3 = and i32 %0, 1
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @getch()
br label %15
7: ; preds = %2
%8 = load i32, i32* @stdscr
%9 = trunc i64 %1 to i32
%10 = mul nsw i32 %9, 100
%11 = tail call i32 @wtimeout(i32 %8, i32 %10)
%12 = tail call i32 (...) @getch()
%13 = load i32, i32* @stdscr
%14 = tail call i32 @wtimeout(i32 %13, i32 -1)
br label %15
15: ; preds = %5, %7
%16 = phi i32 [ %6, %5 ], [ %12, %7 ]
%17 = and i32 %0, 2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = tail call i32 @translate_key(i32 %16)
%21 = icmp eq i32 %20, 1024
br i1 %21, label %22, label %27
22: ; preds = %19, %22
%23 = tail call i32 (...) @getch()
%24 = tail call i32 @translate_key(i32 %23)
%25 = or i32 %24, 1024
%26 = icmp eq i32 %25, 1024
br i1 %26, label %22, label %27
27: ; preds = %22, %19, %15
%28 = phi i32 [ %16, %15 ], [ %20, %19 ], [ %25, %22 ]
%29 = sext i32 %28 to i64
ret i64 %29
}