// File: /workspace/github_cpp_makefile_star2_O1/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 0x62313
movq %rdx, %rbx
movq %rsi, %rbp
movq %rcx, 0x10(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x8(%rsp)
movq 0x8(%rdx), %r15
movslq 0x24(%rsi), %rax
movq (%rdx), %rcx
movq %rax, %rdx
shlq $0x4, %rdx
movl (%rcx,%rdx), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movq $-0x1, %r9
xorl %r13d, %r13d
movq $-0x1, %r8
movq $-0x1, %rcx
movq $-0x1, %rdx
movq 0x58(%rbp), %rdi
cmpw $0x0, 0x12(%rdi,%r13)
js 0x62238
cmpq $-0x1, %rdx
je 0x6221a
subq $0x10, %rsp
movq %rbx, %rdi
movq %r15, %rsi
pushq %rax
pushq %r14
callq 0x61cc6
addq $0x20, %rsp
movq $-0x1, %rdx
movq $-0x1, %rcx
movq $-0x1, %r8
movq $-0x1, %r9
jmp 0x62263
movslq 0xc(%rdi,%r13), %rsi
cmpq $-0x1, %rsi
je 0x621fe
cmpq $-0x1, %rdx
jne 0x62255
movslq (%rdi,%r13), %rdx
movslq -0x14(%rdi,%r13), %r8
incq %r8
movslq 0x4(%rdi,%r13), %rcx
incq %rcx
incq %rsi
movq %rsi, %r9
addq $0x20, %r13
decq %r12
jne 0x621f1
cmpq $-0x1, %rdx
je 0x62288
subq $0x10, %rsp
movq %rbx, %rdi
movq %r15, %rsi
pushq %rax
pushq %r14
callq 0x61cc6
addq $0x20, %rsp
subq $0x10, %rsp
movq %rbx, %rdi
movq %r15, %rsi
movq $-0x1, %rdx
movq $-0x1, %rcx
movq $-0x1, %r8
movq $-0x1, %r9
pushq %rax
pushq %r14
callq 0x61cc6
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 0x622e0
movq %r14, %rsi
movq %rbp, %rdx
callq 0x62075
cmpq $0x0, 0x18(%rsp)
je 0x6230d
movl $0x10, %ecx
movq %r14, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rdx
movsd 0x20(%rsp), %xmm0
callq 0x44add
movq %rax, %rbp
addq %r15, %rax
movq %rax, 0x8(%rbx)
movq %r15, 0x8(%rbx)
jmp 0x62315
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq