#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 */
typedef int time_t;
typedef char chtype;

/* Variables and functions */
char ERR;
int /*<<< orphan*/ FALSE;
int /*<<< orphan*/ TRUE;
char getch();
int /*<<< orphan*/ nodelay(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdscr;
int time(int *);
int WaitForUser() {
  time_t t;
  chtype ch;
  nodelay(stdscr, TRUE);
  t = time((time_t *)0);
  while (1) {
    if ((ch = getch()) != ERR) {
      if (ch == '\033') {
        nodelay(stdscr, FALSE);
        return ch;
      } else {
        nodelay(stdscr, FALSE);
        return 0;
      }
    }
    if (time((time_t *)0) - t > 5) {
      nodelay(stdscr, FALSE);
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<WaitForUser>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
call   1040 <nodelay@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1030 <time@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    %al,-0x9(%rbp)
movsbl %al,%eax
mov    0x2e79(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11cd <WaitForUser+0x9d>
movsbl -0x9(%rbp),%eax
cmp    $0x1b,%eax
jne    11aa <WaitForUser+0x7a>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1040 <nodelay@plt>
movsbl -0x9(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120a <WaitForUser+0xda>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1040 <nodelay@plt>
movl   $0x0,-0x4(%rbp)
jmp    120a <WaitForUser+0xda>
xor    %eax,%eax
mov    %eax,%edi
call   1030 <time@plt>
sub    -0x8(%rbp),%eax
cmp    $0x5,%eax
jle    1205 <WaitForUser+0xd5>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1040 <nodelay@plt>
movl   $0x0,-0x4(%rbp)
jmp    120a <WaitForUser+0xda>
jmp    115b <WaitForUser+0x2b>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WaitForUser>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2ea2(%rip),%r14
mov    (%r14),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
call   1040 <nodelay@plt>
xor    %edi,%edi
call   1030 <time@plt>
mov    %eax,%ebp
mov    0x2e89(%rip),%r15
nop
xor    %eax,%eax
call   1050 <getch@plt>
cmp    (%r15),%al
jne    118f <WaitForUser+0x5f>
xor    %edi,%edi
call   1030 <time@plt>
sub    %ebp,%eax
cmp    $0x6,%eax
jl     1160 <WaitForUser+0x30>
mov    (%r14),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
call   1040 <nodelay@plt>
xor    %eax,%eax
jmp    11af <WaitForUser+0x7f>
mov    %eax,%ebx
mov    (%r14),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
call   1040 <nodelay@plt>
xor    %ecx,%ecx
cmp    $0x1b,%bl
mov    $0x1b,%eax
cmovne %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdscr = global i32 0
@TRUE = global i32 0
@ERR = global i8 0
@FALSE = global i32 0
define i32 @WaitForUser() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = load i32, i32* @stdscr
%5 = load i32, i32* @TRUE
%6 = call i32 @nodelay(i32 %4, i32 %5)
%7 = call i32 @time(i32* null)
store i32 %7, i32* %2
br label %8
8: ; preds = %0, %37
%9 = call signext i8 (...) @getch()
store i8 %9, i8* %3
%10 = sext i8 %9 to i32
%11 = load i8, i8* @ERR
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %10, %12
br i1 %13, label %14, label %28
14: ; preds = %8
%15 = load i8, i8* %3
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 27
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i32, i32* @stdscr
%20 = load i32, i32* @FALSE
%21 = call i32 @nodelay(i32 %19, i32 %20)
%22 = load i8, i8* %3
%23 = sext i8 %22 to i32
store i32 %23, i32* %1
br label %38
24: ; preds = %14
%25 = load i32, i32* @stdscr
%26 = load i32, i32* @FALSE
%27 = call i32 @nodelay(i32 %25, i32 %26)
store i32 0, i32* %1
br label %38
28: ; preds = %8
%29 = call i32 @time(i32* null)
%30 = load i32, i32* %2
%31 = sub nsw i32 %29, %30
%32 = icmp sgt i32 %31, 5
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i32, i32* @stdscr
%35 = load i32, i32* @FALSE
%36 = call i32 @nodelay(i32 %34, i32 %35)
store i32 0, i32* %1
br label %38
37: ; preds = %28
br label %8
38: ; preds = %33, %24, %18
%39 = load i32, i32* %1
ret i32 %39
}

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