#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__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timeval *);
int input_timeout(int filedes, double time) {
  fd_set set;
  struct timeval timeout;
  FD_ZERO(&set);
  FD_SET(filedes, &set);
  int sec = (int)time;
  int usec = (int)((time - sec) * 1e6);
  timeout.tv_sec = sec;
  timeout.tv_usec = usec;
  return ({
    long int _result;
    do
      _result = (long int)(select(FD_SETSIZE, &set, NULL, NULL, &timeout));
    while (_result == -1L && errno == EINTR);
    _result;
  });
}


x86 assembly (clang, -O0):
<input_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1050 <FD_SET@plt>
cvttsd2si -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
movsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x24(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  0xe8d(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0xffffffffffffffff,-0x30(%rbp)
mov    %al,-0x39(%rbp)
jne    11d5 <input_timeout+0xa5>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    118a <input_timeout+0x5a>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<input_timeout>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
movaps %xmm0,0x10(%rsp)
mov    %edi,%ebx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
movapd 0x10(%rsp),%xmm1
cvttsd2si %xmm1,%eax
cvttpd2dq %xmm1,%xmm0
cvtdq2pd %xmm0,%xmm0
subsd  %xmm0,%xmm1
mulsd  0xe87(%rip),%xmm1
cvttsd2si %xmm1,%ecx
mov    %eax,(%rsp)
mov    %ecx,0x4(%rsp)
mov    0x2e55(%rip),%r15
mov    %rsp,%r13
mov    0x2e43(%rip),%r12
mov    0x2e34(%rip),%rbx
nopl   0x0(%rax)
mov    (%r15),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1040 <select@plt>
cmp    $0xffffffffffffffff,%rax
jne    11c1 <input_timeout+0x91>
mov    (%rbx),%rcx
cmp    %rcx,(%r12)
je     11a0 <input_timeout+0x70>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @input_timeout(i32 %0, double %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %3
store double %1, double* %4
%11 = call i32 @FD_ZERO(i32* %5)
%12 = load i32, i32* %3
%13 = call i32 @FD_SET(i32 %12, i32* %5)
%14 = load double, double* %4
%15 = fptosi double %14 to i32
store i32 %15, i32* %7
%16 = load double, double* %4
%17 = load i32, i32* %7
%18 = sitofp i32 %17 to double
%19 = fsub double %16, %18
%20 = fmul double %19, 1.000000e+06
%21 = fptosi double %20 to i32
store i32 %21, i32* %8
%22 = load i32, i32* %7
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %8
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i32 %24, i32* %25
br label %26
26: ; preds = %36, %2
%27 = load i32, i32* @FD_SETSIZE
%28 = call i64 @select(i32 %27, i32* %5, i32* null, i32* null, %struct.timeval* %6)
store i64 %28, i64* %9
br label %29
29: ; preds = %26
%30 = load i64, i64* %9
%31 = icmp eq i64 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br label %36
36: ; preds = %32, %29
%37 = phi i1 [ false, %29 ], [ %35, %32 ]
br i1 %37, label %26, label %38
38: ; preds = %36
%39 = load i64, i64* %9
store i64 %39, i64* %10
%40 = load i64, i64* %10
%41 = trunc i64 %40 to i32
ret i32 %41
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @input_timeout(i32 %0, double %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 = fptosi double %1 to i32
%10 = sitofp i32 %9 to double
%11 = fsub double %1, %10
%12 = fmul double %11, 1.000000e+06
%13 = fptosi double %12 to i32
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %9, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %13, i32* %15
br label %16
16: ; preds = %16, %2
%17 = load i32, i32* @FD_SETSIZE
%18 = call i64 @select(i32 %17, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%19 = icmp eq i64 %18, -1
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %16, label %24
24: ; preds = %16
%25 = trunc i64 %18 to i32
ret i32 %25
}