#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*/ EXIT_SUCCESS;
int FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main() {
  fd_set rfds;
  struct timeval tv;
  int retval;
  char buf[1024] = {0};
  FD_ZERO(&rfds);
  FD_SET(0, &rfds);
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  retval = select(1, &rfds, NULL, NULL, &tv);
  if (retval == -1)
    perror("select()");
  else if (retval) {
    printf("Data is available now.\n");
    printf("FD_ISSET(0, fd_set) is %d\n", FD_ISSET(0, &rfds));
    read(0, buf, 1024);
    printf("buf = %s\n", buf);
  } else
    printf("No data within five seconds.\n");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0x8(%rbp),%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   10b0 <FD_SET@plt>
movl   $0x5,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    120f <main+0x7f>
lea    0xdfb(%rip),%rdi
call   1090 <perror@plt>
jmp    1282 <main+0xf2>
cmpl   $0x0,-0x1c(%rbp)
je     126f <main+0xdf>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
mov    %eax,%esi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1060 <read@plt>
lea    -0x420(%rbp),%rsi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127d <main+0xed>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1282 <main+0xf2>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x420,%rsp
lea    0x20(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <select@plt>
cmp    $0xffffffff,%eax
jne    1216 <main+0x76>
lea    0xdfd(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
test   %eax,%eax
jne    1228 <main+0x88>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
jmp    1208 <main+0x68>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rsi
xor    %edi,%edi
call   1040 <FD_ISSET@plt>
lea    0xdc2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x20(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    0xdb9(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1208 <main+0x68>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [9 x i8] c"select()\00"
@.str.1 = [24 x i8] c"Data is available now.\0A\00"
@.str.2 = [27 x i8] c"FD_ISSET(0, fd_set) is %d\0A\00"
@.str.3 = [10 x i8] c"buf = %s\0A\00"
@.str.4 = [30 x i8] c"No data within five seconds.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca [1024 x i8]
store i32 0, i32* %1
%6 = bitcast [1024 x i8]* %5 to i8*
%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 = call i32 @select(i32 1, i32* %2, i32* null, i32* null, %struct.timeval* %3)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %30
16: ; preds = %0
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @FD_ISSET(i32 0, i32* %2)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = call i32 @read(i32 0, i8* %23, i32 1024)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %29
27: ; preds = %16
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %19
br label %30
30: ; preds = %29, %14
%31 = load i32, i32* @EXIT_SUCCESS
%32 = call i32 @exit(i32 %31)
unreachable
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [9 x i8] c"select()\00"
@.str.2 = [27 x i8] c"FD_ISSET(0, fd_set) is %d\0A\00"
@.str.3 = [10 x i8] c"buf = %s\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [29 x i8] c"No data within five seconds.\00"
@str.5 = [23 x i8] c"Data is available now.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca [1024 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.timeval* %2 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = call i32 @FD_ZERO(i32* %1)
%8 = call i32 @FD_SET(i32 0, i32* %1)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 5, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %10
%11 = call i32 @select(i32 1, i32* %1, i32* null, i32* null, %struct.timeval* %2)
switch i32 %11, label %14 [
i32 -1, label %12
i32 0, label %20
]
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %22
14: ; preds = %0
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.5, i64 0, i64 0))
%16 = call i32 @FD_ISSET(i32 0, i32* %1)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @read(i32 0, i8* %6, i32 1024)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %22
20: ; preds = %0
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %14, %20, %12
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
}