// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: lable_large_indels(overlap_region_alloc*, long long, Correct_dumy*, double)
void lable_large_indels(overlap_region_alloc* overlap_list, long long read_length, Correct_dumy* dumy, double max_ov_diff_ec)
{
    long long i, j, c_i, c_n; uint32_t operLen; uint8_t oper;
    int is_delete = 0; window_list *c_idx;
    for (i = 0; i < (long long)overlap_list->length; i++)
    {
        ///should has at least 3 windows for this overlap
        if (overlap_list->list[i].is_match == 1 && overlap_list->list[i].w_list.n >= 3)
        {
            ///here w_list_length >= 3
            ///skip the first and last window
            for (j = 1; j + 1 < (long long)(overlap_list->list[i].w_list.n); j++)
            {
                ///this window is not matched, it seems to have large difference
                if(overlap_list->list[i].w_list.a[j].y_end == -1)
                {
                    overlap_list->list[i].is_match = 100;
                    is_delete = 1;
                    goto end_rem;
                }

                c_idx = &(overlap_list->list[i].w_list.a[j]);
                c_n = c_idx->clen;
                ///if there are <=2 cigar elements, skip it
                if(c_n < 3) continue;

                ///skip the first and last cigar elements
                for (c_i = 1; c_i + 1 < c_n; c_i++)
                {
                    get_cigar_cell(c_idx, &(overlap_list->list[i].w_list), c_i, &oper, &operLen);
                    
                    
                    if(operLen <= 5)
                    {
                        continue;
                    }
                    ///>=6 bp deletion or insertion 
                    if(oper == 2 || oper == 3)
                    {
                        overlap_list->list[i].is_match = 100;
                        is_delete = 1;
                        goto end_rem;
                    }
                }
            }
        }

        end_rem:;
    }


    if(is_delete == 1)
    {
        long long window_start, window_end;
        Window_Pool w_inf;
        init_Window_Pool(&w_inf, read_length, WINDOW, (int)(1.0/max_ov_diff_ec));
        int flag = 0;
        long long realLen = 0, realLen_100 = 0;
        int to_recover = 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_fully_covered_interval(window_start, window_end, 
            overlap_list, dumy, &realLen, &realLen_100);


            switch (flag)
            {
                case 1:    ///match
                    break;
                case 0:    ///unmatch
                    break;
                case -2: ///unmatch, and the next window also cannot match
                    break;
            }

            ///it seems there is a long indel at the reference read itself
            if(realLen == 0 && realLen_100 > 0)
            {
                to_recover = 1;
                break;
            }
        }

        if(to_recover == 1)
        {
            for (i = 0; i < (long long)overlap_list->length; i++)
            {
                if (overlap_list->list[i].is_match == 100)
                {
                    overlap_list->list[i].is_match = 1;
                }
            }
        }
    }


    for (i = is_delete = 0; i < (long long)(overlap_list->length); i++)
    {
        if (overlap_list->list[i].is_match == 1)
        {
            overlap_list->list[i].without_large_indel = 1;
            is_delete++;
        }

        if (overlap_list->list[i].is_match == 100)
        {
            overlap_list->list[i].is_match = 1;
            overlap_list->list[i].without_large_indel = 0;
            is_delete++;
        }
    }

    // if(is_delete) radix_sort_overlap_region_dp_srt(overlap_list->list, overlap_list->list+overlap_list->length);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, (%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movq 0x10(%rdi), %rax
