#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 const);
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]);
    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;
  }
  print_options(argv[2][0]);
  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    126e <main+0x6e>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xda3(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1631 <main+0x431>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6b,%eax
je     12b6 <main+0xb6>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6d,%eax
je     12b6 <main+0xb6>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6a,%eax
je     12b6 <main+0xb6>
movl   $0x2,-0x4(%rbp)
jmp    1631 <main+0x431>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
mov    $0x1b6,%edx
call   10e0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    12fa <main+0xfa>
mov    -0x18(%rbp),%rdi
call   10f0 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1631 <main+0x431>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%edi
call   1120 <print_options@plt>
lea    -0x238(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    0x2ca3(%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    1389 <main+0x189>
mov    0x2c5f(%rip),%rax
mov    (%rax),%rax
mov    0x2c4d(%rip),%rcx
cmp    (%rcx),%rax
jne    1389 <main+0x189>
jmp    130a <main+0x10a>
cmpl   $0x0,-0x23c(%rbp)
jge    13ae <main+0x1ae>
lea    0xc8e(%rip),%rdi
call   10f0 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    1631 <main+0x431>
mov    -0x1c(%rbp),%edi
lea    -0x238(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     144a <main+0x24a>
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x1ff,%edx
call   1090 <read@plt>
mov    %eax,-0x224(%rbp)
lea    0xc4c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x240(%rbp)
mov    -0x240(%rbp),%eax
cmp    -0x224(%rbp),%eax
jge    143c <main+0x23c>
movslq -0x240(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%esi
ff
lea    0xc20(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x240(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x240(%rbp)
jmp    13f9 <main+0x1f9>
lea    0xbe6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2b6f(%rip),%rax
mov    (%rax),%edi
lea    -0x238(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     161d <main+0x41d>
lea    -0x22c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2b3d(%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    14ae <main+0x2ae>
jmp    1622 <main+0x422>
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    1517 <main+0x317>
lea    -0x22c(%rbp),%rdi
lea    -0x220(%rbp),%rsi
lea    -0x234(%rbp),%rdx
call   10d0 <keyboard_fill_report@plt>
mov    %eax,-0x230(%rbp)
jmp    1575 <main+0x375>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6d,%eax
jne    1550 <main+0x350>
lea    -0x22c(%rbp),%rdi
lea    -0x220(%rbp),%rsi
lea    -0x234(%rbp),%rdx
call   1100 <mouse_fill_report@plt>
mov    %eax,-0x230(%rbp)
jmp    1570 <main+0x370>
lea    -0x22c(%rbp),%rdi
lea    -0x220(%rbp),%rsi
lea    -0x234(%rbp),%rdx
call   1030 <joystick_fill_report@plt>
mov    %eax,-0x230(%rbp)
jmp    1575 <main+0x375>
cmpl   $0xffffffff,-0x230(%rbp)
jne    1587 <main+0x387>
jmp    1622 <main+0x422>
mov    -0x1c(%rbp),%edi
lea    -0x22c(%rbp),%rsi
mov    -0x230(%rbp),%edx
call   1040 <write@plt>
cmp    -0x230(%rbp),%eax
je     15bd <main+0x3bd>
mov    -0x18(%rbp),%rdi
call   10f0 <perror@plt>
movl   $0x5,-0x4(%rbp)
jmp    1631 <main+0x431>
cmpl   $0x0,-0x234(%rbp)
jne    1618 <main+0x418>
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     1613 <main+0x413>
mov    -0x18(%rbp),%rdi
call   10f0 <perror@plt>
movl   $0x6,-0x4(%rbp)
jmp    1631 <main+0x431>
jmp    1618 <main+0x418>
jmp    161d <main+0x41d>
jmp    130a <main+0x10a>
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     125a <main+0x4a>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rax
mov    (%rax),%rdx
lea    0xdb5(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%eax
jmp    14bd <main+0x2ad>
mov    0x10(%rsp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%cl
mov    $0x2,%eax
add    $0x96,%cl
cmp    $0x3,%cl
ja     14bd <main+0x2ad>
cmp    $0x2,%cl
je     14bd <main+0x2ad>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%r14
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
mov    $0x1b6,%edx
call   10f0 <open@plt>
cmp    $0xffffffff,%eax
je     14e2 <main+0x2d2>
mov    %eax,%r13d
mov    %r14,0x20(%rsp)
mov    0x10(%rsp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%edi
call   1130 <print_options@plt>
mov    %r13d,%r12d
add    $0x1,%r12d
lea    0x1c(%rsp),%rbx
lea    0xd6e(%rip),%r15
jmp    12fa <main+0xea>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2ce9(%rip),%rax
mov    (%rax),%rax
mov    0x2cd7(%rip),%rcx
cmp    (%rcx),%rax
jne    14cf <main+0x2bf>
mov    %rbx,%rdi
call   10c0 <FD_ZERO@plt>
mov    0x2cb7(%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     12e0 <main+0xd0>
test   %eax,%eax
js     14cf <main+0x2bf>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     13a7 <main+0x197>
mov    %r13d,%edi
lea    0x30(%rsp),%rsi
mov    $0x1ff,%edx
call   10a0 <read@plt>
mov    %eax,%ebp
lea    0xccc(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebp,%ebp
jle    1398 <main+0x188>
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    1380 <main+0x170>
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0x1c(%rsp),%rbx
mov    0x2c12(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     12fa <main+0xea>
lea    0x28(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    0x2be4(%rip),%rax
mov    (%rax),%edi
lea    0x30(%rsp),%rsi
mov    $0x1ff,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     14b3 <main+0x2a3>
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     143f <main+0x22f>
cmp    $0x6b,%al
jne    1446 <main+0x236>
call   10e0 <keyboard_fill_report@plt>
jmp    144b <main+0x23b>
call   1110 <mouse_fill_report@plt>
jmp    144b <main+0x23b>
call   1030 <joystick_fill_report@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
lea    0x28(%rsp),%rsi
je     14b3 <main+0x2a3>
mov    %r13d,%edi
mov    %r14d,%edx
call   1050 <write@plt>
cmp    %r14d,%eax
jne    14f1 <main+0x2e1>
cmpl   $0x0,0xc(%rsp)
jne    12fa <main+0xea>
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     12fa <main+0xea>
mov    0x20(%rsp),%rdi
call   1100 <perror@plt>
mov    $0x6,%eax
jmp    14bd <main+0x2ad>
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    0xb55(%rip),%rdi
call   1100 <perror@plt>
mov    $0x4,%eax
jmp    14bd <main+0x2ad>
mov    %r14,%rdi
call   1100 <perror@plt>
mov    $0x3,%eax
jmp    14bd <main+0x2ad>
mov    0x20(%rsp),%rdi
call   1100 <perror@plt>
mov    $0x5,%eax
jmp    14bd <main+0x2ad>

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

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 %16
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)
br label %109
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = load i8, i8* %18
switch i8 %19, label %109 [
i8 107, label %20
i8 109, label %20
i8 106, label %20
]
20: ; preds = %16, %16, %16
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = tail call i32 @open(i8* %22, i32 %23, i32 438)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = tail call i32 @perror(i8* %22)
br label %109
28: ; preds = %20
%29 = load i8*, i8** %17
%30 = load i8, i8* %29
%31 = tail call i32 @print_options(i8 signext %30)
%32 = add nuw nsw i32 %24, 1
br label %33
33: ; preds = %71, %28
%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 %24, i32* %6)
%38 = call i32 @select(i32 %32, 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 %24, i32* %6)
%50 = icmp eq i64 %49, 0
br i1 %50, label %67, label %51
51: ; preds = %48
%52 = call i32 @read(i32 %24, 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** %17
%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 %24, 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* %22)
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 %24, 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* %22)
br label %109
107: ; preds = %90, %72
%108 = call i32 @close(i32 %24)
br label %109
109: ; preds = %16, %107, %105, %96, %46, %26, %12
%110 = phi i32 [ 1, %12 ], [ 3, %26 ], [ 4, %46 ], [ 0, %107 ], [ 5, %96 ], [ 6, %105 ], [ 2, %16 ]
ret i32 %110
}