// File: 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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x1e0, %rsp            # imm = 0x1E0
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movsd %xmm0, -0x48(%rbp)
movsd %xmm1, -0x50(%rbp)
movsd 0x32b584(%rip), %xmm1   # 0x3822a8
divsd -0x50(%rbp), %xmm1
movsd 0x32b56f(%rip), %xmm0   # 0x3822a0
ucomisd %xmm1, %xmm0
jb 0x56d4e
movsd 0x32b569(%rip), %xmm0   # 0x3822a8
divsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x188(%rbp)
jmp 0x56d60
movsd 0x32b54a(%rip), %xmm0   # 0x3822a0
movsd %xmm0, -0x188(%rbp)
jmp 0x56d60
movsd -0x188(%rbp), %xmm0
movsd 0x32b288(%rip), %xmm2   # 0x381ff8
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
cvttsd2si %xmm1, %rcx
cvttsd2si %xmm0, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
ja 0x56dae
jmp 0x57502
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x28(%rbp), %rcx
movq -0x38(%rbp), %r9
movq -0x30(%rbp), %r10
movq 0x30(%rbp), %rax
movl $0x1, %r8d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x4dc00
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %esi
movq -0x40(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x4dfd0
movq 0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq 0x40(%rbp), %rdx
callq 0x4c460
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movq -0x60(%rbp), %rsi
movq -0x80(%rbp), %rdx
movsd 0x32b1d5(%rip), %xmm0   # 0x382000
divsd -0xc0(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
leaq -0xb8(%rbp), %rdi
callq 0x9f470
movq -0xb0(%rbp), %rax
addq $0x3e, %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq 0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
shlq %rsi
callq 0xa62c0
movq $0x0, -0x70(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x56f99
movq -0x18(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x58(%rbp), %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq 0x10(%rbp), %rax
movq (%rax), %r8
movq 0x10(%rbp), %rax
movq (%rax), %r9
addq -0x68(%rbp), %r9
movq 0x18(%rbp), %r11
movsd -0xc0(%rbp), %xmm0
movq -0xb0(%rbp), %r10
movq 0x40(%rbp), %rax
movsd 0x32813f(%rip), %xmm1   # 0x37f020
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x4ea30
cmpl $0x0, %eax
jne 0x56efe
jmp 0x56f88
movq -0x70(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0x56f7c
movq -0x18(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x70(%rbp), %rax
addq %rax, %rsi
leaq -0x168(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x18(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x58(%rbp), %rax
addq %rax, %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x70(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x18(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x58(%rbp), %rax
addq %rax, %rdi
leaq -0x168(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x56e79
movq -0x70(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
ja 0x56fb5
jmp 0x57502
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq -0x38(%rbp), %r9
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %r8d
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x4dc00
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xa62c0
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x2180
movq 0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq 0x40(%rbp), %rdx
callq 0x4c460
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movq -0x38(%rbp), %rsi
movq 0x38(%rbp), %rdx
movsd 0x32afc0(%rip), %xmm0   # 0x382000
divsd -0xc0(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
leaq -0xb8(%rbp), %rdi
callq 0x9f470
movq -0xb0(%rbp), %rax
addq $0x3e, %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq 0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
shlq %rsi
callq 0xa62c0
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x571a2
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x58(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0x170(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
movq -0x170(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq 0x10(%rbp), %rax
movq (%rax), %r8
movq 0x10(%rbp), %rax
movq (%rax), %r9
addq -0x68(%rbp), %r9
movq 0x18(%rbp), %r11
movsd -0xc0(%rbp), %xmm0
movq -0xb0(%rbp), %r10
movq 0x40(%rbp), %rax
xorl %edx, %edx
movsd 0x32b165(%rip), %xmm1   # 0x382278
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x4ea30
cmpl $0x0, %eax
jne 0x5712d
jmp 0x57191
cmpq $0x0, -0x28(%rbp)
je 0x5718f
movq -0x78(%rbp), %rdi
movq -0x170(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %esi
movq -0x28(%rbp), %rcx
xorl %edx, %edx
movl $0x1, %eax
cmpq $0x0, %rcx
cmovnel %eax, %edx
movsd 0x32b11a(%rip), %xmm0   # 0x382278
xorl %eax, %eax
movl %eax, %ecx
callq 0xa7100
cmpl $0x0, %eax
je 0x5718f
movq -0x170(%rbp), %rax
movb $0x3, 0x2c(%rax)
movq -0x170(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
jmp 0x57191
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x57086
cmpq $0x0, -0x28(%rbp)
je 0x571d8
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jle 0x571d8
movq -0x18(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq 0x28(%rbp), %rdx
movsd -0xc0(%rbp), %xmm0
movq -0x38(%rbp), %rcx
movq -0xb0(%rbp), %r8
callq 0x37650
movsd 0x32b020(%rip), %xmm0   # 0x382200
mulsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0x178(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x574c1
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x58(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0x170(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
movq -0x170(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1a8(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1a0(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x198(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x190(%rbp)
cmpq $0x0, 0x28(%rbp)
je 0x572b2
movq 0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1c0(%rbp)
jmp 0x572bd
xorl %eax, %eax
movq %rax, -0x1c0(%rbp)
jmp 0x572bd
movq -0x190(%rbp), %r11
movq -0x198(%rbp), %rbx
movq -0x1a0(%rbp), %r9
movq -0x1a8(%rbp), %r8
movq -0x1b0(%rbp), %rsi
movq -0x1b8(%rbp), %rdi
movq -0x1c0(%rbp), %r10
movq -0xb0(%rbp), %rax
movsd -0xc0(%rbp), %xmm1
movsd 0x32af7b(%rip), %xmm2   # 0x382280
addsd -0x178(%rbp), %xmm2
xorl %ecx, %ecx
movsd 0x32af61(%rip), %xmm0   # 0x382278
movq %rcx, %rdx
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
callq 0x52920
movsd %xmm0, -0x180(%rbp)
movq -0x170(%rbp), %rax
movb $0x0, 0x2c(%rax)
movsd -0x180(%rbp), %xmm1
movsd -0xc0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x5748b
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x170(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jae 0x57428
movq -0x170(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x70(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
je 0x573a3
jmp 0x57417
movq -0x170(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x70(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x170(%rbp), %rsi
movq -0x28(%rbp), %r8
movq -0x40(%rbp), %rax
movq (%rax), %r9
movq 0x10(%rbp), %rax
movq (%rax), %r14
movq 0x10(%rbp), %rax
movq (%rax), %rbx
addq -0x68(%rbp), %rbx
movq 0x18(%rbp), %r11
movq -0x170(%rbp), %rax
movl 0x1c(%rax), %r10d
movq -0x170(%rbp), %rax
movl 0x10(%rax), %eax
xorl %ecx, %ecx
movq %rcx, %rdx
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movl %eax, 0x20(%rsp)
callq 0xa6b10
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x57370
movq -0x78(%rbp), %rcx
movq -0x18(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x170(%rbp), %rax
movb $0x1, 0x2c(%rax)
movq -0x170(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x34ab0
movq -0x170(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq -0x40(%rbp), %rcx
movsd -0xc0(%rbp), %xmm0
movq -0xb0(%rbp), %r8
callq 0x36730
jmp 0x574ae
movsd -0x180(%rbp), %xmm1
movsd -0x178(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x574ac
movq -0x170(%rbp), %rax
movb $0x3, 0x2c(%rax)
jmp 0x574ae
jmp 0x574b0
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x57200
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
movsd -0xc0(%rbp), %xmm0
movq -0xb0(%rbp), %r10
movq 0x40(%rbp), %rax
movl $0x1, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x4b540
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbx
popq %r14
popq %rbp
retq
nop