xorl %edi, %edi
testq %rax, %rax
cmovleq %rdi, %rax
pushq $0x1
popq %rcx
pushq $0x2
popq %r8
xorl %r9d, %r9d
cmpq %rax, %r9
je 0x22ba4
movq (%rbx), %r10
imulq $0xa8, %r9, %r15
cmpb $0x1, 0x2c(%r10,%r15)
jne 0x22b9c
leaq (%r10,%r15), %r11
movq 0x48(%r11), %r14
cmpq $0x3, %r14
jb 0x22b9c
addq %r15, %r10
addq $0x2c, %r10
movq %rcx, %rbp
leaq 0x1(%rbp), %r15
cmpq %r14, %r15
jge 0x22b9c
movq 0x58(%r11), %r12
shlq $0x5, %rbp
cmpl $-0x1, 0xc(%r12,%rbp)
je 0x22b93
addq %rbp, %r12
movl 0x1c(%r12), %r13d
movq %r15, %rbp
cmpq $0x3, %r13
jb 0x22b2b
movq %r8, %rbp
cmpq %rbp, %r13
je 0x22b8e
movq 0x44(%r10), %rdx
movl 0x18(%r12), %esi
addl %ebp, %esi
decl %esi
movzwl (%rdx,%rsi,2), %edx
movl %edx, %esi
andl $0x3ffe, %esi           # imm = 0x3FFE
cmpw $0x6, %si
setb %sil
testw %dx, %dx
setns %dl
incq %rbp
orb %sil, %dl
jne 0x22b58
jmp 0x22b93
movq %r15, %rbp
jmp 0x22b2b
movb $0x64, (%r10)
pushq $0x1
popq %rdx
movl %edx, %edi
incq %r9
jmp 0x22af4
cmpl $0x1, %edi
jne 0x22cbe
movsd 0x1c06eb(%rip), %xmm1   # 0x1e32a0
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
movslq %eax, %rdx
leaq 0x30(%rsp), %r15
andl $0x0, 0x30(%r15)
movq 0x18(%rsp), %rax
movq %rax, (%r15)
movl $0x177, %ecx            # imm = 0x177
movq %rcx, 0x8(%r15)
movq %rdx, 0x28(%r15)
andq $0x0, 0x18(%r15)
cmpq %rcx, %rax
movl $0x177, %edx            # imm = 0x177
cmovlq %rax, %rdx
decq %rdx
movq %rdx, 0x20(%r15)
addq $0x176, %rax            # imm = 0x176
cqto
idivq %rcx
movq %rax, 0x10(%r15)
leaq 0x10(%rsp), %rax
andq $0x0, (%rax)
leaq 0x8(%rsp), %rax
andq $0x0, (%rax)
movq (%rsp), %rax
leaq 0x30(%rax), %r13
xorl %r12d, %r12d
leaq 0x28(%rsp), %rbp
leaq 0x20(%rsp), %r14
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x52721
cmpl $-0x2, %r12d
je 0x22cb1
testl %eax, %eax
je 0x22cb1
xorpd %xmm0, %xmm0
movapd %xmm0, (%r13)
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
movq (%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x8(%rsp), %r9
callq 0x5320e
movl %eax, %r12d
cmpq $0x0, 0x10(%rsp)
jne 0x22c2d
cmpq $0x0, 0x8(%rsp)
jle 0x22c2d
movq 0x10(%rbx), %rcx
xorl %eax, %eax
testq %rcx, %rcx
cmovgq %rcx, %rax
pushq $0x2c
popq %rcx
movq %rax, %rdx
subq $0x1, %rdx
jb 0x22cbe
movq (%rbx), %rsi
cmpb $0x64, (%rsi,%rcx)
jne 0x22ca8
movb $0x1, (%rsi,%rcx)
addq $0xa8, %rcx
jmp 0x22c95
movq 0x10(%rbx), %rcx
xorl %eax, %eax
testq %rcx, %rcx
cmovgq %rcx, %rax
pushq $0x2d
popq %rcx
subq $0x1, %rax
jb 0x22cef
movq (%rbx), %rdx
movzbl -0x1(%rdx,%rcx), %esi
cmpl $0x64, %esi
je 0x22cdf
cmpl $0x1, %esi
jne 0x22ce6
movb $0x1, (%rdx,%rcx)
jmp 0x22ce6
movw $0x1, -0x1(%rdx,%rcx)
addq $0xa8, %rcx
jmp 0x22cc1
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq