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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDWR;
int STDIN_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int joystick_fill_report(char *, char *, int *);
int keyboard_fill_report(char *, char *, int *);
int /*<<< orphan*/ memset(char *, int, int);
int mouse_fill_report(char *, char *, int *);
int open(char const *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ print_options(char);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, const char *argv[]) {
  const char *filename = NULL;
  int fd = 0;
  char buf[512];
  int cmd_len;
  char report[8];
  int to_send = 8;
  int hold = 0;
  fd_set rfds;
  int retval, i;
  if (argc < 3) {
    fprintf(stderr, "Usage: %s devname mouse|keyboard|joystick\n", argv[0]);
    print_options('k');
    print_options('m');
    print_options('j');
    return 1;
  }
  if (argv[2][0] != 'k' && argv[2][0] != 'm' && argv[2][0] != 'j')
    return 2;
  filename = argv[1];
  if ((fd = open(filename, O_RDWR, 0666)) == -1) {
    perror(filename);
    return 3;
  }
  while (42) {
    FD_ZERO(&rfds);
    FD_SET(STDIN_FILENO, &rfds);
    FD_SET(fd, &rfds);
    retval = select(fd + 1, &rfds, NULL, NULL, NULL);
    if (retval == -1 && errno == EINTR)
      continue;
    if (retval < 0) {
      perror("select()");
      return 4;
    }
    if (FD_ISSET(fd, &rfds)) {
      cmd_len = read(fd, buf, 512 - 1);
      printf("recv report:");
      for (i = 0; i < cmd_len; i++)
        printf(" %02x", buf[i]);
      printf("\n");
    }
    if (FD_ISSET(STDIN_FILENO, &rfds)) {
      memset(report, 0x0, sizeof(report));
      cmd_len = read(STDIN_FILENO, buf, 512 - 1);
      if (cmd_len == 0)
        break;
      buf[cmd_len - 1] = '\0';
      hold = 0;
      memset(report, 0x0, sizeof(report));
      if (argv[2][0] == 'k')
        to_send = keyboard_fill_report(report, buf, &hold);
      else if (argv[2][0] == 'm')
        to_send = mouse_fill_report(report, buf, &hold);
      else
        to_send = joystick_fill_report(report, buf, &hold);
      if (to_send == -1)
        break;
      if (write(fd, report, to_send) != to_send) {
        perror(filename);
        return 5;
      }
      if (!hold) {
        memset(report, 0x0, sizeof(report));
        if (write(fd, report, to_send) != to_send) {
          perror(filename);
          return 6;
        }
      }
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x8,-0x230(%rbp)
movl   $0x0,-0x234(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    128c <main+0x8c>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xda3(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x6b,%edi
call   1120 <print_options@plt>
mov    $0x6d,%edi
call   1120 <print_options@plt>
mov    $0x6a,%edi
call   1120 <print_options@plt>
movl   $0x1,-0x4(%rbp)
jmp    1644 <main+0x444>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6b,%eax
je     12d4 <main+0xd4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6d,%eax
je     12d4 <main+0xd4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6a,%eax
je     12d4 <main+0xd4>
movl   $0x2,-0x4(%rbp)
jmp    1644 <main+0x444>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
mov    $0x1b6,%edx
call   10e0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1318 <main+0x118>
mov    -0x18(%rbp),%rdi
call   10f0 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1644 <main+0x444>
jmp    131d <main+0x11d>
lea    -0x238(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
lea    -0x238(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x1c(%rbp),%edi
lea    -0x238(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
lea    -0x238(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10c0 <select@plt>
mov    %eax,-0x23c(%rbp)
cmpl   $0xffffffff,-0x23c(%rbp)
jne    139c <main+0x19c>
mov    0x2c4c(%rip),%rax
mov    (%rax),%rax
mov    0x2c3a(%rip),%rcx
cmp    (%rcx),%rax
jne    139c <main+0x19c>
jmp    131d <main+0x11d>
cmpl   $0x0,-0x23c(%rbp)
jge    13c1 <main+0x1c1>
lea    0xc7b(%rip),%rdi
call   10f0 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    1644 <main+0x444>
mov    -0x1c(%rbp),%edi
lea    -0x238(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     145d <main+0x25d>
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x1ff,%edx
call   1090 <read@plt>
mov    %eax,-0x224(%rbp)
lea    0xc39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x240(%rbp)
mov    -0x240(%rbp),%eax
cmp    -0x224(%rbp),%eax
jge    144f <main+0x24f>
movslq -0x240(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%esi
ff
lea    0xc0d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x240(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x240(%rbp)
jmp    140c <main+0x20c>
lea    0xbd3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2b5c(%rip),%rax
mov    (%rax),%edi
lea    -0x238(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1630 <main+0x430>
lea    -0x22c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2b2a(%rip),%rax
mov    (%rax),%edi
lea    -0x220(%rbp),%rsi
mov    $0x1ff,%edx
call   1090 <read@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0x0,-0x224(%rbp)
jne    14c1 <main+0x2c1>
jmp    1635 <main+0x435>
mov    -0x224(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x220(%rbp,%rax,1)
movl   $0x0,-0x234(%rbp)
lea    -0x22c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6b,%eax
jne    152a <main+0x32a>
lea    -0x22c(%rbp),%rdi
lea    -0x220(%rbp),%rsi
lea    -0x234(%rbp),%rdx
call   10d0 <keyboard_fill_report@plt>
mov    %eax,-0x230(%rbp)
jmp    1588 <main+0x388>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6d,%eax
jne    1563 <main+0x363>
lea    -0x22c(%rbp),%rdi
lea    -0x220(%rbp),%rsi
lea    -0x234(%rbp),%rdx
call   1100 <mouse_fill_report@plt>
mov    %eax,-0x230(%rbp)
jmp    1583 <main+0x383>
lea    -0x22c(%rbp),%rdi
lea    -0x220(%rbp),%rsi
lea    -0x234(%rbp),%rdx
call   1030 <joystick_fill_report@plt>
mov    %eax,-0x230(%rbp)
jmp    1588 <main+0x388>
cmpl   $0xffffffff,-0x230(%rbp)
jne    159a <main+0x39a>
jmp    1635 <main+0x435>
mov    -0x1c(%rbp),%edi
lea    -0x22c(%rbp),%rsi
mov    -0x230(%rbp),%edx
call   1040 <write@plt>
cmp    -0x230(%rbp),%eax
je     15d0 <main+0x3d0>
mov    -0x18(%rbp),%rdi
call   10f0 <perror@plt>
movl   $0x5,-0x4(%rbp)
jmp    1644 <main+0x444>
cmpl   $0x0,-0x234(%rbp)
jne    162b <main+0x42b>
lea    -0x22c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    -0x1c(%rbp),%edi
lea    -0x22c(%rbp),%rsi
mov    -0x230(%rbp),%edx
call   1040 <write@plt>
cmp    -0x230(%rbp),%eax
je     1626 <main+0x426>
mov    -0x18(%rbp),%rdi
call   10f0 <perror@plt>
movl   $0x6,-0x4(%rbp)
jmp    1644 <main+0x444>
jmp    162b <main+0x42b>
jmp    1630 <main+0x430>
jmp    131d <main+0x11d>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
mov    %rsi,0x10(%rsp)
movl   $0x0,0xc(%rsp)
cmp    $0x2,%edi
jg     1278 <main+0x68>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rax
mov    (%rax),%rdx
lea    0xdb5(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x6b,%edi
call   1130 <print_options@plt>
mov    $0x6d,%edi
call   1130 <print_options@plt>
mov    $0x6a,%edi
call   1130 <print_options@plt>
mov    $0x1,%eax
jmp    14cd <main+0x2bd>
mov    0x10(%rsp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%cl
mov    $0x2,%eax
add    $0x96,%cl
cmp    $0x3,%cl
ja     14cd <main+0x2bd>
cmp    $0x2,%cl
je     14cd <main+0x2bd>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%r14
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
mov    $0x1b6,%edx
call   10f0 <open@plt>
cmp    $0xffffffff,%eax
je     14f2 <main+0x2e2>
mov    %eax,%r13d
mov    %r14,0x20(%rsp)
mov    %eax,%r12d
add    $0x1,%r12d
lea    0x1c(%rsp),%rbx
lea    0xd61(%rip),%r15
jmp    130a <main+0xfa>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2cd9(%rip),%rax
mov    (%rax),%rax
mov    0x2cc7(%rip),%rcx
cmp    (%rcx),%rax
jne    14df <main+0x2cf>
mov    %rbx,%rdi
call   10c0 <FD_ZERO@plt>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1120 <FD_SET@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1120 <FD_SET@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10d0 <select@plt>
cmp    $0xffffffff,%eax
je     12f0 <main+0xe0>
test   %eax,%eax
js     14df <main+0x2cf>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     13b7 <main+0x1a7>
mov    %r13d,%edi
lea    0x30(%rsp),%rsi
mov    $0x1ff,%edx
call   10a0 <read@plt>
mov    %eax,%ebp
lea    0xcbc(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebp,%ebp
jle    13a8 <main+0x198>
mov    %ebp,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl 0x30(%rsp,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1390 <main+0x180>
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0x1c(%rsp),%rbx
mov    0x2c02(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     130a <main+0xfa>
lea    0x28(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    0x2bd4(%rip),%rax
mov    (%rax),%edi
lea    0x30(%rsp),%rsi
mov    $0x1ff,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     14c3 <main+0x2b3>
cltq
lea    0x30(%rsp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
movl   $0x0,0xc(%rsp)
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    0x10(%rsp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%al
mov    %rbp,%rdi
lea    0x30(%rsp),%rsi
lea    0xc(%rsp),%rdx
cmp    $0x6d,%al
je     144f <main+0x23f>
cmp    $0x6b,%al
jne    1456 <main+0x246>
call   10e0 <keyboard_fill_report@plt>
jmp    145b <main+0x24b>
call   1110 <mouse_fill_report@plt>
jmp    145b <main+0x24b>
call   1030 <joystick_fill_report@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
lea    0x28(%rsp),%rsi
je     14c3 <main+0x2b3>
mov    %r13d,%edi
mov    %r14d,%edx
call   1050 <write@plt>
cmp    %r14d,%eax
jne    1501 <main+0x2f1>
cmpl   $0x0,0xc(%rsp)
jne    130a <main+0xfa>
lea    0x28(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1050 <write@plt>
cmp    %r14d,%eax
je     130a <main+0xfa>
mov    0x20(%rsp),%rdi
call   1100 <perror@plt>
mov    $0x6,%eax
jmp    14cd <main+0x2bd>
mov    %r13d,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x238,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb45(%rip),%rdi
call   1100 <perror@plt>
mov    $0x4,%eax
jmp    14cd <main+0x2bd>
mov    %r14,%rdi
call   1100 <perror@plt>
mov    $0x3,%eax
jmp    14cd <main+0x2bd>
mov    0x20(%rsp),%rdi
call   1100 <perror@plt>
mov    $0x5,%eax
jmp    14cd <main+0x2bd>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [43 x i8] c"Usage: %s devname mouse|keyboard|joystick\0A\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [9 x i8] c"select()\00"
@.str.2 = [13 x i8] c"recv report:\00"
@.str.3 = [6 x i8] c" %02x\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i32
%10 = alloca [8 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i32 0, i32* %7
store i32 8, i32* %11
store i32 0, i32* %12
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %27
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @print_options(i8 signext 107)
%25 = call i32 @print_options(i8 signext 109)
%26 = call i32 @print_options(i8 signext 106)
store i32 1, i32* %3
br label %193
27: ; preds = %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 107
br i1 %34, label %35, label %52
35: ; preds = %27
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = getelementptr inbounds i8, i8* %38, i64 0
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 109
br i1 %42, label %43, label %52
43: ; preds = %35
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = getelementptr inbounds i8, i8* %46, i64 0
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %49, 106
br i1 %50, label %51, label %52
51: ; preds = %43
store i32 2, i32* %3
br label %193
52: ; preds = %43, %35, %27
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
store i8* %55, i8** %6
%56 = load i8*, i8** %6
%57 = load i32, i32* @O_RDWR
%58 = call i32 @open(i8* %56, i32 %57, i32 438)
store i32 %58, i32* %7
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %52
%61 = load i8*, i8** %6
%62 = call i32 @perror(i8* %61)
store i32 3, i32* %3
br label %193
63: ; preds = %52
br label %64
64: ; preds = %63, %79, %189
%65 = call i32 @FD_ZERO(i32* %13)
%66 = load i32, i32* @STDIN_FILENO
%67 = call i32 @FD_SET(i32 %66, i32* %13)
%68 = load i32, i32* %7
%69 = call i32 @FD_SET(i32 %68, i32* %13)
%70 = load i32, i32* %7
%71 = add nsw i32 %70, 1
%72 = call i32 @select(i32 %71, i32* %13, i32* null, i32* null, i32* null)
store i32 %72, i32* %14
%73 = load i32, i32* %14
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %80
75: ; preds = %64
%76 = load i64, i64* @errno
%77 = load i64, i64* @EINTR
%78 = icmp eq i64 %76, %77
br i1 %78, label %79, label %80
79: ; preds = %75
br label %64
80: ; preds = %75, %64
%81 = load i32, i32* %14
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 4, i32* %3
br label %193
85: ; preds = %80
%86 = load i32, i32* %7
%87 = call i64 @FD_ISSET(i32 %86, i32* %13)
%88 = icmp ne i64 %87, 0
br i1 %88, label %89, label %110
89: ; preds = %85
%90 = load i32, i32* %7
%91 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%92 = call i32 @read(i32 %90, i8* %91, i32 511)
store i32 %92, i32* %9
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %15
br label %94
94: ; preds = %105, %89
%95 = load i32, i32* %15
%96 = load i32, i32* %9
%97 = icmp slt i32 %95, %96
br i1 %97, label %98, label %108
98: ; preds = %94
%99 = load i32, i32* %15
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %100
%102 = load i8, i8* %101
%103 = sext i8 %102 to i32
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %103)
br label %105
105: ; preds = %98
%106 = load i32, i32* %15
%107 = add nsw i32 %106, 1
store i32 %107, i32* %15
br label %94
108: ; preds = %94
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %110
110: ; preds = %108, %85
%111 = load i32, i32* @STDIN_FILENO
%112 = call i64 @FD_ISSET(i32 %111, i32* %13)
%113 = icmp ne i64 %112, 0
br i1 %113, label %114, label %189
114: ; preds = %110
%115 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%116 = call i32 @memset(i8* %115, i32 0, i32 8)
%117 = load i32, i32* @STDIN_FILENO
%118 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%119 = call i32 @read(i32 %117, i8* %118, i32 511)
store i32 %119, i32* %9
%120 = load i32, i32* %9
%121 = icmp eq i32 %120, 0
br i1 %121, label %122, label %123
122: ; preds = %114
br label %190
123: ; preds = %114
%124 = load i32, i32* %9
%125 = sub nsw i32 %124, 1
%126 = sext i32 %125 to i64
%127 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %126
store i8 0, i8* %127
store i32 0, i32* %12
%128 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%129 = call i32 @memset(i8* %128, i32 0, i32 8)
%130 = load i8**, i8*** %5
%131 = getelementptr inbounds i8*, i8** %130, i64 2
%132 = load i8*, i8** %131
%133 = getelementptr inbounds i8, i8* %132, i64 0
%134 = load i8, i8* %133
%135 = sext i8 %134 to i32
%136 = icmp eq i32 %135, 107
br i1 %136, label %137, label %141
137: ; preds = %123
%138 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%139 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%140 = call i32 @keyboard_fill_report(i8* %138, i8* %139, i32* %12)
store i32 %140, i32* %11
br label %158
141: ; preds = %123
%142 = load i8**, i8*** %5
%143 = getelementptr inbounds i8*, i8** %142, i64 2
%144 = load i8*, i8** %143
%145 = getelementptr inbounds i8, i8* %144, i64 0
%146 = load i8, i8* %145
%147 = sext i8 %146 to i32
%148 = icmp eq i32 %147, 109
br i1 %148, label %149, label %153
149: ; preds = %141
%150 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%151 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%152 = call i32 @mouse_fill_report(i8* %150, i8* %151, i32* %12)
store i32 %152, i32* %11
br label %157
153: ; preds = %141
%154 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%155 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%156 = call i32 @joystick_fill_report(i8* %154, i8* %155, i32* %12)
store i32 %156, i32* %11
br label %157
157: ; preds = %153, %149
br label %158
158: ; preds = %157, %137
%159 = load i32, i32* %11
%160 = icmp eq i32 %159, -1
br i1 %160, label %161, label %162
161: ; preds = %158
br label %190
162: ; preds = %158
%163 = load i32, i32* %7
%164 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%165 = load i32, i32* %11
%166 = call i32 @write(i32 %163, i8* %164, i32 %165)
%167 = load i32, i32* %11
%168 = icmp ne i32 %166, %167
br i1 %168, label %169, label %172
169: ; preds = %162
%170 = load i8*, i8** %6
%171 = call i32 @perror(i8* %170)
store i32 5, i32* %3
br label %193
172: ; preds = %162
%173 = load i32, i32* %12
%174 = icmp ne i32 %173, 0
br i1 %174, label %188, label %175
175: ; preds = %172
%176 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%177 = call i32 @memset(i8* %176, i32 0, i32 8)
%178 = load i32, i32* %7
%179 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%180 = load i32, i32* %11
%181 = call i32 @write(i32 %178, i8* %179, i32 %180)
%182 = load i32, i32* %11
%183 = icmp ne i32 %181, %182
br i1 %183, label %184, label %187
184: ; preds = %175
%185 = load i8*, i8** %6
%186 = call i32 @perror(i8* %185)
store i32 6, i32* %3
br label %193
187: ; preds = %175
br label %188
188: ; preds = %187, %172
br label %189
189: ; preds = %188, %110
br label %64
190: ; preds = %161, %122
%191 = load i32, i32* %7
%192 = call i32 @close(i32 %191)
store i32 0, i32* %3
br label %193
193: ; preds = %190, %184, %169, %83, %60, %51, %18
%194 = load i32, i32* %3
ret i32 %194
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [43 x i8] c"Usage: %s devname mouse|keyboard|joystick\0A\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [9 x i8] c"select()\00"
@.str.2 = [13 x i8] c"recv report:\00"
@.str.3 = [6 x i8] c" %02x\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = alloca [8 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%9 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%10 = bitcast i32* %6 to i8*
%11 = icmp slt i32 %0, 3
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @print_options(i8 signext 107)
%17 = tail call i32 @print_options(i8 signext 109)
%18 = tail call i32 @print_options(i8 signext 106)
br label %109
19: ; preds = %2
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = load i8, i8* %21
switch i8 %22, label %109 [
i8 107, label %23
i8 109, label %23
i8 106, label %23
]
23: ; preds = %19, %19, %19
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDWR
%27 = tail call i32 @open(i8* %25, i32 %26, i32 438)
%28 = icmp eq i32 %27, -1
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = add nuw nsw i32 %27, 1
br label %33
31: ; preds = %23
%32 = tail call i32 @perror(i8* %25)
br label %109
33: ; preds = %71, %29
%34 = call i32 @FD_ZERO(i32* %6)
%35 = load i32, i32* @STDIN_FILENO
%36 = call i32 @FD_SET(i32 %35, i32* %6)
%37 = call i32 @FD_SET(i32 %27, i32* %6)
%38 = call i32 @select(i32 %30, i32* %6, i32* null, i32* null, i32* null)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = load i64, i64* @errno
%42 = load i64, i64* @EINTR
%43 = icmp eq i64 %41, %42
br i1 %43, label %71, label %46
44: ; preds = %33
%45 = icmp slt i32 %38, 0
br i1 %45, label %46, label %48
46: ; preds = %40, %44
%47 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %109
48: ; preds = %44
%49 = call i64 @FD_ISSET(i32 %27, i32* %6)
%50 = icmp eq i64 %49, 0
br i1 %50, label %67, label %51
51: ; preds = %48
%52 = call i32 @read(i32 %27, i8* %7, i32 511)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%54 = icmp sgt i32 %52, 0
br i1 %54, label %55, label %65
55: ; preds = %51
%56 = zext i32 %52 to i64
br label %57
57: ; preds = %55, %57
%58 = phi i64 [ 0, %55 ], [ %63, %57 ]
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %61)
%63 = add nuw nsw i64 %58, 1
%64 = icmp eq i64 %63, %56
br i1 %64, label %65, label %57
65: ; preds = %57, %51
%66 = call i32 @putchar(i32 10)
br label %67
67: ; preds = %65, %48
%68 = load i32, i32* @STDIN_FILENO
%69 = call i64 @FD_ISSET(i32 %68, i32* %6)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %67, %101, %98, %40
br label %33
72: ; preds = %67
%73 = call i32 @memset(i8* %8, i32 0, i32 8)
%74 = load i32, i32* @STDIN_FILENO
%75 = call i32 @read(i32 %74, i8* %7, i32 511)
%76 = icmp eq i32 %75, 0
br i1 %76, label %107, label %77
77: ; preds = %72
%78 = add nsw i32 %75, -1
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %79
store i8 0, i8* %80
store i32 0, i32* %5
%81 = call i32 @memset(i8* %8, i32 0, i32 8)
%82 = load i8*, i8** %20
%83 = load i8, i8* %82
switch i8 %83, label %88 [
i8 107, label %84
i8 109, label %86
]
84: ; preds = %77
%85 = call i32 @keyboard_fill_report(i8* %8, i8* %7, i32* %5)
br label %90
86: ; preds = %77
%87 = call i32 @mouse_fill_report(i8* %8, i8* %7, i32* %5)
br label %90
88: ; preds = %77
%89 = call i32 @joystick_fill_report(i8* %8, i8* %7, i32* %5)
br label %90
90: ; preds = %86, %88, %84
%91 = phi i32 [ %85, %84 ], [ %87, %86 ], [ %89, %88 ]
%92 = icmp eq i32 %91, -1
br i1 %92, label %107, label %93
93: ; preds = %90
%94 = call i32 @write(i32 %27, i8* %8, i32 %91)
%95 = icmp eq i32 %94, %91
br i1 %95, label %98, label %96
96: ; preds = %93
%97 = call i32 @perror(i8* %25)
br label %109
98: ; preds = %93
%99 = load i32, i32* %5
%100 = icmp eq i32 %99, 0
br i1 %100, label %101, label %71
101: ; preds = %98
%102 = call i32 @memset(i8* %8, i32 0, i32 8)
%103 = call i32 @write(i32 %27, i8* %8, i32 %91)
%104 = icmp eq i32 %103, %91
br i1 %104, label %71, label %105
105: ; preds = %101
%106 = call i32 @perror(i8* %25)
br label %109
107: ; preds = %90, %72
%108 = call i32 @close(i32 %27)
br label %109
109: ; preds = %19, %107, %105, %96, %46, %31, %12
%110 = phi i32 [ 1, %12 ], [ 3, %31 ], [ 4, %46 ], [ 0, %107 ], [ 5, %96 ], [ 6, %105 ], [ 2, %19 ]
ret i32 %110
}