#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 */
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int read(int /*<<< orphan*/, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct timeval *);
int main(int argc, char *argv[]) {
  fd_set reads, temps;
  int result, str_len;
  char buf[30];
  struct timeval timeout;
  FD_ZERO(&reads);
  FD_SET(0, &reads);
  while (1) {
    temps = reads;
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    result = select(1, &temps, 0, 0, &timeout);
    if (result == -1) {
      puts("select() error!");
      break;
    } else if (result == 0) {
      puts("Time out!");
    } else {
      if (FD_ISSET(0, &temps)) {
        str_len = read(0, buf, 30);
        buf[str_len] = 0;
        printf("message from console: %s\n", buf);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x5,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x50(%rbp),%r8
mov    %ecx,%edx
call   1080 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e3 <main+0x73>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
jmp    124e <main+0xde>
cmpl   $0x0,-0x1c(%rbp)
jne    11fe <main+0x8e>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
jmp    1244 <main+0xd4>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     123f <main+0xcf>
lea    -0x40(%rbp),%rsi
xor    %edi,%edi
mov    $0x1e,%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xde0(%rip),%rdi
call   1050 <printf@plt>
jmp    1244 <main+0xd4>
jmp    1249 <main+0xd9>
jmp    119a <main+0x2a>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
lea    0x8(%rsp),%rbx
lea    0x10(%rsp),%r14
lea    0xe6b(%rip),%r15
lea    0x20(%rsp),%r12
lea    0xe69(%rip),%r13
jmp    11c8 <main+0x58>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1080 <select@plt>
test   %eax,%eax
je     11c0 <main+0x50>
cmp    $0xffffffff,%eax
je     1230 <main+0xc0>
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11c8 <main+0x58>
xor    %edi,%edi
mov    %r12,%rsi
mov    $0x1e,%edx
call   1060 <read@plt>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
jmp    11c8 <main+0x58>
lea    0xdc9(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [16 x i8] c"select() error!\00"
@.str.1 = [10 x i8] c"Time out!\00"
@.str.2 = [26 x i8] c"message from console: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [30 x i8]
%11 = alloca %struct.timeval
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 @FD_ZERO(i32* %6)
%13 = call i32 @FD_SET(i32 0, i32* %6)
br label %14
14: ; preds = %2, %41
%15 = load i32, i32* %6
store i32 %15, i32* %7
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 5, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %17
%18 = call i32 @select(i32 1, i32* %7, i32 0, i32 0, %struct.timeval* %11)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %42
23: ; preds = %14
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %40
28: ; preds = %23
%29 = call i64 @FD_ISSET(i32 0, i32* %7)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%33 = call i32 @read(i32 0, i8* %32, i32 30)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 %35
store i8 0, i8* %36
%37 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%38 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %31, %28
br label %40
40: ; preds = %39, %26
br label %41
41: ; preds = %40
br label %14
42: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [16 x i8] c"select() error!\00"
@.str.1 = [10 x i8] c"Time out!\00"
@.str.2 = [26 x i8] c"message from console: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [30 x i8]
%6 = alloca %struct.timeval
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%10 = bitcast %struct.timeval* %6 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* %6, i64 0, i32 0
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
br label %15
15: ; preds = %22, %2
%16 = load i32, i32* %3
store i32 %16, i32* %4
store i32 5, i32* %13
store i64 0, i64* %14
%17 = call i32 @select(i32 1, i32* %4, i32 0, i32 0, %struct.timeval* %6)
switch i32 %17, label %23 [
i32 -1, label %18
i32 0, label %20
]
18: ; preds = %15
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
ret i32 0
20: ; preds = %15
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %26, %23
br label %15
23: ; preds = %15
%24 = call i64 @FD_ISSET(i32 0, i32* %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %22, label %26
26: ; preds = %23
%27 = call i32 @read(i32 0, i8* %9, i32 30)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %22
}