#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*/ *);
unsigned char read_char(int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct timeval *);
unsigned char read_char_timeout(int fd, int timeout, int *failed) {
  unsigned char c = 0xff;
  fd_set rfds;
  struct timeval timeout_struct;
  timeout_struct.tv_sec = 0;
  timeout_struct.tv_usec = timeout;
  *failed = 0;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  int result = select(fd + 1, &rfds, 0, 0, &timeout_struct);
  if (result > 0) {
    c = read_char(fd);
  } else
    *failed = 1;
  return c;
}


x86 assembly (clang, -O0):
<read_char_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movb   $0xff,-0x11(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x18(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
mov    %ecx,%edx
call   1040 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11b7 <read_char_timeout+0x77>
mov    -0x4(%rbp),%edi
call   1050 <read_char@plt>
mov    %al,-0x11(%rbp)
jmp    11c1 <read_char_timeout+0x81>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
movzbl -0x11(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_char_timeout>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %edi,%ebx
movq   $0x0,0x18(%rsp)
mov    %esi,0x10(%rsp)
movl   $0x0,(%rdx)
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1030 <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   1040 <select@plt>
test   %eax,%eax
jle    1199 <read_char_timeout+0x59>
mov    %ebx,%edi
call   1050 <read_char@plt>
jmp    11a2 <read_char_timeout+0x62>
movl   $0x1,(%r14)
mov    $0xff,%al
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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