#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__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int _read_data(int fd, char *buf, int len, int time) {
  fd_set rfds;
  struct timeval tv;
  int n, remain = len;
  int sec = time / 1000000;
  int usec = time % 1000000;
  while (1) {
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    tv.tv_sec = sec;
    tv.tv_usec = usec;
    n = select(fd + 1, &rfds, NULL, NULL, &tv);
    if (n == -1)
      return -1;
    else if (n == 0)
      return 0;
    if (FD_ISSET(fd, &rfds)) {
      if ((n = read(fd, buf, len)) <= 0) {
        return -1;
      }
      return n;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<_read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x38(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11db <_read_data+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <_read_data+0xf3>
cmpl   $0x0,-0x2c(%rbp)
jne    11f1 <_read_data+0xa1>
movl   $0x0,-0x4(%rbp)
jmp    1243 <_read_data+0xf3>
jmp    11f6 <_read_data+0xa6>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     123e <_read_data+0xee>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jg     1233 <_read_data+0xe3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <_read_data+0xf3>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1243 <_read_data+0xf3>
jmp    1187 <_read_data+0x37>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,0xc(%rsp)
mov    %rsi,0x18(%rsp)
mov    %edi,%ebp
movslq %ecx,%r14
imul   $0x431bde83,%r14,%r15
mov    %r15,%rax
shr    $0x3f,%rax
sar    $0x32,%r15
add    %eax,%r15d
imul   $0xf4240,%r15d,%eax
sub    %eax,%r14d
lea    0x1(%rbp),%r12d
lea    0x24(%rsp),%rbx
lea    0x10(%rsp),%r13
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
mov    %r15d,0x10(%rsp)
mov    %r14d,0x14(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1060 <select@plt>
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jb     1201 <_read_data+0xb1>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11a0 <_read_data+0x50>
mov    %ebp,%edi
mov    0x18(%rsp),%rsi
mov    0xc(%rsp),%edx
call   1040 <read@plt>
mov    %eax,%ecx
test   %eax,%eax
mov    $0xffffffff,%eax
cmovg  %ecx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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