#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*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
bool can_read_file_descriptor(int fd) {
  fd_set read_fds;
  int result;
  struct timeval timeout;
  do {
    FD_ZERO(&read_fds);
    FD_SET(fd, &read_fds);
    timeout.tv_sec = 0;
    timeout.tv_usec = 0;
    result = select(fd + 1, &read_fds, NULL, NULL, &timeout);
  } while (result < 0 && errno == EINTR);
  return result != 0;
}


x86 assembly (clang, -O0):
<can_read_file_descriptor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1050 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x21(%rbp)
jge    11a6 <can_read_file_descriptor+0x76>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    113b <can_read_file_descriptor+0xb>
cmpl   $0x0,-0xc(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<can_read_file_descriptor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r14d
lea    0x1(%r14),%ebp
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%r15
mov    0x2e8a(%rip),%r12
mov    0x2e7b(%rip),%r13
nopl   (%rax)
mov    %rbx,%rdi
call   1030 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1050 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1040 <select@plt>
test   %eax,%eax
jns    119a <can_read_file_descriptor+0x6a>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     1160 <can_read_file_descriptor+0x30>
xor    %ecx,%ecx
test   %eax,%eax
setne  %cl
mov    %ecx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @can_read_file_descriptor(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
store i32 %0, i32* %2
br label %6
6: ; preds = %22, %1
%7 = call i32 @FD_ZERO(i32* %3)
%8 = load i32, i32* %2
%9 = call i32 @FD_SET(i32 %8, i32* %3)
%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 = load i32, i32* %2
%13 = add nsw i32 %12, 1
%14 = call i32 @select(i32 %13, i32* %3, i32* null, i32* null, %struct.timeval* %5)
store i32 %14, i32* %4
br label %15
15: ; preds = %6
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br label %22
22: ; preds = %18, %15
%23 = phi i1 [ false, %15 ], [ %21, %18 ]
br i1 %23, label %6, label %24
24: ; preds = %22
%25 = load i32, i32* %4
%26 = icmp ne i32 %25, 0
%27 = zext i1 %26 to i32
ret i32 %27
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @can_read_file_descriptor(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = add nsw i32 %0, 1
%7 = bitcast %struct.timeval* %3 to i8*
br label %8
8: ; preds = %8, %1
%9 = call i32 @FD_ZERO(i32* %2)
%10 = call i32 @FD_SET(i32 %0, i32* %2)
%11 = call i32 @select(i32 %6, i32* %2, i32* null, i32* null, %struct.timeval* %3)
%12 = icmp slt i32 %11, 0
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
%16 = select i1 %12, i1 %15, i1 false
br i1 %16, label %8, label %17
17: ; preds = %8
%18 = icmp ne i32 %11, 0
%19 = zext i1 %18 to i32
ret i32 %19
}