#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;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ STDIN_FILENO;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(scalar_t__, char *, int);
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main(void) {
  char rbuf[10];
  fd_set rd_fds;
  int ret, len;
  struct timeval tv;
  tv.tv_sec = 3;
  tv.tv_usec = 0;
  while (1) {
    FD_ZERO(&rd_fds);
    FD_SET(STDIN_FILENO, &rd_fds);
    tv.tv_sec = 3;
    tv.tv_usec = 0;
    ret = select(STDIN_FILENO + 1, &rd_fds, NULL, NULL, &tv);
    switch (ret) {
    case -1:
      perror("select");
      break;
    case 0:
      printf("timeout, goto next loop\n");
    default:
      printf("ret = %d\n", ret);
      if (FD_ISSET(STDIN_FILENO, &rd_fds)) {
        len = read(STDIN_FILENO, rbuf, 9);
        rbuf[len] = '\0';
        printf("%s", rbuf);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x3,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x14(%rbp),%rdi
mov    %rdi,-0x48(%rbp)
call   1060 <FD_ZERO@plt>
mov    -0x48(%rbp),%rsi
mov    0x2e3a(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    (%rax),%rdi
call   1090 <FD_SET@plt>
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rax
movl   $0x3,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    (%rax),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0xffffffff,%eax
je     1207 <main+0x97>
jmp    11f7 <main+0x87>
mov    -0x34(%rbp),%eax
test   %eax,%eax
je     1218 <main+0xa8>
jmp    1226 <main+0xb6>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
jmp    128e <main+0x11e>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%rdi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
je     1289 <main+0x119>
mov    0x2d86(%rip),%rax
mov    (%rax),%rdi
lea    -0xe(%rbp),%rsi
mov    $0x9,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0xe(%rbp,%rax,1)
lea    -0xe(%rbp),%rsi
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128e <main+0x11e>
jmp    118e <main+0x1e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x3,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x18(%rsp),%rbx
mov    0x2e35(%rip),%r15
lea    0x8(%rsp),%r14
lea    0xe50(%rip),%r12
lea    0x1e(%rsp),%r13
jmp    11cc <main+0x4c>
xchg   %ax,%ax
lea    0xe39(%rip),%rdi
call   1090 <perror@plt>
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    (%r15),%rdi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
movl   $0x3,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    (%r15),%rdi
add    $0x1,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1080 <select@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1220 <main+0xa0>
cmp    $0xffffffff,%ebp
je     11c0 <main+0x40>
jmp    122c <main+0xac>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     11cc <main+0x4c>
mov    (%r15),%rdi
mov    %r13,%rsi
mov    $0x9,%edx
call   1060 <read@plt>
cltq
movb   $0x0,0x1e(%rsp,%rax,1)
lea    0xdac(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11cc <main+0x4c>

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i64 0
@.str = [7 x i8] c"select\00"
@.str.2 = [10 x i8] c"ret = %d\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@str = [24 x i8] c"timeout, goto next loop\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 3, i32* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %8
br label %9
9: ; preds = %25, %0
%10 = call i32 @FD_ZERO(i32* %2)
%11 = load i64, i64* @STDIN_FILENO
%12 = call i32 @FD_SET(i64 %11, i32* %2)
store i32 3, i32* %7
store i64 0, i64* %8
%13 = load i64, i64* @STDIN_FILENO
%14 = add nsw i64 %13, 1
%15 = call i32 @select(i64 %14, i32* %2, i32* null, i32* null, %struct.timeval* %3)
switch i32 %15, label %20 [
i32 -1, label %16
i32 0, label %18
]
16: ; preds = %9
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %25
18: ; preds = %9
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %9, %18
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%22 = load i64, i64* @STDIN_FILENO
%23 = call i32 @FD_ISSET(i64 %22, i32* %2)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20, %26, %16
br label %9
26: ; preds = %20
%27 = load i64, i64* @STDIN_FILENO
%28 = call i32 @read(i64 %27, i8* %4, i32 9)
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %25
}