#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 {
  long tv_sec;
  long tv_usec;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
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_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int mc_io_readable(int fd, unsigned int timedout) {
  struct timeval tv = {0, 0};
  fd_set fds;
  fd_set fd_error;
  int retval;
  FD_ZERO(&fds);
  FD_ZERO(&fd_error);
  FD_SET(fd, &fds);
  FD_SET(fd, &fd_error);
  if (timedout > 0) {
    tv.tv_sec = (long)(timedout * 0.001);
    tv.tv_usec = (long)((timedout % 1000) * 1000);
  }
  retval = select((int)(fd + 1), &fds, NULL, &fd_error, &tv);
  if (retval < 0) {
    return (-1);
  }
  if (retval > 0) {
    if (FD_ISSET(fd, &fd_error)) {
      return (-1);
    }
    if (FD_ISSET(fd, &fds)) {
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<mc_io_readable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
lea    -0x2c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
lea    -0x30(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <FD_SET@plt>
cmpl   $0x0,-0xc(%rbp)
jbe    11d7 <mc_io_readable+0x87>
mov    -0xc(%rbp),%eax
cvtsi2sd %rax,%xmm0
movsd  0xe4e(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,%eax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x30(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1060 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    120b <mc_io_readable+0xbb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <mc_io_readable+0x115>
cmpl   $0x0,-0x34(%rbp)
jle    125e <mc_io_readable+0x10e>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1237 <mc_io_readable+0xe7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <mc_io_readable+0x115>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1259 <mc_io_readable+0x109>
movl   $0x1,-0x4(%rbp)
jmp    1265 <mc_io_readable+0x115>
jmp    125e <mc_io_readable+0x10e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mc_io_readable>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
xorpd  %xmm0,%xmm0
movapd %xmm0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
test   %ebp,%ebp
je     11cd <mc_io_readable+0x8d>
mov    %ebp,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  0xe5b(%rip),%xmm0
cvttsd2si %xmm0,%rcx
mov    %rcx,0x10(%rsp)
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%eax
sub    %eax,%ebp
imul   $0x3e8,%ebp,%eax
mov    %rax,0x18(%rsp)
lea    0x1(%rbx),%edi
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r8
xor    %edx,%edx
call   1050 <select@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     121a <mc_io_readable+0xda>
je     1218 <mc_io_readable+0xd8>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
jne    121a <mc_io_readable+0xda>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    121a <mc_io_readable+0xda>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64, i32, i32 }
define i32 @mc_io_readable(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = bitcast %struct.timeval* %6 to i8*
%11 = call i32 @FD_ZERO(i32* %7)
%12 = call i32 @FD_ZERO(i32* %8)
%13 = load i32, i32* %4
%14 = call i32 @FD_SET(i32 %13, i32* %7)
%15 = load i32, i32* %4
%16 = call i32 @FD_SET(i32 %15, i32* %8)
%17 = load i32, i32* %5
%18 = icmp ugt i32 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %2
%20 = load i32, i32* %5
%21 = uitofp i32 %20 to double
%22 = fmul double %21, 1.000000e-03
%23 = fptosi double %22 to i64
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i64 %23, i64* %24
%25 = load i32, i32* %5
%26 = urem i32 %25, 1000
%27 = mul i32 %26, 1000
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 %28, i64* %29
br label %30
30: ; preds = %19, %2
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
%33 = call i32 @select(i32 %32, i32* %7, i32* null, i32* %8, %struct.timeval* %6)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 -1, i32* %3
br label %52
37: ; preds = %30
%38 = load i32, i32* %9
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %51
40: ; preds = %37
%41 = load i32, i32* %4
%42 = call i64 @FD_ISSET(i32 %41, i32* %8)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 -1, i32* %3
br label %52
45: ; preds = %40
%46 = load i32, i32* %4
%47 = call i64 @FD_ISSET(i32 %46, i32* %7)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 1, i32* %3
br label %52
50: ; preds = %45
br label %51
51: ; preds = %50, %37
store i32 0, i32* %3
br label %52
52: ; preds = %51, %49, %44, %36
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64, i32, i32 }
define i32 @mc_io_readable(i32 %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.timeval* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = call i32 @FD_ZERO(i32* %4)
%10 = call i32 @FD_ZERO(i32* %5)
%11 = call i32 @FD_SET(i32 %0, i32* %4)
%12 = call i32 @FD_SET(i32 %0, i32* %5)
%13 = icmp eq i32 %1, 0
br i1 %13, label %23, label %14
14: ; preds = %2
%15 = uitofp i32 %1 to double
%16 = fmul double %15, 1.000000e-03
%17 = fptosi double %16 to i64
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i64 %17, i64* %18
%19 = urem i32 %1, 1000
%20 = mul nuw nsw i32 %19, 1000
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 %21, i64* %22
br label %23
23: ; preds = %14, %2
%24 = add nsw i32 %0, 1
%25 = call i32 @select(i32 %24, i32* %4, i32* null, i32* %5, %struct.timeval* %3)
%26 = icmp slt i32 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %23
%28 = icmp eq i32 %25, 0
br i1 %28, label %35, label %29
29: ; preds = %27
%30 = call i64 @FD_ISSET(i32 %0, i32* %5)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = call i64 @FD_ISSET(i32 %0, i32* %4)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32, %27
br label %36
36: ; preds = %32, %29, %23, %35
%37 = phi i32 [ 0, %35 ], [ -1, %23 ], [ -1, %29 ], [ 1, %32 ]
ret i32 %37
}