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

/* Variables and functions */
int /*<<< orphan*/ ETIMEDOUT;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int read_byte(int fd, unsigned int timeout) {
  fd_set rfds;
  struct timeval tv;
  unsigned char byte;
  int got;
  int retval;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  tv.tv_sec = timeout / 1000000;
  tv.tv_usec = timeout % 1000000;
  retval = select(fd + 1, &rfds, NULL, NULL, &tv);
  if (retval == 0)
    errno = ETIMEDOUT;
  if (retval < 1)
    return -1;
  got = read(fd, &byte, 1);
  assert(got != 0);
  if (got < 0)
    return -1;
  return (int)byte;
}


x86 assembly (clang, -O0):
<read_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11ca <read_byte+0x7a>
mov    0x2e19(%rip),%rax
mov    (%rax),%ecx
mov    0x2e18(%rip),%rax
mov    %ecx,(%rax)
cmpl   $0x1,-0x24(%rbp)
jge    11e0 <read_byte+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <read_byte+0xd2>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0x20(%rbp)
jge    121b <read_byte+0xcb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <read_byte+0xd2>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_byte>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x1c(%rsp),%r14
mov    %r14,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <FD_SET@plt>
mov    %ebp,%eax
imul   $0x431bde83,%rax,%rax
shr    $0x32,%rax
mov    %eax,0x10(%rsp)
imul   $0xf4240,%eax,%eax
sub    %eax,%ebp
mov    %ebp,0x14(%rsp)
lea    0x1(%rbx),%edi
lea    0x10(%rsp),%r8
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
je     11da <read_byte+0x8a>
mov    $0xffffffff,%ebp
jle    11f1 <read_byte+0xa1>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%ebx
xor    %edi,%edi
test   %eax,%eax
setne  %dil
call   1040 <assert@plt>
test   %ebx,%ebx
js     11f1 <read_byte+0xa1>
movzbl 0xf(%rsp),%ebp
jmp    11f1 <read_byte+0xa1>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    0x2df6(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @read_byte(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = call i32 @FD_ZERO(i32* %6)
%12 = load i32, i32* %4
%13 = call i32 @FD_SET(i32 %12, i32* %6)
%14 = load i32, i32* %5
%15 = udiv i32 %14, 1000000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %5
%18 = urem i32 %17, 1000000
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %6, i32* null, i32* null, %struct.timeval* %7)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = load i32, i32* @ETIMEDOUT
store i32 %26, i32* @errno
br label %27
27: ; preds = %25, %2
%28 = load i32, i32* %10
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 -1, i32* %3
br label %44
31: ; preds = %27
%32 = load i32, i32* %4
%33 = call i32 @read(i32 %32, i8* %8, i32 1)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp ne i32 %34, 0
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %31
store i32 -1, i32* %3
br label %44
41: ; preds = %31
%42 = load i8, i8* %8
%43 = zext i8 %42 to i32
store i32 %43, i32* %3
br label %44
44: ; preds = %41, %40, %30
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @read_byte(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i8
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = call i32 @FD_ZERO(i32* %3)
%9 = call i32 @FD_SET(i32 %0, i32* %3)
%10 = udiv i32 %1, 1000000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %10, i32* %11
%12 = urem i32 %1, 1000000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %12, i32* %13
%14 = add nsw i32 %0, 1
%15 = call i32 @select(i32 %14, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = load i32, i32* @ETIMEDOUT
store i32 %18, i32* @errno
br label %30
19: ; preds = %2
%20 = icmp slt i32 %15, 1
br i1 %20, label %30, label %21
21: ; preds = %19
%22 = call i32 @read(i32 %0, i8* %5, i32 1)
%23 = icmp ne i32 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = icmp slt i32 %22, 0
br i1 %26, label %30, label %27
27: ; preds = %21
%28 = load i8, i8* %5
%29 = zext i8 %28 to i32
br label %30
30: ; preds = %17, %21, %19, %27
%31 = phi i32 [ %29, %27 ], [ -1, %19 ], [ -1, %21 ], [ -1, %17 ]
ret i32 %31
}