// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rsi, %r15
movq %rdi, %r12
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r13
cmpq %rsi, %rbx
jge 0x623ec
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %r11
movq (%r11), %rbp
movq %rbx, %rax
shlq $0x4, %rax
addq %r12, %rax
movl (%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %r13, %rsi
jne 0x6234f
movl 0x8(%rax), %r10d
cmpq %rcx, %r10
jae 0x6234f
movl 0x4(%rax), %esi
cmpq %r9, %rsi
jae 0x62340
testq %rdi, %rdi
je 0x6232a
cmpl %esi, 0x4(%rdi)
jae 0x62340
cmpl %r10d, 0x8(%rdi)
jae 0x62340
movq (%r11), %r10
leaq 0x1(%r10), %rsi
movq %rsi, (%r11)
shlq $0x4, %r10
movups (%rax), %xmm1
movups %xmm1, (%r12,%r10)
incq %rbx
addq $0x10, %rax
cmpq %rbx, %r15
jne 0x622fc
movq %r15, %rbx
movq (%r11), %rax
movl %ecx, 0x20(%rsp)
movl %r9d, 0x1c(%rsp)
subq %rbp, %rax
jle 0x623ec
movq %rdx, 0x10(%rsp)
xorl %edx, %edx
cmpq $-0x1, %r14
leaq 0x18(%rsp), %rsi
cmoveq %rdx, %rsi
cmpq $-0x1, %rcx
cmoveq %rdx, %rsi
movq %r12, %rcx
movq %rbp, %r12
shlq $0x4, %rbp
movq %rcx, (%rsp)
addq %rcx, %rbp
movq %rbp, %rdx
movq %rax, %rcx
callq 0x62079
movq 0x78(%rsp), %r10
movq %r12, 0x8(%rsp)
leaq (%rax,%r12), %rcx
movq %rcx, (%r10)
testq %rax, %rax
jle 0x623e8
movl $0x32, %r8d
movl $0x1388, %r9d           # imm = 0x1388
movq %rbp, %rdi
movq %rax, %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
pushq $0x10
pushq $0x200                  # imm = 0x200
movq %r10, %rbp
callq 0x438d5
addq $0x10, %rsp
addq 0x8(%rsp), %rax
movq %rax, (%rbp)
movq (%rsp), %r12
cmpq %r15, %rbx
jge 0x62423
movq %rbx, %rax
shlq $0x4, %rax
addq %r12, %rax
addq $0x8, %rax
movl -0x8(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %r13, %rcx
jne 0x62423
movl (%rax), %ecx
cmpq %r14, %rcx
jae 0x62423
incq %rbx
addq $0x10, %rax
cmpq %rbx, %r15
jne 0x623ff
movq %r15, %rbx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq