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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ gettext(char *);
int read(int, char *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ set_error(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
ssize_t read_to(int fd, char *whereto, size_t len, double timeout) {
  for (;;) {
    ssize_t rc;
    struct timeval to;
    fd_set rfds;
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    to.tv_sec = (long)(timeout / 1000.0);
    to.tv_usec = (long)(timeout * 1000.0) % 1000000;
    rc = select(fd + 1, &rfds, NULL, NULL, &to);
    if (rc == 0)
      return -2;
    else if (rc == -1) {
      if (errno == EAGAIN)
        continue;
      if (errno == EINTR)
        return -3;
      set_error(gettext("myread::select failed: %s"), strerror(errno));
      return -1;
    }
    return read(fd, whereto, len);
  }
}


x86 assembly (clang, -O0):
<read_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
lea    -0x3c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1090 <FD_SET@plt>
movsd  -0x20(%rbp),%xmm0
movsd  0xe56(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x38(%rbp)
movsd  0xe41(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
cvttsd2si %xmm0,%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x30(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x3c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    120a <read_to+0x9a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12a5 <read_to+0x135>
cmpl   $0xffffffff,-0x24(%rbp)
jne    128d <read_to+0x11d>
mov    0x2dc5(%rip),%rax
mov    (%rax),%rax
mov    0x2da3(%rip),%rcx
cmp    (%rcx),%rax
jne    1233 <read_to+0xc3>
jmp    1188 <read_to+0x18>
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%rax
jne    1259 <read_to+0xe9>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12a5 <read_to+0x135>
lea    0xda8(%rip),%rdi
call   1030 <gettext@plt>
mov    %eax,-0x40(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %eax,%esi
call   1040 <set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <read_to+0x135>
jmp    1292 <read_to+0x122>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_to>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,(%rsp)
mov    %edi,%r12d
movsd  0xe6e(%rip),%xmm1
movapd %xmm0,%xmm2
divsd  %xmm1,%xmm2
cvttsd2si %xmm2,%r15
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rbx
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %rbx,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x12,%rdx
add    %rax,%rdx
imul   $0xf4240,%rdx,%rax
sub    %rax,%rbx
lea    0x1(%r12),%r13d
lea    0x14(%rsp),%rbp
lea    0x18(%rsp),%r14
nop
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
mov    %r15,0x18(%rsp)
mov    %rbx,0x20(%rsp)
mov    %r13d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1070 <select@plt>
cmp    $0xffffffff,%eax
jne    1268 <read_to+0xf8>
mov    0x2dc5(%rip),%rax
mov    (%rax),%rcx
mov    0x2da3(%rip),%rax
cmp    (%rax),%rcx
je     11e0 <read_to+0x70>
mov    0x2da7(%rip),%rdx
mov    $0xfffffffd,%eax
cmp    (%rdx),%rcx
je     1284 <read_to+0x114>
lea    0xdc6(%rip),%rdi
call   1030 <gettext@plt>
mov    %eax,%ebx
mov    0x2d90(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <set_error@plt>
mov    $0xffffffff,%eax
jmp    1284 <read_to+0x114>
test   %eax,%eax
jne    1273 <read_to+0x103>
mov    $0xfffffffe,%eax
jmp    1284 <read_to+0x114>
mov    %r12d,%edi
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
call   1050 <read@plt>
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
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [26 x i8] c"myread::select failed: %s\00"
define i32 @read_to(i32 %0, i8* %1, i64 %2, double %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca double
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store double %3, double* %9
br label %13
13: ; preds = %39, %4
%14 = call i32 @FD_ZERO(i32* %12)
%15 = load i32, i32* %6
%16 = call i32 @FD_SET(i32 %15, i32* %12)
%17 = load double, double* %9
%18 = fdiv double %17, 1.000000e+03
%19 = fptosi double %18 to i64
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i64 %19, i64* %20
%21 = load double, double* %9
%22 = fmul double %21, 1.000000e+03
%23 = fptosi double %22 to i64
%24 = srem i64 %23, 1000000
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 %24, i64* %25
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
%28 = call i32 @select(i32 %27, i32* %12, i32* null, i32* null, %struct.timeval* %11)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %13
store i32 -2, i32* %5
br label %56
32: ; preds = %13
%33 = load i32, i32* %10
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %50
35: ; preds = %32
%36 = load i64, i64* @errno
%37 = load i64, i64* @EAGAIN
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
br label %13
40: ; preds = %35
%41 = load i64, i64* @errno
%42 = load i64, i64* @EINTR
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 -3, i32* %5
br label %56
45: ; preds = %40
%46 = call i32 @gettext(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%47 = load i64, i64* @errno
%48 = call i32 @strerror(i64 %47)
%49 = call i32 @set_error(i32 %46, i32 %48)
store i32 -1, i32* %5
br label %56
50: ; preds = %32
br label %51
51: ; preds = %50
%52 = load i32, i32* %6
%53 = load i8*, i8** %7
%54 = load i64, i64* %8
%55 = call i32 @read(i32 %52, i8* %53, i64 %54)
store i32 %55, i32* %5
br label %56
56: ; preds = %51, %45, %44, %31
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [26 x i8] c"myread::select failed: %s\00"
define i32 @read_to(i32 %0, i8* nocapture %1, i64 %2, double %3) {
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = bitcast %struct.timeval* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = fdiv double %3, 1.000000e+03
%10 = fptosi double %9 to i64
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%12 = fmul double %3, 1.000000e+03
%13 = fptosi double %12 to i64
%14 = srem i64 %13, 1000000
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%16 = add nsw i32 %0, 1
br label %17
17: ; preds = %35, %4
%18 = call i32 @FD_ZERO(i32* %6)
%19 = call i32 @FD_SET(i32 %0, i32* %6)
store i64 %10, i64* %11
store i64 %14, i64* %15
%20 = call i32 @select(i32 %16, i32* %6, i32* null, i32* null, %struct.timeval* %5)
switch i32 %20, label %33 [
i32 0, label %36
i32 -1, label %21
]
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @EAGAIN
%24 = icmp eq i64 %22, %23
br i1 %24, label %35, label %25
25: ; preds = %21
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %22, %26
br i1 %27, label %36, label %28
28: ; preds = %25
%29 = call i32 @gettext(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%30 = load i64, i64* @errno
%31 = call i32 @strerror(i64 %30)
%32 = call i32 @set_error(i32 %29, i32 %31)
br label %36
33: ; preds = %17
%34 = call i32 @read(i32 %0, i8* %1, i64 %2)
br label %36
35: ; preds = %21
br label %17
36: ; preds = %17, %28, %33, %25
%37 = phi i32 [ -3, %25 ], [ %34, %33 ], [ -1, %28 ], [ -2, %17 ]
ret i32 %37
}