#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*/ 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");
      break;
    default:
      if (FD_ISSET(0, &testfds)) {
        ioctl(0, FIONREAD, &nread);
        if (nread == 0) {
          printf("keyboard done\n");
          break;
        }
        nread = read(0, buffer, nread);
        buffer[nread] = 0;
        printf("read %d from keyboard: %s\n", nread, buffer);
      }
      break;
    }
  }
  return 0;
}


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   10a0 <FD_SET@plt>
mov    -0x9c(%rbp),%eax
mov    %eax,-0xa0(%rbp)
movl   $0x2,-0xa8(%rbp)
movl   $0x7a120,-0xa4(%rbp)
mov    0x2e05(%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     1235 <main+0xb5>
jmp    120f <main+0x8f>
mov    -0xac(%rbp),%eax
test   %eax,%eax
jne    1246 <main+0xc6>
jmp    1222 <main+0xa2>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12dd <main+0x15d>
lea    0xdcd(%rip),%rdi
call   1090 <perror@plt>
jmp    12dd <main+0x15d>
xor    %edi,%edi
lea    -0xa0(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
je     12d8 <main+0x158>
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x98(%rbp),%rdx
call   1050 <ioctl@plt>
cmpl   $0x0,-0x98(%rbp)
jne    1294 <main+0x114>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12dd <main+0x15d>
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    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12dd <main+0x15d>
jmp    11ac <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   10b0 <FD_SET@plt>
movabs $0x7a12000000002,%rbp
mov    0x2e0f(%rip),%r13
lea    0x10(%rsp),%rbx
lea    0x18(%rsp),%r14
lea    0xe48(%rip),%r15
lea    0x20(%rsp),%r12
jmp    11f8 <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    %rbp,0x18(%rsp)
mov    0x0(%r13),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1090 <select@plt>
test   %eax,%eax
je     11f0 <main+0x60>
cmp    $0xffffffff,%eax
je     1286 <main+0xf6>
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     11f8 <main+0x68>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xc(%rsp),%rdx
call   1060 <ioctl@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     1275 <main+0xe5>
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <read@plt>
mov    %eax,0xc(%rsp)
movslq %eax,%rsi
movb   $0x0,0x20(%rsp,%rsi,1)
lea    0xd9e(%rip),%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f8 <main+0x68>
lea    0xdae(%rip),%rdi
call   1030 <puts@plt>
jmp    11f8 <main+0x68>
lea    0xd73(%rip),%rdi
call   10a0 <perror@plt>
jmp    11f8 <main+0x68>

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 = [27 x i8] c"read %d from keyboard: %s\0A\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, %42
%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 %21 [
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 %42
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %42
21: ; preds = %10
%22 = call i32 @FD_ISSET(i32 0, i32* %6)
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %21
%25 = load i32, i32* @FIONREAD
%26 = call i32 @ioctl(i32 0, i32 %25, i32* %4)
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %42
31: ; preds = %24
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%33 = load i32, i32* %4
%34 = call i32 @read(i32 0, i8* %32, i32 %33)
store i32 %34, i32* %4
%35 = load i32, i32* %4
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %36
store i8 0, i8* %37
%38 = load i32, i32* %4
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %38, i8* %39)
br label %41
41: ; preds = %31, %21
br label %42
42: ; preds = %41, %29, %19, %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 = [27 x i8] c"read %d from keyboard: %s\0A\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 = %26, %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 %23 [
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 %26
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %26
23: ; preds = %15
%24 = call i32 @FD_ISSET(i32 0, i32* %4)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23, %34, %32, %21, %19
br label %15
27: ; preds = %23
%28 = load i32, i32* @FIONREAD
%29 = call i32 @ioctl(i32 0, i32 %28, i32* %2)
%30 = load i32, i32* %2
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0))
br label %26
34: ; preds = %27
%35 = call i32 @read(i32 0, i8* %6, i32 %30)
store i32 %35, i32* %2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %35, i8* %6)
br label %26
}