#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 ERR;
int /*<<< orphan*/ FALSE;
int /*<<< orphan*/ TRUE;
int getch();
int /*<<< orphan*/ nodelay(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdscr;
int ncurses_handle_input(void) {
  int ch;
  ch = getch();
  if (ch == 27) {
    nodelay(stdscr, TRUE);
    if (getch() == ERR) {
      nodelay(stdscr, FALSE);
      return (-1);
    }
    nodelay(stdscr, FALSE);
  } else if (ch == ERR)
    ch = 0;
  return (ch);
}


x86 assembly (clang, -O0):
<ncurses_handle_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <getch@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x1b,-0x8(%rbp)
jne    11a8 <ncurses_handle_input+0x88>
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
call   1030 <nodelay@plt>
mov    $0x0,%al
call   1040 <getch@plt>
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%eax
jne    118c <ncurses_handle_input+0x6c>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1030 <nodelay@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <ncurses_handle_input+0xac>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1030 <nodelay@plt>
jmp    11c6 <ncurses_handle_input+0xa6>
mov    -0x8(%rbp),%eax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%eax
jne    11c1 <ncurses_handle_input+0xa1>
movl   $0x0,-0x8(%rbp)
jmp    11c6 <ncurses_handle_input+0xa6>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ncurses_handle_input>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getch@plt>
cmp    $0x1b,%eax
jne    1175 <ncurses_handle_input+0x55>
mov    0x2eaa(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
call   1030 <nodelay@plt>
xor    %eax,%eax
call   1040 <getch@plt>
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%eax
mov    (%rbx),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffff,%eax
mov    $0x1b,%ebx
cmove  %eax,%ebx
call   1030 <nodelay@plt>
jmp    1181 <ncurses_handle_input+0x61>
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%eax
cmovne %eax,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stdscr = global i32 0
@TRUE = global i32 0
@ERR = global i32 0
@FALSE = global i32 0
define i32 @ncurses_handle_input() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @getch()
store i32 %3, i32* %2
%4 = load i32, i32* %2
%5 = icmp eq i32 %4, 27
br i1 %5, label %6, label %21
6: ; preds = %0
%7 = load i32, i32* @stdscr
%8 = load i32, i32* @TRUE
%9 = call i32 @nodelay(i32 %7, i32 %8)
%10 = call i32 (...) @getch()
%11 = load i32, i32* @ERR
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = load i32, i32* @stdscr
%15 = load i32, i32* @FALSE
%16 = call i32 @nodelay(i32 %14, i32 %15)
store i32 -1, i32* %1
br label %29
17: ; preds = %6
%18 = load i32, i32* @stdscr
%19 = load i32, i32* @FALSE
%20 = call i32 @nodelay(i32 %18, i32 %19)
br label %27
21: ; preds = %0
%22 = load i32, i32* %2
%23 = load i32, i32* @ERR
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %2
br label %26
26: ; preds = %25, %21
br label %27
27: ; preds = %26, %17
%28 = load i32, i32* %2
store i32 %28, i32* %1
br label %29
29: ; preds = %27, %13
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@stdscr = global i32 0
@TRUE = global i32 0
@ERR = global i32 0
@FALSE = global i32 0
define i32 @ncurses_handle_input() {
%1 = tail call i32 (...) @getch()
%2 = icmp eq i32 %1, 27
br i1 %2, label %3, label %14
3: ; preds = %0
%4 = load i32, i32* @stdscr
%5 = load i32, i32* @TRUE
%6 = tail call i32 @nodelay(i32 %4, i32 %5)
%7 = tail call i32 (...) @getch()
%8 = load i32, i32* @ERR
%9 = icmp eq i32 %7, %8
%10 = load i32, i32* @stdscr
%11 = load i32, i32* @FALSE
%12 = tail call i32 @nodelay(i32 %10, i32 %11)
%13 = select i1 %9, i32 -1, i32 27
br label %18
14: ; preds = %0
%15 = load i32, i32* @ERR
%16 = icmp eq i32 %1, %15
%17 = select i1 %16, i32 0, i32 %1
br label %18
18: ; preds = %3, %14
%19 = phi i32 [ %17, %14 ], [ %13, %3 ]
ret i32 %19
}