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

/* Variables and functions */
int /*<<< orphan*/ EVIOCGBIT(int /*<<< orphan*/, int);
int /*<<< orphan*/ EV_KEY;
int KEY_MAX;
scalar_t__ event_test_bit(unsigned int, unsigned char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned char *);
int /*<<< orphan*/ log_f(char *, ...);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
void event_key_log(int f) {
  unsigned char key_bitmask[KEY_MAX / 8 + 1];
  unsigned i;
  unsigned count;
  memset(key_bitmask, 0, sizeof(key_bitmask));
  if (ioctl(f, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) < 0) {
    log_f("event: error in ioctl(EVIOCGBIT(EV_KEY,%d))\n", (int)KEY_MAX);
    return;
  }
  log_f("event: EV_KEY:");
  count = 0;
  for (i = 0; i < KEY_MAX; ++i) {
    if (event_test_bit(i, key_bitmask)) {
      const char *desc;
      ++count;
      switch (i) {
      case 0x13f:
        desc = "BTN_PLAY";
        break;
      default:
        desc = 0;
        break;
      }
      if (desc)
        log_f(" %s", desc);
      else
        log_f(" 0x%x", i);
    }
  }
  log_f(" [%u]\n", count);
}


x86 assembly (clang, -O0):
<event_key_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
add    $0x7,%ecx
test   %eax,%eax
cmovs  %ecx,%eax
sar    $0x3,%eax
add    $0x1,%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x48(%rbp),%rax
mov    -0x4(%rbp),%ecx
mov    %ecx,-0x3c(%rbp)
mov    0x2e33(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%esi
call   1040 <EVIOCGBIT@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%esi
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    11f9 <event_key_log+0xa9>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1050 <log_f@plt>
movl   $0x1,-0x24(%rbp)
jmp    12d4 <event_key_log+0x184>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <log_f@plt>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%eax
jae    12bc <event_key_log+0x16c>
mov    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%edi
call   1030 <event_test_bit@plt>
cmp    $0x0,%rax
je     12a9 <event_key_log+0x159>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x13f,%eax
jne    1269 <event_key_log+0x119>
jmp    1259 <event_key_log+0x109>
lea    0xddc(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    1271 <event_key_log+0x121>
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1293 <event_key_log+0x143>
mov    -0x30(%rbp),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <log_f@plt>
jmp    12a4 <event_key_log+0x154>
mov    -0x1c(%rbp),%esi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <log_f@plt>
jmp    12a9 <event_key_log+0x159>
jmp    12ae <event_key_log+0x15e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1215 <event_key_log+0xc5>
mov    -0x20(%rbp),%esi
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1050 <log_f@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<event_key_log>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
mov    0x2e70(%rip),%r13
mov    0x0(%r13),%eax
lea    0x7(%rax),%ebx
test   %eax,%eax
cmovns %eax,%ebx
sar    $0x3,%ebx
add    $0x1,%ebx
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1060 <memset@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
call   1040 <EVIOCGBIT@plt>
mov    %r15d,%edi
mov    %eax,%esi
mov    %r12,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
js     1237 <event_key_log+0xe7>
lea    0xe6a(%rip),%rdi
xor    %eax,%eax
call   1050 <log_f@plt>
cmpl   $0x0,0x0(%r13)
je     122b <event_key_log+0xdb>
lea    0xe71(%rip),%rbx
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    11f7 <event_key_log+0xa7>
mov    %rbx,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <log_f@plt>
add    $0x1,%r15d
cmp    0x0(%r13),%r15d
jae    122b <event_key_log+0xdb>
mov    %r15d,%edi
mov    %r12,%rsi
call   1030 <event_test_bit@plt>
test   %rax,%rax
je     11ed <event_key_log+0x9d>
add    $0x1,%r14d
cmp    $0x13f,%r15d
jne    11e0 <event_key_log+0x90>
lea    0xe2a(%rip),%rdi
lea    0xe1a(%rip),%rsi
xor    %eax,%eax
call   1050 <log_f@plt>
jmp    11ed <event_key_log+0x9d>
lea    0xe1d(%rip),%rdi
mov    %r14d,%esi
jmp    1242 <event_key_log+0xf2>
mov    0x0(%r13),%esi
lea    0xdbe(%rip),%rdi
xor    %eax,%eax
call   1050 <log_f@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@KEY_MAX = global i32 0
@EV_KEY = global i32 0
@.str = [45 x i8] c"event: error in ioctl(EVIOCGBIT(EV_KEY,%d))\0A\00"
@.str.1 = [15 x i8] c"event: EV_KEY:\00"
@.str.2 = [9 x i8] c"BTN_PLAY\00"
@.str.3 = [4 x i8] c" %s\00"
@.str.4 = [6 x i8] c" 0x%x\00"
@.str.5 = [7 x i8] c" [%u]\0A\00"
define void @event_key_log(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %2
%9 = load i32, i32* @KEY_MAX
%10 = sdiv i32 %9, 8
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %3
%14 = alloca i8, i64 %12
store i64 %12, i64* %4
%15 = trunc i64 %12 to i32
%16 = call i32 @memset(i8* %14, i32 0, i32 %15)
%17 = load i32, i32* %2
%18 = load i32, i32* @EV_KEY
%19 = trunc i64 %12 to i32
%20 = call i32 @EVIOCGBIT(i32 %18, i32 %19)
%21 = call i64 @ioctl(i32 %17, i32 %20, i8* %14)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %1
%24 = load i32, i32* @KEY_MAX
%25 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 1, i32* %7
br label %59
26: ; preds = %1
%27 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %6
store i32 0, i32* %5
br label %28
28: ; preds = %53, %26
%29 = load i32, i32* %5
%30 = load i32, i32* @KEY_MAX
%31 = icmp ult i32 %29, %30
br i1 %31, label %32, label %56
32: ; preds = %28
%33 = load i32, i32* %5
%34 = call i64 @event_test_bit(i32 %33, i8* %14)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %52
36: ; preds = %32
%37 = load i32, i32* %6
%38 = add i32 %37, 1
store i32 %38, i32* %6
%39 = load i32, i32* %5
switch i32 %39, label %41 [
i32 319, label %40
]
40: ; preds = %36
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %8
br label %42
41: ; preds = %36
store i8* null, i8** %8
br label %42
42: ; preds = %41, %40
%43 = load i8*, i8** %8
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i8*, i8** %8
%47 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %46)
br label %51
48: ; preds = %42
%49 = load i32, i32* %5
%50 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %48, %45
br label %52
52: ; preds = %51, %32
br label %53
53: ; preds = %52
%54 = load i32, i32* %5
%55 = add i32 %54, 1
store i32 %55, i32* %5
br label %28
56: ; preds = %28
%57 = load i32, i32* %6
%58 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %57)
store i32 0, i32* %7
br label %59
59: ; preds = %56, %23
%60 = load i8*, i8** %3
%61 = load i32, i32* %7
switch i32 %61, label %63 [
i32 0, label %62
i32 1, label %62
]
62: ; preds = %59, %59
ret void
63: ; preds = %59
unreachable
}

