#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 */
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ TCIFLUSH;
int read(int /*<<< orphan*/, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ tcflush(int /*<<< orphan*/, int /*<<< orphan*/);
int term_read() {
  int n = 1;
  unsigned char ch;
  struct timeval tv;
  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(0, &rfds);
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  n = select(1, &rfds, NULL, NULL, &tv);
  if (n > 0) {
    n = read(0, &ch, 1);
    tcflush(0, TCIFLUSH);
    if (n == 1)
      return ch;
    return n;
  }
  return -1;
}


x86 assembly (clang, -O0):
<term_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x1,-0x8(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x24(%rbp),%rsi
call   1070 <FD_SET@plt>
movl   $0x1,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11ec <term_read+0x9c>
xor    %edi,%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <tcflush@plt>
cmpl   $0x1,-0x8(%rbp)
jne    11e1 <term_read+0x91>
movzbl -0x9(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <term_read+0xa3>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <term_read+0xa3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_read>:
push   %rbx
sub    $0x20,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
movl   $0x1,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
jle    11c6 <term_read+0x76>
lea    0xb(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ebx
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1030 <tcflush@plt>
cmp    $0x1,%ebx
jne    11cb <term_read+0x7b>
movzbl 0xb(%rsp),%ebx
jmp    11cb <term_read+0x7b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@TCIFLUSH = global i32 0
define i32 @term_read() {
%1 = alloca i8
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @FD_ZERO(i32* %3)
%7 = call i32 @FD_SET(i32 0, i32* %3)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 1, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %9
%10 = call i32 @select(i32 1, i32* %3, i32* null, i32* null, %struct.timeval* %2)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %0
%13 = call i32 @read(i32 0, i8* %1, i32 1)
%14 = load i32, i32* @TCIFLUSH
%15 = call i32 @tcflush(i32 0, i32 %14)
%16 = icmp eq i32 %13, 1
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8, i8* %1
%19 = zext i8 %18 to i32
br label %20
20: ; preds = %0, %12, %17
%21 = phi i32 [ %19, %17 ], [ %13, %12 ], [ -1, %0 ]
ret i32 %21
}