// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_cns_chain(overlap_region_alloc*, overlap_region*, Candidates_list*, asg64_v*, long, double, long)
int64_t gen_cns_chain(overlap_region_alloc* ol, overlap_region *z, Candidates_list *cl, asg64_v* iidx, int64_t max_lgap, double sgap_rate, int64_t need_filter_khit)
{
    int64_t k, wn = z->w_list.n, aln_n, qs, qe, ts, te, ci, id, rcn = cl->length, kn; k_mer_hit *ka; 
    if(wn <= 0) return 0;
    qs = qe = ts = te = -1; ci = 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) {
                // if(z->y_id == 109111 || z->y_id == 75436) {
                //     fprintf(stderr, "\n[M::%s::] q::[%ld, %ld), t::[%ld, %ld), ci::%ld, cn::%lld\n", 
                //         __func__, qs, qe, ts, te, ci, cl->length);
                // }
                ci = push_adp_k_hits(cl, rcn, qs, qe, ts, te, id, ci, z->y_id);
                aln_n++;//[qs, qe); [ts, te)
            }
            qs = qe = ts = te = -1;
        }
    }
    if(qs != -1) {
        // if(z->y_id == 109111 || z->y_id == 75436) {
        //     fprintf(stderr, "\n[M::%s::] q::[%ld, %ld), t::[%ld, %ld), ci::%ld, cn::%lld\n", 
        //         __func__, qs, qe, ts, te, ci, cl->length);
        // }
        ci = push_adp_k_hits(cl, rcn, qs, qe, ts, te, id, ci, z->y_id);
        aln_n++;//[qs, qe); [ts, te)
    }
    // if(z->y_id == 109111 || z->y_id == 75436) {
    //     fprintf(stderr, "\n[M::%s::] q::[%ld, %ld), t::[%ld, %ld), ci::%ld, cn::%lld, rcn::%ld\n", 
    //                     __func__, qs, qe, ts, te, ci, cl->length, rcn);
    // }
    push_adp_k_hits(cl, rcn, (uint64_t)-1, (uint64_t)-1, (uint64_t)-1, (uint64_t)-1, id, ci, z->y_id);

    ka = cl->list+rcn; kn = cl->length-rcn;
    // if(z->y_id == 66) {
    //     fprintf(stderr, "[M::%s::] kn::%ld, q_pos::%u, t_pos::%u\n", __func__, kn, ka[kn-1].self_offset, ka[kn-1].offset);
    // }
    // if(z->y_id == 109111 || z->y_id == 75436) {
    //     prt_khit(cl, ol, NULL, 109111, "sa");
    // }
    if(iidx) gen_weight_khits(iidx, ka, kn);
    // if(z->y_id == 109111 || z->y_id == 75436) {
    //     prt_khit(cl, ol, NULL, 109111, "sb");
    // }
    if(need_filter_khit) {
        kn = lchain_dp_trace(ka, kn, max_lgap, sgap_rate, SGAP); cl->length = rcn + kn;
    }
    cl->length = rcn;
    return kn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movsd %xmm0, 0x20(%rsp)
movq 0x48(%rsi), %r12
testq %r12, %r12
jle 0x31878
movq %rdx, %rbx
movq %rsi, %rbp
movq %rcx, 0x10(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x8(%rsp)
movq 0x8(%rdx), %rax
movq %rax, (%rsp)
movslq 0x24(%rsi), %rax
movq (%rdx), %rcx
movq %rax, %rdx
shlq $0x4, %rdx
movl (%rcx,%rdx), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
pushq $-0x1
popq %r13
xorl %r15d, %r15d
movq %r13, %rdx
movq %r13, %rcx
movq %r13, %r8
movq %r13, %r9
subq $0x1, %r12
jb 0x31855
movq 0x58(%rbp), %rdi
cmpw $0x0, 0x12(%rdi,%r15)
js 0x31824
cmpq $-0x1, %rdx
je 0x31816
subq $0x10, %rsp
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
pushq %rax
pushq %r14
callq 0x3125d
addq $0x20, %rsp
movq %r13, %rdx
movq %r13, %rcx
movq %r13, %r8
movq %r13, %r9
jmp 0x3184f
movslq 0xc(%rdi,%r15), %rsi
cmpq $-0x1, %rsi
je 0x317f8
cmpq $-0x1, %rdx
jne 0x31841
movslq (%rdi,%r15), %rdx
movslq -0x14(%rdi,%r15), %r8
incq %r8
movslq 0x4(%rdi,%r15), %rcx
incq %rcx
incq %rsi
movq %rsi, %r9
addq $0x20, %r15
jmp 0x317e5
cmpq $-0x1, %rdx
je 0x3187c
subq $0x10, %rsp
movq %rbx, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
pushq %rax
pushq %r14
callq 0x3125d
addq $0x20, %rsp
jmp 0x31880
xorl %ebp, %ebp
jmp 0x318f0
movq (%rsp), %r15
subq $0x10, %rsp
pushq $-0x1
popq %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %rcx, %rdx
movq %rcx, %r8
movq %rcx, %r9
pushq %rax
pushq %r14
callq 0x3125d
addq $0x20, %rsp
movq %r15, %r14
shlq $0x4, %r14
addq (%rbx), %r14
movq 0x8(%rbx), %rbp
subq %r15, %rbp
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x318c8
movq %r14, %rsi
movq %rbp, %rdx
callq 0x315c9
cmpq $0x0, 0x18(%rsp)
je 0x318ec
pushq $0x10
popq %rcx
movq %r14, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
movsd 0x20(%rsp), %xmm0
callq 0x17c0d
movq %rax, %rbp
movq %r15, 0x8(%rbx)
movq %rbp, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq