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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int main() {
  int fd, num;
  fd_set rfds, wfds;
  char buf[20];
  memset(buf, 0, 20);
  fd = open("/dev/poll_select", O_RDWR | O_NONBLOCK);
  if (fd != -1) {
    while (1) {
      FD_ZERO(&rfds);
      FD_ZERO(&wfds);
      FD_SET(fd, &rfds);
      FD_SET(fd, &wfds);
      select(fd + 1, &rfds, &wfds, NULL, NULL);
      if (FD_ISSET(fd, &rfds)) {
        printf("Data can be read!\n");
        read(fd, buf, 20);
        memset(buf, 0, 20);
      }
      if (FD_ISSET(fd, &wfds)) {
        printf("Data can be written!\n");
      }
      sleep(1);
    }
  } else {
    printf("cannot open the device\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
lea    0xe3f(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     1291 <main+0x101>
jmp    11d8 <main+0x48>
lea    -0xc(%rbp),%rdi
call   1070 <FD_ZERO@plt>
lea    -0x10(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1080 <select@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1260 <main+0xd0>
lea    0xdd7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1060 <read@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1282 <main+0xf2>
lea    0xda7(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    11d8 <main+0x48>
lea    0xda2(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    0x2e32(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe38(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     129a <main+0x10a>
mov    %eax,%ebx
mov    %eax,%r14d
add    $0x1,%r14d
lea    0xc(%rsp),%r15
lea    0x8(%rsp),%rbp
lea    0xe4d(%rip),%r13
lea    0x10(%rsp),%r12
jmp    120a <main+0x7a>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %r15,%rdi
call   1070 <FD_ZERO@plt>
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   10a0 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10a0 <FD_SET@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1276 <main+0xe6>
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x14,%edx
call   1060 <read@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1200 <main+0x70>
lea    0xd98(%rip),%rdi
call   1030 <puts@plt>
jmp    1200 <main+0x70>
lea    0xd70(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/dev/poll_select\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [19 x i8] c"Data can be read!\0A\00"
@.str.2 = [22 x i8] c"Data can be written!\0A\00"
@.str.3 = [24 x i8] c"cannot open the device\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [20 x i8]
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 20)
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %1
%12 = load i32, i32* %1
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %43
14: ; preds = %0
br label %15
15: ; preds = %14, %41
%16 = call i32 @FD_ZERO(i32* %3)
%17 = call i32 @FD_ZERO(i32* %4)
%18 = load i32, i32* %1
%19 = call i32 @FD_SET(i32 %18, i32* %3)
%20 = load i32, i32* %1
%21 = call i32 @FD_SET(i32 %20, i32* %4)
%22 = load i32, i32* %1
%23 = add nsw i32 %22, 1
%24 = call i32 @select(i32 %23, i32* %3, i32* %4, i32* null, i32* null)
%25 = load i32, i32* %1
%26 = call i64 @FD_ISSET(i32 %25, i32* %3)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %15
%29 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %1
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 20)
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%34 = call i32 @memset(i8* %33, i32 0, i32 20)
br label %35
35: ; preds = %28, %15
%36 = load i32, i32* %1
%37 = call i64 @FD_ISSET(i32 %36, i32* %4)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = call i32 @sleep(i32 1)
br label %15
43: ; preds = %0
%44 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"/dev/poll_select\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@str = [23 x i8] c"cannot open the device\00"
@str.4 = [21 x i8] c"Data can be written!\00"
@str.5 = [18 x i8] c"Data can be read!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [20 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 20)
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %9, %8
%11 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %34, label %13
13: ; preds = %0
%14 = add nuw nsw i32 %11, 1
br label %15
15: ; preds = %13, %32
%16 = call i32 @FD_ZERO(i32* %1)
%17 = call i32 @FD_ZERO(i32* %2)
%18 = call i32 @FD_SET(i32 %11, i32* %1)
%19 = call i32 @FD_SET(i32 %11, i32* %2)
%20 = call i32 @select(i32 %14, i32* %1, i32* %2, i32* null, i32* null)
%21 = call i64 @FD_ISSET(i32 %11, i32* %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %15
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.5, i64 0, i64 0))
%25 = call i32 @read(i32 %11, i8* %6, i32 20)
%26 = call i32 @memset(i8* %6, i32 0, i32 20)
br label %27
27: ; preds = %23, %15
%28 = call i64 @FD_ISSET(i32 %11, i32* %2)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = call i32 @sleep(i32 1)
br label %15
34: ; preds = %0
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
ret i32 0
}