#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_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ warn(char *, int /*<<< orphan*/);
int netWaitForData(int sd, int microseconds) {
  struct timeval tv;
  fd_set set;
  int readyCount;
  for (;;) {
    if (microseconds >= 1000000) {
      tv.tv_sec = microseconds / 1000000;
      tv.tv_usec = microseconds % 1000000;
    } else {
      tv.tv_sec = 0;
      tv.tv_usec = microseconds;
    }
    FD_ZERO(&set);
    FD_SET(sd, &set);
    readyCount = select(sd + 1, &set, NULL, NULL, &tv);
    if (readyCount < 0) {
      if (errno == EINTR)
        continue;
      else
        warn("select failure %s", strerror(errno));
    } else {
      return readyCount;
    }
  }
}


x86 assembly (clang, -O0):
<netWaitForData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0xf4240,-0x8(%rbp)
jl     118c <netWaitForData+0x3c>
mov    -0x8(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
jmp    1199 <netWaitForData+0x49>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x14(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1216 <netWaitForData+0xc6>
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    11f4 <netWaitForData+0xa4>
jmp    115e <netWaitForData+0xe>
mov    0x2de5(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %eax,%esi
lea    0xdf4(%rip),%rdi
call   1030 <warn@plt>
jmp    121f <netWaitForData+0xcf>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
jmp    115e <netWaitForData+0xe>

x86 assembly (clang, -O2):
<netWaitForData>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    %esi,%eax
imul   $0x431bde83,%rax,%r13
shr    $0x32,%r13
imul   $0xf4240,%r13d,%eax
mov    %esi,%r12d
sub    %eax,%r12d
xor    %eax,%eax
cmp    $0xf4240,%esi
cmovl  %eax,%r13d
lea    0x1(%r14),%ebp
cmovl  %esi,%r12d
lea    0x14(%rsp),%rbx
lea    0x8(%rsp),%r15
nopl   0x0(%rax)
mov    %r13d,0x8(%rsp)
mov    %r12d,0xc(%rsp)
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1050 <select@plt>
test   %eax,%eax
jns    11fd <netWaitForData+0xad>
mov    0x2e07(%rip),%rax
mov    (%rax),%rdi
mov    0x2df5(%rip),%rax
cmp    (%rax),%rdi
je     11a0 <netWaitForData+0x50>
call   1060 <strerror@plt>
lea    0xe0c(%rip),%rdi
mov    %eax,%esi
call   1030 <warn@plt>
jmp    11a0 <netWaitForData+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [18 x i8] c"select failure %s\00"
define i32 @netWaitForData(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
br label %8
8: ; preds = %43, %35, %2
%9 = load i32, i32* %4
%10 = icmp sge i32 %9, 1000000
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = load i32, i32* %4
%13 = sdiv i32 %12, 1000000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* %4
%16 = srem i32 %15, 1000000
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i32 %16, i32* %17
br label %22
18: ; preds = %8
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 0, i32* %19
%20 = load i32, i32* %4
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i32 %20, i32* %21
br label %22
22: ; preds = %18, %11
%23 = call i32 @FD_ZERO(i32* %6)
%24 = load i32, i32* %3
%25 = call i32 @FD_SET(i32 %24, i32* %6)
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
%28 = call i32 @select(i32 %27, i32* %6, i32* null, i32* null, %struct.timeval* %5)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %22
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
br label %8
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = call i32 @strerror(i64 %37)
%39 = call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %36
br label %43
41: ; preds = %22
%42 = load i32, i32* %7
ret i32 %42
43: ; preds = %40
br label %8
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [18 x i8] c"select failure %s\00"
define i32 @netWaitForData(i32 %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp sgt i32 %1, 999999
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%10 = udiv i32 %1, 1000000
%11 = urem i32 %1, 1000000
%12 = add nsw i32 %0, 1
%13 = select i1 %7, i32 %10, i32 0
%14 = select i1 %7, i32 %11, i32 %1
br label %15
15: ; preds = %27, %2
store i32 %13, i32* %8
store i32 %14, i32* %9
%16 = call i32 @FD_ZERO(i32* %4)
%17 = call i32 @FD_SET(i32 %0, i32* %4)
%18 = call i32 @select(i32 %12, i32* %4, i32* null, i32* null, %struct.timeval* %3)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = call i32 @strerror(i64 %21)
%26 = call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %20
br label %15
28: ; preds = %15
ret i32 %18
}