// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: filter_adp_k_hits(k_mer_hit*, long, Chain_Data*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, long, long*, k_mer_hit*, double, long)
int64_t filter_adp_k_hits(k_mer_hit *ka, int64_t kn, Chain_Data *dp, uint64_t qs, uint64_t qe, uint64_t ts, uint64_t te, uint64_t readID, int64_t ci, int64_t *cmi, k_mer_hit *m,
double small_bw_rate, int64_t min_small_bw)
{
    int64_t k = ci, cmi0 = (*cmi); k_mer_hit *p = NULL, n;
    for (; k < kn && ka[k].readID == readID && ka[k].self_offset < qs; k++) {
        p = &(ka[k]);
        // fprintf(stderr, "[M::%s::] p->q::%ld, p->t::%ld\n", __func__, p->self_offset, p->offset);
        if(p->offset >= ts) continue;
        if((m) && ((m->offset >= p->offset) || (m->self_offset >= p->self_offset))) continue;
        ka[(*cmi)++] = *p;
    }
    n.self_offset = qs; n.offset = ts; p = &n;
    if(qs == (uint64_t)-1 || qe == (uint64_t)-1) p = NULL;
    // fprintf(stderr, "[M::%s::] cmi0::%ld, cmi::%ld\n", __func__, cmi0, (*cmi)); back = (*cmi);
    if((*cmi) > cmi0) {
        (*cmi) = cmi0 + filter_bad_khits(m, p, ka+cmi0, (*cmi)-cmi0, small_bw_rate, min_small_bw);
        if((*cmi) > cmi0) {
            (*cmi) = cmi0 + lchain_refine(ka+cmi0, (*cmi)-cmi0, ka+cmi0, dp, 50, 5000, 512, 16); 
        }
    }
    // if(back != (*cmi)) {
    //     fprintf(stderr, "sbsbsbsb[M::%s::] cmi0::%ld, cmi::%ld\n", __func__, cmi0, (*cmi));
    // }
    
    for (; k < kn && ka[k].readID == readID && ka[k].self_offset < qe; k++);
    return k;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %rbx
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, %r12
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %r10
movq 0x70(%rsp), %r13
movq 0x68(%rsp), %rbp
movq (%r10), %r11
movq %r13, %rax
shlq $0x4, %rax
leaq (%rax,%r12), %r15
addq $0x8, %r15
cmpq %r14, %r13
jge 0x31d8c
movl -0x8(%r15), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rbp, %rax
jne 0x31d8c
movl (%r15), %eax
cmpq %rcx, %rax
jae 0x31d8c
movl -0x4(%r15), %edx
cmpq %r9, %rdx
jae 0x31d83
testq %rdi, %rdi
je 0x31d69
cmpl %edx, 0x4(%rdi)
jae 0x31d83
cmpl %eax, 0x8(%rdi)
jae 0x31d83
leaq -0x8(%r15), %rax
movq (%r10), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r10)
shlq $0x4, %rdx
movups (%rax), %xmm1
movups %xmm1, (%r12,%rdx)
incq %r13
addq $0x10, %r15
jmp 0x31d36
movl %ecx, 0x20(%rsp)
movl %r9d, 0x1c(%rsp)
movq (%r10), %rax
subq %r11, %rax
jle 0x31e18
xorl %edx, %edx
cmpq $-0x1, %rbx
leaq 0x18(%rsp), %rsi
cmoveq %rdx, %rsi
cmpq $-0x1, %rcx
cmoveq %rdx, %rsi
movq %r11, %rcx
shlq $0x4, %rcx
addq %rcx, %r12
movq %r12, %rdx
movq %rax, %rcx
movq %r11, 0x8(%rsp)
callq 0x31b64
movq 0x8(%rsp), %r11
movq 0x78(%rsp), %rdx
leaq (%rax,%r11), %rcx
movq %rcx, (%rdx)
testq %rax, %rax
jle 0x31e18
pushq $0x32
popq %r8
movl $0x1388, %r9d           # imm = 0x1388
movq %r12, %rdi
movq %rax, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
pushq $0x10
pushq $0x200                  # imm = 0x200
movq %r11, %r12
callq 0x1751e
popq %rcx
popq %rdx
addq %r12, %rax
movq 0x78(%rsp), %rcx
movq %rax, (%rcx)
cmpq %r13, %r14
movq %r13, %rax
cmovgq %r14, %rax
cmpq %r14, %r13
jge 0x31e4a
movl -0x8(%r15), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rbp, %rcx
jne 0x31e47
movl (%r15), %ecx
cmpq %rbx, %rcx
jae 0x31e47
incq %r13
addq $0x10, %r15
jmp 0x31e22
movq %r13, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq