#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 /*<<< orphan*/ 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*/ STDIN_FILENO;
int /*<<< orphan*/ printf(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
int main(void) {
  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(STDIN_FILENO, &rfds);
  struct timeval tv = {.tv_sec = 3, .tv_usec = 0};
  select(1, &rfds, NULL, NULL, &tv);
  if (FD_ISSET(STDIN_FILENO, &rfds)) {
    char buf[100];
    int len = read(STDIN_FILENO, buf, 100);
    printf("key press\n");
  } else
    printf("timeout\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    0xe6c(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11fb <main+0x9b>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x84(%rbp)
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
jmp    1207 <main+0xa7>
lea    0xe11(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    0x2e65(%rip),%r14
mov    (%r14),%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
movq   $0x3,(%rsp)
mov    %rsp,%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11cd <main+0x6d>
mov    (%r14),%edi
lea    0x10(%rsp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
lea    0xe3d(%rip),%rdi
jmp    11d4 <main+0x74>
lea    0xe2c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@STDIN_FILENO = global i32 0
@__const.main.tv = %struct.timeval { i32 3, i32 0 }
@.str = [11 x i8] c"key press\0A\00"
@.str.1 = [9 x i8] c"timeout\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca [100 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @FD_ZERO(i32* %2)
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @FD_SET(i32 %7, i32* %2)
%9 = bitcast %struct.timeval* %3 to i8*
%10 = call i32 @select(i32 1, i32* %2, i32* null, i32* null, %struct.timeval* %3)
%11 = load i32, i32* @STDIN_FILENO
%12 = call i64 @FD_ISSET(i32 %11, i32* %2)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %0
%15 = load i32, i32* @STDIN_FILENO
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 100)
store i32 %17, i32* %5
%18 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %21
19: ; preds = %0
%20 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@STDIN_FILENO = global i32 0
@str = [8 x i8] c"timeout\00"
@str.2 = [10 x i8] c"key press\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.timeval*
%4 = alloca [100 x i8]
%5 = bitcast i32* %1 to i8*
%6 = call i32 @FD_ZERO(i32* %1)
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @FD_SET(i32 %7, i32* %1)
%9 = bitcast i64* %2 to i8*
store i64 3, i64* %2
%10 = call i32 @select(i32 1, i32* %1, i32* null, i32* null, %struct.timeval* %3)
%11 = load i32, i32* @STDIN_FILENO
%12 = call i64 @FD_ISSET(i32 %11, i32* %1)
%13 = icmp eq i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %0
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* @STDIN_FILENO
%17 = call i32 @read(i32 %16, i8* %15, i32 100)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.2, i64 0, i64 0))
br label %21
19: ; preds = %0
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
ret i32 0
}