// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
movq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x60264
movq %r9, %r14
movq %rdx, %rbp
movq %rdi, %rbx
movq %r8, %r13
movsd 0x1c584d(%rip), %xmm0   # 0x225520
movsd %xmm1, 0x8(%rsp)
divsd %xmm1, %xmm0
minsd 0x1c5843(%rip), %xmm0   # 0x225528
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x1c5573(%rip), %xmm0   # 0x225268
cvttsd2si %xmm0, %r12
sarq $0x3f, %rcx
andq %rcx, %r12
orq %rax, %r12
movq 0x8(%rdx), %rdx
movq %rsi, 0x40(%rsp)
movq %rbp, %rcx
movl $0x1, %r8d
movq %r13, %r9
pushq 0x150(%rsp)
movq 0x30(%rsp), %r15
pushq %r15
callq 0x59e98
addq $0x10, %rsp
leaq 0x48(%rsp), %rcx
movq %rax, (%rcx)
movq %r15, %rdi
movq %r13, 0x38(%rsp)
movl %r13d, %esi
movq %r14, %rdx
callq 0x5a10c
movq 0x138(%rsp), %rdi
movq %rbx, (%rsp)
movq %rbx, %rsi
movq 0x160(%rsp), %rdx
callq 0x59139
movq %r12, 0x10(%rsp)
leaq 0x7e(,%r12,2), %r13
movq 0x130(%rsp), %rax
cmpq %r13, 0x10(%rax)
jge 0x5fd9c
movq 0x130(%rsp), %rbx
movq (%rbx), %rdi
movq %r13, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %r13, 0x10(%rbx)
movq (%rsp), %r13
cmpq $0x0, 0x10(%r13)
movq %rbp, 0x20(%rsp)
je 0x5fe8e
movq 0x10(%rsp), %rax
addq $0x3f, %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebx, %ebx
movsd 0x8(%rsp), %xmm0
movq (%r13), %rdi
addq %r12, %rdi
movq 0x8(%rbp), %rdx
movq (%r14), %rcx
movq 0x130(%rsp), %rax
movq (%rax), %r8
movq 0x30(%rsp), %rax
leaq (%r8,%rax), %r9
subq $0x10, %rsp
movq %rbp, %rsi
movsd 0x1c5733(%rip), %xmm1   # 0x225530
pushq 0x20(%rsp)
pushq 0x150(%rsp)
callq 0x5a68a
addq $0x20, %rsp
testl %eax, %eax
je 0x5fe72
cmpq %rbx, %r15
je 0x5fe6f
movq (%rsp), %rax
movq (%rax), %r13
imulq $0xa8, %rbx, %rbp
addq %r13, %rbp
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
addq %r12, %r13
movl $0xa8, %edx
movq %rbp, %rdi
movq 0x20(%rsp), %rbp
movq %r13, %rsi
movq (%rsp), %r13
callq 0x2160
movq (%r13), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x50(%rsp), %rsi
callq 0x2160
incq %rbx
movsd 0x8(%rsp), %xmm0
incq %r15
addq $0xa8, %r12
cmpq 0x10(%r13), %r15
jb 0x5fdcc
jmp 0x5fe90
xorl %ebx, %ebx
movq %rbx, 0x10(%r13)
testq %rbx, %rbx
movq 0x38(%rsp), %rbx
movq 0x40(%rsp), %rsi
je 0x60264
subq $0x10, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %r13, %rdi
xorl %edx, %edx
movq %rbp, %rcx
movl $0x1, %r8d
movq %rbx, %r9
callq 0x59e98
addq $0x10, %rsp
cmpq %rbx, 0x10(%r14)
jge 0x5fee6
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x2370
movq %rax, (%r14)
movq %rbx, 0x10(%r14)
movq %rbx, 0x8(%r14)
movq (%r14), %rdi
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
callq 0x2160
movq 0x138(%rsp), %rdi
movq %r13, %rsi
movq 0x160(%rsp), %rdx
callq 0x59139
movq 0x158(%rsp), %rax
leaq 0x7e(,%rax,2), %r13
movq 0x130(%rsp), %rax
cmpq %r13, 0x10(%rax)
jge 0x5ff4a
movq 0x130(%rsp), %rbx
movq (%rbx), %rdi
movq %r13, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %r13, 0x10(%rbx)
movq 0x158(%rsp), %rax
addq $0x3f, %rax
movq %rax, 0x8(%rsp)
movq (%rsp), %r13
cmpq $0x0, 0x10(%r13)
je 0x60036
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x20(%rsp), %rbp
movq (%r13), %r12
leaq (%r12,%rbx), %rdi
movl 0x4(%r12,%rbx), %eax
movl %eax, 0x10(%rsp)
movl 0x8(%r12,%rbx), %r13d
movq (%r14), %rcx
movq 0x130(%rsp), %rax
movq (%rax), %r8
movq 0x8(%rsp), %rax
leaq (%r8,%rax), %r9
subq $0x10, %rsp
movq %rbp, %rsi
xorl %edx, %edx
movsd 0x28(%rsp), %xmm0
movsd 0x1c5580(%rip), %xmm1   # 0x225538
pushq 0x168(%rsp)
pushq 0x150(%rsp)
callq 0x5a68a
addq $0x20, %rsp
testq %rbp, %rbp
je 0x6001e
testl %eax, %eax
je 0x6001e
incl %r13d
subl 0x10(%rsp), %r13d
je 0x6001e
movl 0x28(%r12,%rbx), %eax
testl %eax, %eax
je 0x6001e
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd 0x1c54f5(%rip), %xmm0   # 0x2254f0
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x6001e
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x6001e
movb $0x3, 0x2c(%r12,%rbx)
movq (%rsp), %rcx
addq %rax, 0x18(%rcx)
incq %r15
addq $0xa8, %rbx
movq (%rsp), %r13
cmpq 0x10(%r13), %r15
jb 0x5ff74
movq 0x148(%rsp), %r15
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x6006f
cmpq $0x0, 0x18(%r13)
jle 0x6006f
movq %r13, %rdi
movq 0x138(%rsp), %rsi
movq %r15, %rdx
movq 0x38(%rsp), %rcx
movq 0x158(%rsp), %r8
callq 0x4b691
cmpq $0x0, 0x10(%r13)
je 0x6021c
movsd 0x1c53ee(%rip), %xmm0   # 0x225470
mulsd 0x18(%rsp), %xmm0
movsd 0x1c5470(%rip), %xmm1   # 0x225500
movsd %xmm0, 0x28(%rsp)
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
xorl %r12d, %r12d
testq %r15, %r15
je 0x600ae
movq 0x10(%r15), %rax
jmp 0x600b0
xorl %eax, %eax
movq (%r13), %rcx
imulq $0xa8, %r12, %rdx
movl 0x8(%rcx,%rdx), %ebp
subl 0x4(%rcx,%rdx), %ebp
leaq (%rcx,%rdx), %r13
movq 0x130(%rsp), %rcx
movq (%rcx), %r9
movq 0x8(%rsp), %rcx
leaq (%r9,%rcx), %r10
movq (%r14), %r8
subq $0x8, %rsp
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movsd 0x20(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
pushq $0x0
pushq 0x168(%rsp)
pushq %rax
pushq 0x158(%rsp)
pushq %r10
callq 0x5d0fb
addq $0x30, %rsp
movb $0x0, 0x2c(%r13)
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x6013f
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x6020b
movb $0x3, 0x2c(%r13)
jmp 0x6020b
movq %rbp, 0x30(%rsp)
cmpq $0x0, 0x48(%r13)
je 0x601b8
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x58(%r13), %rdi
cmpl $0x0, 0x1c(%rdi,%r15)
jne 0x601ab
addq %r15, %rdi
movq (%r14), %r9
movq 0x130(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rsp), %rcx
leaq (%rax,%rcx), %r10
movl 0x10(%r13), %r11d
movq %r14, %rbx
movl 0x1c(%r13), %r14d
subq $0x8, %rsp
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x28(%rsp), %r8
pushq %r11
pushq %r14
movq %rbx, %r14
pushq 0x150(%rsp)
pushq %r10
pushq %rax
callq 0x8ab12
addq $0x30, %rsp
incq %rbp
addq $0x20, %r15
cmpq 0x48(%r13), %rbp
jb 0x60150
movq 0x30(%rsp), %rcx
incl %ecx
movq (%rsp), %rax
addq %rcx, 0x18(%rax)
movb $0x1, 0x2c(%r13)
movq %r13, %rdi
movq 0x158(%rsp), %r15
movq %r15, %rsi
callq 0x4a082
movq %r13, %rdi
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
movq 0x138(%rsp), %rdx
movq %r14, %rcx
movsd 0x18(%rsp), %xmm0
movq %r15, %r8
callq 0x4aed4
movq 0x148(%rsp), %r15
incq %r12
movq (%rsp), %r13
cmpq 0x10(%r13), %r12
jb 0x600a3
subq $0x20, %rsp
movq 0x180(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq 0x150(%rsp), %rcx
movq 0x158(%rsp), %r8
movq 0x160(%rsp), %r9
callq 0x584ad
addq $0x20, %rsp
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq