#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 */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char getchar();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int fd;
  char c;
  fd_set rd_fds, wr_fds;
  if (2 != argc) {
    printf("Syntax: cmd filename\n");
    exit(EXIT_FAILURE);
  }
  fd = open(argv[1], O_RDONLY);
  if (-1 == fd) {
    perror(argv[1]);
    exit(EXIT_FAILURE);
  }
  printf("Press [ENTER] poll\nPress [q] to quit:");
  getchar();
  do {
    FD_ZERO(&rd_fds);
    FD_ZERO(&wr_fds);
    FD_SET(fd, &rd_fds);
    FD_SET(fd, &wr_fds);
    select(fd + 1, &rd_fds, &wr_fds, NULL, NULL);
    if (FD_ISSET(fd, &rd_fds))
      printf("You can READ!!");
    if (FD_ISSET(fd, &wr_fds))
      printf("You can WRITE!!");
    c = getchar();
  } while ('q' != c);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     11de <main+0x3e>
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1220 <main+0x80>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xdef(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
lea    -0x1c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
lea    -0x20(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1080 <select@plt>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     129a <main+0xfa>
lea    0xda7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12bc <main+0x11c>
lea    0xd94(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%ecx
mov    $0x71,%eax
cmp    %ecx,%eax
jne    1233 <main+0x93>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    12aa <main+0xfa>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12b8 <main+0x108>
mov    %eax,%ebx
lea    0xe15(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
mov    %ebx,%r14d
add    $0x1,%r14d
lea    0x4(%rsp),%r15
mov    %rsp,%rbp
lea    0xe28(%rip),%r12
lea    0xe12(%rip),%r13
jmp    122b <main+0x7b>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <getchar@plt>
cmp    $0x71,%al
je     1292 <main+0xe2>
mov    %r15,%rdi
call   1080 <FD_ZERO@plt>
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   10d0 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1279 <main+0xc9>
mov    %r13,%rdi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1220 <main+0x70>
mov    %r12,%rdi
call   1050 <printf@plt>
jmp    1220 <main+0x70>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd94(%rip),%rdi
call   1030 <puts@plt>
jmp    12c1 <main+0x111>
mov    0x8(%rbp),%rdi
call   10b0 <perror@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Syntax: cmd filename\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [38 x i8] c"Press [ENTER] poll\0APress [q] to quit:\00"
@.str.2 = [15 x i8] c"You can READ!!\00"
@.str.3 = [16 x i8] c"You can WRITE!!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 2, %10
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %31
24: ; preds = %16
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @perror(i8* %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %16
%32 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%33 = call signext i8 (...) @getchar()
br label %34
34: ; preds = %57, %31
%35 = call i32 @FD_ZERO(i32* %8)
%36 = call i32 @FD_ZERO(i32* %9)
%37 = load i32, i32* %6
%38 = call i32 @FD_SET(i32 %37, i32* %8)
%39 = load i32, i32* %6
%40 = call i32 @FD_SET(i32 %39, i32* %9)
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
%43 = call i32 @select(i32 %42, i32* %8, i32* %9, i32* null, i32* null)
%44 = load i32, i32* %6
%45 = call i64 @FD_ISSET(i32 %44, i32* %8)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %34
%48 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %34
%50 = load i32, i32* %6
%51 = call i64 @FD_ISSET(i32 %50, i32* %9)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %49
%56 = call signext i8 (...) @getchar()
store i8 %56, i8* %7
br label %57
57: ; preds = %55
%58 = load i8, i8* %7
%59 = sext i8 %58 to i32
%60 = icmp ne i32 113, %59
br i1 %60, label %34, label %61
61: ; preds = %57
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
ret i32 0
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [38 x i8] c"Press [ENTER] poll\0APress [q] to quit:\00"
@.str.2 = [15 x i8] c"You can READ!!\00"
@.str.3 = [16 x i8] c"You can WRITE!!\00"
@str = [21 x i8] c"Syntax: cmd filename\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = load i8*, i8** %13
%20 = tail call i32 @perror(i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %12
%24 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call signext i8 (...) @getchar()
%26 = add nuw nsw i32 %16, 1
br label %27
27: ; preds = %42, %23
%28 = call i32 @FD_ZERO(i32* %3)
%29 = call i32 @FD_ZERO(i32* %4)
%30 = call i32 @FD_SET(i32 %16, i32* %3)
%31 = call i32 @FD_SET(i32 %16, i32* %4)
%32 = call i32 @select(i32 %26, i32* %3, i32* %4, i32* null, i32* null)
%33 = call i64 @FD_ISSET(i32 %16, i32* %3)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %27
%36 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %27
%38 = call i64 @FD_ISSET(i32 %16, i32* %4)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = call signext i8 (...) @getchar()
%44 = icmp eq i8 %43, 113
br i1 %44, label %45, label %27
45: ; preds = %42
%46 = call i32 @close(i32 %16)
ret i32 0
}