// File: /workspace/github_cpp_makefile_star2_O2/exrok[P]evdoublebind/./src/evdoublebind.c
// Function: map_events
void map_events(char *input_path){
    struct input_event buf[INPUT_BUF_LEN];
    u_int32_t previous_scancode = 0, current_scancode = 0;
    int fd = open_input(input_path);

#if KEY_TIMEOUT_CONFIG != 0
    time_t last_sec = 0;
    suseconds_t last_usec = 0;
#endif
    while(1) {
        int rd = read(fd, buf, sizeof(buf));
        if (rd == -1) {
            close(fd);
            //Wait for device initialization
            for (int i = 0; i < 20; i++) { //Try acessing the path for 2s
                if (access(input_path, F_OK) != -1) break;
                usleep(100000);
            }
            int fd = open_input(input_path); //Try again maybe device reset.
            rd = read(fd, buf, sizeof(buf));
            if (rd != -1) {
                perror("Error: Opening Keyboard");
                exit(1);
            }
        }
        struct input_event *end = buf + (rd / sizeof(struct input_event));
        int after_scancode = 0;
        for(const struct input_event *curr = buf; curr < end; curr++){
            if (curr->type == EV_MSC && curr->code == MSC_SCAN) {
                current_scancode = curr->value;
                after_scancode = 1;
                continue;
            }

            if (curr->type != EV_KEY || !after_scancode || curr->value == V_KEY_REPEAT) continue;
            after_scancode = 0;

            if (curr->value == V_KEY_DOWN) {
#if KEY_TIMEOUT_CONFIG != 0
                last_sec = curr->time.tv_sec;
                last_usec = curr->time.tv_usec;
#endif
                previous_scancode = current_scancode;
            } else if (current_scancode == previous_scancode) {
#if KEY_TIMEOUT_CONFIG != 0
                long sec_diff = curr->time.tv_sec - last_sec;
                if (sec_diff > 1) continue;
                if (curr->time.tv_usec - last_usec + (sec_diff * 1000000)  > KEY_TIMEOUT_CONFIG)
                    continue;
#endif
                const KeyBind *index = find_keybind(curr->code);
                if (!index) continue;
                if (index->tap_modifier)
                    tap_mod_key(index->tap_key,index->tap_modifier, fd);
                else
                    tap_key(index->tap_key, fd);

            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, %rbx
callq 0x134e
movl %eax, 0xc(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x20(%rsp), %r12
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movl $0x180, %edx            # imm = 0x180
movl 0xc(%rsp), %edi
movq %r12, %rsi
callq 0x1060
cmpl $-0x1, %eax
jne 0x1442
movl 0xc(%rsp), %edi
callq 0x1050
pushq $0x14
popq %r15
subl $0x1, %r15d
jb 0x141c
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1090
cmpl $-0x1, %eax
jne 0x141c
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x10b0
jmp 0x13fb
movq %rbx, %rdi
callq 0x134e
movl $0x180, %edx            # imm = 0x180
movl %eax, %edi
movq %r12, %rsi
callq 0x1060
movq %rax, %rcx
pushq $-0x1
popq %rax
cmpl $-0x1, %ecx
jne 0x155d
cltq
xorl %r13d, %r13d
xorl %edx, %edx
pushq $0x18
popq %rcx
divq %rcx
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %r15
addq $0x20, %r15
movq 0x18(%rsp), %rdx
pushq $0x1
popq %rsi
movq 0x10(%rsp), %rdi
cmpq %r15, %r12
jae 0x1549
movzwl 0x10(%r12), %eax
cmpw $0x4, %ax
jne 0x148d
cmpw $0x4, 0x12(%r12)
jne 0x14c4
movl 0x14(%r12), %r14d
jmp 0x14c1
cmpw $0x1, %ax
sete %al
testl %r13d, %r13d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x14c4
movl 0x14(%r12), %eax
cmpl $0x2, %eax
je 0x14c1
cmpl $0x1, %eax
jne 0x14ca
movq (%r12), %rdi
movq 0x8(%r12), %rdx
xorl %r13d, %r13d
movl %r14d, %ebp
jmp 0x14c4
movl %esi, %r13d
addq $0x18, %r12
jmp 0x1468
xorl %r13d, %r13d
cmpl %ebp, %r14d
jne 0x14c4
movq (%r12), %rax
subq %rdi, %rax
cmpq $0x1, %rax
jg 0x14bc
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
subq %rdx, %rax
addq 0x8(%r12), %rax
cmpq $0x35b60, %rax          # imm = 0x35B60
jg 0x14bc
movq %rdi, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movzwl 0x12(%r12), %edi
callq 0x1325
testq %rax, %rax
je 0x1534
movzwl 0x4(%rax), %ecx
movzwl 0x2(%rax), %edi
testw %cx, %cx
je 0x152b
movzwl %cx, %esi
movl 0xc(%rsp), %edx
callq 0x126d
jmp 0x1534
movl 0xc(%rsp), %esi
callq 0x12db
movl %r14d, %ebp
movq 0x18(%rsp), %rdx
pushq $0x1
popq %rsi
movq 0x10(%rsp), %rdi
jmp 0x14c4
movq %rdi, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
leaq 0x20(%rsp), %r12
jmp 0x13d8
leaq 0xaa0(%rip), %rdi       # 0x2004
callq 0x1255
pushq $0x1
popq %rdi
callq 0x10a0