// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: cigar_gen_by_chain_adv_local(overlap_region*, Candidates_list*, 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_local(overlap_region *z, Candidates_list *cl, 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 ch_idx = z->shared_seed, ch_n;
    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);
    for (i = ch_idx; i < cl->length && cl->list[i].readID == cl->list[ch_idx].readID; i++); ch_n = i-ch_idx;
    
    // fprintf(stderr, "[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++) {
        // 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_direct(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 (i = 0; i < aux_n; i++) {
        if(!(is_ualn_win(aux_o->w_list.a[i]))) continue;
        // if((aux_o->w_list.a[i].x_end+1-aux_o->w_list.a[i].x_start) <= FORCE_CNS_L) {
            // fprintf(stderr, "[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);
        // }
        //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);
    }
    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, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %rsi, 0x18(%rsp)
testq %rcx, %rcx
jle 0x390aa
movq %rcx, %r8
movq %rdx, %r14
movq %rdi, %r13
movq 0xa8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xa0(%rsp), %rdx
movslq 0x24(%rdi), %rax
movl 0x10(%rdi), %ecx
movq %rax, %rbx
shlq $0x4, %rbx
testq %rdx, %rdx
movq %r9, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
je 0x390b9
movq (%rdx), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %rcx, %rcx
movl (%rdx,%rcx), %ecx
shrl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
jmp 0x390e1
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r9, %r9
je 0x390d4
movq (%r9), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %rcx, %rcx
movl (%rdx,%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jmp 0x390e1
movq 0x10(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, (%rsp)
movq 0xe0(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movq 0xd0(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq 0xc8(%rsp), %r12
movq 0xc0(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0xb8(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq 0x18(%rsp), %rcx
addq (%rcx), %rbx
movq 0xb0(%rsp), %rdx
movq %rdx, 0x28(%rsp)
movq 0x8(%rcx), %rcx
cmpq %rax, %rcx
movq %rax, %r15
cmovgq %rcx, %r15
movq %rbx, %rsi
movq %rax, %rdx
cmpq %rcx, %rdx
jge 0x39167
movl (%rbx), %edi
xorl (%rsi), %edi
testl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
jne 0x39164
incq %rdx
addq $0x10, %rsi
jmp 0x3914a
movq %rdx, %r15
subq %rax, %r15
subq $0x8, %rsp
movq %rbx, %rsi
movq %r15, %rdx
movq %r14, %rcx
pushq 0x10(%rsp)
callq 0x366d4
popq %rcx
popq %rdx
andq $0x0, 0x60(%r12)
movq %rax, %rbp
andq $0x0, 0x48(%r12)
movl 0x10(%r13), %eax
movl %eax, 0x10(%r12)
movl 0x1c(%r13), %eax
movl %eax, 0x1c(%r12)
movq 0x4(%r13), %rax
movq %rax, 0x4(%r12)
movq %r13, 0x20(%rsp)
movq 0x14(%r13), %rax
movq %rax, 0x14(%r12)
xorl %r13d, %r13d
testq %rbp, %rbp
cmovleq %r13, %rbp
cmpq %r13, %rbp
je 0x39231
testq %r13, %r13
je 0x391dc
movl 0x4(%r14), %eax
cmpl -0x14(%r14), %eax
jbe 0x39360
subq $0x10, %rsp
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq 0x60(%rsp), %r8
movq 0x58(%rsp), %r9
movsd 0x68(%rsp), %xmm0
pushq 0x10(%rsp)
pushq 0x20(%rsp)
pushq %r12
pushq 0x60(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
pushq 0x50(%rsp)
pushq 0x88(%rsp)
callq 0x38748
addq $0x50, %rsp
incq %r13
addq $0x1c, %r14
jmp 0x391c4
movq 0x48(%r12), %r14
xorl %ebx, %ebx
testq %r14, %r14
movl $0x0, %r15d
cmovgq %r14, %r15
pushq $0x12
popq %rbp
movq 0x20(%rsp), %r13
cmpq %rbx, %r15
je 0x392c5
movq 0x58(%r12), %rax
cmpw $0x7fff, 0x2(%rax,%rbp) # imm = 0x7FFF
jne 0x392bc
cmpl $0x0, 0xa(%rax,%rbp)
jne 0x392bc
cmpw $0x0, (%rax,%rbp)
jns 0x392bc
subq $0x10, %rsp
movq %r13, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq 0x60(%rsp), %r8
movq 0x58(%rsp), %r9
movsd 0x68(%rsp), %xmm0
pushq 0x70(%rsp)
pushq 0x18(%rsp)
pushq 0x28(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
pushq 0x50(%rsp)
pushq 0x88(%rsp)
callq 0x37ce8
addq $0x50, %rsp
incq %rbx
addq $0x20, %rbp
jmp 0x3924d
movq 0x48(%r12), %rcx
cmpq %r14, %rcx
jle 0x3933e
pushq $0x1c
popq %rax
xorl %esi, %esi
xorl %edx, %edx
cmpq %rcx, %rdx
jge 0x39328
movq 0x58(%r12), %rdi
cmpq %r14, %rdx
jge 0x392fc
cmpw $0x7fff, -0x8(%rdi,%rax) # imm = 0x7FFF
jne 0x392fc
cmpl $0x0, (%rdi,%rax)
jne 0x392fc
cmpw $0x0, -0xa(%rdi,%rax)
js 0x3931f
leaq 0x1(%rsi), %r8
shlq $0x5, %rsi
movups -0x1c(%rdi,%rax), %xmm0
movups -0xc(%rdi,%rax), %xmm1
movups %xmm1, 0x10(%rdi,%rsi)
movups %xmm0, (%rdi,%rsi)
movq 0x48(%r12), %rcx
movq %r8, %rsi
incq %rdx
addq $0x20, %rax
jmp 0x392d6
movq %rsi, 0x48(%r12)
movq 0x58(%r12), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x19dc0
movq %r13, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
movq (%rsp), %rcx
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x382a0
leaq 0x1ac296(%rip), %rdi    # 0x1e55fd
leaq 0x1aa335(%rip), %rsi    # 0x1e36a3
leaq 0x1ac467(%rip), %rcx    # 0x1e57dc
movl $0x44d7, %edx           # imm = 0x44D7
callq 0x2110