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


x86 assembly (clang, -O0):
<cr_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1050 <FD_SET@plt>
cmpl   $0x1,-0x10(%rbp)
jne    11a4 <cr_select+0x74>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x4(%rbp)
jmp    11c1 <cr_select+0x91>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x1c(%rbp),%rdx
lea    -0x18(%rbp),%r8
mov    %rcx,%rsi
call   1040 <select@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cr_select>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %edi,%ebx
movslq %esi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0xc(%rsp)
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_SET@plt>
add    $0x1,%ebx
cmp    $0x1,%r14d
jne    119a <cr_select+0x6a>
lea    0x4(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %ebx,%edi
xor    %edx,%edx
jmp    11a8 <cr_select+0x78>
lea    0x4(%rsp),%rdx
lea    0x8(%rsp),%r8
mov    %ebx,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1040 <select@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @cr_select(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %6
%11 = sdiv i32 %10, 1000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* %6
%14 = srem i32 %13, 1000
%15 = mul nsw i32 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @FD_ZERO(i32* %9)
%18 = load i32, i32* %5
%19 = call i32 @FD_SET(i32 %18, i32* %9)
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %26
22: ; preds = %3
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
%25 = call i32 @select(i32 %24, i32* %9, i32* null, i32* null, %struct.timeval* %8)
store i32 %25, i32* %4
br label %30
26: ; preds = %3
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
%29 = call i32 @select(i32 %28, i32* null, i32* %9, i32* null, %struct.timeval* %8)
store i32 %29, i32* %4
br label %30
30: ; preds = %26, %22
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @cr_select(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = sdiv i32 %1, 1000
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %8, i32* %9
%10 = srem i32 %1, 1000
%11 = mul nsw i32 %10, 1000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @FD_ZERO(i32* %5)
%14 = call i32 @FD_SET(i32 %0, i32* %5)
%15 = icmp eq i32 %2, 1
%16 = add nsw i32 %0, 1
br i1 %15, label %17, label %19
17: ; preds = %3
%18 = call i32 @select(i32 %16, i32* %5, i32* null, i32* null, %struct.timeval* %4)
br label %21
19: ; preds = %3
%20 = call i32 @select(i32 %16, i32* null, i32* %5, i32* null, %struct.timeval* %4)
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ]
ret i32 %22
}