// File: /workspace/github_cpp_makefile_star2_O3/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 %xmm1, 0x8(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, (%rsp)
movq %rsi, 0x38(%rsp)
movq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x5fae9
movq %r9, %r15
movq %rdi, %rbp
movsd 0x1d20b6(%rip), %xmm0   # 0x231620
divsd 0x8(%rsp), %xmm0
minsd 0x1d20b0(%rip), %xmm0   # 0x231628
movq 0x130(%rsp), %r12
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x1d1d78(%rip), %xmm0   # 0x231308
cvttsd2si %xmm0, %rbx
sarq $0x3f, %rcx
andq %rcx, %rbx
orq %rax, %rbx
movq (%rsp), %rcx
movq 0x8(%rcx), %rdx
movq 0x38(%rsp), %rsi
movl $0x1, %r8d
movq 0x10(%rsp), %r13
movq %r13, %r9
pushq 0x150(%rsp)
movq 0x48(%rsp), %r14
pushq %r14
callq 0x592aa
addq $0x10, %rsp
leaq 0x48(%rsp), %rcx
movq %rax, (%rcx)
movq %r14, %rdi
movl %r13d, %esi
movq %r15, %rdx
callq 0x59623
movq 0x138(%rsp), %rdi
movq %rbp, %rsi
movq 0x160(%rsp), %rdx
callq 0x58595
movq %rbx, 0x20(%rsp)
leaq 0x7e(,%rbx,2), %r13
cmpq %r13, 0x10(%r12)
jge 0x5f62d
movq 0x130(%rsp), %rbx
movq (%rbx), %rdi
movq %r13, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %r13, 0x10(%rbx)
cmpq $0x0, 0x10(%rbp)
je 0x5fae9
movq 0x20(%rsp), %r13
leaq 0x3f(%r13), %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %rbp, 0x28(%rsp)
movq (%rbp), %rdi
addq %r12, %rdi
movq (%rsp), %rsi
movq 0x8(%rsi), %rdx
movq (%r15), %rcx
movq 0x130(%rsp), %rax
movq (%rax), %r8
movq 0x30(%rsp), %rax
leaq (%r8,%rax), %r9
subq $0x10, %rsp
movsd 0x18(%rsp), %xmm0
movsd 0x1d1fa5(%rip), %xmm1   # 0x231630
pushq %r13
pushq 0x150(%rsp)
callq 0x59be2
addq $0x20, %rsp
testl %eax, %eax
je 0x5f6fc
cmpq %rbx, %r14
je 0x5f6f9
movq (%rbp), %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 0x28(%rsp), %rbp
movq %r13, %rsi
movq 0x20(%rsp), %r13
callq 0x2160
movq (%rbp), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x50(%rsp), %rsi
callq 0x2160
incq %rbx
incq %r14
addq $0xa8, %r12
cmpq 0x10(%rbp), %r14
jb 0x5f653
movq %rbx, 0x10(%rbp)
testq %rbx, %rbx
je 0x5fae9
subq $0x10, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
xorl %edx, %edx
movq 0x10(%rsp), %rcx
movl $0x1, %r8d
movq 0x20(%rsp), %r12
movq %r12, %r9
callq 0x592aa
addq $0x10, %rsp
movq (%r15), %rdi
cmpq %r12, 0x10(%r15)
jge 0x5f76b
movq %r12, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, (%r15)
movq %r12, 0x10(%r15)
movq 0x148(%rsp), %r14
movq %r12, 0x8(%r15)
movq 0x40(%rsp), %rsi
movq %r12, %rdx
callq 0x2160
movq 0x138(%rsp), %rdi
movq %rbp, %rsi
movq 0x160(%rsp), %rdx
callq 0x58595
movq 0x158(%rsp), %rax
leaq 0x7e(,%rax,2), %r13
movq 0x130(%rsp), %rax
cmpq %r13, 0x10(%rax)
jge 0x5f7d4
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)
cmpq $0x0, 0x10(%rbp)
je 0x5f8c9
xorl %ebx, %ebx
xorl %r14d, %r14d
movq (%rbp), %r12
leaq (%r12,%rbx), %rdi
movl 0x4(%r12,%rbx), %ebp
movl 0x8(%r12,%rbx), %r13d
movq (%r15), %rcx
movq 0x130(%rsp), %rax
movq (%rax), %r8
movq 0x8(%rsp), %rax
leaq (%r8,%rax), %r9
subq $0x10, %rsp
movq 0x10(%rsp), %rsi
xorl %edx, %edx
movsd 0x28(%rsp), %xmm0
movsd 0x1d1e01(%rip), %xmm1   # 0x231638
pushq 0x168(%rsp)
pushq 0x150(%rsp)
callq 0x59be2
addq $0x20, %rsp
testl %eax, %eax
je 0x5f897
incl %r13d
subl %ebp, %r13d
je 0x5f897
movl 0x28(%r12,%rbx), %eax
testl %eax, %eax
je 0x5f897
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd 0x1d1d7d(%rip), %xmm0   # 0x2315f0
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x5f897
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x5f897
movb $0x3, 0x2c(%r12,%rbx)
movq 0x28(%rsp), %rcx
addq %rax, 0x18(%rcx)
incq %r14
movq 0x28(%rsp), %rbp
movq 0x10(%rbp), %rax
addq $0xa8, %rbx
cmpq %rax, %r14
jb 0x5f7f5
cmpq $0x0, (%rsp)
movq 0x148(%rsp), %r14
movq 0x10(%rsp), %r12
jne 0x5f8cb
jmp 0x5f8f4
xorl %eax, %eax
cmpq $0x0, 0x18(%rbp)
jle 0x5f8f4
movq %rbp, %rdi
movq 0x138(%rsp), %rsi
movq %r14, %rdx
movq %r12, %rcx
movq 0x158(%rsp), %r8
callq 0x4ad19
movq 0x10(%rbp), %rax
testq %rax, %rax
je 0x5fa9f
movsd 0x1d1c6b(%rip), %xmm0   # 0x231570
mulsd 0x18(%rsp), %xmm0
movsd 0x1d1ced(%rip), %xmm1   # 0x231600
movsd %xmm0, 0x10(%rsp)
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
xorl %r12d, %r12d
testq %r14, %r14
je 0x5f931
movq 0x10(%r14), %rax
jmp 0x5f933
xorl %eax, %eax
movq (%rbp), %rcx
imulq $0xa8, %r12, %rdx
movl 0x8(%rcx,%rdx), %esi
subl 0x4(%rcx,%rdx), %esi
movq %rsi, 0x30(%rsp)
leaq (%rcx,%rdx), %r13
movq 0x130(%rsp), %rcx
movq (%rcx), %r9
movq 0x8(%rsp), %rcx
leaq (%r9,%rcx), %r10
movq (%r15), %r8
subq $0x8, %rsp
movq %r13, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
pushq $0x0
pushq 0x168(%rsp)
pushq %rax
pushq 0x158(%rsp)
pushq %r10
callq 0x5c901
addq $0x30, %rsp
movb $0x0, 0x2c(%r13)
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x5f9c9
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x5fa92
movb $0x3, 0x2c(%r13)
jmp 0x5fa92
movq 0x48(%r13), %rax
testq %rax, %rax
je 0x5fa42
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x58(%r13), %rdi
cmpl $0x0, 0x1c(%rdi,%r14)
jne 0x5fa36
addq %r14, %rdi
movq (%r15), %r9
movq 0x130(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rsp), %rcx
leaq (%rax,%rcx), %r10
movl 0x10(%r13), %r11d
movq %r15, %rbx
movl 0x1c(%r13), %r15d
subq $0x8, %rsp
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x8(%rsp), %r8
pushq %r11
pushq %r15
movq %rbx, %r15
pushq 0x150(%rsp)
pushq %r10
pushq %rax
callq 0x89ef9
addq $0x30, %rsp
movq 0x48(%r13), %rax
incq %rbp
addq $0x20, %r14
cmpq %rax, %rbp
jb 0x5f9d7
movq 0x30(%rsp), %rax
incl %eax
movq 0x28(%rsp), %rbp
addq %rax, 0x18(%rbp)
movb $0x1, 0x2c(%r13)
movq %r13, %rdi
movq 0x158(%rsp), %rbx
movq %rbx, %rsi
callq 0x49760
movq %r13, %rdi
movq (%rsp), %rsi
movq 0x138(%rsp), %rdx
movq %r15, %rcx
movsd 0x18(%rsp), %xmm0
movq %rbx, %r8
callq 0x4a52c
movq 0x148(%rsp), %r14
incq %r12
cmpq 0x10(%rbp), %r12
jb 0x5f926
subq $0x20, %rsp
movq 0x180(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
movq 0x150(%rsp), %rcx
movq 0x158(%rsp), %r8
movq 0x160(%rsp), %r9
callq 0x578f5
addq $0x20, %rsp
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq