#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 */
int /*<<< orphan*/ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main() {
  char buffer[128];
  int result, nread;
  fd_set inputs, testfds;
  struct timeval timeout;
  FD_ZERO(&inputs);
  FD_SET(0, &inputs);
  while (1) {
    testfds = inputs;
    timeout.tv_sec = 2;
    timeout.tv_usec = 500000;
    result = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0, &timeout);
    switch (result) {
    case 0:
      printf("timeout\n");
      break;
    case -1:
      perror("select");
      exit(1);
    default:
      if (FD_ISSET(0, &testfds)) {
        ioctl(0, FIONREAD, &nread);
        if (nread == 0) {
          printf("keyboard done\n");
          exit(0);
        }
        nread = read(0, buffer, nread);
        buffer[nread] = 0;
        printf("read %d from keyboard: %s", nread, buffer);
      }
      break;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x9c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x9c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x9c(%rbp),%eax
mov    %eax,-0xa0(%rbp)
movl   $0x2,-0xa8(%rbp)
movl   $0x7a120,-0xa4(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0xa0(%rbp),%rsi
lea    -0xa8(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x94(%rbp)
mov    -0x94(%rbp),%eax
mov    %eax,-0xac(%rbp)
sub    $0xffffffff,%eax
je     1245 <main+0xb5>
jmp    121f <main+0x8f>
mov    -0xac(%rbp),%eax
test   %eax,%eax
jne    125b <main+0xcb>
jmp    1232 <main+0xa2>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f4 <main+0x164>
lea    0xdbd(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
lea    -0xa0(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
je     12ef <main+0x15f>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x98(%rbp),%rdx
call   1050 <ioctl@plt>
cmpl   $0x0,-0x98(%rbp)
jne    12ab <main+0x11b>
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    -0x90(%rbp),%rsi
mov    -0x98(%rbp),%edx
xor    %edi,%edi
call   1060 <read@plt>
mov    %eax,-0x98(%rbp)
movslq -0x98(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
mov    -0x98(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f4 <main+0x164>
jmp    11bc <main+0x2c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
movabs $0x7a12000000002,%r12
mov    0x2dff(%rip),%rbp
lea    0x10(%rsp),%rbx
lea    0x18(%rsp),%r14
lea    0xe37(%rip),%r15
lea    0x20(%rsp),%r13
jmp    1208 <main+0x68>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    0x14(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %r12,0x18(%rsp)
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1090 <select@plt>
test   %eax,%eax
je     1200 <main+0x60>
cmp    $0xffffffff,%eax
je     1284 <main+0xe4>
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     1208 <main+0x68>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xc(%rsp),%rdx
call   1060 <ioctl@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     129a <main+0xfa>
xor    %edi,%edi
mov    %r13,%rsi
call   1070 <read@plt>
mov    %eax,0xc(%rsp)
movslq %eax,%rsi
movb   $0x0,0x20(%rsp,%rsi,1)
lea    0xd8f(%rip),%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1208 <main+0x68>
lea    0xd75(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@FD_SETSIZE = global i32 0
@.str = [9 x i8] c"timeout\0A\00"
@.str.1 = [7 x i8] c"select\00"
@FIONREAD = global i32 0
@.str.2 = [15 x i8] c"keyboard done\0A\00"
@.str.3 = [26 x i8] c"read %d from keyboard: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
store i32 0, i32* %1
%8 = call i32 @FD_ZERO(i32* %5)
%9 = call i32 @FD_SET(i32 0, i32* %5)
br label %10
10: ; preds = %0, %44
%11 = load i32, i32* %5
store i32 %11, i32* %6
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 2, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i32 500000, i32* %13
%14 = load i32, i32* @FD_SETSIZE
%15 = call i32 @select(i32 %14, i32* %6, i32* null, i32* null, %struct.timeval* %7)
store i32 %15, i32* %3
%16 = load i32, i32* %3
switch i32 %16, label %22 [
i32 0, label %17
i32 -1, label %19
]
17: ; preds = %10
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %44
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %10
%23 = call i32 @FD_ISSET(i32 0, i32* %6)
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %43
25: ; preds = %22
%26 = load i32, i32* @FIONREAD
%27 = call i32 @ioctl(i32 0, i32 %26, i32* %4)
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %25
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%35 = load i32, i32* %4
%36 = call i32 @read(i32 0, i8* %34, i32 %35)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %38
store i8 0, i8* %39
%40 = load i32, i32* %4
%41 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %40, i8* %41)
br label %43
43: ; preds = %33, %22
br label %44
44: ; preds = %43, %17
br label %10
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@FD_SETSIZE = global i32 0
@.str.1 = [7 x i8] c"select\00"
@FIONREAD = global i32 0
@.str.3 = [26 x i8] c"read %d from keyboard: %s\00"
@str = [8 x i8] c"timeout\00"
@str.4 = [14 x i8] c"keyboard done\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast %struct.timeval* %5 to i8*
%11 = call i32 @FD_ZERO(i32* %3)
%12 = call i32 @FD_SET(i32 0, i32* %3)
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
br label %15
15: ; preds = %27, %0
%16 = load i32, i32* %3
store i32 %16, i32* %4
store i32 2, i32* %13
store i32 500000, i32* %14
%17 = load i32, i32* @FD_SETSIZE
%18 = call i32 @select(i32 %17, i32* %4, i32* null, i32* null, %struct.timeval* %5)
switch i32 %18, label %24 [
i32 0, label %19
i32 -1, label %21
]
19: ; preds = %15
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %27
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = call i32 @FD_ISSET(i32 0, i32* %4)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24, %36, %19
br label %15
28: ; preds = %24
%29 = load i32, i32* @FIONREAD
%30 = call i32 @ioctl(i32 0, i32 %29, i32* %2)
%31 = load i32, i32* %2
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0))
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %28
%37 = call i32 @read(i32 0, i8* %6, i32 %31)
store i32 %37, i32* %2
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %38
store i8 0, i8* %39
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %37, i8* %6)
br label %27
}