#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(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int hasinput(int fd) {
  fd_set fds;
  struct timeval timeout;
  if (fd < 0)
    return 0;
  timeout.tv_sec = 0;
  timeout.tv_usec = 0;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  return select(fd + 1, &fds, NULL, NULL, &timeout) > 0;
}


x86 assembly (clang, -O0):
<hasinput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1151 <hasinput+0x21>
movl   $0x0,-0x4(%rbp)
jmp    119f <hasinput+0x6f>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
cmp    $0x0,%rax
setg   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hasinput>:
test   %edi,%edi
js     1185 <hasinput+0x55>
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
add    $0x1,%ebx
lea    0x10(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setg   %al
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @hasinput(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %20
9: ; preds = %1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i64 0, i64* %11
%12 = call i32 @FD_ZERO(i32* %4)
%13 = load i32, i32* %3
%14 = call i32 @FD_SET(i32 %13, i32* %4)
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
%17 = call i64 @select(i32 %16, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%18 = icmp sgt i64 %17, 0
%19 = zext i1 %18 to i32
store i32 %19, i32* %2
br label %20
20: ; preds = %9, %8
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @hasinput(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = icmp slt i32 %0, 0
br i1 %6, label %15, label %7
7: ; preds = %1
%8 = bitcast %struct.timeval* %3 to i8*
%9 = call i32 @FD_ZERO(i32* %2)
%10 = call i32 @FD_SET(i32 %0, i32* %2)
%11 = add nuw nsw i32 %0, 1
%12 = call i64 @select(i32 %11, i32* %2, i32* null, i32* null, %struct.timeval* %3)
%13 = icmp sgt i64 %12, 0
%14 = zext i1 %13 to i32
br label %15
15: ; preds = %1, %7
%16 = phi i32 [ %14, %7 ], [ 0, %1 ]
ret i32 %16
}