// File: chhylp123[P]hifiasm/Correct.cpp
// Function: partition_overlaps_advance(overlap_region_alloc*, All_reads*, UC_Read*, UC_Read*, Correct_dumy*, haplotype_evdience_alloc*, int)
void partition_overlaps_advance(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);

    uint64_t k, l, m;
    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), 0, NULL);
    for (k = 1, l = m = 0; k <= hap->length; ++k) {  
        if (k == hap->length || hap->list[k].site != hap->list[l].site) {
            m += insert_snp_ee(hap, hap->list+l, k-l, hap->list+m, g_read, NULL);
            l = k;
        }
    }
    hap->length = m;

    // generate_haplotypes_naive_advance(hap, overlap_list, NULL);
    generate_haplotypes_naive_HiFi(hap, overlap_list, 0.04, g_read, 1, 0, ((uint64_t)-1));
    // 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 $0xc0, %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, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movsd 0x336e29(%rip), %xmm0   # 0x382000
leaq 0x37961a(%rip), %rax    # 0x3c47f8
divsd 0xc0(%rax), %xmm0
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
leaq -0x98(%rbp), %rdi
movl $0x177, %edx            # imm = 0x177
callq 0x9f470
movl $0x0, -0x9c(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x9f520
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x9d(%rbp)
je 0x4b23b
cmpl $-0x2, -0x9c(%rbp)
setne %al
movb %al, -0x9d(%rbp)
movb -0x9d(%rbp), %al
testb $0x1, %al
jne 0x4b24a
jmp 0x4b2fd
movq -0x28(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0xa08e0
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, -0xa4(%rbp)
subl $-0x2, %eax
je 0x4b2ad
jmp 0x4b290
movl -0xa4(%rbp), %eax
testl %eax, %eax
je 0x4b2ab
jmp 0x4b29c
movl -0xa4(%rbp), %eax
subl $0x1, %eax
jne 0x4b2af
jmp 0x4b2a9
jmp 0x4b2af
jmp 0x4b2af
jmp 0x4b2af
movq -0x60(%rbp), %rax
movq -0x28(%rbp), %rcx
addq 0x30(%rcx), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%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 0x4b208
movq -0x30(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x230, %rsi            # imm = 0x230
movq -0x8(%rbp), %rdx
addq $0x10, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0xa24b0
movq $0x1, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
ja 0x4b3dd
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
je 0x4b380
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
je 0x4b3ca
movq -0x30(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
imulq $0x18, -0x40(%rbp), %rax
addq %rax, %rsi
movq -0x38(%rbp), %rdx
subq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rax
movq (%rax), %rcx
imulq $0x18, -0x48(%rbp), %rax
addq %rax, %rcx
movq -0x18(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
callq 0x49450
cltq
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x4b3cc
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x4b338
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x18(%rbp), %rdx
movsd 0x333c32(%rip), %xmm0   # 0x37f030
movl $0x1, %ecx
xorps %xmm1, %xmm1
movq $-0x1, %r8
callq 0x41ef0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x28(%rbp), %rdx
leaq 0x3793cf(%rip), %rax    # 0x3c47f8
movsd 0xc0(%rax), %xmm0
callq 0x3f660
addq $0xc0, %rsp
popq %rbp
retq
nop