// File: /workspace/github_cpp_makefile_star2_O2/exrok[P]evdoublebind/./src/inspector.cc
// Function: poll_keyboard_events(Settings&, KeyboardList&)
int poll_keyboard_events(Settings &settings, KeyboardList &keyboards) {

    struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
    struct xkb_rule_names names = settings.xkb_names;
    struct xkb_keymap *keymap = xkb_keymap_new_from_names(ctx, &names,
                                       XKB_KEYMAP_COMPILE_NO_FLAGS);
    struct xkb_state *state = xkb_state_new(keymap);
    int epfd, ret;
    struct epoll_event evs[16];


    epfd = epoll_create1(0);
    if (epfd < 0) {
        fprintf(stderr, "Couldn't create epoll instance: %s\n",
                strerror(errno));
        return -errno;
    }

    for (int i =0; i < keyboards.len; i++) {
        struct epoll_event ev;
        memset(&ev, 0, sizeof(ev));
        ev.events = EPOLLIN;
        ev.data.ptr = keyboards.keyboards + i;
        ret = epoll_ctl(epfd, EPOLL_CTL_ADD, keyboards.keyboards[i].fd, &ev);
        if (ret) {
            ret = -errno;
            fprintf(stderr, "Couldn't add %s to epoll: %s\n",
                    keyboards.keyboards[i].name, strerror(errno));
            goto err_epoll;
        }
    }

    while (1) {
        ret = epoll_wait(epfd, evs, 16, -1);
        if (ret < 0) {
            if (errno == EINTR) continue;
            ret = -errno;
            fprintf(stderr, "Poll Error for events: %s\n",
                    strerror(errno));
            goto err_epoll;
        }
        for (int i = 0; i < ret; i++) {
            read_events(keymap, state, (Keyboard *)evs[i].data.ptr);
            // kbd = evs[i].data.ptr;
            // ret = read_keyboard(kbd);
            // if (ret) {
            //     goto err_epoll;
            // }
        }
    }

    close(epfd);
    return 0;

err_epoll:
    close(epfd);
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %rbp
movq %rdi, %rbx
xorl %edi, %edi
callq 0x3170
movq 0x20(%rbx), %rcx
leaq 0xc0(%rsp), %rsi
movq %rcx, 0x20(%rsi)
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movaps %xmm1, 0x10(%rsi)
movaps %xmm0, (%rsi)
movq %rax, %rdi
xorl %edx, %edx
callq 0x30e0
movq %rax, %r14
movq %rax, %rdi
callq 0x3140
movq %rax, %r15
xorl %edi, %edi
callq 0x3240
testl %eax, %eax
jns 0x3997
movq 0x3687(%rip), %rax      # 0x6ff0
movq (%rax), %rbx
callq 0x3050
movq %rax, %r14
movl (%rax), %edi
callq 0x3230
leaq 0x1265(%rip), %rsi      # 0x4be7
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3160
movl (%r14), %ebp
jmp 0x3a80
movl %eax, %ebx
xorl %r13d, %r13d
movq %rbp, %r12
movslq 0x200(%rbp), %rax
cmpq %rax, %r13
jge 0x39d6
movl $0x1, (%rsp)
movq %r12, 0x4(%rsp)
movl 0x4(%r12), %edx
movl %ebx, %edi
pushq $0x1
popq %rsi
movq %rsp, %rcx
callq 0x3120
testl %eax, %eax
jne 0x3a45
incq %r13
addq $0x10, %r12
jmp 0x399f
pushq $-0x1
popq %r12
movl %ebx, %edi
movq %rsp, %rsi
pushq $0x10
popq %rdx
movl %r12d, %ecx
callq 0x31c0
testl %eax, %eax
js 0x3a12
movl %eax, %eax
imulq $0xc, %rax, %rbp
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0x39da
movq 0x4(%rsp,%r13), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x37b8
addq $0xc, %r13
jmp 0x39f7
callq 0x3050
movl (%rax), %ebp
cmpl $0x4, %ebp
je 0x39da
movq 0x35cb(%rip), %rax      # 0x6ff0
movq (%rax), %r14
movl %ebp, %edi
callq 0x3230
leaq 0x11f3(%rip), %rsi      # 0x4c29
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3160
jmp 0x3a79
callq 0x3050
movl (%rax), %ebp
movq 0x359d(%rip), %rax      # 0x6ff0
movq (%rax), %r14
movq 0x8(%r12), %r15
movl %ebp, %edi
callq 0x3230
leaq 0x11a2(%rip), %rsi      # 0x4c0b
movq %r14, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3160
movl %ebx, %edi
callq 0x3100
negl %ebp
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq