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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/ *,
           struct timeval *);
int timed_read(int from, unsigned char *buf, int n, int block) {
  if (block) {
    return read(from, buf, n);
  } else {
    int ret;
    fd_set readfd;
    struct timeval to;
    FD_ZERO(&readfd);
    FD_SET(from, &readfd);
    to.tv_sec = 0;
    to.tv_usec = 20 * 1000;
    ret = select(from + 1, &readfd, 0, &readfd, &to);
    if (ret == 0)
      return -2;
    return read(from, buf, n);
  }
}


x86 assembly (clang, -O0):
<timed_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1176 <timed_read+0x36>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x4(%rbp)
jmp    11dd <timed_read+0x9d>
lea    -0x20(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x28(%rbp)
movl   $0x4e20,-0x30(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rcx
xor    %edx,%edx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
call   1050 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11cb <timed_read+0x8b>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11dd <timed_read+0x9d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timed_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
test   %ecx,%ecx
je     116d <timed_read+0x2d>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <read@plt>
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
movl   $0x4e20,0x8(%rsp)
lea    0x1(%rbp),%edi
lea    0x8(%rsp),%r8
mov    %r15,%rsi
xor    %edx,%edx
mov    %r15,%rcx
call   1050 <select@plt>
test   %eax,%eax
je     11bd <timed_read+0x7d>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
jmp    11c2 <timed_read+0x82>
mov    $0xfffffffe,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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