#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;
scalar_t__ ETIMEDOUT;
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 read_timeout(int fd, int timeout_ms) {
  struct timeval tv;
  tv.tv_sec = timeout_ms / 1000;
  tv.tv_usec = (timeout_ms % 1000) * 1000;
  while (1) {
    fd_set rfds;
    int rc;
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    rc = select(fd + 1, &rfds, NULL, NULL, &tv);
    if (rc == -1) {
      if (errno != EINTR)
        return -1;
      continue;
    }
    if (rc == 1)
      return 0;
    if (tv.tv_sec == 0 && tv.tv_usec == 0) {
      errno = ETIMEDOUT;
      return -1;
    }
  }
}


x86 assembly (clang, -O0):
<read_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c7 <read_timeout+0x97>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
je     11c2 <read_timeout+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <read_timeout+0xe6>
jmp    1160 <read_timeout+0x30>
cmpl   $0x1,-0x20(%rbp)
jne    11dd <read_timeout+0xad>
movl   $0x0,-0x4(%rbp)
jmp    1216 <read_timeout+0xe6>
cmpl   $0x0,-0x18(%rbp)
jne    1211 <read_timeout+0xe1>
cmpl   $0x0,-0x14(%rbp)
jne    1211 <read_timeout+0xe1>
mov    0x2de0(%rip),%rax
mov    (%rax),%rcx
mov    0x2dde(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <read_timeout+0xe6>
jmp    1160 <read_timeout+0x30>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
movslq %esi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0xc(%rsp)
lea    0x1(%r14),%r12d
lea    0x14(%rsp),%rbx
lea    0x8(%rsp),%r15
mov    0x2e4d(%rip),%r13
jmp    11a0 <read_timeout+0x70>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    11ef <read_timeout+0xbf>
mov    %rbx,%rdi
call   1030 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1050 <FD_SET@plt>
xor    %ebp,%ebp
mov    %r12d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1040 <select@plt>
cmp    $0xffffffff,%eax
je     1190 <read_timeout+0x60>
cmp    $0x1,%eax
je     11f4 <read_timeout+0xc4>
mov    0x8(%rsp),%eax
or     0xc(%rsp),%eax
jne    11a0 <read_timeout+0x70>
mov    0x2df6(%rip),%rax
mov    (%rax),%rax
mov    0x2df4(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
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
@ETIMEDOUT = global i64 0
define i32 @read_timeout(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = sdiv i32 %9, 1000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* %5
%13 = srem i32 %12, 1000
%14 = mul nsw i32 %13, 1000
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i32 %14, i32* %15
br label %16
16: ; preds = %2, %30, %45
%17 = call i32 @FD_ZERO(i32* %7)
%18 = load i32, i32* %4
%19 = call i32 @FD_SET(i32 %18, i32* %7)
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %7, i32* null, i32* null, %struct.timeval* %6)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %16
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %3
br label %46
30: ; preds = %25
br label %16
31: ; preds = %16
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 0, i32* %3
br label %46
35: ; preds = %31
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%37 = load i32, i32* %36
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%41 = load i32, i32* %40
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = load i64, i64* @ETIMEDOUT
store i64 %44, i64* @errno
store i32 -1, i32* %3
br label %46
45: ; preds = %39, %35
br label %16
46: ; preds = %43, %34, %29
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@ETIMEDOUT = global i64 0
define i32 @read_timeout(i32 %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = bitcast %struct.timeval* %3 to i8*
%6 = sdiv i32 %1, 1000
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 %6, i32* %7
%8 = srem i32 %1, 1000
%9 = mul nsw i32 %8, 1000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i32 %9, i32* %10
%11 = bitcast i32* %4 to i8*
%12 = add nsw i32 %0, 1
br label %13
13: ; preds = %29, %2
%14 = call i32 @FD_ZERO(i32* %4)
%15 = call i32 @FD_SET(i32 %0, i32* %4)
%16 = call i32 @select(i32 %12, i32* %4, i32* null, i32* null, %struct.timeval* %3)
switch i32 %16, label %21 [
i32 -1, label %17
i32 1, label %30
]
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %29, label %30
21: ; preds = %13
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 0
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, 0
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i64, i64* @ETIMEDOUT
store i64 %28, i64* @errno
br label %30
29: ; preds = %17, %21
br label %13
30: ; preds = %17, %13, %27
%31 = phi i32 [ -1, %27 ], [ 0, %13 ], [ -1, %17 ]
ret i32 %31
}