// File: /workspace/github_cpp_makefile_star2_O3/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 0x3a62
movq %rsp, %rbx
movq %rbx, %rdi
callq 0x329b
movl (%rbx), %ebp
jmp 0x3b60
movl %eax, %ebx
cmpl $0x0, 0x200(%rbp)
jle 0x3aae
xorl %r13d, %r13d
movq %rbp, %r12
movl $0x1, (%rsp)
movq %r12, 0x4(%rsp)
movl 0x4(%r12), %edx
movl %ebx, %edi
movl $0x1, %esi
movq %rsp, %rcx
callq 0x3120
testl %eax, %eax
jne 0x3b25
incq %r13
movslq 0x200(%rbp), %rax
addq $0x10, %r12
cmpq %rax, %r13
jl 0x3a73
movq %rsp, %r12
movl %ebx, %edi
movq %r12, %rsi
movl $0x10, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x31c0
testl %eax, %eax
js 0x3af2
je 0x3ab1
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %r13
xorl %ebp, %ebp
movq 0x4(%rsp,%rbp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x388f
addq $0xc, %rbp
cmpq %rbp, %r13
jne 0x3ad7
jmp 0x3ab1
callq 0x3050
movl (%rax), %ebp
cmpl $0x4, %ebp
je 0x3ab1
movq 0x34eb(%rip), %rax      # 0x6ff0
movq (%rax), %r14
movl %ebp, %edi
callq 0x3230
leaq 0x1113(%rip), %rsi      # 0x4c29
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3160
jmp 0x3b59
callq 0x3050
movl (%rax), %ebp
movq 0x34bd(%rip), %rax      # 0x6ff0
movq (%rax), %r14
movq 0x8(%r12), %r15
movl %ebp, %edi
callq 0x3230
leaq 0x10c2(%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