// File: /workspace/github_cpp_makefile_star2_O1/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 $0xf8, %rsp
movq %rsi, %r13
movq %rdi, %rbx
xorl %edi, %edi
callq 0x3170
movq 0x20(%rbx), %rcx
leaq 0xd0(%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
js 0x3b6c
movl %eax, %ebx
cmpl $0x0, 0x200(%r13)
setg %al
jle 0x3b45
xorl %ebp, %ebp
movq %r13, %r12
movq %r13, 0x8(%rsp)
movb %al, 0x3(%rsp)
movq $0x0, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x1, 0x10(%rsp)
movq %r12, 0x14(%rsp)
movl 0x4(%r12), %edx
movl %ebx, %edi
movl $0x1, %esi
leaq 0x10(%rsp), %rcx
callq 0x3120
movl %eax, %r13d
movl $0x0, %ecx
testl %eax, %eax
jne 0x3b32
testl %r13d, %r13d
jne 0x3b47
incq %rbp
movq 0x8(%rsp), %r13
movslq 0x200(%r13), %rax
addq $0x10, %r12
cmpq %rax, %rbp
setl %al
jl 0x3acc
jmp 0x3b4b
movq %r12, %rdi
leaq 0x4(%rsp), %rsi
callq 0x329b
movl 0x4(%rsp), %ecx
jmp 0x3b10
jmp 0x3b4b
movb 0x3(%rsp), %al
movl %ecx, %ebp
testb $0x1, %al
je 0x3b7d
movl %ebx, %edi
callq 0x3100
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x32e5
movl (%rbx), %eax
jmp 0x3b5a
leaq 0x10(%rsp), %r12
movl %ebx, %edi
movq %r12, %rsi
movl $0x10, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x31c0
testl %eax, %eax
js 0x3bc3
je 0x3b82
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %r13
xorl %ebp, %ebp
movq 0x14(%rsp,%rbp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x38ea
addq $0xc, %rbp
cmpq %rbp, %r13
jne 0x3ba8
jmp 0x3b82
callq 0x3050
movl (%rax), %edi
cmpl $0x4, %edi
je 0x3b82
movl %edi, %eax
negl %eax
movl %eax, %ebp
movq 0x3414(%rip), %rax      # 0x6ff0
movq (%rax), %r14
callq 0x3230
leaq 0x103e(%rip), %rsi      # 0x4c29
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3160
jmp 0x3b51