#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;
  int tv_usec;
};
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*/ printf(char *);
int /*<<< orphan*/ select(scalar_t__, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          struct timeval *);
int main(void) {
  struct timeval tv;
  fd_set readfds;
  tv.tv_sec = 2;
  tv.tv_usec = 500000;
  FD_ZERO(&readfds);
  FD_SET(0, &readfds);
  select(0 + 1, &readfds, NULL, NULL, &tv);
  if (FD_ISSET(0, &readfds))
    printf("A key was pressed!\n");
  else
    printf("Timed out.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2,-0x10(%rbp)
movl   $0x7a120,-0xc(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11c0 <main+0x70>
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
jmp    11cc <main+0x7c>
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movabs $0x7a12000000002,%rax
mov    %rax,(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
mov    %rsp,%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
lea    0xe5e(%rip),%rax
lea    0xe62(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [20 x i8] c"A key was pressed!\0A\00"
@.str.1 = [12 x i8] c"Timed out.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
store i32 0, i32* %1
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
store i32 2, i32* %4
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
store i32 500000, i32* %5
%6 = call i32 @FD_ZERO(i32* %3)
%7 = call i32 @FD_SET(i32 0, i32* %3)
%8 = call i32 @select(i64 1, i32* %3, i32* null, i32* null, %struct.timeval* %2)
%9 = call i64 @FD_ISSET(i32 0, i32* %3)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %15
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %11
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@str = [11 x i8] c"Timed out.\00"
@str.2 = [19 x i8] c"A key was pressed!\00"
define i32 @main() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
store i32 2, i32* %5
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
store i32 500000, i32* %6
%7 = call i32 @FD_ZERO(i32* %2)
%8 = call i32 @FD_SET(i32 0, i32* %2)
%9 = call i32 @select(i64 1, i32* %2, i32* null, i32* null, %struct.timeval* %1)
%10 = call i64 @FD_ISSET(i32 0, i32* %2)
%11 = icmp eq i64 %10, 0
%12 = select i1 %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @str.2, i64 0, i64 0)
%13 = call i32 @puts(i8* dereferenceable(1) %12)
ret i32 0
}