// File: /workspace/github_cpp_makefile_star2_O3/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:
movq 0x10(%rdi), %rax
testq %rax, %rax
jle 0x50dc8
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, -0x30(%rsp)
movq %rdi, -0x28(%rsp)
movq (%rdi), %rcx
xorl %r8d, %r8d
xorl %edi, %edi
imulq $0xa8, %r8, %rsi
cmpb $0x1, 0x2c(%rcx,%rsi)
jne 0x50bb0
leaq (%rcx,%rsi), %r11
movq 0x48(%r11), %r10
cmpq $0x3, %r10
jl 0x50bb0
leaq (%rcx,%rsi), %r9
addq $0x2c, %r9
movq 0x58(%r11), %r11
movl $0x1, %ebx
movl $0x2, %esi
movq %rbx, %r14
shlq $0x5, %r14
cmpl $-0x1, 0xc(%r11,%r14)
je 0x50ba7
movq %rsi, %rbx
addq %r11, %r14
movl 0x1c(%r14), %r15d
cmpq $0x3, %r15
jae 0x50b6a
leaq 0x1(%rbx), %rsi
cmpq %r10, %rsi
jne 0x50b40
jmp 0x50bb0
movl $0x2, %r12d
cmpq %r12, %r15
je 0x50b5f
movq 0x44(%r9), %rsi
movl 0x18(%r14), %r13d
addl %r12d, %r13d
decl %r13d
movzwl (%rsi,%r13,2), %esi
movl %esi, %ebp
andl $0x3ffe, %ebp           # imm = 0x3FFE
cmpw $0x6, %bp
setb %bpl
testw %si, %si
setns %sil
incq %r12
orb %bpl, %sil
jne 0x50b70
movb $0x64, (%r9)
movl $0x1, %edi
incq %r8
cmpq %rax, %r8
jne 0x50b06
cmpl $0x1, %edi
movq -0x28(%rsp), %rsi
jne 0x50d8d
movl $0x177, %r10d           # imm = 0x177
movq -0x30(%rsp), %rdi
cmpq %r10, %rdi
movl $0x177, %r15d           # imm = 0x177
cmovlq %rdi, %r15
decq %r15
leaq -0x1(%rdi), %rbx
leaq -0x18(%rsp), %rcx
movq $0x0, (%rcx)
leaq 0x30(%rdx), %r8
leaq -0x1(%rax), %r11
xorl %r13d, %r13d
xorps %xmm0, %xmm0
xorl %r9d, %r9d
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %ecx, %ecx
movq %rbx, -0x8(%rsp)
movq %r8, -0x20(%rsp)
cmpq %rbx, %r15
jne 0x50c33
testq %rdi, %rdi
je 0x50d8d
cmpl $0x1, %ebp
movl $0x1, %ebp
je 0x50d8d
leaq (%r15,%r10), %r12
cmpq %rdi, %r12
cmovgeq %rbx, %r12
cmpl $-0x2, %ecx
je 0x50d8d
movaps %xmm0, (%r8)
testq %r14, %r14
je 0x50c56
movq 0x48(%rdx), %rcx
jmp 0x50c60
movq $0x0, 0x48(%rdx)
xorl %ecx, %ecx
cmpq %rax, %rcx
jge 0x50c92
movq (%rsi), %rdi
imulq $0xa8, %rcx, %r8
movl 0x4(%rdi,%r8), %esi
cmpq %rsi, %r15
jge 0x50ca0
movq $0x0, 0x48(%rdx)
xorl %ecx, %ecx
movq -0x28(%rsp), %rsi
movq -0x30(%rsp), %rdi
jmp 0x50d4a
movq %rax, 0x48(%rdx)
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
jmp 0x50d4f
movq %r15, %r9
subq %r14, %r9
movq $0x0, -0x10(%rsp)
leaq (%rdi,%r8), %r13
addq $0x4, %r13
xorl %edi, %edi
xorl %r10d, %r10d
movl (%r13), %ebx
movl 0x4(%r13), %r8d
cmpq %r15, %r8
cmovaeq %r15, %r8
cmpq %r14, %rbx
movq %r14, %rsi
cmovgq %rbx, %rsi
subq %rsi, %r8
js 0x50d0f
cmpq %r8, %r9
jne 0x50cfd
movzbl 0x28(%r13), %r8d
leaq -0x10(%rsp), %rsi
cmpl $0x1, %r8d
je 0x50cfa
cmpl $0x64, %r8d
jne 0x50cfd
leaq -0x18(%rsp), %rsi
incq (%rsi)
incq %rdi
testq %r10, %r10
jne 0x50d09
movq %rcx, 0x48(%rdx)
movl $0x1, %r10d
cmpq %rcx, %r11
je 0x50d23
incq %rcx
addq $0xa8, %r13
cmpq %rbx, %r15
jge 0x50cbc
xorl %ecx, %ecx
testq %rdi, %rdi
setne %cl
movq -0x10(%rsp), %r9
movq -0x18(%rsp), %r13
movq -0x28(%rsp), %rsi
movq -0x30(%rsp), %rdi
movl $0x177, %r10d           # imm = 0x177
movq -0x8(%rsp), %rbx
movq -0x20(%rsp), %r8
addq %r10, %r14
movq %r12, %r15
testq %r9, %r9
jne 0x50c17
movq %r12, %r15
testq %r13, %r13
jle 0x50c17
testq %rax, %rax
jle 0x50d8d
movq (%rsi), %rcx
addq $0x2c, %rcx
movq %rax, %rdx
cmpb $0x64, (%rcx)
jne 0x50d81
movb $0x1, (%rcx)
addq $0xa8, %rcx
decq %rdx
jne 0x50d79
testq %rax, %rax
jle 0x50dbe
movq (%rsi), %rcx
addq $0x2d, %rcx
movzbl -0x1(%rcx), %edx
cmpl $0x64, %edx
je 0x50dac
cmpl $0x1, %edx
jne 0x50db2
movb $0x1, (%rcx)
jmp 0x50db2
movw $0x1, -0x1(%rcx)
addq $0xa8, %rcx
decq %rax
jne 0x50d99
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq