#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_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char timedUserInput(int to, char def) {
  fd_set set;
  struct timeval timeout;
  char tmpStr[30];
  FD_ZERO(&set);
  FD_SET(0, &set);
  timeout.tv_sec = to;
  timeout.tv_usec = 0;
  int ready_for_reading = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
  if (ready_for_reading == 1) {
    read(0, tmpStr, 30);
    def = tmpStr[0];
  }
  return def;
}


x86 assembly (clang, -O0):
<timedUserInput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0xc(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x1,-0x44(%rbp)
jne    11b3 <timedUserInput+0x73>
lea    -0x40(%rbp),%rsi
xor    %edi,%edi
mov    $0x1e,%edx
call   1030 <read@plt>
mov    -0x40(%rbp),%al
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timedUserInput>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
mov    %ebp,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
cmp    $0x1,%eax
jne    11a6 <timedUserInput+0x66>
lea    0x20(%rsp),%rsi
xor    %edi,%edi
mov    $0x1e,%edx
call   1030 <read@plt>
mov    0x20(%rsp),%r14b
mov    %r14d,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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