// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0xa2440
movq $0x0, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movsd 0x338e99(%rip), %xmm0   # 0x382000
leaq 0x37b68a(%rip), %rax    # 0x3c47f8
divsd 0xc0(%rax), %xmm0
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
leaq -0x88(%rbp), %rdi
movl $0x177, %edx            # imm = 0x177
callq 0x9f470
movl $0x0, -0x8c(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x9f520
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0xb9(%rbp)
je 0x491cb
cmpl $-0x2, -0x8c(%rbp)
setne %al
movb %al, -0xb9(%rbp)
movb -0xb9(%rbp), %al
testb $0x1, %al
jne 0x491da
jmp 0x4928d
movq -0x28(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0xa08e0
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0xc0(%rbp)
subl $-0x2, %eax
je 0x4923d
jmp 0x49220
movl -0xc0(%rbp), %eax
testl %eax, %eax
je 0x4923b
jmp 0x4922c
movl -0xc0(%rbp), %eax
subl $0x1, %eax
jne 0x4923f
jmp 0x49239
jmp 0x4923f
jmp 0x4923f
jmp 0x4923f
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
addq 0x30(%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x8(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x10(%rbp), %r9
movq -0x30(%rbp), %r10
movq -0x20(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x3c640
jmp 0x49198
movq -0x30(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x230, %rsi            # imm = 0x230
movq -0x8(%rbp), %rdx
addq $0x10, %rdx
movl $0x1, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0xa24b0
movq $-0x1, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq $-0x1, -0xa8(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jge 0x493ae
movq -0x98(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x18, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
je 0x4939b
cmpq $0x0, -0x38(%rbp)
je 0x49366
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0xa8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rax
subq -0xa8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq -0x8(%rbp), %rcx
movq -0x10(%rbp), %r8
movq -0x18(%rbp), %r9
callq 0x3db50
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq %rax, -0x98(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x4939d
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x492dc
cmpq $-0x1, -0xa8(%rbp)
je 0x49406
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0xa8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rax
subq -0xa8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq -0x8(%rbp), %rcx
movq -0x10(%rbp), %r8
movq -0x18(%rbp), %r9
callq 0x3db50
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movl 0x10(%rbp), %r8d
callq 0x3fc20
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x28(%rbp), %rdx
leaq 0x37b3be(%rip), %rax    # 0x3c47f8
movsd 0xc0(%rax), %xmm0
callq 0x3f660
addq $0xe0, %rsp
popq %rbp
retq