#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*/ uint8_t;
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ keyBitmask;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int BTN_MISC;
int /*<<< orphan*/ EVIOCGBIT(int /*<<< orphan*/, int);
int /*<<< orphan*/ EV_KEY;
int KEY_MAX;
int KEY_OK;
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ contains_nonzero_byte(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ strstr(char *, char *);
void find_keyboards(unsigned int *keyboards, int *index) {
  struct dirent *input_dir_entry;
  DIR *event_dir;
  int fd;
  uint8_t keyBitmask[(KEY_MAX + 1) / 8];
  char pathname[PATH_MAX];
  int i;
  if (*index > 0) {
    for (i = 0; i > *index; i++) {
      close(keyboards[i]);
      keyboards[i] = 0;
    }
    *index = 0;
  }
  memset(keyBitmask, 0, sizeof(keyBitmask));
  event_dir = opendir("/dev/input");
  if (!event_dir)
    return;
  while ((input_dir_entry = readdir(event_dir)) != NULL) {
    if (strstr(input_dir_entry->d_name, "event")) {
      snprintf(pathname, PATH_MAX, "/dev/input/%s", input_dir_entry->d_name);
      fd = open(pathname, O_RDONLY);
      if (fd < 0) {
        continue;
      }
      ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keyBitmask)), keyBitmask);
      if (contains_nonzero_byte(keyBitmask, 0, (BTN_MISC + 7) / 8) ||
          contains_nonzero_byte(keyBitmask, (KEY_OK + 7) / 8,
                                (KEY_MAX + 1 + 7) / 8)) {
        keyboards[*index] = fd;
        ++*index;
      } else {
        close(fd);
      }
    }
  }
  closedir(event_dir);
}


