// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movsd %xmm0, 0x10(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, 0x18(%rsp)
movq %rdi, %rbp
movq 0xd8(%rsp), %rdx
movq 0xd0(%rsp), %r14
movq %rcx, %rdi
movq %rbp, %rsi
callq 0x58595
movq %r15, 0x20(%rsp)
movq 0x8(%r15), %rax
cmpq %r14, %rax
movq %r14, %rsi
cmovlq %rax, %rsi
decq %rsi
movq %rax, 0x28(%rsp)
decq %rax
leaq 0x30(%rbx), %r9
xorl %edi, %edi
xorps %xmm0, %xmm0
movl $0x0, 0xc(%rsp)
xorl %r12d, %r12d
movq %r9, 0x38(%rsp)
movq %rax, 0x40(%rsp)
cmpq %rax, %rsi
jne 0x587ea
cmpq $0x0, 0x28(%rsp)
je 0x5894c
cmpl $0x1, 0xc(%rsp)
movl $0x1, 0xc(%rsp)
je 0x5894c
leaq (%rsi,%r14), %r15
cmpq 0x28(%rsp), %r15
cmovgeq %rax, %r15
cmpl $-0x2, %r12d
je 0x5894c
movaps %xmm0, (%r9)
testq %rdi, %rdi
je 0x58810
movq 0x48(%rbx), %rax
jmp 0x5881a
movq $0x0, 0x48(%rbx)
xorl %eax, %eax
movq 0x10(%rbp), %rcx
cmpq %rcx, %rax
jae 0x5884c
movq (%rbp), %rdx
imulq $0xa8, %rax, %r8
movl 0x4(%rdx,%r8), %ecx
cmpq %rcx, %rsi
jge 0x5885f
movq $0x0, 0x48(%rbx)
movaps %xmm0, (%r9)
xorl %r12d, %r12d
jmp 0x588ff
movq %rcx, 0x48(%rbx)
movaps %xmm0, (%r9)
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
jmp 0x588ff
movq %rsi, %rcx
subq %rdi, %rcx
incq %rcx
movaps %xmm0, (%r9)
addq %r8, %rdx
addq $0x8, %rdx
xorl %r8d, %r8d
movl -0x4(%rdx), %r9d
movl (%rdx), %r10d
cmpq %rsi, %r10
cmovaeq %rsi, %r10
cmpq %rdi, %r9
movq %rdi, %r11
cmovgq %r9, %r11
subq %r11, %r10
js 0x588db
cmpq %r14, %rcx
jne 0x588b2
incq %r10
cmpq %r14, %r10
jne 0x588b2
movq 0x28(%rbx), %r10
movq 0x30(%rbx), %r11
movq %rax, (%r10,%r11,8)
incq 0x30(%rbx)
jmp 0x588cc
movq 0x28(%rbx), %r10
movq 0x38(%rbx), %r11
incq %r11
movq %r11, 0x38(%rbx)
movq 0x40(%rbx), %r12
subq %r11, %r12
movq %rax, (%r10,%r12,8)
testq %r8, %r8
jne 0x588d5
movq %rax, 0x48(%rbx)
movl $0x1, %r8d
cmpq %r9, %rsi
jl 0x588f0
incq %rax
addq $0xa8, %rdx
cmpq 0x10(%rbp), %rax
jb 0x58876
movq 0x30(%rbx), %rax
xorl %r12d, %r12d
addq 0x38(%rbx), %rax
setne %r12b
leaq (%rdi,%r14), %r13
movq 0x20(%rsp), %rax
movq (%rax), %r9
subq $0x8, %rsp
movq %rbp, %rdx
movq %rbx, %rcx
movq 0x20(%rsp), %r8
movsd 0x18(%rsp), %xmm0
pushq 0xe0(%rsp)
pushq $0x1f
pushq %r14
callq 0x48448
addq $0x20, %rsp
movq %r13, %rdi
movq %r15, %rsi
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %r9
xorps %xmm0, %xmm0
jmp 0x587c6
movq %rbp, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %r13
movq %r13, %rdx
movq 0x20(%rsp), %r12
movq %r12, %rcx
movq %rbx, %r8
movq 0x30(%rsp), %r15
movq %r15, %r9
movsd 0x10(%rsp), %xmm0
movaps %xmm0, %xmm1
pushq %r14
pushq 0xa8(%rsp)
callq 0x88afb
subq $0x10, %rsp
movq 0xf8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbx, %r8
movq 0xb0(%rsp), %r9
callq 0x578f5
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq