#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 */
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ STDIN_FILENO;
int getchar();
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int read_console() {
  int rb;
  struct timeval tv;
  int retval;
  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(STDIN_FILENO, &rfds);
  tv.tv_sec = 0;
  tv.tv_usec = 10000;
  retval = select(1, &rfds, NULL, NULL, &tv);
  if (!retval)
    rb = 0;
  else {
    if (FD_ISSET(STDIN_FILENO, &rfds))
      rb = getchar();
    else
      rb = 0;
  }
  return rb;
}


x86 assembly (clang, -O0):
<read_console>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x20(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1070 <FD_SET@plt>
movq   $0x0,-0x10(%rbp)
movl   $0x2710,-0x18(%rbp)
mov    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11b4 <read_console+0x64>
movl   $0x0,-0x4(%rbp)
jmp    11eb <read_console+0x9b>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11df <read_console+0x8f>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x4(%rbp)
jmp    11e6 <read_console+0x96>
movl   $0x0,-0x4(%rbp)
jmp    11eb <read_console+0x9b>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_console>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    0x2e73(%rip),%r15
mov    (%r15),%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
movq   $0x0,0x18(%rsp)
movl   $0x2710,0x10(%rsp)
xor    %r14d,%r14d
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
je     11c2 <read_console+0x72>
mov    (%r15),%edi
lea    0xc(%rsp),%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11c2 <read_console+0x72>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i32 0
define i32 @read_console() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i32
%5 = call i32 @FD_ZERO(i32* %4)
%6 = load i32, i32* @STDIN_FILENO
%7 = call i32 @FD_SET(i32 %6, i32* %4)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
store i32 10000, i32* %9
%10 = call i32 @select(i32 1, i32* %4, i32* null, i32* null, %struct.timeval* %2)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %0
store i32 0, i32* %1
br label %22
14: ; preds = %0
%15 = load i32, i32* @STDIN_FILENO
%16 = call i64 @FD_ISSET(i32 %15, i32* %4)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (...) @getchar()
store i32 %19, i32* %1
br label %21
20: ; preds = %14
store i32 0, i32* %1
br label %21
21: ; preds = %20, %18
br label %22
22: ; preds = %21, %13
%23 = load i32, i32* %1
ret i32 %23
}

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