x86 assembly (clang, -O0):
<find_keyboards>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,%eax
add    $0x1,%eax
add    $0x8,%ecx
test   %eax,%eax
cmovs  %ecx,%eax
sar    $0x3,%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jle    1280 <find_keyboards+0xd0>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jle    1276 <find_keyboards+0xc6>
mov    -0x8(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1080 <close@plt>
mov    -0x8(%rbp),%rax
movslq -0x44(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    123a <find_keyboards+0x8a>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xd64(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12bc <find_keyboards+0x10c>
movl   $0x1,-0x48(%rbp)
jmp    1415 <find_keyboards+0x265>
jmp    12c1 <find_keyboards+0x111>
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1405 <find_keyboards+0x255>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xd25(%rip),%rsi
call   10d0 <strstr@plt>
cmp    $0x0,%rax
je     1400 <find_keyboards+0x250>
mov    -0x50(%rbp),%rdi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xd01(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    0x2cc0(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1339 <find_keyboards+0x189>
jmp    12c1 <find_keyboards+0x111>
mov    -0x60(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,-0x64(%rbp)
mov    0x2c8e(%rip),%rcx
mov    (%rcx),%edi
shl    $0x2,%rax
mov    %eax,%esi
call   1030 <EVIOCGBIT@plt>
mov    -0x64(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %eax,%esi
call   1070 <ioctl@plt>
mov    -0x58(%rbp),%rdi
mov    0x2c48(%rip),%rax
mov    (%rax),%eax
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
xor    %esi,%esi
call   10c0 <contains_nonzero_byte@plt>
cmp    $0x0,%rax
jne    13d2 <find_keyboards+0x222>
mov    -0x58(%rbp),%rdi
mov    0x2c2d(%rip),%rax
mov    (%rax),%eax
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    0x2c0f(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   10c0 <contains_nonzero_byte@plt>
cmp    $0x0,%rax
je     13f3 <find_keyboards+0x243>
mov    -0x24(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    13fb <find_keyboards+0x24b>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
jmp    1400 <find_keyboards+0x250>
jmp    12c1 <find_keyboards+0x111>
mov    -0x20(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_keyboards>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2df8(%rip),%rax
mov    (%rax),%ecx
lea    0x1(%rcx),%eax
add    $0x8,%ecx
test   %eax,%eax
cmovns %eax,%ecx
mov    %rsi,%rbx
mov    %rdi,-0x38(%rbp)
sar    $0x3,%ecx
mov    %rcx,%rdx
lea    0xf(,%rcx,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
cmpl   $0x0,(%rsi)
jle    121c <find_keyboards+0x6c>
movl   $0x0,(%rbx)
shl    $0x2,%edx
mov    %r13,%rdi
xor    %esi,%esi
mov    %rdx,-0x40(%rbp)
call   1060 <memset@plt>
lea    0xdcc(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     136a <find_keyboards+0x1ba>
mov    %rax,%r15
mov    %rbx,-0x30(%rbp)
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1362 <find_keyboards+0x1b2>
mov    %rax,%rbx
jmp    1286 <find_keyboards+0xd6>
nop
mov    -0x30(%rbp),%rcx
movslq (%rcx),%rax
mov    -0x38(%rbp),%rdx
mov    %r14d,(%rdx,%rax,4)
addl   $0x1,(%rcx)
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1362 <find_keyboards+0x1b2>
mov    (%rbx),%rdi
lea    0xd7b(%rip),%rsi
call   10d0 <strstr@plt>
test   %rax,%rax
je     1272 <find_keyboards+0xc2>
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    (%rbx),%rcx
mov    %r12,%rdi
lea    0xd61(%rip),%rdx
call   1050 <snprintf@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1272 <find_keyboards+0xc2>
mov    %eax,%r14d
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rsi
call   1030 <EVIOCGBIT@plt>
mov    %r14d,%edi
mov    %eax,%esi
mov    %r13,%rdx
call   1070 <ioctl@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edx
lea    0x7(%rdx),%eax
add    $0xe,%edx
test   %eax,%eax
cmovns %eax,%edx
sar    $0x3,%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10c0 <contains_nonzero_byte@plt>
test   %rax,%rax
jne    1260 <find_keyboards+0xb0>
mov    0x2cab(%rip),%rax
mov    (%rax),%esi
lea    0x7(%rsi),%eax
add    $0xe,%esi
test   %eax,%eax
cmovns %eax,%esi
sar    $0x3,%esi
mov    0x2c8c(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rdx),%eax
add    $0xf,%edx
test   %eax,%eax
cmovns %eax,%edx
sar    $0x3,%edx
mov    %r13,%rdi
call   10c0 <contains_nonzero_byte@plt>
test   %rax,%rax
jne    1260 <find_keyboards+0xb0>
mov    %r14d,%edi
call   1080 <close@plt>
jmp    1272 <find_keyboards+0xc2>
mov    %r15,%rdi
call   1090 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@KEY_MAX = global i32 0
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [6 x i8] c"event\00"
@.str.2 = [14 x i8] c"/dev/input/%s\00"
@O_RDONLY = global i32 0
@EV_KEY = global i32 0
@BTN_MISC = global i32 0
@KEY_OK = global i32 0
define void @find_keyboards(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
%13 = load i32, i32* @KEY_MAX
%14 = add nsw i32 %13, 1
%15 = sdiv i32 %14, 8
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %8
%18 = alloca i32, i64 %16
store i64 %16, i64* %9
%19 = load i32, i32* @PATH_MAX
%20 = zext i32 %19 to i64
%21 = alloca i8, i64 %20
store i64 %20, i64* %10
%22 = load i32*, i32** %4
%23 = load i32, i32* %22
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %47
25: ; preds = %2
store i32 0, i32* %11
br label %26
26: ; preds = %42, %25
%27 = load i32, i32* %11
%28 = load i32*, i32** %4
%29 = load i32, i32* %28
%30 = icmp sgt i32 %27, %29
br i1 %30, label %31, label %45
31: ; preds = %26
%32 = load i32*, i32** %3
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = load i32*, i32** %3
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
store i32 0, i32* %41
br label %42
42: ; preds = %31
%43 = load i32, i32* %11
%44 = add nsw i32 %43, 1
store i32 %44, i32* %11
br label %26
45: ; preds = %26
%46 = load i32*, i32** %4
store i32 0, i32* %46
br label %47
47: ; preds = %45, %2
%48 = mul nuw i64 4, %16
%49 = trunc i64 %48 to i32
%50 = call i32 @memset(i32* %18, i32 0, i32 %49)
%51 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32* %51, i32** %6
%52 = load i32*, i32** %6
%53 = icmp ne i32* %52, null
br i1 %53, label %55, label %54
54: ; preds = %47
store i32 1, i32* %12
br label %117
55: ; preds = %47
br label %56
56: ; preds = %113, %76, %55
%57 = load i32*, i32** %6
%58 = call %struct.dirent* @readdir(i32* %57)
store %struct.dirent* %58, %struct.dirent** %5
%59 = icmp ne %struct.dirent* %58, null
br i1 %59, label %60, label %114
60: ; preds = %56
%61 = load %struct.dirent*, %struct.dirent** %5
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = call i64 @strstr(i8* %63, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %113
66: ; preds = %60
%67 = load i32, i32* @PATH_MAX
%68 = load %struct.dirent*, %struct.dirent** %5
%69 = getelementptr inbounds %struct.dirent, %struct.dirent* %68, i32 0, i32 0
%70 = load i8*, i8** %69
%71 = call i32 @snprintf(i8* %21, i32 %67, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %70)
%72 = load i32, i32* @O_RDONLY
%73 = call i32 @open(i8* %21, i32 %72)
store i32 %73, i32* %7
%74 = load i32, i32* %7
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %77
76: ; preds = %66
br label %56
77: ; preds = %66
%78 = load i32, i32* %7
%79 = load i32, i32* @EV_KEY
%80 = mul nuw i64 4, %16
%81 = trunc i64 %80 to i32
%82 = call i32 @EVIOCGBIT(i32 %79, i32 %81)
%83 = call i32 @ioctl(i32 %78, i32 %82, i32* %18)
%84 = load i32, i32* @BTN_MISC
%85 = add nsw i32 %84, 7
%86 = sdiv i32 %85, 8
%87 = call i64 @contains_nonzero_byte(i32* %18, i32 0, i32 %86)
%88 = icmp ne i64 %87, 0
br i1 %88, label %99, label %89
89: ; preds = %77
%90 = load i32, i32* @KEY_OK
%91 = add nsw i32 %90, 7
%92 = sdiv i32 %91, 8
%93 = load i32, i32* @KEY_MAX
%94 = add nsw i32 %93, 1
%95 = add nsw i32 %94, 7
%96 = sdiv i32 %95, 8
%97 = call i64 @contains_nonzero_byte(i32* %18, i32 %92, i32 %96)
%98 = icmp ne i64 %97, 0
br i1 %98, label %99, label %109
99: ; preds = %89, %77
%100 = load i32, i32* %7
%101 = load i32*, i32** %3
%102 = load i32*, i32** %4
%103 = load i32, i32* %102
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds i32, i32* %101, i64 %104
store i32 %100, i32* %105
%106 = load i32*, i32** %4
%107 = load i32, i32* %106
%108 = add nsw i32 %107, 1
store i32 %108, i32* %106
br label %112
109: ; preds = %89
%110 = load i32, i32* %7
%111 = call i32 @close(i32 %110)
br label %112
112: ; preds = %109, %99
br label %113
113: ; preds = %112, %60
br label %56
114: ; preds = %56
%115 = load i32*, i32** %6
%116 = call i32 @closedir(i32* %115)
store i32 0, i32* %12
br label %117
117: ; preds = %114, %54
%118 = load i8*, i8** %8
%119 = load i32, i32* %12
switch i32 %119, label %121 [
i32 0, label %120
i32 1, label %120
]
120: ; preds = %117, %117
ret void
121: ; preds = %117
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@KEY_MAX = global i32 0
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [6 x i8] c"event\00"
@.str.2 = [14 x i8] c"/dev/input/%s\00"
@O_RDONLY = global i32 0
@EV_KEY = global i32 0
@BTN_MISC = global i32 0
@KEY_OK = global i32 0
define void @find_keyboards(i32* nocapture %0, i32* nocapture %1) {
%3 = load i32, i32* @KEY_MAX
%4 = add nsw i32 %3, 1
%5 = sdiv i32 %4, 8
%6 = zext i32 %5 to i64
%7 = alloca i32, i64 %6
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = load i32, i32* %1
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* %1
br label %14
14: ; preds = %13, %2
%15 = shl nsw i32 %5, 2
%16 = call i32 @memset(i32* %7, i32 0, i32 %15)
%17 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %66, label %19
19: ; preds = %14
%20 = call %struct.dirent* @readdir(i32* %17)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %64, label %22
22: ; preds = %19, %61
%23 = phi %struct.dirent* [ %62, %61 ], [ %20, %19 ]
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = call i64 @strstr(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %61, label %28
28: ; preds = %22
%29 = load i32, i32* @PATH_MAX
%30 = load i8*, i8** %24
%31 = call i32 @snprintf(i8* %10, i32 %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %10, i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %61, label %35
35: ; preds = %28
%36 = load i32, i32* @EV_KEY
%37 = call i32 @EVIOCGBIT(i32 %36, i32 %15)
%38 = call i32 @ioctl(i32 %33, i32 %37, i32* %7)
%39 = load i32, i32* @BTN_MISC
%40 = add nsw i32 %39, 7
%41 = sdiv i32 %40, 8
%42 = call i64 @contains_nonzero_byte(i32* %7, i32 0, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %53
44: ; preds = %35
%45 = load i32, i32* @KEY_OK
%46 = add nsw i32 %45, 7
%47 = sdiv i32 %46, 8
%48 = load i32, i32* @KEY_MAX
%49 = add nsw i32 %48, 8
%50 = sdiv i32 %49, 8
%51 = call i64 @contains_nonzero_byte(i32* %7, i32 %47, i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %59, label %53
53: ; preds = %44, %35
%54 = load i32, i32* %1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %0, i64 %55
store i32 %33, i32* %56
%57 = load i32, i32* %1
%58 = add nsw i32 %57, 1
store i32 %58, i32* %1
br label %61
59: ; preds = %44
%60 = call i32 @close(i32 %33)
br label %61
61: ; preds = %22, %59, %53, %28
%62 = call %struct.dirent* @readdir(i32* %17)
%63 = icmp eq %struct.dirent* %62, null
br i1 %63, label %64, label %22
64: ; preds = %61, %19
%65 = call i32 @closedir(i32* %17)
br label %66
66: ; preds = %14, %64
ret void
}