// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: ul_lalign_hpc(overlap_region_alloc*, Candidates_list*, ul_idx_t const*, char*, unsigned long, UC_Read*, UC_Read*, Correct_dumy*, haplotype_evdience_alloc*, kvec_t_u64_warp*, kvec_t_u32_warp*, double, double, long, void*)
void ul_lalign_hpc(overlap_region_alloc* ol, Candidates_list *cl, const ul_idx_t *uref, char *qstr, 
                        uint64_t ql, UC_Read* qu, UC_Read* tu, Correct_dumy* dumy, 
                        haplotype_evdience_alloc* hap, kvec_t_u64_warp* v_idx, kvec_t_u32_warp* q_idx,  
                        double e_rate, double eh_rate, int64_t wl, void *km)
{
    uint64_t i, qhl, bs, k, ovl, whl; Window_Pool w; double err; overlap_region t; overlap_region *z;
    whl = MIN((((double)THRESHOLD_MAX_SIZE)/eh_rate), WINDOW);
    ol->mapped_overlaps_length = 0;
    if(ol->length <= 0) return;
    
    ///hpc alignment
    ///init hpc seq
    qhl = update_ol_track(ol, cl, uref->hpc_g, uref, 1, ql, qstr, q_idx);
    gen_hpc_str(qstr, ql, qu, &qhl);
    ///verify hpc seq
    clear_Correct_dumy(dumy, ol, km); err = eh_rate; 
    init_Window_Pool(&w, qhl, whl, (int)(1.0/err));
    bs = (w.window_length)+(THRESHOLD_MAX_SIZE<<1)+1;
    resize_UC_Read(tu, bs<<1);
    for (i = k = 0; i < ol->length; i++) {
        if(!align_ul_ed_post(&(ol->list[i]), uref, uref->hpc_g, qu->seq, tu->seq, tu->seq+bs, dumy, err, w.window_length, OVERLAP_THRESHOLD_FILTER_HPC, km)) {
            continue;
        }
        // fprintf(stderr, "+++[M::%s] yid::%u, x::[%u, %u), y::[%u, %u), aln::%u, err::%lu\n", __func__, ol->list[i].y_id, 
        // ol->list[i].x_pos_s, ol->list[i].x_pos_e+1, ol->list[i].y_pos_s, ol->list[i].y_pos_e+1,
        // ol->list[i].align_length, col_errors(&(ol->list[i])));
        if(k != i) {
            t = ol->list[k];
            ol->list[k] = ol->list[i];
            ol->list[i] = t;
        }
        k++;
    }
    ol->length = k;
    if(ol->length <= 0) return;


    ///base alignment
    update_ol_track(ol, cl, NULL, uref, 1, ql, NULL, NULL);
    resize_UC_Read(qu, ql); qu->length = ql; memcpy(qu->seq, qstr, ql);
    clear_Correct_dumy(dumy, ol, km); err = e_rate; 
    init_Window_Pool(&w, ql, wl, (int)(1.0/err));
    bs = (w.window_length)+(THRESHOLD_MAX_SIZE<<1)+1;
    resize_UC_Read(tu, bs<<1);
    for (i = 0; i < ol->length; i++) {
        z = &(ol->list[i]); ovl = z->x_pos_e+1-z->x_pos_s;
        if(!align_ul_ed_post(z, uref, NULL, qu->seq, tu->seq, tu->seq+bs, dumy, err, w.window_length, -1, km)) {
            continue;
        }
        // fprintf(stderr, "---[M::%s] yid::%u, x::[%u, %u), y::[%u, %u), aln::%u, err::%lu\n", __func__, ol->list[i].y_id, 
        // ol->list[i].x_pos_s, ol->list[i].x_pos_e+1, ol->list[i].y_pos_s, ol->list[i].y_pos_e+1,
        // ol->list[i].align_length, col_errors(&(ol->list[i])));
        if(uref && simi_pass(ovl, z->align_length, uref?1:0, -1, NULL)) {
            z->is_match = 3; ol->mapped_overlaps_length += z->align_length;
        }
    }

    if(uref && ol->mapped_overlaps_length > 0) {
        set_herror_win(ol, dumy, v_idx, err, ql, w.window_length);
    }

    double e_max = err*1.5, rr;
    for (i = k = 0; i < ol->length; i++) {
        z = &(ol->list[i]); ovl = z->x_pos_e + 1 - z->x_pos_s;
        rr = gen_extend_err(z, uref, NULL, NULL, qu->seq, tu->seq, tu->seq+bs,
                    dumy, v_idx?v_idx->a.a:NULL, w.window_length, -1, err, (e_max+0.000001), NULL);
        z->is_match = 0;///must be here;
        if (rr <= err) {
            for (k = 0; k < z->w_list.n; k++) {
                if(z->w_list.a[k].clen) continue;
                gen_backtrace_adv(&(z->w_list.a[k]), z, NULL, NULL, uref, qu->seq, tu->seq, tu->seq+bs,
                dumy, z->y_pos_strand, z->y_id);
            }
            
            ol->mapped_overlaps_length += ovl; k++;
            z->is_match = 1; append_unmatched_wins(z, w.window_length);
            calculate_ul_boundary_cigars(z, uref, dumy, qu, err, w.window_length);
        } else if (rr <= e_max) {
            z->is_match = 3;
        }
    }

    partition_ul_overlaps_advance(ol, uref, qu, tu, dumy, hap, 1, err, w.window_length, km);
    
    // recalcate_window(overlap_list, R_INF, g_read, dumy, overlap_read);
    // partition_overlaps(overlap_list, R_INF, g_read, dumy, hap, force_repeat);
    // recalcate_window_ul_advance(overlap_list, uref, g_read, dumy, overlap_read, max_ov_diff_ec, w_inf.window_length, km);
    // recalcate_window_advance(overlap_list, NULL, uref, g_read, dumy, overlap_read, v_idx, w_inf.window_length, max_ov_diff_ec, max_ov_diff_ec);
    /**
    refine_ed_aln(overlap_list, NULL, uref, g_read, dumy, overlap_read, v_idx, w_inf.window_length, max_ov_diff_ec, max_ov_diff_ec);
    **/
    // fprintf(stderr, "[M::%s-beg] occ[0]->%lu, occ[1]->%lu, occ[2]->%lu, occ[3]->%lu\n", __func__, 
    // ovlp_occ(overlap_list, 0), ovlp_occ(overlap_list, 1), ovlp_occ(overlap_list, 2), ovlp_occ(overlap_list, 3));
    ///after this function, overlap_list is sorted by x_pos_e; used for g_chain
    /**
    partition_ul_overlaps_advance(overlap_list, uref, g_read, overlap_read, dumy, hap, force_repeat, max_ov_diff_ec, w_inf.window_length, km);
    **/
    // print_ovlp_occ_stat(overlap_list, g_read->length, 1);
    // print_ovlp_occ_stat(overlap_list, g_read->length, 2);
    // fprintf(stderr, "[M::%s-end] occ[0]->%lu, occ[1]->%lu, occ[2]->%lu, occ[3]->%lu\n", __func__, 
    // ovlp_occ(overlap_list, 0), ovlp_occ(overlap_list, 1), ovlp_occ(overlap_list, 2), ovlp_occ(overlap_list, 3));
    // debug_phasing_status(overlap_list, uref->ug, 0, hap, g_read, 20, 1176);
    // debug_phasing_status(overlap_list, uref->ug, 0, hap, g_read, 20, 1167);
    // debug_phasing_status(overlap_list, uref->ug, 0, hap, g_read, 20, 1170);
    /**
    
    
    if(is_consensus)
    {
        generate_consensus(overlap_list, R_INF, g_read, dumy, g, DAGCon, current_cigar, second_round);
    }


    (*fully_cov) = check_if_fully_covered(overlap_list, R_INF, g_read, dumy, g, abnormal);
    **/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movsd %xmm0, (%rsp)
andq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x2f9f2
movq %r9, %r15
movq %rdi, %rbp
movq %rdx, %r12
movq %rcx, %r13
movq %r8, %r14
movsd 0x1b408c(%rip), %xmm0   # 0x1e3570
movsd %xmm1, 0x30(%rsp)
divsd %xmm1, %xmm0
minsd 0x1b4082(%rip), %xmm0   # 0x1e3578
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x1b3d92(%rip), %xmm0   # 0x1e3298
cvttsd2si %xmm0, %rbx
sarq $0x3f, %rcx
andq %rcx, %rbx
orq %rax, %rbx
movq 0x8(%rdx), %rdx
pushq $0x1
popq %r8
movq %rsi, 0x8(%rsp)
movq 0x8(%rsp), %rsi
movq %r12, %rcx
movq %r14, %r9
pushq 0x150(%rsp)
pushq %r13
callq 0x29f29
popq %rcx
popq %rdx
leaq 0x48(%rsp), %rcx
movq %rax, (%rcx)
movq %r13, 0x40(%rsp)
movq %r13, %rdi
movq %r14, 0x38(%rsp)
movl %r14d, %esi
movq %r15, %rdx
callq 0x2a12e
movq 0x138(%rsp), %rdi
movq %rbp, %rsi
movq 0x160(%rsp), %rdx
callq 0x29361
leaq 0x3f(%rbx), %rax
movq %rax, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
leaq 0x7e(,%rbx,2), %rsi
movq 0x130(%rsp), %rdi
callq 0x562fa
movq 0x8(%rsp), %rsi
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %rbp, 0x18(%rsp)
movq %r12, 0x10(%rsp)
cmpq 0x10(%rbp), %rbx
jae 0x2f67c
movq (%rbp), %rdi
addq %r13, %rdi
movq 0x8(%r12), %rdx
movq (%r15), %rcx
movq 0x130(%rsp), %rax
movq (%rax), %r8
movq 0x20(%rsp), %rax
leaq (%r8,%rax), %r9
subq $0x10, %rsp
movq %r12, %rsi
movsd 0x40(%rsp), %xmm0
movsd 0x1b3f97(%rip), %xmm1   # 0x1e3588
pushq 0x38(%rsp)
pushq 0x150(%rsp)
callq 0x2a687
addq $0x20, %rsp
testl %eax, %eax
je 0x2f668
cmpq %r14, %rbx
je 0x2f665
movq 0x18(%rsp), %rax
movq (%rax), %rbp
imulq $0xa8, %r14, %r12
addq %rbp, %r12
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x2160
addq %r13, %rbp
movl $0xa8, %edx
movq %r12, %rdi
movq 0x10(%rsp), %r12
movq %rbp, %rsi
movq 0x18(%rsp), %rbp
callq 0x2160
movq (%rbp), %rdi
addq %r13, %rdi
movl $0xa8, %edx
leaq 0x50(%rsp), %rsi
callq 0x2160
incq %r14
movq 0x8(%rsp), %rsi
incq %rbx
addq $0xa8, %r13
jmp 0x2f5af
movq %r14, 0x10(%rbp)
testq %r14, %r14
movq 0x38(%rsp), %rbx
movq 0x40(%rsp), %r14
je 0x2f9f2
subq $0x10, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
xorl %r13d, %r13d
pushq $0x1
popq %r8
movq %rbp, %rdi
xorl %edx, %edx
movq %r12, %rcx
movq %rbx, %r9
callq 0x29f29
popq %rcx
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x562fa
movq %rbx, 0x8(%r15)
movq (%r15), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2160
movq 0x138(%rsp), %rdi
movq %rbp, %rsi
movq 0x160(%rsp), %rdx
callq 0x29361
movq 0x158(%rsp), %rax
leaq 0x3f(%rax), %r12
leaq 0x7e(,%rax,2), %rsi
movq 0x130(%rsp), %rdi
callq 0x562fa
xorl %ebx, %ebx
movq %r15, 0x8(%rsp)
cmpq 0x10(%rbp), %rbx
jae 0x2f7d2
movq (%rbp), %r14
leaq (%r14,%r13), %rdi
movq %r15, %rax
movl 0x4(%r14,%r13), %r15d
movl 0x8(%r14,%r13), %ebp
movq (%rax), %rcx
movq 0x130(%rsp), %rax
movq (%rax), %r8
leaq (%r8,%r12), %r9
subq $0x10, %rsp
movq 0x20(%rsp), %rsi
xorl %edx, %edx
movsd 0x10(%rsp), %xmm0
movsd 0x1b3e20(%rip), %xmm1   # 0x1e3580
pushq 0x168(%rsp)
pushq 0x150(%rsp)
callq 0x2a687
addq $0x20, %rsp
testl %eax, %eax
je 0x2f7b9
incl %ebp
subl %r15d, %ebp
je 0x2f7b9
movl 0x28(%r14,%r13), %eax
testl %eax, %eax
je 0x2f7b9
cvtsi2sd %rbp, %xmm0
mulsd 0x1b3db0(%rip), %xmm0   # 0x1e3548
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x2f7b9
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x2f7b9
movb $0x3, 0x2c(%r14,%r13)
movq 0x18(%rsp), %rcx
addq %rax, 0x18(%rcx)
incq %rbx
addq $0xa8, %r13
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %rbp
jmp 0x2f716
cmpq $0x0, 0x10(%rsp)
movq 0x148(%rsp), %rbx
je 0x2f80e
cmpq $0x0, 0x18(%rbp)
jle 0x2f80e
movq %rbp, %rdi
movq 0x138(%rsp), %rsi
movq %rbx, %rdx
movsd (%rsp), %xmm0
movq 0x38(%rsp), %rcx
movq 0x158(%rsp), %r8
callq 0x1e2db
movsd 0x1b3caa(%rip), %xmm0   # 0x1e34c0
mulsd (%rsp), %xmm0
movsd 0x1b3d2d(%rip), %xmm1   # 0x1e3550
movsd %xmm0, 0x20(%rsp)
addsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
xorl %r13d, %r13d
cmpq 0x10(%rbp), %r13
jae 0x2f9a2
testq %rbx, %rbx
je 0x2f84b
movq 0x10(%rbx), %rax
jmp 0x2f84d
xorl %eax, %eax
movq (%rbp), %rcx
imulq $0xa8, %r13, %rdx
movl 0x8(%rcx,%rdx), %r14d
subl 0x4(%rcx,%rdx), %r14d
leaq (%rcx,%rdx), %rbp
movq 0x130(%rsp), %rcx
movq (%rcx), %r9
leaq (%r9,%r12), %r10
movq (%r15), %r8
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movsd 0x8(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
pushq $0x0
pushq 0x168(%rsp)
pushq %rax
pushq 0x158(%rsp)
pushq %r10
callq 0x2cc26
addq $0x30, %rsp
movb $0x0, 0x2c(%rbp)
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x2f8d8
movsd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2f995
movb $0x3, 0x2c(%rbp)
jmp 0x2f995
movq %r13, 0x28(%rsp)
incl %r14d
xorl %ebx, %ebx
xorl %r13d, %r13d
cmpq 0x48(%rbp), %r13
jae 0x2f948
movq 0x58(%rbp), %rdi
cmpl $0x0, 0x1c(%rdi,%rbx)
jne 0x2f93f
addq %rbx, %rdi
movq (%r15), %r9
movq 0x130(%rsp), %rax
movq (%rax), %rax
leaq (%rax,%r12), %r10
movl 0x10(%rbp), %r11d
movl 0x1c(%rbp), %r15d
subq $0x8, %rsp
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x18(%rsp), %r8
pushq %r11
pushq %r15
movq 0x20(%rsp), %r15
pushq 0x150(%rsp)
pushq %r10
pushq %rax
callq 0x567ea
addq $0x30, %rsp
incq %r13
addq $0x20, %rbx
jmp 0x2f8e5
movq 0x18(%rsp), %rax
addq %r14, 0x18(%rax)
movb $0x1, 0x2c(%rbp)
movq %rbp, %rdi
movq 0x158(%rsp), %rbx
movq %rbx, %rsi
callq 0x1cf34
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq 0x138(%rsp), %rdx
movq %r15, %rcx
movsd (%rsp), %xmm0
movq %rbx, %r8
callq 0x1dbea
movq 0x148(%rsp), %rbx
movq 0x28(%rsp), %r13
incq %r13
movq 0x18(%rsp), %rbp
jmp 0x2f836
subq $0x20, %rsp
movq 0x180(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
movq %r15, %rdx
movq 0x150(%rsp), %rcx
movq 0x158(%rsp), %r8
movq 0x160(%rsp), %r9
movsd 0x20(%rsp), %xmm0
callq 0x28a1d
addq $0x20, %rsp
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq