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

/* Variables and functions */
scalar_t__ EINTR;
int ERR;
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int check_for_keystroke() {
  fd_set fds;
  int ret_val;
  struct timeval tv;
  FD_ZERO(&fds);
  FD_SET(0, &fds);
  tv.tv_sec = 0;
  tv.tv_usec = 5000;
  do {
    ret_val = select(1, &fds, NULL, NULL, &tv);
  } while ((ret_val < 0) && (errno == EINTR));
  if (FD_ISSET(0, &fds)) {
    return (1);
  } else {
    return (ERR);
  }
}


x86 assembly (clang, -O0):
<check_for_keystroke>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x8(%rbp),%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x1388,-0x20(%rbp)
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x21(%rbp)
jge    11b0 <check_for_keystroke+0x70>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    116b <check_for_keystroke+0x2b>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11dc <check_for_keystroke+0x9c>
movl   $0x1,-0x4(%rbp)
jmp    11e8 <check_for_keystroke+0xa8>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_for_keystroke>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
movl   $0x1388,0x8(%rsp)
lea    0x8(%rsp),%r12
mov    0x2e59(%rip),%r15
mov    0x2e4a(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1050 <select@plt>
test   %eax,%eax
jns    11b0 <check_for_keystroke+0x70>
mov    (%rbx),%rax
cmp    %rax,(%r15)
je     1190 <check_for_keystroke+0x50>
lea    0x4(%rsp),%rsi
xor    %edi,%edi
call   1030 <FD_ISSET@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11d2 <check_for_keystroke+0x92>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@ERR = global i32 0
define i32 @check_for_keystroke() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = call i32 @FD_ZERO(i32* %2)
%6 = call i32 @FD_SET(i32 0, i32* %2)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 5000, i32* %8
br label %9
9: ; preds = %18, %0
%10 = call i32 @select(i32 1, i32* %2, i32* null, i32* null, %struct.timeval* %4)
store i32 %10, i32* %3
br label %11
11: ; preds = %9
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br label %18
18: ; preds = %14, %11
%19 = phi i1 [ false, %11 ], [ %17, %14 ]
br i1 %19, label %9, label %20
20: ; preds = %18
%21 = call i64 @FD_ISSET(i32 0, i32* %2)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 1, i32* %1
br label %26
24: ; preds = %20
%25 = load i32, i32* @ERR
store i32 %25, i32* %1
br label %26
26: ; preds = %24, %23
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@ERR = global i32 0
define i32 @check_for_keystroke() {
%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 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 5000, i32* %8
br label %9
9: ; preds = %9, %0
%10 = call i32 @select(i32 1, i32* %1, i32* null, i32* null, %struct.timeval* %2)
%11 = icmp slt i32 %10, 0
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %9, label %16
16: ; preds = %9
%17 = call i64 @FD_ISSET(i32 0, i32* %1)
%18 = icmp eq i64 %17, 0
%19 = load i32, i32* @ERR
%20 = select i1 %18, i32 %19, i32 1
ret i32 %20
}