#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 */
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ STDIN_FILENO;
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int evsource_kb_has_event(void) {
  struct timeval tv;
  int retval;
  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(STDIN_FILENO, &rfds);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  retval = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv);
  return retval == 1;
}


x86 assembly (clang, -O0):
<evsource_kb_has_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x18(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    0x2e98(%rip),%rax
mov    (%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <FD_SET@plt>
movq   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evsource_kb_has_event>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    0x2e95(%rip),%rbx
mov    (%rbx),%rdi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    (%rbx),%rdi
add    $0x1,%rdi
lea    0x10(%rsp),%r8
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
xor    %ecx,%ecx
cmp    $0x1,%eax
sete   %cl
mov    %ecx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@STDIN_FILENO = global i64 0
define i32 @evsource_kb_has_event() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @FD_ZERO(i32* %3)
%5 = load i64, i64* @STDIN_FILENO
%6 = call i32 @FD_SET(i64 %5, i32* %3)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
store i64 0, i64* %8
%9 = load i64, i64* @STDIN_FILENO
%10 = add nsw i64 %9, 1
%11 = call i32 @select(i64 %10, i32* %3, i32* null, i32* null, %struct.timeval* %1)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 1
%14 = zext i1 %13 to i32
ret i32 %14
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@STDIN_FILENO = global i64 0
define i32 @evsource_kb_has_event() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i32 @FD_ZERO(i32* %2)
%6 = load i64, i64* @STDIN_FILENO
%7 = call i32 @FD_SET(i64 %6, i32* %2)
%8 = bitcast %struct.timeval* %1 to i8*
%9 = load i64, i64* @STDIN_FILENO
%10 = add nsw i64 %9, 1
%11 = call i32 @select(i64 %10, i32* %2, i32* null, i32* null, %struct.timeval* %1)
%12 = icmp eq i32 %11, 1
%13 = zext i1 %12 to i32
ret i32 %13
}