#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 /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
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 sock_ready(int fd, int *to_read, int *to_write, float timeout) {
  int ret = 0;
  struct timeval *tmoutp = NULL;
  struct timeval tmout;
  fd_set *rdsp = NULL;
  fd_set readset;
  fd_set *wrsp = NULL;
  fd_set writeset;
  if (to_read) {
    FD_ZERO(&readset);
    FD_SET(fd, &readset);
    rdsp = &readset;
  }
  if (to_write) {
    FD_ZERO(&writeset);
    FD_SET(fd, &writeset);
    wrsp = &writeset;
  }
  if (timeout >= 0.0) {
    tmout.tv_sec = (long)timeout;
    tmout.tv_usec = (long)((timeout - (float)tmout.tv_sec) * 1e6);
    tmoutp = &tmout;
  }
  ret = select(fd + 1, rdsp, wrsp, NULL, tmoutp);
  if (ret < 0) {
    perror("sock_ready: (err) select");
    return -1;
  }
  if (!ret)
    return 0;
  if (to_read && FD_ISSET(fd, &readset)) {
    *to_read = 1;
    ret = 1;
  }
  if (to_write && FD_ISSET(fd, &writeset)) {
    *to_write = 1;
    ret = 1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<sock_ready>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movss  %xmm0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x50(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11af <sock_ready+0x5f>
lea    -0x44(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1070 <FD_SET@plt>
lea    -0x44(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d7 <sock_ready+0x87>
lea    -0x54(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x54(%rbp),%rsi
call   1070 <FD_SET@plt>
lea    -0x54(%rbp),%rax
mov    %rax,-0x50(%rbp)
movss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1227 <sock_ready+0xd7>
cvttss2si -0x1c(%rbp),%rax
mov    %rax,-0x38(%rbp)
movss  -0x1c(%rbp),%xmm0
cvtsi2ssq -0x38(%rbp),%xmm1
subss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xdee(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x30(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x40(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x28(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1267 <sock_ready+0x117>
lea    0xdb2(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <sock_ready+0x197>
cmpl   $0x0,-0x20(%rbp)
jne    127d <sock_ready+0x12d>
movl   $0x0,-0x4(%rbp)
jmp    12e7 <sock_ready+0x197>
cmpq   $0x0,-0x10(%rbp)
je     12af <sock_ready+0x15f>
mov    -0x8(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12af <sock_ready+0x15f>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12e1 <sock_ready+0x191>
mov    -0x8(%rbp),%edi
lea    -0x54(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12e1 <sock_ready+0x191>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_ready>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movss  %xmm0,0xc(%rsp)
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
test   %rsi,%rsi
je     11a9 <sock_ready+0x59>
lea    0x14(%rsp),%r13
mov    %r13,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %r13,%rsi
call   1070 <FD_SET@plt>
test   %r14,%r14
je     11b1 <sock_ready+0x61>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
jmp    11b3 <sock_ready+0x63>
xor    %r13d,%r13d
test   %r14,%r14
jne    118f <sock_ready+0x3f>
xor    %ebx,%ebx
xorps  %xmm0,%xmm0
movss  0xc(%rsp),%xmm1
ucomiss %xmm0,%xmm1
jae    11c6 <sock_ready+0x76>
xor    %r8d,%r8d
jmp    11fa <sock_ready+0xaa>
cvttss2si %xmm1,%rax
mov    %rax,0x18(%rsp)
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
subss  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
mulsd  0xe15(%rip),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,0x20(%rsp)
lea    0x18(%rsp),%r8
lea    0x1(%r12),%edi
xor    %ebp,%ebp
mov    %r13,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
js     1264 <sock_ready+0x114>
je     1275 <sock_ready+0x125>
mov    %eax,%ebx
test   %r15,%r15
je     1239 <sock_ready+0xe9>
lea    0x14(%rsp),%rsi
mov    %r12d,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1239 <sock_ready+0xe9>
movl   $0x1,(%r15)
mov    $0x1,%ebx
test   %r14,%r14
je     1260 <sock_ready+0x110>
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
call   1030 <FD_ISSET@plt>
mov    %ebx,%ebp
test   %rax,%rax
je     1275 <sock_ready+0x125>
movl   $0x1,(%r14)
mov    $0x1,%ebp
jmp    1275 <sock_ready+0x125>
mov    %ebx,%ebp
jmp    1275 <sock_ready+0x125>
lea    0xd9d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@.str = [25 x i8] c"sock_ready: (err) select\00"
define i32 @sock_ready(i32 %0, i32* %1, i32* %2, float %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca float
%10 = alloca i32
%11 = alloca %struct.timeval*
%12 = alloca %struct.timeval
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32*
%16 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store float %3, float* %9
store i32 0, i32* %10
store %struct.timeval* null, %struct.timeval** %11
store i32* null, i32** %13
store i32* null, i32** %15
%17 = load i32*, i32** %7
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %4
%20 = call i32 @FD_ZERO(i32* %14)
%21 = load i32, i32* %6
%22 = call i32 @FD_SET(i32 %21, i32* %14)
store i32* %14, i32** %13
br label %23
23: ; preds = %19, %4
%24 = load i32*, i32** %8
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = call i32 @FD_ZERO(i32* %16)
%28 = load i32, i32* %6
%29 = call i32 @FD_SET(i32 %28, i32* %16)
store i32* %16, i32** %15
br label %30
30: ; preds = %26, %23
%31 = load float, float* %9
%32 = fpext float %31 to double
%33 = fcmp oge double %32, 0.000000e+00
br i1 %33, label %34, label %47
34: ; preds = %30
%35 = load float, float* %9
%36 = fptosi float %35 to i64
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i64 %36, i64* %37
%38 = load float, float* %9
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%40 = load i64, i64* %39
%41 = sitofp i64 %40 to float
%42 = fsub float %38, %41
%43 = fpext float %42 to double
%44 = fmul double %43, 1.000000e+06
%45 = fptosi double %44 to i64
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 %45, i64* %46
store %struct.timeval* %12, %struct.timeval** %11
br label %47
47: ; preds = %34, %30
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
%50 = load i32*, i32** %13
%51 = load i32*, i32** %15
%52 = load %struct.timeval*, %struct.timeval** %11
%53 = call i32 @select(i32 %49, i32* %50, i32* %51, i32* null, %struct.timeval* %52)
store i32 %53, i32* %10
%54 = load i32, i32* %10
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %47
%57 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %82
58: ; preds = %47
%59 = load i32, i32* %10
%60 = icmp ne i32 %59, 0
br i1 %60, label %62, label %61
61: ; preds = %58
store i32 0, i32* %5
br label %82
62: ; preds = %58
%63 = load i32*, i32** %7
%64 = icmp ne i32* %63, null
br i1 %64, label %65, label %71
65: ; preds = %62
%66 = load i32, i32* %6
%67 = call i64 @FD_ISSET(i32 %66, i32* %14)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = load i32*, i32** %7
store i32 1, i32* %70
store i32 1, i32* %10
br label %71
71: ; preds = %69, %65, %62
%72 = load i32*, i32** %8
%73 = icmp ne i32* %72, null
br i1 %73, label %74, label %80
74: ; preds = %71
%75 = load i32, i32* %6
%76 = call i64 @FD_ISSET(i32 %75, i32* %16)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = load i32*, i32** %8
store i32 1, i32* %79
store i32 1, i32* %10
br label %80
80: ; preds = %78, %74, %71
%81 = load i32, i32* %10
store i32 %81, i32* %5
br label %82
82: ; preds = %80, %61, %56
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str = [25 x i8] c"sock_ready: (err) select\00"
define i32 @sock_ready(i32 %0, i32* writeonly %1, i32* writeonly %2, float %3) {
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
%8 = bitcast %struct.timeval* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = icmp eq i32* %1, null
br i1 %11, label %15, label %12
12: ; preds = %4
%13 = call i32 @FD_ZERO(i32* %6)
%14 = call i32 @FD_SET(i32 %0, i32* %6)
br label %15
15: ; preds = %12, %4
%16 = phi i32* [ %6, %12 ], [ null, %4 ]
%17 = icmp eq i32* %2, null
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = call i32 @FD_ZERO(i32* %7)
%20 = call i32 @FD_SET(i32 %0, i32* %7)
br label %21
21: ; preds = %18, %15
%22 = phi i32* [ %7, %18 ], [ null, %15 ]
%23 = fcmp ult float %3, 0.000000e+00
br i1 %23, label %33, label %24
24: ; preds = %21
%25 = fptosi float %3 to i64
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i64 %25, i64* %26
%27 = sitofp i64 %25 to float
%28 = fsub float %3, %27
%29 = fpext float %28 to double
%30 = fmul double %29, 1.000000e+06
%31 = fptosi double %30 to i64
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 %31, i64* %32
br label %33
33: ; preds = %24, %21
%34 = phi %struct.timeval* [ %5, %24 ], [ null, %21 ]
%35 = add nsw i32 %0, 1
%36 = call i32 @select(i32 %35, i32* %16, i32* %22, i32* null, %struct.timeval* %34)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %53
40: ; preds = %33
%41 = icmp eq i32 %36, 0
br i1 %41, label %53, label %42
42: ; preds = %40
br i1 %11, label %47, label %43
43: ; preds = %42
%44 = call i64 @FD_ISSET(i32 %0, i32* %6)
%45 = icmp eq i64 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %43
store i32 1, i32* %1
br label %47
47: ; preds = %46, %43, %42
%48 = phi i32 [ 1, %46 ], [ %36, %43 ], [ %36, %42 ]
br i1 %17, label %53, label %49
49: ; preds = %47
%50 = call i64 @FD_ISSET(i32 %0, i32* %7)
%51 = icmp eq i64 %50, 0
br i1 %51, label %53, label %52
52: ; preds = %49
store i32 1, i32* %2
br label %53
53: ; preds = %47, %49, %52, %40, %38
%54 = phi i32 [ -1, %38 ], [ 0, %40 ], [ 1, %52 ], [ %48, %49 ], [ %48, %47 ]
ret i32 %54
}