// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: lchain_align(overlap_region_alloc*, ul_idx_t const*, UC_Read*, Correct_dumy*, UC_Read*, Graph*, Graph*, Cigar_record*, haplotype_evdience_alloc*, Round2_alignment*, kvec_t_u64_warp*, window_list_alloc*, int, int, int*, int*, double, long long, void*)
void lchain_align(overlap_region_alloc* overlap_list, const ul_idx_t *uref, 
                        UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read, 
                        Graph* g, Graph* DAGCon, Cigar_record* current_cigar, 
                        haplotype_evdience_alloc* hap, Round2_alignment* second_round, 
                        kvec_t_u64_warp* v_idx, window_list_alloc* win_ciagr_buf, 
                        int force_repeat, int is_consensus, int* fully_cov, int* abnormal, 
                        double max_ov_diff_ec, long long winLen, void *km)
{
    
    clear_Correct_dumy(dumy, overlap_list, km);

    long long window_start, window_end;

    Window_Pool w_inf;

    init_Window_Pool(&w_inf, g_read->length, winLen, (int)(1.0/max_ov_diff_ec));
    int flag = 0;
    
    while(get_Window(&w_inf, &window_start, &window_end) && flag != -2) {
        dumy->length = 0; dumy->lengthNT = 0;
        flag = get_interval(window_start, window_end, overlap_list, dumy, w_inf.window_length);

        switch (flag) {
            case 1:    ///no match here
                break;
            case 0:    ///no match here
                break;
            case -2: ///if flag == -2, loop would be terminated
                break;
        }

        ///dumy->lengthNT represent how many overlaps that the length of them is not equal to WINDOW; may larger or less than WINDOW
        ///dumy->length represent how many overlaps that the length of them is WINDOW
        ///now the windows which are larger than WINDOW are verified one-by-one, to improve it, we can do it group-bygroup
        verify_ul_window(window_start, window_end, overlap_list, dumy, uref, g_read->seq, max_ov_diff_ec, w_inf.window_length, /**THRESHOLD**/THRESHOLD_MAX_SIZE, 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);
    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 $0x58, %rsp
movsd %xmm0, (%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbp
movq 0xe8(%rsp), %rdx
movq 0xe0(%rsp), %r14
movq %rcx, %rdi
movq %rbp, %rsi
callq 0x29361
movq 0x8(%r15), %rax
movsd 0x1b9d55(%rip), %xmm0   # 0x1e32a0
divsd (%rsp), %xmm0
cvttsd2si %xmm0, %ecx
leaq 0x20(%rsp), %rsi
andl $0x0, 0x30(%rsi)
movslq %ecx, %rcx
movq %rax, (%rsi)
movq %r14, 0x8(%rsi)
movq %rcx, 0x28(%rsi)
andq $0x0, 0x18(%rsi)
cmpq %r14, %rax
movq %r14, %rcx
cmovlq %rax, %rcx
decq %rcx
movq %rcx, 0x20(%rsi)
addq %r14, %rax
decq %rax
cqto
idivq %r14
movq %rax, 0x10(%rsi)
leaq 0x30(%rbx), %r14
xorl %r13d, %r13d
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x52721
cmpl $-0x2, %r13d
je 0x29613
testl %eax, %eax
je 0x29613
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %r8
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x5277b
movl %eax, %r13d
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq (%r15), %r9
subq $0x8, %rsp
movq %rbp, %rdx
movq %rbx, %rcx
movq %r12, %r8
movsd 0x8(%rsp), %xmm0
pushq 0xf0(%rsp)
pushq $0x1f
pushq 0x40(%rsp)
callq 0x1bf54
addq $0x20, %rsp
jmp 0x29597
movq %rbp, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r15, %rcx
movq %rbx, %r8
movq 0x18(%rsp), %r14
movq %r14, %r9
movsd (%rsp), %xmm0
movaps %xmm0, %xmm1
pushq 0x28(%rsp)
pushq 0xb8(%rsp)
callq 0x55815
popq %rax
popq %rcx
movq 0x28(%rsp), %rax
subq $0x20, %rsp
movq 0x108(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rbx, %r8
movq 0xc0(%rsp), %r9
movsd 0x20(%rsp), %xmm0
callq 0x28a1d
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq