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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
long long currtime_usec();
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int tcp_read_fully_deadline(int fd, void *buf, int count,
                            long long deadline_msec) {
  fd_set rfds;
  struct timeval tv;
  int ret, remain;
  char *bp;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  bp = buf;
  remain = count;
  while (remain > 0) {
    long long tval = (deadline_msec * 1000) - currtime_usec();
    if (tval <= 0) {
      return -1;
    }
    tv.tv_sec = tval / 1000000;
    tv.tv_usec = tval % 1000000;
    ret = select(fd + 1, &rfds, NULL, NULL, &tv);
    if (ret > 0) {
      int nr = read(fd, bp, remain);
      if (nr <= 0) {
        return -1;
      }
      bp += nr;
      remain -= nr;
    } else {
      return -1;
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<tcp_read_fully_deadline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jle    1270 <tcp_read_fully_deadline+0x120>
imul   $0x3e8,-0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1030 <currtime_usec@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jg     11cb <tcp_read_fully_deadline+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <tcp_read_fully_deadline+0x126>
mov    -0x50(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,-0x38(%rbp)
mov    -0x50(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x30(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    125f <tcp_read_fully_deadline+0x10f>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jg     123e <tcp_read_fully_deadline+0xee>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <tcp_read_fully_deadline+0x126>
mov    -0x54(%rbp),%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x54(%rbp),%ecx
mov    -0x40(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x40(%rbp)
jmp    126b <tcp_read_fully_deadline+0x11b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <tcp_read_fully_deadline+0x126>
jmp    1189 <tcp_read_fully_deadline+0x39>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_read_fully_deadline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1070 <FD_SET@plt>
imul   $0x3e8,%r14,%r15
lea    0x1(%rbp),%r12d
mov    %r13d,(%rsp)
mov    %r13d,%r14d
mov    $0xffffffff,%r13d
nopl   0x0(%rax,%rax,1)
test   %r14d,%r14d
jle    1211 <tcp_read_fully_deadline+0xc1>
xor    %eax,%eax
call   1030 <currtime_usec@plt>
mov    %r15,%rcx
sub    %rax,%rcx
jle    1215 <tcp_read_fully_deadline+0xc5>
mov    %rcx,%rax
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mul    %rdx
shr    $0x12,%rdx
mov    %rdx,0x8(%rsp)
imul   $0xf4240,%rdx,%rax
sub    %rax,%rcx
mov    %rcx,0x10(%rsp)
mov    %r12d,%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1060 <select@plt>
test   %eax,%eax
jle    1215 <tcp_read_fully_deadline+0xc5>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
cltq
add    %rax,%rbx
sub    %eax,%r14d
test   %eax,%eax
jg     11a0 <tcp_read_fully_deadline+0x50>
jmp    1215 <tcp_read_fully_deadline+0xc5>
mov    (%rsp),%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @tcp_read_fully_deadline(i32 %0, i8* %1, i32 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i64 %3, i64* %9
%17 = call i32 @FD_ZERO(i32* %10)
%18 = load i32, i32* %6
%19 = call i32 @FD_SET(i32 %18, i32* %10)
%20 = load i8*, i8** %7
store i8* %20, i8** %14
%21 = load i32, i32* %8
store i32 %21, i32* %13
br label %22
22: ; preds = %62, %4
%23 = load i32, i32* %13
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %63
25: ; preds = %22
%26 = load i64, i64* %9
%27 = mul nsw i64 %26, 1000
%28 = call i64 (...) @currtime_usec()
%29 = sub nsw i64 %27, %28
store i64 %29, i64* %15
%30 = load i64, i64* %15
%31 = icmp sle i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -1, i32* %5
br label %65
33: ; preds = %25
%34 = load i64, i64* %15
%35 = sdiv i64 %34, 1000000
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i64 %35, i64* %36
%37 = load i64, i64* %15
%38 = srem i64 %37, 1000000
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 %38, i64* %39
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
%42 = call i32 @select(i32 %41, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %42, i32* %12
%43 = load i32, i32* %12
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %61
45: ; preds = %33
%46 = load i32, i32* %6
%47 = load i8*, i8** %14
%48 = load i32, i32* %13
%49 = call i32 @read(i32 %46, i8* %47, i32 %48)
store i32 %49, i32* %16
%50 = load i32, i32* %16
%51 = icmp sle i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %45
store i32 -1, i32* %5
br label %65
53: ; preds = %45
%54 = load i32, i32* %16
%55 = load i8*, i8** %14
%56 = sext i32 %54 to i64
%57 = getelementptr inbounds i8, i8* %55, i64 %56
store i8* %57, i8** %14
%58 = load i32, i32* %16
%59 = load i32, i32* %13
%60 = sub nsw i32 %59, %58
store i32 %60, i32* %13
br label %62
61: ; preds = %33
store i32 -1, i32* %5
br label %65
62: ; preds = %53
br label %22
63: ; preds = %22
%64 = load i32, i32* %8
store i32 %64, i32* %5
br label %65
65: ; preds = %63, %61, %52, %32
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @tcp_read_fully_deadline(i32 %0, i8* nocapture %1, i32 %2, i64 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = call i32 @FD_ZERO(i32* %5)
%10 = call i32 @FD_SET(i32 %0, i32* %5)
%11 = mul nsw i64 %3, 1000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%14 = add nsw i32 %0, 1
br label %15
15: ; preds = %28, %4
%16 = phi i8* [ %1, %4 ], [ %32, %28 ]
%17 = phi i32 [ %2, %4 ], [ %33, %28 ]
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %34
19: ; preds = %15
%20 = call i64 (...) @currtime_usec()
%21 = sub nsw i64 %11, %20
%22 = icmp slt i64 %21, 1
br i1 %22, label %34, label %23
23: ; preds = %19
%24 = udiv i64 %21, 1000000
store i64 %24, i64* %12
%25 = urem i64 %21, 1000000
store i64 %25, i64* %13
%26 = call i32 @select(i32 %14, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = call i32 @read(i32 %0, i8* %16, i32 %17)
%30 = icmp slt i32 %29, 1
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %16, i64 %31
%33 = sub nsw i32 %17, %29
br i1 %30, label %34, label %15
34: ; preds = %28, %23, %19, %15
%35 = phi i32 [ %2, %15 ], [ -1, %19 ], [ -1, %23 ], [ -1, %28 ]
ret i32 %35
}