// File: /workspace/github_cpp_makefile_star2_O1/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 $0x228, %rsp            # imm = 0x228
movq %rdi, %rbx
callq 0x1377
movl %eax, 0xc(%rsp)
xorl %esi, %esi
xorl %edi, %edi
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %rdi, %r12
movq %rsi, %r15
movl $0x180, %edx            # imm = 0x180
movl 0xc(%rsp), %edi
leaq 0xa0(%rsp), %rsi
callq 0x1060
cmpl $-0x1, %eax
jne 0x146e
movl 0xc(%rsp), %edi
callq 0x1050
movl $0x14, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1090
cmpl $-0x1, %eax
jne 0x143f
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x10b0
decl %ebp
jne 0x1422
movq %rbx, %rdi
callq 0x1377
movl $0x180, %edx            # imm = 0x180
movl %eax, %edi
leaq 0xa0(%rsp), %rsi
callq 0x1060
movq %rax, %rcx
movq $-0x1, %rax
cmpl $-0x1, %ecx
jne 0x1658
movslq %eax, %rcx
movq %rcx, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rdx
cmpq $0x18, %rcx
movq %r15, %rsi
movq %r12, %rdi
jb 0x13f3
shrq $0x4, %rdx
leaq (%rdx,%rdx,2), %rax
leaq (%rsp,%rax,8), %rbp
addq $0xa0, %rbp
xorl %r15d, %r15d
leaq 0xa0(%rsp), %r12
movzwl 0x10(%r12), %eax
cmpw $0x4, %ax
jne 0x14cb
cmpw $0x4, 0x12(%r12)
jne 0x14cb
movl 0x14(%r12), %r13d
jmp 0x14ff
cmpw $0x1, %ax
sete %al
testl %r15d, %r15d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1505
movl 0x14(%r12), %eax
cmpl $0x2, %eax
je 0x14ff
cmpl $0x1, %eax
jne 0x1513
movq (%r12), %rdi
movq 0x8(%r12), %rsi
xorl %r15d, %r15d
movl %r13d, %r14d
jmp 0x1505
movl $0x1, %r15d
addq $0x18, %r12
cmpq %rbp, %r12
jb 0x14af
jmp 0x13f3
xorl %r15d, %r15d
cmpl %r14d, %r13d
jne 0x1505
movq (%r12), %rax
subq %rdi, %rax
cmpq $0x1, %rax
jg 0x1505
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
subq %rsi, %rax
addq 0x8(%r12), %rax
cmpq $0x35b60, %rax          # imm = 0x35B60
jg 0x1505
movslq 0x2cba(%rip), %rax      # 0x4200
testq %rax, %rax
jle 0x1566
movzwl 0x12(%r12), %edx
leaq 0x2b28(%rip), %rcx      # 0x4080
cmpw %dx, (%rcx)
je 0x1568
addq $0x6, %rcx
decq %rax
jne 0x1558
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x1505
movq %rdi, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movzwl 0x2(%rcx), %eax
movzwl 0x4(%rcx), %ecx
xorps %xmm0, %xmm0
testw %cx, %cx
je 0x15f9
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x90(%rsp)
movw $0x1, %dx
movw %dx, 0x30(%rsp)
movw %cx, 0x32(%rsp)
movl $0x1, %esi
movl %esi, 0x34(%rsp)
movw %dx, 0x48(%rsp)
movw %ax, 0x4a(%rsp)
movl %esi, 0x4c(%rsp)
movw %dx, 0x60(%rsp)
movw %ax, 0x62(%rsp)
movw %dx, 0x78(%rsp)
movw %cx, 0x7a(%rsp)
movl $0x78, %edx
jmp 0x163b
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x60(%rsp)
movw $0x1, %cx
movw %cx, 0x30(%rsp)
movw %ax, 0x32(%rsp)
movl $0x1, 0x34(%rsp)
movw %cx, 0x48(%rsp)
movw %ax, 0x4a(%rsp)
movl $0x48, %edx
movl 0xc(%rsp), %edi
leaq 0x20(%rsp), %rsi
callq 0x1030
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
jmp 0x1505
leaq 0x9a5(%rip), %rdi       # 0x2004
callq 0x1267
movl $0x1, %edi
callq 0x10a0