#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*/ keys;

/* Variables and functions */
int CHAR_BIT;
int /*<<< orphan*/ EVIOCGKEY(int);
int KEY_MAX;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int ioctl(int, int /*<<< orphan*/, unsigned long *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc < 1) {
    fprintf(stderr, "usage: %s <event-dev>...\n", argv[0]);
    return 1;
  }
  int i;
  for (i = 1; i < argc; i++) {
    const char *dev = argv[i];
    int fd = open(dev, O_RDONLY);
    if (fd == -1) {
      fprintf(stderr, "could not open '%s': %s\n", dev, strerror(errno));
      continue;
    }
    unsigned long keys[KEY_MAX / sizeof(unsigned long) / CHAR_BIT];
    int r = ioctl(fd, EVIOCGKEY(sizeof(keys)), keys);
    if (r == -1) {
      fprintf(stderr, "could not retrieve keys from '%s': %s\n", dev,
              strerror(errno));
      continue;
    }
    printf("[%s]\n", dev);
    size_t j;
    size_t num_longs = r / sizeof(unsigned long);
    for (j = 0; j < num_longs; j++) {
      printf("%lX%c", keys[j], (j + 1) == num_longs ? '\n' : ':');
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jge    11ad <main+0x4d>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139f <main+0x23f>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1398 <main+0x238>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    122e <main+0xce>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    138a <main+0x22a>
mov    0x2d8b(%rip),%rax
movslq (%rax),%rax
shr    $0x3,%rax
mov    0x2d95(%rip),%rcx
movslq (%rcx),%rcx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    %rcx,%rax
mov    %rsp,%rdx
mov    %rdx,-0x30(%rbp)
lea    0xf(,%rcx,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%ecx
mov    %ecx,-0x6c(%rbp)
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <EVIOCGKEY@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %eax,%esi
call   1040 <ioctl@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    12e7 <main+0x187>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rdx
mov    %rax,%rcx
lea    0xd5f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x4,-0x40(%rbp)
jmp    1369 <main+0x209>
mov    -0x20(%rbp),%rsi
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movslq -0x3c(%rbp),%rax
shr    $0x3,%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x50(%rbp),%rax
jae    1362 <main+0x202>
mov    -0x68(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x48(%rbp),%rcx
add    $0x1,%rcx
mov    -0x50(%rbp),%rdi
mov    $0x3a,%edx
mov    $0xa,%eax
cmp    %rdi,%rcx
cmove  %eax,%edx
lea    0xd16(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    130d <main+0x1ad>
movl   $0x0,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     1385 <main+0x225>
jmp    1380 <main+0x220>
jmp    138a <main+0x22a>
jmp    138a <main+0x22a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b4 <main+0x54>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
test   %edi,%edi
jle    132f <main+0x1cf>
xor    %eax,%eax
cmp    $0x1,%edi
je     1352 <main+0x1f2>
mov    %edi,%eax
mov    %rax,-0x38(%rbp)
mov    $0x1,%ebx
lea    0xec7(%rip),%r13
mov    %r14,-0x30(%rbp)
jmp    11e7 <main+0x87>
nop
mov    0x2e29(%rip),%rax
mov    (%rax),%ebx
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %ebx,%edi
lea    0xe73(%rip),%rsi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    -0x40(%rbp),%rsp
mov    -0x30(%rbp),%r14
mov    -0x48(%rbp),%rbx
add    $0x1,%rbx
cmp    -0x38(%rbp),%rbx
je     1350 <main+0x1f0>
mov    (%r14,%rbx,8),%r15
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1240 <main+0xe0>
mov    %eax,%r12d
mov    0x2db5(%rip),%rax
movslq (%rax),%rax
shr    $0x3,%rax
mov    0x2dbf(%rip),%rcx
movslq (%rcx),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     1280 <main+0x120>
xor    %edx,%edx
div    %rcx
mov    %rax,%rdi
jmp    1286 <main+0x126>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2d89(%rip),%rax
mov    (%rax),%r12d
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %r12d,%edi
lea    0xdb8(%rip),%rsi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11d9 <main+0x79>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edx,%edx
div    %ecx
mov    %eax,%edi
mov    %rsp,-0x40(%rbp)
mov    %rsp,%r14
lea    0xf(,%rdi,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
shl    $0x3,%edi
call   1050 <EVIOCGKEY@plt>
mov    %r12d,%edi
mov    %eax,%esi
mov    %r14,%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
mov    %rbx,-0x48(%rbp)
je     11a0 <main+0x40>
mov    %eax,%r12d
lea    0xd8f(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x8,%r12d
mov    $0xa,%ecx
jb     11cd <main+0x6d>
movslq %r12d,%r12
shr    $0x3,%r12
cmp    $0x2,%r12
mov    $0x1,%r15d
cmovae %r12,%r15
add    $0xffffffffffffffff,%r12
xor    %eax,%eax
nop
mov    (%r14,%rax,8),%rsi
lea    0x1(%rax),%rbx
cmp    %rax,%r12
mov    $0x3a,%edx
cmove  %ecx,%edx
mov    %r13,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xa,%ecx
mov    %rbx,%rax
cmp    %rbx,%r15
jne    1300 <main+0x1a0>
jmp    11cd <main+0x6d>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xcbe(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    1352 <main+0x1f2>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"usage: %s <event-dev>...\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"could not open '%s': %s\0A\00"
@errno = global i32 0
@KEY_MAX = global i32 0
@CHAR_BIT = global i32 0
@.str.2 = [39 x i8] c"could not retrieve keys from '%s': %s\0A\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
@.str.4 = [6 x i8] c"%lX%c\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 i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
%14 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 1, i32* %3
br label %100
23: ; preds = %2
store i32 1, i32* %6
br label %24
24: ; preds = %96, %23
%25 = load i32, i32* %6
%26 = load i32, i32* %4
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %99
28: ; preds = %24
%29 = load i8**, i8*** %5
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %29, i64 %31
%33 = load i8*, i8** %32
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %28
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %7
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %41, i8* %43)
br label %96
45: ; preds = %28
%46 = load i32, i32* @KEY_MAX
%47 = sext i32 %46 to i64
%48 = udiv i64 %47, 8
%49 = load i32, i32* @CHAR_BIT
%50 = sext i32 %49 to i64
%51 = udiv i64 %48, %50
%52 = call i8* @llvm.stacksave()
store i8* %52, i8** %9
%53 = alloca i64, i64 %51
store i64 %51, i64* %10
%54 = load i32, i32* %8
%55 = mul nuw i64 8, %51
%56 = trunc i64 %55 to i32
%57 = call i32 @EVIOCGKEY(i32 %56)
%58 = call i32 @ioctl(i32 %54, i32 %57, i64* %53)
store i32 %58, i32* %11
%59 = load i32, i32* %11
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %67
61: ; preds = %45
%62 = load i32, i32* @stderr
%63 = load i8*, i8** %7
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %63, i8* %65)
store i32 4, i32* %12
br label %92
67: ; preds = %45
%68 = load i8*, i8** %7
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %68)
%70 = load i32, i32* %11
%71 = sext i32 %70 to i64
%72 = udiv i64 %71, 8
store i64 %72, i64* %14
store i64 0, i64* %13
br label %73
73: ; preds = %88, %67
%74 = load i64, i64* %13
%75 = load i64, i64* %14
%76 = icmp ult i64 %74, %75
br i1 %76, label %77, label %91
77: ; preds = %73
%78 = load i64, i64* %13
%79 = getelementptr inbounds i64, i64* %53, i64 %78
%80 = load i64, i64* %79
%81 = load i64, i64* %13
%82 = add i64 %81, 1
%83 = load i64, i64* %14
%84 = icmp eq i64 %82, %83
%85 = zext i1 %84 to i64
%86 = select i1 %84, i32 10, i32 58
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64 %80, i32 %86)
br label %88
88: ; preds = %77
%89 = load i64, i64* %13
%90 = add i64 %89, 1
store i64 %90, i64* %13
br label %73
91: ; preds = %73
store i32 0, i32* %12
br label %92
92: ; preds = %91, %61
%93 = load i8*, i8** %9
%94 = load i32, i32* %12
switch i32 %94, label %102 [
i32 0, label %95
i32 4, label %96
]
95: ; preds = %92
br label %96
96: ; preds = %95, %92, %39
%97 = load i32, i32* %6
%98 = add nsw i32 %97, 1
store i32 %98, i32* %6
br label %24
99: ; preds = %24
store i32 0, i32* %3
br label %100
100: ; preds = %99, %17
%101 = load i32, i32* %3
ret i32 %101
102: ; preds = %92
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"usage: %s <event-dev>...\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"could not open '%s': %s\0A\00"
@errno = global i32 0
@KEY_MAX = global i32 0
@CHAR_BIT = global i32 0
@.str.2 = [39 x i8] c"could not retrieve keys from '%s': %s\0A\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
@.str.4 = [6 x i8] c"%lX%c\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 1
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = icmp eq i32 %0, 1
br i1 %5, label %63, label %6
6: ; preds = %4
%7 = zext i32 %0 to i64
br label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %63
12: ; preds = %6, %60
%13 = phi i64 [ 1, %6 ], [ %61, %60 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 %13
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %22)
br label %60
24: ; preds = %12
%25 = load i32, i32* @KEY_MAX
%26 = sext i32 %25 to i64
%27 = lshr i64 %26, 3
%28 = load i32, i32* @CHAR_BIT
%29 = sext i32 %28 to i64
%30 = udiv i64 %27, %29
%31 = call i8* @llvm.stacksave()
%32 = alloca i64, i64 %30
%33 = trunc i64 %30 to i32
%34 = shl i32 %33, 3
%35 = call i32 @EVIOCGKEY(i32 %34)
%36 = call i32 @ioctl(i32 %17, i32 %35, i64* %32)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %43
38: ; preds = %24
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* %41)
br label %59
43: ; preds = %24
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%45 = sext i32 %36 to i64
%46 = lshr i64 %45, 3
%47 = icmp ult i32 %36, 8
br i1 %47, label %59, label %48
48: ; preds = %43
%49 = call i64 @llvm.umax.i64(i64 %46, i64 1)
br label %50
50: ; preds = %48, %50
%51 = phi i64 [ %54, %50 ], [ 0, %48 ]
%52 = getelementptr inbounds i64, i64* %32, i64 %51
%53 = load i64, i64* %52
%54 = add nuw nsw i64 %51, 1
%55 = icmp eq i64 %54, %46
%56 = select i1 %55, i32 10, i32 58
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64 %53, i32 %56)
%58 = icmp eq i64 %54, %49
br i1 %58, label %59, label %50
59: ; preds = %50, %43, %38
br label %60
60: ; preds = %59, %19
%61 = add nuw nsw i64 %13, 1
%62 = icmp eq i64 %61, %7
br i1 %62, label %63, label %12
63: ; preds = %60, %4, %8
%64 = phi i32 [ 1, %8 ], [ 0, %4 ], [ 0, %60 ]
ret i32 %64
}