#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 _fd_read(int fd, int timeoutMs, char *buffer, int length) {
  int position = 0, bytesRead = 0, selectResult = -1;
  struct timeval timeout;
  fd_set fds;
  while (position < length) {
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    timeout.tv_sec = timeoutMs / 1000;
    timeout.tv_usec = (timeoutMs % 1000) * 1000;
    selectResult = select(fd + 1, &fds, (fd_set *)0, (fd_set *)0, &timeout);
    if (selectResult == 0)
      return -77;
    if (FD_ISSET(fd, &fds)) {
      bytesRead = read(fd, buffer + position, length - position);
      if (bytesRead <= 0)
        return -1;
      else
        position += bytesRead;
    }
  }
  return length;
}


x86 assembly (clang, -O0):
<_fd_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    124b <_fd_read+0xfb>
lea    -0x34(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11f0 <_fd_read+0xa0>
movl   $0xffffffb3,-0x4(%rbp)
jmp    1251 <_fd_read+0x101>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1246 <_fd_read+0xf6>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x20(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1238 <_fd_read+0xe8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <_fd_read+0x101>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1246 <_fd_read+0xf6>
jmp    117a <_fd_read+0x2a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fd_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r12d
mov    %rdx,0x18(%rsp)
test   %ecx,%ecx
jle    122a <_fd_read+0xda>
mov    %edi,%ebx
movslq %esi,%rax
imul   $0x10624dd3,%rax,%r13
mov    %r13,%rcx
shr    $0x3f,%rcx
sar    $0x26,%r13
add    %ecx,%r13d
imul   $0x3e8,%r13d,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0xc(%rsp)
lea    0x1(%rbx),%r15d
xor    %r14d,%r14d
lea    0x24(%rsp),%rbp
jmp    11b5 <_fd_read+0x65>
nopl   0x0(%rax)
cmp    %r12d,%r14d
jge    122a <_fd_read+0xda>
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
mov    %r13d,0x10(%rsp)
mov    0xc(%rsp),%eax
mov    %eax,0x14(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   1060 <select@plt>
test   %eax,%eax
je     121c <_fd_read+0xcc>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11b0 <_fd_read+0x60>
movslq %r14d,%rax
mov    0x18(%rsp),%rcx
lea    (%rcx,%rax,1),%rsi
mov    %r12d,%edx
sub    %eax,%edx
mov    %ebx,%edi
call   1040 <read@plt>
test   %eax,%eax
jle    1224 <_fd_read+0xd4>
add    %eax,%r14d
jmp    11b0 <_fd_read+0x60>
mov    $0xffffffb3,%r12d
jmp    122a <_fd_read+0xda>
mov    $0xffffffff,%r12d
mov    %r12d,%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 @_fd_read(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 -1, i32* %12
br label %15
15: ; preds = %58, %4
%16 = load i32, i32* %10
%17 = load i32, i32* %9
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %59
19: ; preds = %15
%20 = call i32 @FD_ZERO(i32* %14)
%21 = load i32, i32* %6
%22 = call i32 @FD_SET(i32 %21, i32* %14)
%23 = load i32, i32* %7
%24 = sdiv i32 %23, 1000
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %7
%27 = srem i32 %26, 1000
%28 = mul nsw i32 %27, 1000
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
%32 = call i32 @select(i32 %31, i32* %14, i32* null, i32* null, %struct.timeval* %13)
store i32 %32, i32* %12
%33 = load i32, i32* %12
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %19
store i32 -77, i32* %5
br label %61
36: ; preds = %19
%37 = load i32, i32* %6
%38 = call i64 @FD_ISSET(i32 %37, i32* %14)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %58
40: ; preds = %36
%41 = load i32, i32* %6
%42 = load i8*, i8** %8
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i32, i32* %9
%47 = load i32, i32* %10
%48 = sub nsw i32 %46, %47
%49 = call i32 @read(i32 %41, i8* %45, i32 %48)
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = icmp sle i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %40
store i32 -1, i32* %5
br label %61
53: ; preds = %40
%54 = load i32, i32* %11
%55 = load i32, i32* %10
%56 = add nsw i32 %55, %54
store i32 %56, i32* %10
br label %57
57: ; preds = %53
br label %58
58: ; preds = %57, %36
br label %15
59: ; preds = %15
%60 = load i32, i32* %9
store i32 %60, i32* %5
br label %61
61: ; preds = %59, %52, %35
%62 = load i32, i32* %5
ret i32 %62
}

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