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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timeval *);
int file_ready(int FileDes, int TimeoutMs) {
  fd_set set;
  struct timeval timeout;
  FD_ZERO(&set);
  FD_SET(FileDes, &set);
  if (TimeoutMs >= 0) {
    if (TimeoutMs < 100)
      TimeoutMs = 100;
    timeout.tv_sec = TimeoutMs / 1000;
    timeout.tv_usec = (TimeoutMs % 1000) * 1000;
  }
  return select(FD_SETSIZE, &set, NULL, NULL,
                (TimeoutMs >= 0) ? &timeout : NULL) > 0 &&
         FD_ISSET(FileDes, &set);
}


x86 assembly (clang, -O0):
<file_ready>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1060 <FD_SET@plt>
cmpl   $0x0,-0x8(%rbp)
jl     11a0 <file_ready+0x60>
cmpl   $0x64,-0x8(%rbp)
jge    117e <file_ready+0x3e>
movl   $0x64,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x14(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11c3 <file_ready+0x83>
lea    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11ce <file_ready+0x8e>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    11ce <file_ready+0x8e>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%r8
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
jle    120d <file_ready+0xcd>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_ready>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
test   %ebp,%ebp
js     1197 <file_ready+0x57>
cmp    $0x65,%ebp
mov    $0x64,%eax
cmovae %ebp,%eax
imul   $0x10624dd3,%rax,%rcx
shr    $0x26,%rcx
mov    %ecx,0x8(%rsp)
imul   $0x3e8,%ecx,%ecx
mov    %eax,%ebp
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0xc(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
test   %ebp,%ebp
lea    0x8(%rsp),%r8
cmovs  %rbx,%r8
lea    0x4(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %rax,%rax
jle    11d5 <file_ready+0x95>
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
call   1030 <FD_ISSET@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@FD_SETSIZE = global i32 0
define i32 @file_ready(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = call i32 @FD_ZERO(i32* %5)
%8 = load i32, i32* %3
%9 = call i32 @FD_SET(i32 %8, i32* %5)
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 100
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 100, i32* %4
br label %16
16: ; preds = %15, %12
%17 = load i32, i32* %4
%18 = sdiv i32 %17, 1000
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %4
%21 = srem i32 %20, 1000
%22 = mul nsw i32 %21, 1000
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i32 %22, i32* %23
br label %24
24: ; preds = %16, %2
%25 = load i32, i32* @FD_SETSIZE
%26 = load i32, i32* %4
%27 = icmp sge i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
br label %30
29: ; preds = %24
br label %30
30: ; preds = %29, %28
%31 = phi %struct.timeval* [ %6, %28 ], [ null, %29 ]
%32 = call i64 @select(i32 %25, i32* %5, i32* null, i32* null, %struct.timeval* %31)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* %3
%36 = call i64 @FD_ISSET(i32 %35, i32* %5)
%37 = icmp ne i64 %36, 0
br label %38
38: ; preds = %34, %30
%39 = phi i1 [ false, %30 ], [ %37, %34 ]
%40 = zext i1 %39 to i32
ret i32 %40
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@FD_SETSIZE = global i32 0
define i32 @file_ready(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = call i32 @FD_ZERO(i32* %3)
%8 = call i32 @FD_SET(i32 %0, i32* %3)
%9 = icmp sgt i32 %1, -1
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = icmp ugt i32 %1, 100
%12 = select i1 %11, i32 %1, i32 100
%13 = udiv i32 %12, 1000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %13, i32* %14
%15 = urem i32 %12, 1000
%16 = mul nuw nsw i32 %15, 1000
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %16, i32* %17
br label %18
18: ; preds = %10, %2
%19 = phi i32 [ %12, %10 ], [ %1, %2 ]
%20 = load i32, i32* @FD_SETSIZE
%21 = icmp sgt i32 %19, -1
%22 = select i1 %21, %struct.timeval* %4, %struct.timeval* null
%23 = call i64 @select(i32 %20, i32* %3, i32* null, i32* null, %struct.timeval* %22)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = call i64 @FD_ISSET(i32 %0, i32* %3)
%27 = icmp ne i64 %26, 0
%28 = zext i1 %27 to i32
br label %29
29: ; preds = %25, %18
%30 = phi i32 [ 0, %18 ], [ %28, %25 ]
ret i32 %30
}