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

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int read(int, void *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
ssize_t read_t(int fd, void *buf, size_t count, double timeout) {
  fd_set set;
  struct timeval tv = {(long)timeout, (long)(timeout * 1000000) % 1000000};
  int rc;
  FD_ZERO(&set);
  FD_SET(fd, &set);
  rc = select(fd + 1, &set, NULL, NULL, &tv);
  switch (rc) {
  case -1:
    return rc;
  case 0:
    errno = EAGAIN;
    return -1;
  default:
    return read(fd, buf, count);
  }
}


x86 assembly (clang, -O0):
<read_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
cvttsd2si -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xe91(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x50(%rbp)
movabs $0x431bde82d7b634db,%rcx
de 1b 43
imul   %rcx
mov    -0x50(%rbp),%rax
mov    %rdx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x12,%rcx
add    %rdx,%rcx
imul   $0xf4240,%rcx,%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
lea    -0x24(%rbp),%rdi
mov    %rdi,-0x48(%rbp)
call   1040 <FD_ZERO@plt>
mov    -0x48(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1060 <FD_SET@plt>
mov    -0x48(%rbp),%rsi
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0xffffffff,%eax
je     1206 <read_t+0xc6>
jmp    11f6 <read_t+0xb6>
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     1211 <read_t+0xd1>
jmp    122f <read_t+0xef>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1242 <read_t+0x102>
mov    0x2db8(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbf(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <read_t+0x102>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_t>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
cvttsd2si %xmm0,%rax
mulsd  0xe9f(%rip),%xmm0
mov    %rax,0x8(%rsp)
cvttsd2si %xmm0,%rcx
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x12,%rdx
add    %rax,%rdx
imul   $0xf4240,%rdx,%rax
sub    %rax,%rcx
mov    %rcx,0x10(%rsp)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%r12),%edi
lea    0x8(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
cmp    $0xffffffff,%eax
je     11f6 <read_t+0xb6>
test   %eax,%eax
jne    11e8 <read_t+0xa8>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    0x2e01(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    11f6 <read_t+0xb6>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@EAGAIN = global i32 0
@errno = global i32 0
define i32 @read_t(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
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%14 = load double, double* %9
%15 = fptosi double %14 to i64
store i64 %15, i64* %13
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%17 = load double, double* %9
%18 = fmul double %17, 1.000000e+06
%19 = fptosi double %18 to i64
%20 = srem i64 %19, 1000000
store i64 %20, i64* %16
%21 = call i32 @FD_ZERO(i32* %10)
%22 = load i32, i32* %6
%23 = call i32 @FD_SET(i32 %22, i32* %10)
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
%26 = call i32 @select(i32 %25, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %26, i32* %12
%27 = load i32, i32* %12
switch i32 %27, label %32 [
i32 -1, label %28
i32 0, label %30
]
28: ; preds = %4
%29 = load i32, i32* %12
store i32 %29, i32* %5
br label %37
30: ; preds = %4
%31 = load i32, i32* @EAGAIN
store i32 %31, i32* @errno
store i32 -1, i32* %5
br label %37
32: ; preds = %4
%33 = load i32, i32* %6
%34 = load i8*, i8** %7
%35 = load i64, i64* %8
%36 = call i32 @read(i32 %33, i8* %34, i64 %35)
store i32 %36, i32* %5
br label %37
37: ; preds = %32, %30, %28
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@EAGAIN = global i32 0
@errno = global i32 0
define i32 @read_t(i32 %0, i8* nocapture %1, i64 %2, double %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%10 = fptosi double %3 to i64
store i64 %10, i64* %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%12 = fmul double %3, 1.000000e+06
%13 = fptosi double %12 to i64
%14 = srem i64 %13, 1000000
store i64 %14, i64* %11
%15 = call i32 @FD_ZERO(i32* %5)
%16 = call i32 @FD_SET(i32 %0, i32* %5)
%17 = add nsw i32 %0, 1
%18 = call i32 @select(i32 %17, i32* %5, i32* null, i32* null, %struct.timeval* %6)
switch i32 %18, label %21 [
i32 -1, label %23
i32 0, label %19
]
19: ; preds = %4
%20 = load i32, i32* @EAGAIN
store i32 %20, i32* @errno
br label %23
21: ; preds = %4
%22 = call i32 @read(i32 %0, i8* %1, i64 %2)
br label %23
23: ; preds = %4, %21, %19
%24 = phi i32 [ %22, %21 ], [ -1, %19 ], [ %18, %4 ]
ret i32 %24
}