// File: /workspace/github_cpp_makefile_star2_O1/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, 0x38(%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 0x59139
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
movq %rax, 0x30(%rsp)
leaq 0x30(%rbx), %r8
xorl %edi, %edi
xorps %xmm0, %xmm0
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
movq %r8, 0x40(%rsp)
cmpq 0x30(%rsp), %rsi
jne 0x59337
cmpq $0x0, 0x28(%rsp)
sete %al
cmpl $0x1, 0xc(%rsp)
sete %cl
orb %al, %cl
movl $0x1, %ecx
je 0x5933b
xorl %eax, %eax
movq %rdi, %r12
movq %rsi, %r13
jmp 0x59354
movl 0xc(%rsp), %ecx
leaq (%rdi,%r14), %r12
leaq (%rsi,%r14), %r13
cmpq 0x28(%rsp), %r13
cmovgeq 0x30(%rsp), %r13
movb $0x1, %al
movl %ecx, 0xc(%rsp)
cmpl $-0x2, %r15d
je 0x594b1
testb %al, %al
je 0x594b1
movaps %xmm0, (%r8)
testq %rdi, %rdi
jne 0x59377
movq $0x0, 0x48(%rbx)
movq 0x48(%rbx), %rax
movq 0x10(%rbp), %rcx
cmpq %rcx, %rax
jae 0x593ac
movq (%rbp), %rcx
imulq $0xa8, %rax, %rdx
movl 0x4(%rcx,%rdx), %ecx
cmpq %rcx, %rsi
jge 0x593bf
movq $0x0, 0x48(%rbx)
movaps %xmm0, (%r8)
xorl %r15d, %r15d
jmp 0x5946d
movq %rcx, 0x48(%rbx)
movaps %xmm0, (%r8)
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
jmp 0x5946d
movaps %xmm0, (%r8)
cmpq 0x10(%rbp), %rax
jae 0x5945e
movq (%rbp), %r8
movq %rsi, %rcx
subq %rdi, %rcx
incq %rcx
addq %r8, %rdx
addq $0x8, %rdx
xorl %r8d, %r8d
movl -0x4(%rdx), %r9d
movl (%rdx), %r10d
cmpq %rsi, %r10
cmovgeq %rsi, %r10
cmpq %rdi, %r9
movq %rdi, %r11
cmovgq %r9, %r11
subq %r11, %r10
js 0x59449
cmpq %r14, %rcx
jne 0x59420
incq %r10
cmpq %r14, %r10
jne 0x59420
movq 0x28(%rbx), %r10
movq 0x30(%rbx), %r11
movq %rax, (%r10,%r11,8)
incq 0x30(%rbx)
jmp 0x5943a
movq 0x28(%rbx), %r10
movq 0x38(%rbx), %r11
incq %r11
movq %r11, 0x38(%rbx)
movq 0x40(%rbx), %r15
subq %r11, %r15
movq %rax, (%r10,%r15,8)
testq %r8, %r8
jne 0x59443
movq %rax, 0x48(%rbx)
movl $0x1, %r8d
cmpq %r9, %rsi
jl 0x5945e
incq %rax
addq $0xa8, %rdx
cmpq 0x10(%rbp), %rax
jb 0x593e4
movq 0x30(%rbx), %rax
xorl %r15d, %r15d
addq 0x38(%rbx), %rax
setne %r15b
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 0x48e6b
addq $0x20, %rsp
movq %r12, %rdi
movq %r13, %rsi
movq 0x40(%rsp), %r8
xorps %xmm0, %xmm0
jmp 0x5930c
movq %rbp, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %r13
movq %r13, %rdx
movq 0x20(%rsp), %r12
movq %r12, %rcx
movq %rbx, %r8
movq 0x38(%rsp), %r15
movq %r15, %r9
movsd 0x10(%rsp), %xmm0
movaps %xmm0, %xmm1
pushq %r14
pushq 0xa8(%rsp)
callq 0x897c4
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 0x584ad
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq