#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 /*<<< 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;
  if (read(fd, c, 1) != 1)
    return -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    11d5 <readchar+0x95>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     11ce <readchar+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <readchar+0x95>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readchar>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
movq   $0x0,0x18(%rsp)
imul   $0x3e8,%edx,%eax
mov    %eax,0x10(%rsp)
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x10(%rsp),%r8
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
je     11ad <readchar+0x6d>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
setne  %al
neg    %eax
jmp    11b2 <readchar+0x72>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
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 %29
22: ; preds = %3
%23 = load i32, i32* %5
%24 = load i8*, i8** %6
%25 = call i32 @read(i32 %23, i8* %24, i32 1)
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 -1, i32* %4
br label %29
28: ; preds = %22
store i32 0, i32* %4
br label %29
29: ; preds = %28, %27, %21
%30 = load i32, i32* %4
ret i32 %30
}

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 %20, label %16
16: ; preds = %3
%17 = call i32 @read(i32 %0, i8* %1, i32 1)
%18 = icmp ne i32 %17, 1
%19 = sext i1 %18 to i32
br label %20
20: ; preds = %16, %3
%21 = phi i32 [ -1, %3 ], [ %19, %16 ]
ret i32 %21
}