// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq %r8, %r14
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r11
movq 0x70(%rsp), %rdi
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %rbx
movq 0x58(%rsp), %r13
movq (%r8), %r10
cmpq %rsi, %rbx
jge 0x62827
movq %rbx, %rax
shlq $0x4, %rax
addq %r11, %rax
movl (%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %r13, %rdx
jne 0x62827
movl 0x8(%rax), %edx
cmpq %rcx, %rdx
jae 0x62827
movl 0x4(%rax), %esi
cmpq %r9, %rsi
jae 0x62818
testq %rdi, %rdi
je 0x62802
cmpl %esi, 0x4(%rdi)
jae 0x62818
cmpl %edx, 0x8(%rdi)
jae 0x62818
movq (%r8), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r8)
shlq $0x4, %rdx
movups (%rax), %xmm1
movups %xmm1, (%r11,%rdx)
incq %rbx
addq $0x10, %rax
cmpq %rbx, %r15
jne 0x627d6
movq %r15, %rbx
movl %ecx, 0x10(%rsp)
movl %r9d, 0xc(%rsp)
movq (%r8), %rax
subq %r10, %rax
jle 0x628c9
xorl %edx, %edx
cmpq $-0x1, %r14
leaq 0x8(%rsp), %rsi
cmoveq %rdx, %rsi
cmpq $-0x1, %rcx
cmoveq %rdx, %rsi
movq 0x78(%rsp), %r8
movq %r10, %rbp
shlq $0x4, %rbp
addq %r11, %rbp
movq %rbp, %rdx
movq %rax, %rcx
movq %r12, %r13
movq %r10, %r12
movq %r11, (%rsp)
callq 0x62587
movq (%rsp), %r11
movq %r13, %rcx
movq 0x58(%rsp), %r13
movq 0x68(%rsp), %rdx
leaq (%rax,%r12), %rsi
movq %rsi, (%rdx)
testq %rax, %rax
jle 0x628c9
movl $0x32, %r8d
movl $0x1388, %r9d           # imm = 0x1388
movq %rbp, %rdi
movq %rax, %rsi
movq %rbp, %rdx
pushq $0x10
pushq $0x200                  # imm = 0x200
callq 0x441c0
movq 0x10(%rsp), %r11
addq $0x10, %rsp
addq %r12, %rax
movq 0x68(%rsp), %rcx
movq %rax, (%rcx)
cmpq %r15, %rbx
jge 0x62900
movq %rbx, %rax
shlq $0x4, %rax
addq %r11, %rax
addq $0x8, %rax
movl -0x8(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %r13, %rcx
jne 0x62900
movl (%rax), %ecx
cmpq %r14, %rcx
jae 0x62900
incq %rbx
addq $0x10, %rax
cmpq %rbx, %r15
jne 0x628dc
movq %r15, %rbx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq