#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;
typedef int /*<<< orphan*/ buf;

/* 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*/ bzero(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main() {
  fd_set rd;
  struct timeval tv;
  int err;
  FD_ZERO(&rd);
  FD_SET(0, &rd);
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  FD_SET(STDIN_FILENO, &rd);
  err = select(1, &rd, NULL, NULL, &tv);
  if (err == 0) {
    printf("select time out!\n");
  } else if (err == -1) {
    printf("fail to select!\n");
  } else {
    printf("data is available!\n");
    if (FD_ISSET(STDIN_FILENO, &rd)) {
      char buf[256];
      bzero(buf, sizeof(buf));
      int ret = read(STDIN_FILENO, buf, sizeof(buf) - 1);
      if (ret <= 0) {
        perror("recv");
        return 0;
      }
      printf("get %d bytes of normal data: %s \n", ret, buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   10a0 <FD_SET@plt>
movl   $0x5,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1200 <main+0x80>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c2 <main+0x142>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    121d <main+0x9d>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bd <main+0x13d>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12b8 <main+0x138>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jg     129d <main+0x11d>
lea    0xdab(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <main+0x149>
mov    -0x124(%rbp),%esi
lea    -0x120(%rbp),%rdx
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bd <main+0x13d>
jmp    12c2 <main+0x142>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x128,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e17(%rip),%r14
mov    (%r14),%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <select@plt>
cmp    $0xffffffff,%eax
je     1201 <main+0x71>
test   %eax,%eax
jne    120f <main+0x7f>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
jmp    1268 <main+0xd8>
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
jmp    1268 <main+0xd8>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%edi
lea    0xc(%rsp),%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1268 <main+0xd8>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1275 <main+0xe5>
lea    0xdab(%rip),%rdi
lea    0x20(%rsp),%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd84(%rip),%rdi
call   1090 <perror@plt>
jmp    1268 <main+0xd8>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i32 0
@.str = [18 x i8] c"select time out!\0A\00"
@.str.1 = [17 x i8] c"fail to select!\0A\00"
@.str.2 = [20 x i8] c"data is available!\0A\00"
@.str.3 = [5 x i8] c"recv\00"
@.str.4 = [34 x i8] c"get %d bytes of normal data: %s \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @FD_ZERO(i32* %2)
%8 = call i32 @FD_SET(i32 0, i32* %2)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 5, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %10
%11 = load i32, i32* @STDIN_FILENO
%12 = call i32 @FD_SET(i32 %11, i32* %2)
%13 = call i32 @select(i32 1, i32* %2, i32* null, i32* null, %struct.timeval* %3)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %44
18: ; preds = %0
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %43
23: ; preds = %18
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @STDIN_FILENO
%26 = call i64 @FD_ISSET(i32 %25, i32* %2)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %42
28: ; preds = %23
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%30 = call i32 @bzero(i8* %29, i32 256)
%31 = load i32, i32* @STDIN_FILENO
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 255)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp sle i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %45
38: ; preds = %28
%39 = load i32, i32* %6
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %39, i8* %40)
br label %42
42: ; preds = %38, %23
br label %43
43: ; preds = %42, %21
br label %44
44: ; preds = %43, %16
store i32 0, i32* %1
br label %45
45: ; preds = %44, %36
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i32 0
@.str.3 = [5 x i8] c"recv\00"
@.str.4 = [34 x i8] c"get %d bytes of normal data: %s \0A\00"
@str = [19 x i8] c"data is available!\00"
@str.5 = [16 x i8] c"fail to select!\00"
@str.6 = [17 x i8] c"select time out!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca [256 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.timeval* %2 to i8*
%6 = call i32 @FD_ZERO(i32* %1)
%7 = call i32 @FD_SET(i32 0, i32* %1)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 5, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %9
%10 = load i32, i32* @STDIN_FILENO
%11 = call i32 @FD_SET(i32 %10, i32* %1)
%12 = call i32 @select(i32 1, i32* %1, i32* null, i32* null, %struct.timeval* %2)
switch i32 %12, label %17 [
i32 0, label %13
i32 -1, label %15
]
13: ; preds = %0
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
br label %33
15: ; preds = %0
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
br label %33
17: ; preds = %0
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%19 = load i32, i32* @STDIN_FILENO
%20 = call i64 @FD_ISSET(i32 %19, i32* %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %33, label %22
22: ; preds = %17
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%24 = call i32 @bzero(i8* %23, i32 256)
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @read(i32 %25, i8* %23, i32 255)
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %32
30: ; preds = %22
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i32 %26, i8* %23)
br label %32
32: ; preds = %30, %28
br label %33
33: ; preds = %32, %13, %17, %15
ret i32 0
}