// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq 0x48(%rdi), %rbp
orq $-0x1, 0x44(%rsp)
testq %rbp, %rbp
jle 0x32084
movq %rdi, %r10
movslq 0x24(%rdi), %rax
leaq 0x58(%rsp), %r15
movq %rax, (%r15)
movq %rsi, 0x50(%rsp)
movq (%rsi), %rcx
movq %rax, %rsi
shlq $0x4, %rsi
movl (%rcx,%rsi), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rcx, 0x38(%rsp)
pushq $-0x1
popq %r12
xorl %r14d, %r14d
movq %r12, %rcx
movq %r12, %r8
movq %r12, %r9
movq %r12, %r13
xorl %r11d, %r11d
movsd %xmm0, 0x30(%rsp)
movq %rdx, 0x60(%rsp)
subq $0x1, %rbp
jb 0x31faa
movq 0x58(%r10), %rsi
cmpw $0x0, 0x12(%rsi,%r14)
js 0x31f72
cmpq $-0x1, %rcx
je 0x31f64
movq 0x50(%rsp), %rsi
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
testq %r11, %r11
leaq 0x40(%rsp), %r12
cmoveq %r11, %r12
movq %r12, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movq $0x40, 0x28(%rsp)
movsd 0x30(%rsp), %xmm0
movq %r13, %rbx
movq %r8, %r13
movq %r10, %r12
movq %r11, %r15
callq 0x31ce9
movq %r15, %r11
movq %r12, %r10
pushq $-0x1
popq %r12
movq 0x60(%rsp), %rdx
leaq 0x58(%rsp), %r15
movsd 0x30(%rsp), %xmm0
decl %r13d
movl %r13d, 0x48(%rsp)
decl %ebx
movl %ebx, 0x44(%rsp)
incq %r11
movq %r12, %rcx
movq %r12, %r8
movq %r12, %r9
movq %r12, %r13
jmp 0x31fa1
movslq 0xc(%rsi,%r14), %rdi
cmpq $-0x1, %rdi
je 0x31ee2
cmpq $-0x1, %rcx
jne 0x31f93
movslq (%rsi,%r14), %rcx
movslq -0x14(%rsi,%r14), %r9
incq %r9
movslq 0x4(%rsi,%r14), %r8
incq %r8
incq %rdi
movq %rdi, %r13
addq $0x20, %r14
jmp 0x31ec7
movq 0x50(%rsp), %rbx
movq (%rbx), %rdi
cmpq $-0x1, %rcx
je 0x32020
movq 0x8(%rbx), %rsi
testq %r11, %r11
leaq 0x40(%rsp), %r14
cmoveq %r11, %r14
movq %r14, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movq $0x40, 0x28(%rsp)
movq %rdx, %r14
movsd 0x30(%rsp), %xmm0
movq %r8, %r12
movq %r11, %rbp
callq 0x31ce9
movq %rbp, %r11
movq %r14, %rdx
movsd 0x30(%rsp), %xmm0
decl %r12d
movl %r12d, 0x48(%rsp)
decl %r13d
movl %r13d, 0x44(%rsp)
incq %r11
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
testq %r11, %r11
leaq 0x40(%rsp), %rcx
cmoveq %r11, %rcx
movq %rcx, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movq $0x40, 0x28(%rsp)
pushq $-0x1
popq %rcx
movq %rcx, %r8
movq %rcx, %r9
callq 0x31ce9
movq 0x58(%rsp), %rcx
movq %rcx, %rdx
shlq $0x4, %rdx
cmpq %rax, %rcx
jge 0x32084
movq (%rbx), %rsi
orl $0x7fffffff, (%rsi,%rdx) # imm = 0x7FFFFFFF
incq %rcx
addq $0x10, %rdx
jmp 0x3206c
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq