#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 {
  float tv_sec;
  double tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int block_on_read(int sock, float wait) {
  fd_set readfs;
  struct timeval timeout;
  int rc;
  FD_ZERO(&readfs);
  FD_SET(sock, &readfs);
  timeout.tv_sec = wait;
  timeout.tv_usec = 1000000.0 * (wait - timeout.tv_sec);
  rc = select(sock + 1, &readfs, NULL, NULL, &timeout);
  if (rc < 0)
    perror("select()");
  return rc;
}


x86 assembly (clang, -O0):
<block_on_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movss  %xmm0,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1060 <FD_SET@plt>
movss  -0x8(%rbp),%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x8(%rbp),%xmm0
subss  -0x20(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xe7b(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c1 <block_on_read+0x81>
lea    0xe4c(%rip),%rdi
call   1050 <perror@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block_on_read>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movss  %xmm0,(%rsp)
mov    %edi,%ebx
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
movss  (%rsp),%xmm0
movss  %xmm0,0x8(%rsp)
subss  %xmm0,%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xe80(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
add    $0x1,%ebx
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ac <block_on_read+0x6c>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
jmp    11a2 <block_on_read+0x62>

LLVM IR (-O0):
%struct.timeval = type { float, double }
@.str = [9 x i8] c"select()\00"
define i32 @block_on_read(i32 %0, float %1) {
%3 = alloca i32
%4 = alloca float
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
store i32 %0, i32* %3
store float %1, float* %4
%8 = call i32 @FD_ZERO(i32* %5)
%9 = load i32, i32* %3
%10 = call i32 @FD_SET(i32 %9, i32* %5)
%11 = load float, float* %4
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store float %11, float* %12
%13 = load float, float* %4
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%15 = load float, float* %14
%16 = fsub float %13, %15
%17 = fpext float %16 to double
%18 = fmul double 1.000000e+06, %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store double %18, double* %19
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %5, i32* null, i32* null, %struct.timeval* %6)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %2
%28 = load i32, i32* %7
ret i32 %28
}

LLVM IR (-O2):
%struct.timeval = type { float, double }
@.str = [9 x i8] c"select()\00"
define i32 @block_on_read(i32 %0, float %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 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store float %1, float* %9
%10 = fsub float %1, %1
%11 = fpext float %10 to double
%12 = fmul double %11, 1.000000e+06
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store double %12, double* %13
%14 = add nsw i32 %0, 1
%15 = call i32 @select(i32 %14, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
ret i32 %15
}