LLVM IR (-O2):
@KEY_MAX = global i32 0
@EV_KEY = global i32 0
@.str = [45 x i8] c"event: error in ioctl(EVIOCGBIT(EV_KEY,%d))\0A\00"
@.str.1 = [15 x i8] c"event: EV_KEY:\00"
@.str.2 = [9 x i8] c"BTN_PLAY\00"
@.str.3 = [4 x i8] c" %s\00"
@.str.4 = [6 x i8] c" 0x%x\00"
@.str.5 = [7 x i8] c" [%u]\0A\00"
define void @event_key_log(i32 %0) {
%2 = load i32, i32* @KEY_MAX
%3 = sdiv i32 %2, 8
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @memset(i8* %6, i32 0, i32 %4)
%8 = load i32, i32* @EV_KEY
%9 = call i32 @EVIOCGBIT(i32 %8, i32 %4)
%10 = call i64 @ioctl(i32 %0, i32 %9, i8* %6)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @KEY_MAX
%14 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %39
15: ; preds = %1
%16 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @KEY_MAX
%18 = icmp eq i32 %17, 0
br i1 %18, label %36, label %19
19: ; preds = %15, %31
%20 = phi i32 [ %32, %31 ], [ 0, %15 ]
%21 = phi i32 [ %33, %31 ], [ 0, %15 ]
%22 = call i64 @event_test_bit(i32 %21, i8* %6)
%23 = icmp eq i64 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %19
%25 = add i32 %20, 1
%26 = icmp eq i32 %21, 319
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %31
29: ; preds = %24
%30 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %21)
br label %31
31: ; preds = %27, %29, %19
%32 = phi i32 [ %20, %19 ], [ %25, %29 ], [ %25, %27 ]
%33 = add nuw i32 %21, 1
%34 = load i32, i32* @KEY_MAX
%35 = icmp ult i32 %33, %34
br i1 %35, label %19, label %36
36: ; preds = %31, %15
%37 = phi i32 [ 0, %15 ], [ %32, %31 ]
%38 = call i32 (i8*, ...) @log_f(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %36, %12
ret void
}