#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 */
typedef int /*<<< orphan*/ uint8_t;
struct timeval {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
scalar_t__ read(int, int /*<<< orphan*/ *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int read_timeout(int fd, fd_set *set, uint8_t *result, size_t length,
                 struct timeval *timeout) {
  while (length > 0) {
    FD_SET(fd, set);
    int retval = select(fd + 1, set, NULL, NULL, timeout);
    if (retval == -1 || retval == 0) {
      return -1;
    }
    ssize_t r = read(fd, result, length);
    if (r <= 0) {
      return -1;
    }
    length -= r;
    result += r;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11ee <read_timeout+0xbe>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     1193 <read_timeout+0x63>
cmpl   $0x0,-0x2c(%rbp)
jne    119f <read_timeout+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <read_timeout+0xc5>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jg     11ca <read_timeout+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <read_timeout+0xc5>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    114b <read_timeout+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,(%rsp)
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%r13d
lea    0x1(%r13),%ebp
mov    $0xffffffff,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
test   %rbx,%rbx
je     11a6 <read_timeout+0x76>
mov    %r13d,%edi
mov    %r12,%rsi
call   1050 <FD_SET@plt>
mov    %ebp,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    (%rsp),%r8
call   1040 <select@plt>
add    $0x1,%eax
cmp    $0x2,%eax
jb     11a9 <read_timeout+0x79>
mov    %r13d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
sub    %rax,%rbx
lea    (%r15,%rax,4),%r15
test   %rax,%rax
jg     1160 <read_timeout+0x30>
jmp    11a9 <read_timeout+0x79>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32 }
define i32 @read_timeout(i32 %0, i32* %1, i32* nocapture %2, i64 %3, %struct.timeval* %4) {
%6 = add nsw i32 %0, 1
br label %7
7: ; preds = %16, %5
%8 = phi i64 [ %3, %5 ], [ %19, %16 ]
%9 = phi i32* [ %2, %5 ], [ %20, %16 ]
%10 = icmp eq i64 %8, 0
br i1 %10, label %21, label %11
11: ; preds = %7
%12 = tail call i32 @FD_SET(i32 %0, i32* %1)
%13 = tail call i32 @select(i32 %6, i32* %1, i32* null, i32* null, %struct.timeval* %4)
%14 = add i32 %13, 1
%15 = icmp ult i32 %14, 2
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = tail call i64 @read(i32 %0, i32* %9, i64 %8)
%18 = icmp slt i64 %17, 1
%19 = sub i64 %8, %17
%20 = getelementptr inbounds i32, i32* %9, i64 %17
br i1 %18, label %21, label %7
21: ; preds = %16, %11, %7
%22 = phi i32 [ 0, %7 ], [ -1, %11 ], [ -1, %16 ]
ret i32 %22
}