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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ assert(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main() {
  int keyboard;
  int ret, i;
  char c;
  fd_set readfd;
  struct timeval timeout;
  keyboard = open("/dev/tty", O_RDONLY | O_NONBLOCK);
  assert(keyboard > 0);
  while (1) {
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    FD_ZERO(&readfd);
    FD_SET(keyboard, &readfd);
    ret = select(keyboard + 1, &readfd, NULL, NULL, &timeout);
    if (ret == -1)
      perror("select error");
    else if (ret) {
      if (FD_ISSET(keyboard, &readfd)) {
        i = read(keyboard, &c, 1);
        if ('\n' == c)
          continue;
        printf("the input is %c\n", c);
        if ('q' == c)
          break;
      }
    } else if (ret == 0)
      printf("time out/n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0x5,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    122d <main+0x9d>
lea    0xde6(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cb <main+0x13b>
cmpl   $0x0,-0xc(%rbp)
je     12a9 <main+0x119>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12a4 <main+0x114>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
movsbl -0x11(%rbp),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    1277 <main+0xe7>
jmp    11d1 <main+0x41>
movsbl -0x11(%rbp),%esi
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x11(%rbp),%ecx
mov    $0x71,%eax
cmp    %ecx,%eax
jne    129f <main+0x10f>
jmp    12d0 <main+0x140>
jmp    12a4 <main+0x114>
jmp    12c6 <main+0x136>
cmpl   $0x0,-0xc(%rbp)
jne    12c1 <main+0x131>
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c6 <main+0x136>
jmp    12cb <main+0x13b>
jmp    11d1 <main+0x41>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e3b(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe49(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1060 <assert@plt>
mov    %ebx,%r14d
add    $0x1,%r14d
lea    0x4(%rsp),%rbp
lea    0x8(%rsp),%r15
lea    0xe44(%rip),%r12
jmp    11fa <main+0x6a>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x5,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1080 <select@plt>
test   %eax,%eax
je     11f0 <main+0x60>
cmp    $0xffffffff,%eax
je     1281 <main+0xf1>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11fa <main+0x6a>
mov    %ebx,%edi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl 0x3(%rsp),%r13d
cmp    $0xa,%r13d
je     11fa <main+0x6a>
lea    0xdab(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x71,%r13b
jne    11fa <main+0x6a>
jmp    1292 <main+0x102>
lea    0xd81(%rip),%rdi
call   10a0 <perror@plt>
jmp    11fa <main+0x6a>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [13 x i8] c"select error\00"
@.str.2 = [17 x i8] c"the input is %c\0A\00"
@.str.3 = [11 x i8] c"time out/n\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca %struct.timeval
store i32 0, i32* %1
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
br label %16
16: ; preds = %0, %42, %60
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 5, i32* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %18
%19 = call i32 @FD_ZERO(i32* %6)
%20 = load i32, i32* %2
%21 = call i32 @FD_SET(i32 %20, i32* %6)
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
%24 = call i32 @select(i32 %23, i32* %6, i32* null, i32* null, %struct.timeval* %7)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %60
29: ; preds = %16
%30 = load i32, i32* %3
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %53
32: ; preds = %29
%33 = load i32, i32* %2
%34 = call i64 @FD_ISSET(i32 %33, i32* %6)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %52
36: ; preds = %32
%37 = load i32, i32* %2
%38 = call i32 @read(i32 %37, i8* %5, i32 1)
store i32 %38, i32* %4
%39 = load i8, i8* %5
%40 = sext i8 %39 to i32
%41 = icmp eq i32 10, %40
br i1 %41, label %42, label %43
42: ; preds = %36
br label %16
43: ; preds = %36
%44 = load i8, i8* %5
%45 = sext i8 %44 to i32
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = load i8, i8* %5
%48 = sext i8 %47 to i32
%49 = icmp eq i32 113, %48
br i1 %49, label %50, label %51
50: ; preds = %43
br label %61
51: ; preds = %43
br label %52
52: ; preds = %51, %32
br label %59
53: ; preds = %29
%54 = load i32, i32* %3
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
br label %59
59: ; preds = %58, %52
br label %60
60: ; preds = %59, %27
br label %16
61: ; preds = %50
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [13 x i8] c"select error\00"
@.str.2 = [17 x i8] c"the input is %c\0A\00"
@.str.3 = [11 x i8] c"time out/n\00"
define i32 @main() {
%1 = alloca i8
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp sgt i32 %9, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%15 = add nsw i32 %9, 1
br label %16
16: ; preds = %22, %0
store i32 5, i32* %13
store i64 0, i64* %14
%17 = call i32 @FD_ZERO(i32* %2)
%18 = call i32 @FD_SET(i32 %9, i32* %2)
%19 = call i32 @select(i32 %15, i32* %2, i32* null, i32* null, %struct.timeval* %3)
switch i32 %19, label %23 [
i32 -1, label %20
i32 0, label %34
]
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %34, %23, %30, %26
br label %16
23: ; preds = %16
%24 = call i64 @FD_ISSET(i32 %9, i32* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %22, label %26
26: ; preds = %23
%27 = call i32 @read(i32 %9, i8* %1, i32 1)
%28 = load i8, i8* %1
%29 = icmp eq i8 %28, 10
br i1 %29, label %22, label %30
30: ; preds = %26
%31 = sext i8 %28 to i32
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = icmp eq i8 %28, 113
br i1 %33, label %36, label %22
34: ; preds = %16
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %22
36: ; preds = %30
ret i32 0
}