// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: refine_khits(overlap_region*, Candidates_list*, Chain_Data*, double)
void refine_khits(overlap_region *z, Candidates_list *cl, Chain_Data *dp, double sgap_rate)
{
    int64_t k, wn = z->w_list.n, aln_n, qs, qe, ts, te, ci, cmi, id, mm_gap = 64; 
    k_mer_hit p; p.self_offset = (uint32_t)-1; p.offset = (uint32_t)-1;
    if(wn <= 0) return;
    qs = qe = ts = te = -1; ci = cmi = z->shared_seed; id = cl->list[ci].readID;
    for (k = aln_n = 0; k < wn; k++) {
        if(z->w_list.a[k].extra_end < 0 && z->w_list.a[k].y_end != -1) {///anchor
            if(qs == -1) {
                qs = z->w_list.a[k].x_start; ts = z->w_list.a[k-1].y_end+1;
            }
            qe = z->w_list.a[k].x_end+1; te = z->w_list.a[k].y_end+1;
        } else {
            if(qs != -1) {
                // fprintf(stderr, "\n[M::%s::] q::[%ld, %ld), t::[%ld, %ld), ci::%ld\n", 
                // __func__, qs, qe, ts, te, ci);
                ci = filter_adp_k_hits(cl->list, cl->length, dp, qs, qe, ts, te, id, ci, &cmi, 
                                                                aln_n?&p:NULL, sgap_rate, mm_gap);                
                p.self_offset = qe - 1; p.offset = te -1;
                aln_n++;//[qs, qe); [ts, te)
            }
            qs = qe = ts = te = -1;
        }
    }
    if(qs != -1) {
        // fprintf(stderr, "\n[M::%s::] q::[%ld, %ld), t::[%ld, %ld), ci::%ld\n", 
        //         __func__, qs, qe, ts, te, ci);
        ci = filter_adp_k_hits(cl->list, cl->length, dp, qs, qe, ts, te, id, ci, &cmi, 
                                                                aln_n?&p:NULL, sgap_rate, mm_gap); 
        p.self_offset = qe - 1; p.offset = te -1;
        aln_n++;//[qs, qe); [ts, te)
    }
    // fprintf(stderr, "\n[M::%s::] q::[%ld, %ld), t::[%ld, %ld), ci::%ld\n", 
    //             __func__, qs, qe, ts, te, ci);
    ci = filter_adp_k_hits(cl->list, cl->length, dp, (uint64_t)-1, (uint64_t)-1, (uint64_t)-1, (uint64_t)-1, id, ci, &cmi, 
                                                            aln_n?&p:NULL, sgap_rate, mm_gap);
    for (k = cmi; k < ci; k++) cl->list[k].readID = ((uint32_t)(0x7fffffff));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq 0x48(%rdi), %rbp
movq $-0x1, 0x4c(%rsp)
testq %rbp, %rbp
jle 0x62691
movq %rdi, %r15
movslq 0x24(%rdi), %rax
leaq 0x70(%rsp), %r10
movq %rax, (%r10)
movq %rsi, 0x60(%rsp)
movq (%rsi), %rcx
movq %rax, %rsi
shlq $0x4, %rsi
movl (%rcx,%rsi), %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq $-0x1, %r12
xorl %ebx, %ebx
leaq 0x48(%rsp), %r8
xorl %r14d, %r14d
movq $-0x1, %r9
movq $-0x1, %r13
movq $-0x1, %rcx
movsd %xmm0, 0x38(%rsp)
movq %rdx, 0x58(%rsp)
movq %rdi, 0x68(%rsp)
movq 0x58(%r15), %rsi
cmpw $0x0, 0x12(%rsi,%rbx)
js 0x62571
cmpq $-0x1, %rcx
je 0x62553
movq 0x60(%rsp), %rsi
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
testq %r14, %r14
leaq 0x48(%rsp), %r8
cmoveq %r14, %r8
movq %r8, 0x20(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq %r11, 0x8(%rsp)
movq $0x40, 0x28(%rsp)
movq %r14, 0x40(%rsp)
movq %r13, %r8
movsd 0x38(%rsp), %xmm0
movq %r10, %r15
movq %r11, %r14
callq 0x622b0
leaq 0x48(%rsp), %r8
movq %r14, %r11
movq %r15, %r10
movq 0x68(%rsp), %r15
movq 0x58(%rsp), %rdx
movq 0x40(%rsp), %r14
movsd 0x38(%rsp), %xmm0
decl %r13d
movl %r13d, 0x50(%rsp)
decl %r12d
movl %r12d, 0x4c(%rsp)
incq %r14
movq $-0x1, %rcx
movq $-0x1, %r13
movq $-0x1, %r9
movq $-0x1, %r12
jmp 0x625a0
movslq 0xc(%rsi,%rbx), %rdi
cmpq $-0x1, %rdi
je 0x624c8
cmpq $-0x1, %rcx
jne 0x62592
movslq (%rsi,%rbx), %rcx
movslq -0x14(%rsi,%rbx), %r9
incq %r9
movslq 0x4(%rsi,%rbx), %r13
incq %r13
incq %rdi
movq %rdi, %r12
addq $0x20, %rbx
decq %rbp
jne 0x624b8
movq 0x60(%rsp), %rbx
movq (%rbx), %rdi
cmpq $-0x1, %rcx
je 0x6262e
movq 0x8(%rbx), %rsi
testq %r14, %r14
movq %r8, %rbp
cmoveq %r14, %r8
movq %r8, 0x20(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq %r11, 0x8(%rsp)
movq $0x40, 0x28(%rsp)
movq %r14, 0x40(%rsp)
movq %r13, %r8
movsd 0x38(%rsp), %xmm0
movq %r10, %r15
movq %r11, %r14
callq 0x622b0
movq %rbp, %r8
movq %r14, %r11
movq %r15, %r10
movq 0x58(%rsp), %rdx
movq 0x40(%rsp), %r14
movsd 0x38(%rsp), %xmm0
decl %r13d
movl %r13d, 0x50(%rsp)
decl %r12d
movl %r12d, 0x4c(%rsp)
incq %r14
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
testq %r14, %r14
cmoveq %r14, %r8
movq %r8, 0x20(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq %r11, 0x8(%rsp)
movq $0x40, 0x28(%rsp)
movq $-0x1, %rcx
movq $-0x1, %r8
movq $-0x1, %r9
callq 0x622b0
movq 0x70(%rsp), %rcx
subq %rcx, %rax
jle 0x62691
shlq $0x4, %rcx
movq (%rbx), %rdx
orl $0x7fffffff, (%rdx,%rcx) # imm = 0x7FFFFFFF
addq $0x10, %rcx
decq %rax
jne 0x6267e
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq