#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 */
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
scalar_t__ errno;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ handle_char(int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct timeval *);
int /*<<< orphan*/ term_getc();
void handle_key() {
  fd_set readset;
  int result;
  struct timeval timeout;
  FD_ZERO(&readset);
  FD_SET(0, &readset);
  gettimeofday(&timeout, 0);
  timeout.tv_sec = 0;
  timeout.tv_usec = 1000000 - timeout.tv_usec;
  result = select(1, &readset, 0, 0, &timeout);
  if (result == -1) {
    if (errno != EINTR)
      err(EX_OSERR, "select failed");
    return;
  }
  if (!FD_ISSET(0, &readset))
    return;
  handle_char(term_getc());
}


x86 assembly (clang, -O0):
<handle_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x4(%rbp),%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x4(%rbp),%rsi
call   10a0 <FD_SET@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
movq   $0x0,-0x10(%rbp)
mov    $0xf4240,%eax
sub    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x1,%edi
lea    -0x4(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x18(%rbp),%r8
mov    %ecx,%edx
call   1070 <select@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1213 <handle_key+0x93>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
je     120e <handle_key+0x8e>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1090 <err@plt>
jmp    123a <handle_key+0xba>
xor    %edi,%edi
lea    -0x4(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    122c <handle_key+0xac>
jmp    123a <handle_key+0xba>
mov    $0x0,%al
call   1050 <term_getc@plt>
mov    %eax,%edi
call   1080 <handle_char@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_key>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
movq   $0x0,0x10(%rsp)
mov    $0xf4240,%eax
sub    0x8(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1070 <select@plt>
cmp    $0xffffffff,%eax
je     11fe <handle_key+0x7e>
lea    0x4(%rsp),%rsi
xor    %edi,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
je     1229 <handle_key+0xa9>
xor    %eax,%eax
call   1050 <term_getc@plt>
mov    %eax,%edi
call   1080 <handle_char@plt>
jmp    1229 <handle_key+0xa9>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
je     1229 <handle_key+0xa9>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
call   1090 <err@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@EX_OSERR = global i32 0
@.str = [14 x i8] c"select failed\00"
define void @handle_key() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = call i32 @FD_ZERO(i32* %1)
%5 = call i32 @FD_SET(i32 0, i32* %1)
%6 = call i32 @gettimeofday(%struct.timeval* %3, i32 0)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%9 = load i64, i64* %8
%10 = sub nsw i64 1000000, %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i64 %10, i64* %11
%12 = call i32 @select(i32 1, i32* %1, i32 0, i32 0, %struct.timeval* %3)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %23
15: ; preds = %0
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @EX_OSERR
%21 = call i32 @err(i32 %20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %15
br label %30
23: ; preds = %0
%24 = call i32 @FD_ISSET(i32 0, i32* %1)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %23
br label %30
27: ; preds = %23
%28 = call i32 (...) @term_getc()
%29 = call i32 @handle_char(i32 %28)
br label %30
30: ; preds = %27, %26, %22
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@EX_OSERR = global i32 0
@.str = [14 x i8] c"select failed\00"
define void @handle_key() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = call i32 @FD_ZERO(i32* %1)
%6 = call i32 @FD_SET(i32 0, i32* %1)
%7 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%10 = load i64, i64* %9
%11 = sub nsw i64 1000000, %10
store i64 %11, i64* %9
%12 = call i32 @select(i32 1, i32* %1, i32 0, i32 0, %struct.timeval* %2)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %21
14: ; preds = %0
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %27, label %18
18: ; preds = %14
%19 = load i32, i32* @EX_OSERR
%20 = call i32 @err(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %27
21: ; preds = %0
%22 = call i32 @FD_ISSET(i32 0, i32* %1)
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = call i32 (...) @term_getc()
%26 = call i32 @handle_char(i32 %25)
br label %27
27: ; preds = %21, %14, %18, %24
ret void
}