#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 {
  unsigned int tv_sec;
  scalar_t__ tv_usec;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ ETIME;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int read(int, void *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
ssize_t tread(int fd, void *buf, size_t nbytes, unsigned int timout) {
  int nfds;
  fd_set readfds;
  struct timeval tv;
  tv.tv_sec = timout;
  tv.tv_usec = 0;
  FD_ZERO(&readfds);
  FD_SET(fd, &readfds);
  nfds = select(fd + 1, &readfds, NULL, NULL, &tv);
  if (nfds <= 0) {
    if (nfds == 0)
      errno = ETIME;
    return (-1);
  }
  return (read(fd, buf, nbytes));
}


x86 assembly (clang, -O0):
<tread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1060 <FD_SET@plt>
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   1050 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11c8 <tread+0x88>
cmpl   $0x0,-0x20(%rbp)
jne    11bc <tread+0x7c>
mov    0x2e37(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <tread+0x9b>
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    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tread>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    %ecx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x8(%rsp),%r8
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
jle    119e <tread+0x5e>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
jmp    11b7 <tread+0x77>
mov    $0xffffffff,%eax
jne    11b7 <tread+0x77>
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e23(%rip),%rdx
mov    %ecx,(%rdx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@ETIME = global i32 0
@errno = global i32 0
define i32 @tread(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%13 = load i32, i32* %9
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %15
%16 = call i32 @FD_ZERO(i32* %11)
%17 = load i32, i32* %6
%18 = call i32 @FD_SET(i32 %17, i32* %11)
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
%21 = call i32 @select(i32 %20, i32* %11, i32* null, i32* null, %struct.timeval* %12)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %4
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i32, i32* @ETIME
store i32 %28, i32* @errno
br label %29
29: ; preds = %27, %24
store i32 -1, i32* %5
br label %35
30: ; preds = %4
%31 = load i32, i32* %6
%32 = load i8*, i8** %7
%33 = load i64, i64* %8
%34 = call i32 @read(i32 %31, i8* %32, i64 %33)
store i32 %34, i32* %5
br label %35
35: ; preds = %30, %29
%36 = load i32, i32* %5
ret i32 %36
}

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