#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 /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
int readchar(int fd, unsigned char *c, int timeout) {
  struct timeval tv;
  fd_set set;
  tv.tv_sec = 0;
  tv.tv_usec = timeout * 1000;
  FD_ZERO(&set);
  FD_SET(fd, &set);
  if (!select(fd + 1, &set, NULL, NULL, &tv))
    return -1;
  read(fd, c, 1);
  return 0;
}


x86 assembly (clang, -O0):
<readchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
imul   $0x3e8,-0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%eax
jne    11a8 <readchar+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c0 <readchar+0x80>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readchar>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
movq   $0x0,0x10(%rsp)
imul   $0x3e8,%edx,%eax
mov    %eax,0x8(%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
xor    %r15d,%r15d
lea    0x8(%rsp),%r8
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
je     11a6 <readchar+0x66>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
jmp    11ac <readchar+0x6c>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @readchar(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %10
%11 = load i32, i32* %7
%12 = mul nsw i32 %11, 1000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %12, i32* %13
%14 = call i32 @FD_ZERO(i32* %9)
%15 = load i32, i32* %5
%16 = call i32 @FD_SET(i32 %15, i32* %9)
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
%19 = call i32 @select(i32 %18, i32* %9, i32* null, i32* null, %struct.timeval* %8)
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %3
store i32 -1, i32* %4
br label %26
22: ; preds = %3
%23 = load i32, i32* %5
%24 = load i8*, i8** %6
%25 = call i32 @read(i32 %23, i8* %24, i32 1)
store i32 0, i32* %4
br label %26
26: ; preds = %22, %21
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @readchar(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %8
%9 = mul nsw i32 %2, 1000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %9, i32* %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* %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = call i32 @read(i32 %0, i8* %1, i32 1)
br label %18
18: ; preds = %3, %16
%19 = phi i32 [ 0, %16 ], [ -1, %3 ]
ret i32 %19
}