#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  fd_set global_reads, reads;
  int result, str_len;
  char buf[1024];
  struct timeval timeout;
  FD_ZERO(&global_reads);
  FD_SET(0, &global_reads);
  while (1) {
    reads = global_reads;
    timeout.tv_sec = 3;
    timeout.tv_usec = 0;
    result = select(1, &reads, NULL, NULL, &timeout);
    if (result == -1) {
      fprintf(stderr, "select() failed\n");
      break;
    } else if (result == 0) {
      printf("time out!\n");
      continue;
    }
    if (FD_ISSET(0, &reads)) {
      str_len = read(0, buf, sizeof(buf));
      buf[str_len] = '\0';
      printf("msg from console: %s", buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%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   $0x3,-0x430(%rbp)
movq   $0x0,-0x428(%rbp)
mov    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x430(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11fb <main+0x8b>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
call   1060 <fprintf@plt>
jmp    126e <main+0xfe>
cmpl   $0x0,-0x1c(%rbp)
jne    1218 <main+0xa8>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    119d <main+0x2d>
jmp    121d <main+0xad>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1269 <main+0xf9>
lea    -0x420(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    119d <main+0x2d>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x420,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
lea    0x8(%rsp),%rbx
lea    0x10(%rsp),%r14
lea    0xe6e(%rip),%r15
lea    0x20(%rsp),%r12
lea    0xe4d(%rip),%r13
jmp    11d8 <main+0x58>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
movl   $0x3,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1090 <select@plt>
test   %eax,%eax
je     11d0 <main+0x50>
cmp    $0xffffffff,%eax
je     1242 <main+0xc2>
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11d8 <main+0x58>
xor    %edi,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11d8 <main+0x58>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@stderr = global i32 0
@.str = [17 x i8] c"select() failed\0A\00"
@.str.1 = [11 x i8] c"time out!\0A\00"
@.str.2 = [21 x i8] c"msg from console: %s\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 [1024 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, %27, %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 3, 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* null, i32* null, %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 %24
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %42
24: ; preds = %14
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %14
29: ; preds = %24
br label %30
30: ; preds = %29
%31 = call i64 @FD_ISSET(i32 0, i32* %7)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %30
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%35 = call i32 @read(i32 0, i8* %34, i32 1024)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %33, %30
br label %14
42: ; preds = %21
ret i32 0
}

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