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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int read(int, void *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
ssize_t read_timeout(int fd, void *buf, size_t count, long timeout) {
  struct timeval tm, tm2;
  fd_set rfds;
  int nfds;
  tm.tv_sec = timeout / 1000L;
  tm.tv_usec = (timeout % 1000L) * 1000;
  while (1) {
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    tm2 = tm;
    nfds = select(fd + 1, &rfds, NULL, NULL, &tm2);
    if (nfds == 0) {
      return -2;
    } else if (nfds == -1) {
      if (errno == EINTR || errno == EAGAIN) {
        continue;
      } else {
        return -1;
      }
    } else {
      return read(fd, buf, count);
    }
  }
}


x86 assembly (clang, -O0):
<read_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
imul   $0x3e8,%rdx,%rax
mov    %rax,-0x28(%rbp)
lea    -0x44(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x44(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jne    11da <read_timeout+0x9a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    123c <read_timeout+0xfc>
cmpl   $0xffffffff,-0x48(%rbp)
jne    1229 <read_timeout+0xe9>
mov    0x2df5(%rip),%rax
mov    (%rax),%rax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
je     1218 <read_timeout+0xd8>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rax
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%rax
jne    121d <read_timeout+0xdd>
jmp    1182 <read_timeout+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <read_timeout+0xfc>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,(%rsp)
mov    %edi,%r12d
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rbx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rbx
add    %rax,%rbx
imul   $0x3e8,%rbx,%rax
sub    %rax,%rcx
imul   $0x3e8,%rcx,%r15
lea    0x1(%r12),%r13d
lea    0x14(%rsp),%rbp
lea    0x18(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %rbx,0x18(%rsp)
mov    %r15,0x20(%rsp)
mov    %r13d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1050 <select@plt>
cmp    $0xffffffff,%eax
jne    11fd <read_timeout+0xbd>
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
je     11a0 <read_timeout+0x60>
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%rax
je     11a0 <read_timeout+0x60>
mov    $0xffffffff,%eax
jmp    1219 <read_timeout+0xd9>
mov    %eax,%ecx
mov    $0xfffffffe,%eax
test   %ecx,%ecx
je     1219 <read_timeout+0xd9>
mov    %r12d,%edi
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
call   1030 <read@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @read_timeout(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca %struct.timeval
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%14 = load i64, i64* %9
%15 = sdiv i64 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i64 %15, i64* %16
%17 = load i64, i64* %9
%18 = srem i64 %17, 1000
%19 = mul nsw i64 %18, 1000
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 %19, i64* %20
br label %21
21: ; preds = %4, %44
%22 = call i32 @FD_ZERO(i32* %12)
%23 = load i32, i32* %6
%24 = call i32 @FD_SET(i32 %23, i32* %12)
%25 = bitcast %struct.timeval* %11 to i8*
%26 = bitcast %struct.timeval* %10 to i8*
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
%29 = call i32 @select(i32 %28, i32* %12, i32* null, i32* null, %struct.timeval* %11)
store i32 %29, i32* %13
%30 = load i32, i32* %13
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %21
store i32 -2, i32* %5
br label %51
33: ; preds = %21
%34 = load i32, i32* %13
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %46
36: ; preds = %33
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINTR
%39 = icmp eq i64 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i64, i64* @errno
%42 = load i64, i64* @EAGAIN
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40, %36
br label %21
45: ; preds = %40
store i32 -1, i32* %5
br label %51
46: ; preds = %33
%47 = load i32, i32* %6
%48 = load i8*, i8** %7
%49 = load i64, i64* %8
%50 = call i32 @read(i32 %47, i8* %48, i64 %49)
store i32 %50, i32* %5
br label %51
51: ; preds = %46, %45, %32
%52 = load i32, i32* %5
ret i32 %52
}

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