// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: cigar_gen_by_chain_adv(overlap_region*, Candidates_list*, long, long, ul_ov_t*, long, unsigned long, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, bit_extz_t*, overlap_region*, double, long, unsigned long, long)
void cigar_gen_by_chain_adv(overlap_region *z, Candidates_list *cl, int64_t ch_idx, int64_t ch_n,
ul_ov_t *ov, int64_t on, uint64_t wl, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, char* qstr, 
UC_Read *tu, bit_extz_t *exz, overlap_region *aux_o, double e_rate, int64_t ql, uint64_t rid, int64_t h_khit)
{
    if(on <= 0) return;
    int64_t i, tl, id = z->y_id, m; 
    k_mer_hit *ch_a = cl->list + ch_idx;
    if(hpc_g) tl = hpc_len(*hpc_g, id);
    else if(uref) tl = uref->ug->u.a[id].len;
    else tl = Get_READ_LENGTH((*rref), id);
    // fprintf(stderr, "\n[M::%s::rid->%ld] utg%.6dl(%c), z::[%u, %u)\n", 
    // __func__, rid, (int32_t)z->y_id+1, "+-"[z->y_pos_strand],  z->x_pos_s, z->x_pos_e+1);
    on = fusion_chain_ovlp(z, ch_a, ch_n, ov, on, wl, ql, tl);
    aux_o->w_list.n = aux_o->w_list.c.n = 0; 
    aux_o->y_id = z->y_id; aux_o->y_pos_strand = z->y_pos_strand;
    aux_o->x_pos_s = z->x_pos_s; aux_o->x_pos_e = z->x_pos_e;
    aux_o->y_pos_s = z->y_pos_s; aux_o->y_pos_e = z->y_pos_e;

    
    for (i = 0; i < on; i++) {
        // if(aux_o->y_id == 109111) {
        //     fprintf(stderr, "[M::%s::i->%ld] ovq::[%u, %u), ovt::[%u, %u), hits::[%d, %d)\n", __func__, i, 
        //                                 ov->qs, ov->qe, ov->ts, ov->te,
        //                                 (ov->qn!=((uint32_t)-1))?(int32_t)ov->qn:-1, (int32_t)ov->tn);
        // }
        assert((i<=0)||(ov[i].qs>ov[i-1].qe));
        ovlp_base_aln(z, ch_a, ch_n, &(ov[i]), wl, uref, hpc_g, rref, qstr, tu, exz, aux_o, e_rate, ql, tl, rid);
    }

    int64_t aux_n = aux_o->w_list.n;
    ///for debug
    // if(aux_o->y_id == 109111) {
    //     for (i = 0; i < ((int64_t)aux_o->w_list.n); i++) {
    //         fprintf(stderr, "-0-[aln::i->%ld::ql->%d] q::[%d, %d), t::[%d, %d), err::%d, clen::%u, extra_end::%d, mode::%d\n", i, 
    //                     aux_o->w_list.a[i].x_end+1-aux_o->w_list.a[i].x_start,
    //                     aux_o->w_list.a[i].x_start, aux_o->w_list.a[i].x_end+1, 
    //                     aux_o->w_list.a[i].y_start, aux_o->w_list.a[i].y_end+1, 
    //                     aux_o->w_list.a[i].error, aux_o->w_list.a[i].clen, 
    //                     aux_o->w_list.a[i].extra_end, aux_o->w_list.a[i].error_threshold);
    //     }
    // }

    for (i = 0; i < aux_n; i++) {
        if(!(is_ualn_win(aux_o->w_list.a[i]))) continue;
        //will overwrite ch_a; does not matter
        rechain_aln(z, cl, aux_o, i, wl, uref, hpc_g, rref, qstr, tu, exz, e_rate, ql, tl, h_khit, rid);
    }

    ///for debug
    // if(aux_o->y_id == 109111) {
    //     for (i = 0; i < ((int64_t)aux_o->w_list.n); i++) {
    //         fprintf(stderr, "-2-[aln::i->%ld::ql->%d] q::[%d, %d), t::[%d, %d), err::%d, clen::%u, mode::%d\n", i, 
    //                     aux_o->w_list.a[i].x_end+1-aux_o->w_list.a[i].x_start,
    //                     aux_o->w_list.a[i].x_start, aux_o->w_list.a[i].x_end+1, 
    //                     aux_o->w_list.a[i].y_start, aux_o->w_list.a[i].y_end+1, 
    //                     aux_o->w_list.a[i].error, aux_o->w_list.a[i].clen, aux_o->w_list.a[i].error_threshold);
    //     }
    // }

    if(((int64_t)aux_o->w_list.n) > aux_n) {
        for (i = m = 0; i < ((int64_t)aux_o->w_list.n); i++) {
            if((i < aux_n) && (is_ualn_win(aux_o->w_list.a[i]))) continue;
            aux_o->w_list.a[m++] = aux_o->w_list.a[i];
        }
        aux_o->w_list.n = m;
        radix_sort_window_list_xs_srt(aux_o->w_list.a, aux_o->w_list.a+aux_o->w_list.n);
    }

    ///update z by aux_o
    update_overlap_region(z, aux_o, ql, tl);

    // debug_overlap_region(aux_o, qstr, tu, uref, hpc_g, rref);


    // ch_a = cl->list + ch_idx; //update
    // for (i = 0; i < wn; i++) z->w_list.a[i].clen = 0;///clean cigar
    // if(on > 1) {
    //     fprintf(stderr, "[M::%s::] rid::%lu, on::%ld\n", __func__, rid, on);
    // }
    // if(z->y_id == 126) prt_k_mer_hit(ch_a, ch_n);
    // for (i = ch_i = 0; i < on; i++) {
    //     assert((i<=0)||(ov[i].qs > ov[i-1].qe));
    //     ov[i].sec = 16;///do not know the aln type
    //     ch_i = sub_base_aln(z, dp, ch_a, ch_n, pe, ov[i].qs, ov[i].qe, wl, uref, hpc_g, rref, qstr, tu, exz, e_rate, ql, tl, ch_i, rid);
    //     pe = ov[i].qe;
    // }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm0, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
testq %r9, %r9
jle 0x69ca7
movq %r8, %r14
movq %rdx, %rbx
movq %rdi, %r13
movq 0xe0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xd8(%rsp), %r15
movq 0xb8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xb0(%rsp), %rcx
movq 0xa8(%rsp), %rdx
movl 0x10(%rdi), %eax
shlq $0x4, %rbx
addq (%rsi), %rbx
testq %rcx, %rcx
movq %rsi, 0x58(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
je 0x69cb6
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
movl (%rcx,%rax,8), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x69cdd
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rdx, %rdx
je 0x69cd0
movq (%rdx), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
movl (%rcx,%rax,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0x69cdd
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, (%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x18(%rsp)
subq $0x8, %rsp
movq %rbx, %rsi
movq 0x50(%rsp), %rdx
movq %r14, %rcx
movq %r9, %r8
pushq 0x10(%rsp)
callq 0x67c09
addq $0x10, %rsp
movq %rax, %rbp
xorl %eax, %eax
movq %rax, 0x60(%r15)
movq %rax, 0x48(%r15)
movl 0x10(%r13), %eax
movl %eax, 0x10(%r15)
movl 0x1c(%r13), %eax
movl %eax, 0x1c(%r15)
movl 0x4(%r13), %eax
movl %eax, 0x4(%r15)
movl 0x8(%r13), %eax
movl %eax, 0x8(%r15)
movl 0x14(%r13), %eax
movl %eax, 0x14(%r15)
movq %r13, %r12
movl 0x18(%r13), %eax
movl %eax, 0x18(%r15)
testq %rbp, %rbp
jle 0x69df4
xorl %r13d, %r13d
testq %r13, %r13
je 0x69d9c
movl 0x4(%r14), %eax
cmpl -0x14(%r14), %eax
jbe 0x69f1f
subq $0x10, %rsp
movq %r12, %rdi
movq %rbx, %rsi
movq 0x58(%rsp), %rdx
movq %r14, %rcx
movq 0x28(%rsp), %r8
movq 0x48(%rsp), %r9
movsd 0x60(%rsp), %xmm0
pushq 0x10(%rsp)
pushq 0x20(%rsp)
pushq %r15
pushq 0x58(%rsp)
pushq 0x58(%rsp)
pushq 0x58(%rsp)
pushq 0x50(%rsp)
pushq 0x88(%rsp)
callq 0x67fda
addq $0x50, %rsp
incq %r13
addq $0x1c, %r14
cmpq %r13, %rbp
jne 0x69d89
movq 0x48(%r15), %r14
testq %r14, %r14
movq %r12, %r13
movq 0x58(%rsp), %rbp
jle 0x69e83
movl $0x12, %r12d
xorl %ebx, %ebx
movq 0x58(%r15), %rax
cmpw $0x7fff, 0x2(%rax,%r12) # imm = 0x7FFF
jne 0x69e77
cmpl $0x0, 0xa(%rax,%r12)
jne 0x69e77
cmpw $0x0, (%rax,%r12)
jns 0x69e77
subq $0x10, %rsp
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq 0x28(%rsp), %r8
movq 0x48(%rsp), %r9
movsd 0x60(%rsp), %xmm0
pushq 0x70(%rsp)
pushq 0x18(%rsp)
pushq 0x28(%rsp)
pushq 0x58(%rsp)
pushq 0x58(%rsp)
pushq 0x58(%rsp)
pushq 0x50(%rsp)
pushq 0x88(%rsp)
callq 0x6947b
addq $0x50, %rsp
incq %rbx
addq $0x20, %r12
cmpq %rbx, %r14
jne 0x69e0d
cmpq %r14, 0x48(%r15)
jle 0x69efd
cmpq $0x0, 0x48(%r15)
jle 0x69ee7
xorl %eax, %eax
xorl %ecx, %ecx
xorl %esi, %esi
cmpq %r14, %rcx
jge 0x69eb7
movq 0x58(%r15), %rdx
cmpw $0x7fff, 0x14(%rdx,%rax) # imm = 0x7FFF
jne 0x69eb7
cmpl $0x0, 0x1c(%rdx,%rax)
jne 0x69eb7
cmpw $0x0, 0x12(%rdx,%rax)
js 0x69ed8
movq 0x58(%r15), %rdx
leaq 0x1(%rsi), %rdi
shlq $0x5, %rsi
movups (%rdx,%rax), %xmm0
movups 0x10(%rdx,%rax), %xmm1
movups %xmm1, 0x10(%rdx,%rsi)
movups %xmm0, (%rdx,%rsi)
movq %rdi, %rsi
incq %rcx
addq $0x20, %rax
cmpq 0x48(%r15), %rcx
jl 0x69e96
jmp 0x69ee9
xorl %esi, %esi
movq %rsi, 0x48(%r15)
movq 0x58(%r15), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x46b34
movq %r13, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movq (%rsp), %rcx
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x69a6f
leaq 0x1bd677(%rip), %rdi    # 0x22759d
leaq 0x1bb716(%rip), %rsi    # 0x225643
leaq 0x1bd687(%rip), %rcx    # 0x2275bb
movl $0x4398, %edx           # imm = 0x4398
callq 0x2110