#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;
    printf("before select\n");
    result = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0, &timeout);
    printf("relsut:%d\n", result);
    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    $0xc0,%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)
lea    0xe1d(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0xb4(%rbp)
mov    %al,-0xad(%rbp)
call   1040 <printf@plt>
mov    -0xb4(%rbp),%eax
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%ecx
lea    -0xa0(%rbp),%rsi
lea    -0xa8(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,%ecx
mov    -0xad(%rbp),%al
mov    %ecx,-0x94(%rbp)
mov    -0x94(%rbp),%esi
lea    0xdd7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x94(%rbp),%eax
mov    %eax,-0xac(%rbp)
sub    $0xffffffff,%eax
je     127d <main+0xed>
jmp    1257 <main+0xc7>
mov    -0xac(%rbp),%eax
test   %eax,%eax
jne    1293 <main+0x103>
jmp    126a <main+0xda>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132c <main+0x19c>
lea    0xd9f(%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     1327 <main+0x197>
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x98(%rbp),%rdx
call   1050 <ioctl@plt>
cmpl   $0x0,-0x98(%rbp)
jne    12e3 <main+0x153>
lea    0xd55(%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    0xd19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132c <main+0x19c>
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>
lea    0xe56(%rip),%rbx
mov    0x2e02(%rip),%rbp
lea    0x10(%rsp),%r14
lea    0x18(%rsp),%r15
lea    0xe19(%rip),%r12
jmp    11fc <main+0x5c>
nopl   0x0(%rax)
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
mov    0x14(%rsp),%eax
mov    %eax,0x10(%rsp)
movabs $0x7a12000000002,%rax
mov    %rax,0x18(%rsp)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    0x0(%rbp),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1090 <select@plt>
mov    %eax,%r13d
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r13d,%r13d
je     11f0 <main+0x50>
cmp    $0xffffffff,%r13d
je     12af <main+0x10f>
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     11fc <main+0x5c>
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xc(%rsp),%rdx
call   1060 <ioctl@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     12c5 <main+0x125>
xor    %edi,%edi
mov    %rbx,%r13
mov    %rbp,%rbx
lea    0x20(%rsp),%rbp
mov    %rbp,%rsi
call   1070 <read@plt>
mov    %eax,0xc(%rsp)
movslq %eax,%rsi
movb   $0x0,0x20(%rsp,%rsi,1)
lea    0xd71(%rip),%rdi
mov    %rbp,%rdx
mov    %rbx,%rbp
mov    %r13,%rbx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11fc <main+0x5c>
lea    0xd76(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd6f(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

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

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