#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 termios {
  int *c_cc;
  int /*<<< orphan*/ c_lflag;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ ICANON;
int O_CLOEXEC;
int O_RDWR;
int POLLHUP;
int POLLIN;
int STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
int /*<<< orphan*/ close(int);
int create(int);
int /*<<< orphan*/ destroy(int);
int event(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int keyboard(int);
int open(char const *, int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(int argc, char **argv) {
  int fd;
  const char *path = "/dev/uhid";
  struct pollfd pfds[2];
  int ret;
  struct termios state;
  ret = tcgetattr(STDIN_FILENO, &state);
  if (ret) {
    fprintf(stderr, "Cannot get tty state\n");
  } else {
    state.c_lflag &= ~ICANON;
    state.c_cc[VMIN] = 1;
    ret = tcsetattr(STDIN_FILENO, TCSANOW, &state);
    if (ret)
      fprintf(stderr, "Cannot set tty state\n");
  }
  if (argc >= 2) {
    if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
      fprintf(stderr, "Usage: %s [%s]\n", argv[0], path);
      return EXIT_SUCCESS;
    } else {
      path = argv[1];
    }
  }
  fprintf(stderr, "Open uhid-cdev %s\n", path);
  fd = open(path, O_RDWR | O_CLOEXEC);
  if (fd < 0) {
    fprintf(stderr, "Cannot open uhid-cdev %s: %m\n", path);
    return EXIT_FAILURE;
  }
  fprintf(stderr, "Create uhid device\n");
  ret = create(fd);
  if (ret) {
    close(fd);
    return EXIT_FAILURE;
  }
  pfds[0].fd = STDIN_FILENO;
  pfds[0].events = POLLIN;
  pfds[1].fd = fd;
  pfds[1].events = POLLIN;
  fprintf(stderr, "Press 'q' to quit...\n");
  while (1) {
    ret = poll(pfds, 2, -1);
    if (ret < 0) {
      fprintf(stderr, "Cannot poll for fds: %m\n");
      break;
    }
    if (pfds[0].revents & POLLHUP) {
      fprintf(stderr, "Received HUP on stdin\n");
      break;
    }
    if (pfds[1].revents & POLLHUP) {
      fprintf(stderr, "Received HUP on uhid-cdev\n");
      break;
    }
    if (pfds[0].revents & POLLIN) {
      ret = keyboard(fd);
      if (ret)
        break;
    }
    if (pfds[1].revents & POLLIN) {
      ret = event(fd);
      if (ret)
        break;
    }
  }
  fprintf(stderr, "Destroy uhid device\n");
  destroy(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe33(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
call   10a0 <tcgetattr@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     120c <main+0x5c>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1277 <main+0xc7>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x50(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x58(%rbp),%rax
mov    0x2d9f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   10b0 <tcsetattr@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1272 <main+0xc2>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1277 <main+0xc7>
cmpl   $0x2,-0x8(%rbp)
jl     12ff <main+0x14f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xda6(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
je     12bb <main+0x10b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd8c(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    12ee <main+0x13e>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rcx
lea    0xd6a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    152a <main+0x37a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    12ff <main+0x14f>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd3d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2cc3(%rip),%rax
mov    (%rax),%esi
mov    0x2c5a(%rip),%rax
or     (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    136e <main+0x1be>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd0d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    152a <main+0x37a>
mov    0x2c43(%rip),%rax
mov    (%rax),%edi
lea    0xd03(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <create@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     13b3 <main+0x203>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2c07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    152a <main+0x37a>
mov    0x2be6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2c0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2bf8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2bd4(%rip),%rax
mov    (%rax),%edi
lea    0xca8(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    -0x40(%rbp),%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1090 <poll@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1430 <main+0x280>
mov    0x2b9d(%rip),%rax
mov    (%rax),%edi
lea    0xc87(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    14ff <main+0x34f>
mov    -0x38(%rbp),%eax
mov    0x2ba6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1461 <main+0x2b1>
mov    0x2b6c(%rip),%rax
mov    (%rax),%edi
lea    0xc6f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    14ff <main+0x34f>
mov    -0x2c(%rbp),%eax
mov    0x2b75(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1492 <main+0x2e2>
mov    0x2b3b(%rip),%rax
mov    (%rax),%edi
lea    0xc55(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    14ff <main+0x34f>
mov    -0x38(%rbp),%eax
mov    0x2b34(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     14c6 <main+0x316>
mov    -0x14(%rbp),%edi
call   10d0 <keyboard@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     14c1 <main+0x311>
jmp    14ff <main+0x34f>
jmp    14c6 <main+0x316>
mov    -0x2c(%rbp),%eax
mov    0x2b00(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     14fa <main+0x34a>
mov    -0x14(%rbp),%edi
call   1030 <event@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     14f5 <main+0x345>
jmp    14ff <main+0x34f>
jmp    14fa <main+0x34a>
jmp    13f4 <main+0x244>
mov    0x2ab2(%rip),%rax
mov    (%rax),%edi
lea    0xbe7(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1070 <destroy@plt>
mov    0x2a83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x2dd6(%rip),%r14
mov    (%r14),%edi
lea    0x18(%rsp),%rsi
call   10a0 <tcgetattr@plt>
test   %eax,%eax
je     11e4 <main+0x34>
lea    0xe28(%rip),%rsi
jmp    122a <main+0x7a>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x20(%rsp)
mov    0x18(%rsp),%rax
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    (%r14),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
call   10b0 <tcsetattr@plt>
test   %eax,%eax
je     123a <main+0x8a>
lea    0xdf6(%rip),%rsi
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
call   1080 <fprintf@plt>
cmp    $0x2,%ebx
jl     1290 <main+0xe0>
mov    0x8(%rbp),%rbx
lea    0xdec(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1269 <main+0xb9>
lea    0xddc(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
jne    1297 <main+0xe7>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xdc3(%rip),%rsi
lea    0xd7c(%rip),%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1407 <main+0x257>
lea    0xd69(%rip),%rbx
mov    0x2d1a(%rip),%r15
mov    (%r15),%edi
lea    0xda8(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2d2f(%rip),%rax
mov    0x2cc8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   10c0 <open@plt>
mov    (%r15),%edi
test   %eax,%eax
js     1301 <main+0x151>
mov    %eax,%ebp
lea    0xda5(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   1060 <create@plt>
test   %eax,%eax
je     131e <main+0x16e>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2cb4(%rip),%rax
jmp    140e <main+0x25e>
lea    0xd5b(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2c97(%rip),%rax
jmp    140e <main+0x25e>
mov    (%r14),%eax
mov    %eax,(%rsp)
mov    0x2ca5(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,0xc(%rsp)
mov    %eax,0x10(%rsp)
mov    (%r15),%edi
lea    0xd52(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rsp,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1090 <poll@plt>
lea    0xd48(%rip),%r14
test   %eax,%eax
js     13e2 <main+0x232>
mov    0x2c72(%rip),%r13
mov    %rsp,%r12
jmp    1396 <main+0x1e6>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1090 <poll@plt>
test   %eax,%eax
js     13e2 <main+0x232>
mov    0x8(%rsp),%edx
mov    0x0(%r13),%ecx
test   %edx,%ecx
jne    13d2 <main+0x222>
mov    0x14(%rsp),%eax
test   %ecx,%eax
jne    13db <main+0x22b>
mov    (%rbx),%ecx
test   %edx,%ecx
je     13c1 <main+0x211>
mov    %ebp,%edi
call   10d0 <keyboard@plt>
test   %eax,%eax
jne    13ef <main+0x23f>
mov    0x14(%rsp),%eax
mov    (%rbx),%ecx
test   %eax,%ecx
je     1380 <main+0x1d0>
mov    %ebp,%edi
call   1030 <event@plt>
test   %eax,%eax
je     1380 <main+0x1d0>
jmp    13ef <main+0x23f>
lea    0xceb(%rip),%r14
jmp    13e2 <main+0x232>
lea    0xcf9(%rip),%r14
mov    (%r15),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r15),%edi
lea    0xcfd(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   1070 <destroy@plt>
mov    0x2b9a(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
%struct.termios = type { i32*, i32 }
@.str = [10 x i8] c"/dev/uhid\00"
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Cannot get tty state\0A\00"
@ICANON = global i32 0
@VMIN = global i64 0
@TCSANOW = global i32 0
@.str.2 = [22 x i8] c"Cannot set tty state\0A\00"
@.str.3 = [3 x i8] c"-h\00"
@.str.4 = [7 x i8] c"--help\00"
@.str.5 = [16 x i8] c"Usage: %s [%s]\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [19 x i8] c"Open uhid-cdev %s\0A\00"
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@.str.7 = [30 x i8] c"Cannot open uhid-cdev %s: %m\0A\00"
@EXIT_FAILURE = global i32 0
@.str.8 = [20 x i8] c"Create uhid device\0A\00"
@POLLIN = global i32 0
@.str.9 = [22 x i8] c"Press 'q' to quit...\0A\00"
@.str.10 = [25 x i8] c"Cannot poll for fds: %m\0A\00"
@POLLHUP = global i32 0
@.str.11 = [23 x i8] c"Received HUP on stdin\0A\00"
@.str.12 = [27 x i8] c"Received HUP on uhid-cdev\0A\00"
@.str.13 = [21 x i8] c"Destroy uhid device\0A\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 [2 x %struct.pollfd]
%9 = alloca i32
%10 = alloca %struct.termios
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = load i32, i32* @STDIN_FILENO
%12 = call i32 @tcgetattr(i32 %11, %struct.termios* %10)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %37
18: ; preds = %2
%19 = load i32, i32* @ICANON
%20 = xor i32 %19, -1
%21 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 1
%22 = load i32, i32* %21
%23 = and i32 %22, %20
store i32 %23, i32* %21
%24 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 0
%25 = load i32*, i32** %24
%26 = load i64, i64* @VMIN
%27 = getelementptr inbounds i32, i32* %25, i64 %26
store i32 1, i32* %27
%28 = load i32, i32* @STDIN_FILENO
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %28, i32 %29, %struct.termios* %10)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %18
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %33, %18
br label %37
37: ; preds = %36, %15
%38 = load i32, i32* %4
%39 = icmp sge i32 %38, 2
br i1 %39, label %40, label %65
40: ; preds = %37
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @strcmp(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %40
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @strcmp(i8* %49, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%51 = icmp ne i32 %50, 0
br i1 %51, label %60, label %52
52: ; preds = %46, %40
%53 = load i32, i32* @stderr
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 0
%56 = load i8*, i8** %55
%57 = load i8*, i8** %7
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %56, i8* %57)
%59 = load i32, i32* @EXIT_SUCCESS
store i32 %59, i32* %3
br label %170
60: ; preds = %46
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 1
%63 = load i8*, i8** %62
store i8* %63, i8** %7
br label %64
64: ; preds = %60
br label %65
65: ; preds = %64, %37
%66 = load i32, i32* @stderr
%67 = load i8*, i8** %7
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %67)
%69 = load i8*, i8** %7
%70 = load i32, i32* @O_RDWR
%71 = load i32, i32* @O_CLOEXEC
%72 = or i32 %70, %71
%73 = call i32 @open(i8* %69, i32 %72)
store i32 %73, i32* %6
%74 = load i32, i32* %6
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %65
%77 = load i32, i32* @stderr
%78 = load i8*, i8** %7
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %78)
%80 = load i32, i32* @EXIT_FAILURE
store i32 %80, i32* %3
br label %170
81: ; preds = %65
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
%84 = load i32, i32* %6
%85 = call i32 @create(i32 %84)
store i32 %85, i32* %9
%86 = load i32, i32* %9
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %92
88: ; preds = %81
%89 = load i32, i32* %6
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* @EXIT_FAILURE
store i32 %91, i32* %3
br label %170
92: ; preds = %81
%93 = load i32, i32* @STDIN_FILENO
%94 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%95 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %94, i32 0, i32 0
store i32 %93, i32* %95
%96 = load i32, i32* @POLLIN
%97 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%98 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %97, i32 0, i32 1
store i32 %96, i32* %98
%99 = load i32, i32* %6
%100 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%101 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %100, i32 0, i32 0
store i32 %99, i32* %101
%102 = load i32, i32* @POLLIN
%103 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%104 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %103, i32 0, i32 1
store i32 %102, i32* %104
%105 = load i32, i32* @stderr
%106 = call i32 (i32, i8*, ...) @fprintf(i32 %105, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0))
br label %107
107: ; preds = %92, %163
%108 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%109 = call i32 @poll(%struct.pollfd* %108, i32 2, i32 -1)
store i32 %109, i32* %9
%110 = load i32, i32* %9
%111 = icmp slt i32 %110, 0
br i1 %111, label %112, label %115
112: ; preds = %107
%113 = load i32, i32* @stderr
%114 = call i32 (i32, i8*, ...) @fprintf(i32 %113, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0))
br label %164
115: ; preds = %107
%116 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%117 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %116, i32 0, i32 2
%118 = load i32, i32* %117
%119 = load i32, i32* @POLLHUP
%120 = and i32 %118, %119
%121 = icmp ne i32 %120, 0
br i1 %121, label %122, label %125
122: ; preds = %115
%123 = load i32, i32* @stderr
%124 = call i32 (i32, i8*, ...) @fprintf(i32 %123, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0))
br label %164
125: ; preds = %115
%126 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%127 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %126, i32 0, i32 2
%128 = load i32, i32* %127
%129 = load i32, i32* @POLLHUP
%130 = and i32 %128, %129
%131 = icmp ne i32 %130, 0
br i1 %131, label %132, label %135
132: ; preds = %125
%133 = load i32, i32* @stderr
%134 = call i32 (i32, i8*, ...) @fprintf(i32 %133, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.12, i64 0, i64 0))
br label %164
135: ; preds = %125
%136 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 0
%137 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %136, i32 0, i32 2
%138 = load i32, i32* %137
%139 = load i32, i32* @POLLIN
%140 = and i32 %138, %139
%141 = icmp ne i32 %140, 0
br i1 %141, label %142, label %149
142: ; preds = %135
%143 = load i32, i32* %6
%144 = call i32 @keyboard(i32 %143)
store i32 %144, i32* %9
%145 = load i32, i32* %9
%146 = icmp ne i32 %145, 0
br i1 %146, label %147, label %148
147: ; preds = %142
br label %164
148: ; preds = %142
br label %149
149: ; preds = %148, %135
%150 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %8, i64 0, i64 1
%151 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %150, i32 0, i32 2
%152 = load i32, i32* %151
%153 = load i32, i32* @POLLIN
%154 = and i32 %152, %153
%155 = icmp ne i32 %154, 0
br i1 %155, label %156, label %163
156: ; preds = %149
%157 = load i32, i32* %6
%158 = call i32 @event(i32 %157)
store i32 %158, i32* %9
%159 = load i32, i32* %9
%160 = icmp ne i32 %159, 0
br i1 %160, label %161, label %162
161: ; preds = %156
br label %164
162: ; preds = %156
br label %163
163: ; preds = %162, %149
br label %107
164: ; preds = %161, %147, %132, %122, %112
%165 = load i32, i32* @stderr
%166 = call i32 (i32, i8*, ...) @fprintf(i32 %165, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.13, i64 0, i64 0))
%167 = load i32, i32* %6
%168 = call i32 @destroy(i32 %167)
%169 = load i32, i32* @EXIT_SUCCESS
store i32 %169, i32* %3
br label %170
170: ; preds = %164, %88, %76, %52
%171 = load i32, i32* %3
ret i32 %171
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
%struct.termios = type { i32*, i32 }
@.str = [10 x i8] c"/dev/uhid\00"
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Cannot get tty state\0A\00"
@ICANON = global i32 0
@VMIN = global i64 0
@TCSANOW = global i32 0
@.str.2 = [22 x i8] c"Cannot set tty state\0A\00"
@.str.3 = [3 x i8] c"-h\00"
@.str.4 = [7 x i8] c"--help\00"
@.str.5 = [16 x i8] c"Usage: %s [%s]\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [19 x i8] c"Open uhid-cdev %s\0A\00"
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@.str.7 = [30 x i8] c"Cannot open uhid-cdev %s: %m\0A\00"
@EXIT_FAILURE = global i32 0
@.str.8 = [20 x i8] c"Create uhid device\0A\00"
@POLLIN = global i32 0
@.str.9 = [22 x i8] c"Press 'q' to quit...\0A\00"
@.str.10 = [25 x i8] c"Cannot poll for fds: %m\0A\00"
@POLLHUP = global i32 0
@.str.11 = [23 x i8] c"Received HUP on stdin\0A\00"
@.str.12 = [27 x i8] c"Received HUP on uhid-cdev\0A\00"
@.str.13 = [21 x i8] c"Destroy uhid device\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x %struct.pollfd]
%4 = alloca %struct.termios
%5 = bitcast [2 x %struct.pollfd]* %3 to i8*
%6 = bitcast %struct.termios* %4 to i8*
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %2
%11 = load i32, i32* @ICANON
%12 = xor i32 %11, -1
%13 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%14 = load i32, i32* %13
%15 = and i32 %14, %12
store i32 %15, i32* %13
%16 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%17 = load i32*, i32** %16
%18 = load i64, i64* @VMIN
%19 = getelementptr inbounds i32, i32* %17, i64 %18
store i32 1, i32* %19
%20 = load i32, i32* @STDIN_FILENO
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 %20, i32 %21, %struct.termios* %4)
%23 = icmp eq i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %10, %2
%25 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %10 ]
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* %25)
br label %28
28: ; preds = %24, %10
%29 = icmp sgt i32 %0, 1
br i1 %29, label %30, label %42
30: ; preds = %28
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @strcmp(i8* dereferenceable(1) %32, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp eq i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = call i32 @strcmp(i8* dereferenceable(1) %32, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35, %30
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %1
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %40, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %113
42: ; preds = %35, %28
%43 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %28 ], [ %32, %35 ]
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %43)
%46 = load i32, i32* @O_RDWR
%47 = load i32, i32* @O_CLOEXEC
%48 = or i32 %47, %46
%49 = call i32 @open(i8* %43, i32 %48)
%50 = icmp slt i32 %49, 0
%51 = load i32, i32* @stderr
br i1 %50, label %52, label %54
52: ; preds = %42
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %43)
br label %113
54: ; preds = %42
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
%56 = call i32 @create(i32 %49)
%57 = icmp eq i32 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %54
%59 = call i32 @close(i32 %49)
br label %113
60: ; preds = %54
%61 = load i32, i32* @STDIN_FILENO
%62 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%63 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %61, i32* %63
%64 = load i32, i32* @POLLIN
%65 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i32 %64, i32* %65
%66 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 0
store i32 %49, i32* %66
%67 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 1
store i32 %64, i32* %67
%68 = load i32, i32* @stderr
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0))
%70 = call i32 @poll(%struct.pollfd* %62, i32 2, i32 -1)
%71 = icmp slt i32 %70, 0
br i1 %71, label %105, label %72
72: ; preds = %60
%73 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
%74 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1, i32 2
br label %75
75: ; preds = %72, %102
%76 = load i32, i32* %73
%77 = load i32, i32* @POLLHUP
%78 = and i32 %77, %76
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %105
80: ; preds = %75
%81 = load i32, i32* %74
%82 = and i32 %81, %77
%83 = icmp eq i32 %82, 0
br i1 %83, label %84, label %105
84: ; preds = %80
%85 = load i32, i32* @POLLIN
%86 = and i32 %85, %76
%87 = icmp eq i32 %86, 0
br i1 %87, label %94, label %88
88: ; preds = %84
%89 = call i32 @keyboard(i32 %49)
%90 = icmp eq i32 %89, 0
br i1 %90, label %91, label %109
91: ; preds = %88
%92 = load i32, i32* %74
%93 = load i32, i32* @POLLIN
br label %94
94: ; preds = %91, %84
%95 = phi i32 [ %93, %91 ], [ %85, %84 ]
%96 = phi i32 [ %92, %91 ], [ %81, %84 ]
%97 = and i32 %95, %96
%98 = icmp eq i32 %97, 0
br i1 %98, label %102, label %99
99: ; preds = %94
%100 = call i32 @event(i32 %49)
%101 = icmp eq i32 %100, 0
br i1 %101, label %102, label %109
102: ; preds = %99, %94
%103 = call i32 @poll(%struct.pollfd* %62, i32 2, i32 -1)
%104 = icmp slt i32 %103, 0
br i1 %104, label %105, label %75
105: ; preds = %80, %75, %102, %60
%106 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0), %60 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.10, i64 0, i64 0), %102 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0), %75 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.12, i64 0, i64 0), %80 ]
%107 = load i32, i32* @stderr
%108 = call i32 (i32, i8*, ...) @fprintf(i32 %107, i8* %106)
br label %109
109: ; preds = %99, %88, %105
%110 = load i32, i32* @stderr
%111 = call i32 (i32, i8*, ...) @fprintf(i32 %110, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.13, i64 0, i64 0))
%112 = call i32 @destroy(i32 %49)
br label %113
113: ; preds = %109, %58, %52, %38
%114 = phi i32* [ @EXIT_FAILURE, %52 ], [ @EXIT_FAILURE, %58 ], [ @EXIT_SUCCESS, %109 ], [ @EXIT_SUCCESS, %38 ]
%115 = load i32, i32* %114
ret i32 %115
}