// File: /workspace/github_cpp_makefile_star2_O3/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 $0x218, %rsp            # imm = 0x218
movq %rdi, %rbx
callq 0x1377
movl %eax, 0x4(%rsp)
xorl %edi, %edi
leaq 0x90(%rsp), %rsi
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r10d, %r10d
movl %r10d, 0x8(%rsp)
movq %rdi, %r13
movl $0x180, %edx            # imm = 0x180
movl 0x4(%rsp), %ebp
movl %ebp, %edi
callq 0x1060
cmpl $-0x1, %eax
jne 0x147e
movl %ebp, %edi
callq 0x1050
movl $0x14, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1090
cmpl $-0x1, %eax
jne 0x1442
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x10b0
decl %ebp
jne 0x1425
movq %rbx, %rdi
callq 0x1377
movl $0x180, %edx            # imm = 0x180
movl %eax, %edi
leaq 0x90(%rsp), %r15
movq %r15, %rsi
callq 0x1060
movq %r15, %rsi
leaq 0x80(%rsp), %r15
cmpl $-0x1, %eax
movq %r13, %rdi
movl 0x8(%rsp), %r10d
je 0x14be
jmp 0x1670
movslq %eax, %rcx
movq %rcx, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rdx
cmpq $0x18, %rcx
movq %r13, %rdi
leaq 0x90(%rsp), %rsi
movl 0x8(%rsp), %r10d
jb 0x13fc
shrq $0x4, %rdx
leaq (%rdx,%rdx,2), %rax
leaq (%rsp,%rax,8), %r15
addq $0x90, %r15
xorl %r13d, %r13d
movq %rsi, %rbp
movzwl 0x10(%rbp), %eax
cmpw $0x4, %ax
jne 0x14db
cmpw $0x4, 0x12(%rbp)
jne 0x1512
movl 0x14(%rbp), %r14d
jmp 0x150c
cmpw $0x1, %ax
sete %al
testl %r13d, %r13d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1512
movl 0x14(%rbp), %eax
cmpl $0x2, %eax
je 0x150c
cmpl $0x1, %eax
jne 0x1520
movq (%rbp), %r12
movq 0x8(%rbp), %rdi
xorl %r13d, %r13d
movl %r14d, %r10d
jmp 0x1512
movl $0x1, %r13d
addq $0x18, %rbp
cmpq %r15, %rbp
jb 0x14c4
jmp 0x13fc
xorl %r13d, %r13d
cmpl %r10d, %r14d
jne 0x1512
movq (%rbp), %rax
subq %r12, %rax
cmpq $0x1, %rax
jg 0x157b
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
subq %rdi, %rax
addq 0x8(%rbp), %rax
cmpq $0x35b60, %rax          # imm = 0x35B60
jg 0x157b
movslq 0x2cae(%rip), %rcx      # 0x4200
testq %rcx, %rcx
jle 0x157b
movzwl 0x12(%rbp), %eax
addq %rcx, %rcx
leaq (%rcx,%rcx,2), %rdx
xorl %ecx, %ecx
leaq 0x2b15(%rip), %r8       # 0x4080
cmpw %ax, (%rcx,%r8)
je 0x1580
addq $0x6, %rcx
cmpq %rcx, %rdx
jne 0x156b
movl %r10d, %r14d
jmp 0x1512
movl %r10d, %r14d
movq %rdi, 0x8(%rsp)
movzwl 0x2(%rcx,%r8), %eax
movzwl 0x4(%rcx,%r8), %ecx
xorps %xmm0, %xmm0
testw %cx, %cx
je 0x160b
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x80(%rsp)
movw $0x1, %dx
movw %dx, 0x20(%rsp)
movw %cx, 0x22(%rsp)
movl $0x1, %esi
movl %esi, 0x24(%rsp)
movw %dx, 0x38(%rsp)
movw %ax, 0x3a(%rsp)
movl %esi, 0x3c(%rsp)
movw %dx, 0x50(%rsp)
movw %ax, 0x52(%rsp)
movw %dx, 0x68(%rsp)
movw %cx, 0x6a(%rsp)
movl $0x78, %edx
jmp 0x164d
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x50(%rsp)
movw $0x1, %cx
movw %cx, 0x20(%rsp)
movw %ax, 0x22(%rsp)
movl $0x1, 0x24(%rsp)
movw %cx, 0x38(%rsp)
movw %ax, 0x3a(%rsp)
movl $0x48, %edx
movl 0x4(%rsp), %edi
leaq 0x10(%rsp), %rsi
callq 0x1030
movl %r14d, %r10d
movq 0x8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
jmp 0x1512
leaq 0x98d(%rip), %rdi       # 0x2004
callq 0x1267
movl $0x1, %edi
callq 0x10a0