// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x50(%rbp)
jg 0x5a917
movq $0x0, -0x8(%rbp)
jmp 0x5ac14
movq $-0x1, -0x78(%rbp)
movq $-0x1, -0x70(%rbp)
movq $-0x1, -0x68(%rbp)
movq $-0x1, -0x60(%rbp)
movq -0x18(%rbp), %rax
movslq 0x24(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x80(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x88(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jge 0x5aace
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x5aa44
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $-0x1, 0xc(%rax)
je 0x5aa44
cmpq $-0x1, -0x60(%rbp)
jne 0x5aa04
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x78(%rbp)
jmp 0x5aabb
cmpq $-0x1, -0x60(%rbp)
je 0x5aa9b
movq -0x20(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %r8
movq -0x78(%rbp), %r9
movq -0x88(%rbp), %r11
movq -0x80(%rbp), %r10
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x59f80
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq $-0x1, -0x78(%rbp)
movq $-0x1, -0x70(%rbp)
movq $-0x1, -0x68(%rbp)
movq $-0x1, -0x60(%rbp)
jmp 0x5aabd
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x5a975
cmpq $-0x1, -0x60(%rbp)
je 0x5ab25
movq -0x20(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %r8
movq -0x78(%rbp), %r9
movq -0x88(%rbp), %r11
movq -0x80(%rbp), %r10
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x59f80
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %r11
movq -0x80(%rbp), %r10
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movq $-0x1, %r9
movq %r9, %rdx
movq %r9, %rcx
movq %r9, %r8
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x59f80
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x90(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5abb5
movq -0x28(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
callq 0x5a600
cmpq $0x0, -0x40(%rbp)
je 0x5abfa
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x30(%rbp), %rdx
movsd -0x38(%rbp), %xmm0
movl $0x10, %ecx
callq 0x2af30
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rcx
addq -0x98(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x90(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)