#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 ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
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 *);
ssize_t read_with_timeout(int from, char *buffer, int size, int us) {
  fd_set fds;
  FD_ZERO(&fds);
  FD_SET(from, &fds);
  struct timeval tv;
  tv.tv_sec = us / 1000000;
  tv.tv_usec = us % 1000000;
  int retval = select(from + 1, &fds, 0, 0, &tv);
  if (retval > 0)
    return read(from, buffer, size);
  else
    return -2;
}


x86 assembly (clang, -O0):
<read_with_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x18(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x24(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
mov    %ecx,%edx
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11c1 <read_with_timeout+0x81>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x4(%rbp)
jmp    11c8 <read_with_timeout+0x88>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_with_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
lea    0xc(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
movslq %ebx,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0x4(%rsp)
lea    0x1(%rbp),%edi
mov    %rsp,%r8
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
jle    11b8 <read_with_timeout+0x78>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
jmp    11bd <read_with_timeout+0x7d>
mov    $0xfffffffe,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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