// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: partition_overlaps_advance_back(overlap_region_alloc*, All_reads*, UC_Read*, UC_Read*, Correct_dumy*, haplotype_evdience_alloc*, int)
void partition_overlaps_advance_back(overlap_region_alloc* overlap_list, All_reads* R_INF, 
                        UC_Read* g_read, UC_Read* overlap_read, Correct_dumy* dumy, 
                        haplotype_evdience_alloc* hap, int force_repeat)
{
    ResizeInitHaplotypeEvdience(hap);

    long long i;
    long long window_start, window_end;

    long long num_availiable_win = 0;
    
    Window_Pool w_inf;
    init_Window_Pool(&w_inf, g_read->length, WINDOW, (int)(1.0/asm_opt.max_ov_diff_ec));

    int flag = 0;
    while(get_Window(&w_inf, &window_start, &window_end) && flag != -2)
    {
        dumy->length = 0;
        dumy->lengthNT = 0;
        ///return overlaps that is overlaped with [window_start, window_end]
        flag = get_available_interval(window_start, window_end, overlap_list, dumy);
        switch (flag)
        {
            case 1:    ///found matched overlaps
                break;
            case 0:    ///do not find any matched overlaps
                break;
            case -2: ///do not find any matched overlaps, and the next window also cannot match
                break;
        }
        
        num_availiable_win = num_availiable_win + dumy->length;
        
        ///need to deal with 
        cluster_advance(g_read->seq, window_start, window_end, overlap_list, 
        dumy, R_INF, hap, overlap_read, 1);
    }


    
    ///very time-consuming
    ///Fix-attention ---> able to be sorted locally
    // qsort(hap->list, hap->length, sizeof(haplotype_evdience), cmp_haplotype_evdience);
    SetSnpMatrix(hap, &(hap->nn_snp), &(overlap_list->length), 1, NULL);

    uint64_t pre_site = (uint64_t)-1;
    uint64_t num_of_snps = 0;
    long long pre_i = -1;
    long long sub_length;
    haplotype_evdience* sub_list;

    ////split reads
    for (i = 0; i < hap->length; i++)
    {
        if(pre_site != hap->list[i].site)
        {
            if(i != 0)
            {
                sub_list = hap->list + pre_i;
                sub_length = i - pre_i;
                split_sub_list(hap, sub_list, sub_length, overlap_list, R_INF, g_read);
            }
            num_of_snps++;
            pre_site = hap->list[i].site;
            pre_i = i;
        }
    }

    if(pre_i != -1)
    {
        sub_list = hap->list + pre_i;
        sub_length = i - pre_i;
        split_sub_list(hap, sub_list, sub_length, overlap_list, R_INF, g_read);
    }

    generate_haplotypes_DP(hap, overlap_list, R_INF, g_read->length, force_repeat);
    ///generate_haplotypes_naive(hap, overlap_list, R_INF, g_read->length, force_repeat);

    lable_large_indels(overlap_list, g_read->length, dumy, asm_opt.max_ov_diff_ec);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %r15
movq %r8, %rbx
movq %rcx, 0x28(%rsp)
movq %rdx, %r13
movq %rsi, 0x20(%rsp)
andl $0x0, 0x230(%r9)
movq %rdi, %r12
andq $0x0, 0x220(%r9)
leaq 0x230(%r9), %rax
movq %rax, 0x18(%rsp)
andl $0x0, 0x10(%r9)
andq $0x0, 0x8(%r9)
leaq 0x18(%r9), %rdi
xorl %r14d, %r14d
movl $0x1ac, %edx            # imm = 0x1AC
xorl %esi, %esi
callq 0x20e0
movq %r13, %rbp
movq 0x8(%r13), %rax
leaq 0x22be87(%rip), %rcx    # 0x253758
movsd 0x1bb9c7(%rip), %xmm0   # 0x1e32a0
divsd 0xc0(%rcx), %xmm0
cvttsd2si %xmm0, %ecx
leaq 0x30(%rsp), %rdi
andl $0x0, 0x30(%rdi)
movslq %ecx, %rcx
movq %rax, (%rdi)
movl $0x177, %esi            # imm = 0x177
movq %rsi, 0x8(%rdi)
movq %rcx, 0x28(%rdi)
andq $0x0, 0x18(%rdi)
cmpq %rsi, %rax
movl $0x177, %ecx            # imm = 0x177
cmovlq %rax, %rcx
decq %rcx
movq %rcx, 0x20(%rdi)
addq $0x176, %rax            # imm = 0x176
cqto
idivq %rsi
movq %rax, 0x10(%rdi)
leaq 0x30(%rbx), %r13
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x52721
cmpl $-0x2, %r14d
je 0x2799c
testl %eax, %eax
je 0x2799c
xorpd %xmm0, %xmm0
movapd %xmm0, (%r13)
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x530d0
movl %eax, %r14d
movq (%rbp), %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
subq $0x8, %rsp
movq %r12, %rcx
movq %rbx, %r8
movq 0x28(%rsp), %r9
pushq $0x1
pushq 0x38(%rsp)
pushq %r15
callq 0x215bc
addq $0x20, %rsp
jmp 0x2792c
leaq 0x10(%r12), %rdx
xorl %r13d, %r13d
pushq $0x1
popq %rcx
movq %r15, %rdi
movq 0x18(%rsp), %rsi
xorl %r8d, %r8d
callq 0x53d01
pushq $-0x1
popq %rdx
xorl %r14d, %r14d
movq %rdx, %rax
movl 0x10(%r15), %ecx
cmpq %rcx, %r14
jae 0x27a11
movq (%r15), %rsi
movl (%rsi,%r13), %ecx
cmpq %rcx, %rdx
jne 0x279da
movq %rdx, %rcx
jmp 0x27a05
testq %r14, %r14
je 0x27a03
imulq $0x18, %rax, %rcx
addq %rcx, %rsi
movq %r14, %rdx
subq %rax, %rdx
movq %r15, %rdi
movq %rbp, %r9
callq 0x21d6f
movq (%r15), %rax
movl (%rax,%r13), %ecx
movq %r14, %rax
jmp 0x27a05
xorl %eax, %eax
incq %r14
addq $0x18, %r13
movq %rcx, %rdx
jmp 0x279c0
cmpq $-0x1, %rax
je 0x27a2f
imulq $0x18, %rax, %rsi
addq (%r15), %rsi
subq %rax, %r14
movq %r15, %rdi
movq %r14, %rdx
movq %rbp, %r9
callq 0x21d6f
movq %r15, %rdi
movq %r12, %rsi
callq 0x22db6
movq 0x8(%rbp), %rsi
leaq 0x22bd13(%rip), %rax    # 0x253758
movsd 0xc0(%rax), %xmm0
movq %r12, %rdi
movq %rbx, %rdx
callq 0x22ac3
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq