// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rsi, -0x28(%rsp)
movq %rdi, -0x38(%rsp)
movq 0x10(%rdi), %rax
testq %rax, %rax
jle 0x51c5e
movq %rdx, -0x30(%rsp)
movq -0x38(%rsp), %rcx
movq (%rcx), %rcx
xorl %edi, %edi
xorl %esi, %esi
imulq $0xa8, %rdi, %r8
cmpb $0x1, 0x2c(%rcx,%r8)
jne 0x51a6f
leaq (%rcx,%r8), %r10
movq 0x48(%r10), %r9
cmpq $0x3, %r9
jl 0x51a6f
addq %rcx, %r8
addq $0x2c, %r8
movq 0x58(%r10), %r10
movl $0x1, %r11d
movl $0x2, %r14d
movq %r11, %rbx
shlq $0x5, %rbx
cmpl $-0x1, 0xc(%r10,%rbx)
je 0x51a66
movq %r14, %r11
addq %r10, %rbx
movl 0x1c(%rbx), %r14d
cmpq $0x3, %r14
jae 0x51a26
leaq 0x1(%r11), %r14
cmpq %r9, %r14
jne 0x519fc
jmp 0x51a6f
movl $0x2, %r15d
cmpq %r15, %r14
je 0x51a1b
movq 0x44(%r8), %r12
movl 0x18(%rbx), %r13d
addl %r15d, %r13d
decl %r13d
movzwl (%r12,%r13,2), %ebp
movl %ebp, %r12d
andl $0x3ffe, %r12d          # imm = 0x3FFE
cmpw $0x6, %r12w
setb %r12b
testw %bp, %bp
setns %bpl
incq %r15
orb %r12b, %bpl
jne 0x51a2c
movb $0x64, (%r8)
movl $0x1, %esi
incq %rdi
cmpq %rax, %rdi
jne 0x519c0
cmpl $0x1, %esi
movq -0x30(%rsp), %r8
jne 0x51c5e
movl $0x177, %r9d            # imm = 0x177
movq -0x28(%rsp), %rcx
cmpq %r9, %rcx
movl $0x177, %r14d           # imm = 0x177
cmovlq %rcx, %r14
decq %r14
leaq -0x1(%rcx), %r15
leaq 0x30(%r8), %r13
leaq -0x1(%rax), %r10
xorl %r11d, %r11d
xorps %xmm0, %xmm0
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, -0x8(%rsp)
movq %r13, -0x10(%rsp)
movq %r14, %r12
cmpq %r15, %r14
jne 0x51af7
cmpq $0x0, -0x28(%rsp)
sete %sil
cmpl $0x1, %ebp
sete %dil
orb %sil, %dil
movl $0x1, %edi
je 0x51af9
xorl %esi, %esi
movq %r11, -0x18(%rsp)
movq %r12, %r14
jmp 0x51b14
movl %ebp, %edi
leaq (%r11,%r9), %rsi
movq %rsi, -0x18(%rsp)
leaq (%r12,%r9), %r14
cmpq -0x28(%rsp), %r14
cmovgeq %r15, %r14
movb $0x1, %sil
movl %edi, %ebp
cmpl $-0x2, %ecx
je 0x51c5e
testb %sil, %sil
je 0x51c5e
movaps %xmm0, (%r13)
testq %r11, %r11
jne 0x51b38
movq $0x0, 0x48(%r8)
movq 0x48(%r8), %rcx
cmpq %rax, %rcx
jge 0x51b6e
movq -0x38(%rsp), %rsi
movq (%rsi), %rsi
imulq $0xa8, %rcx, %r8
movl 0x4(%rsi,%r8), %edi
cmpq %rdi, %r12
jge 0x51b7c
movq -0x30(%rsp), %r8
movq $0x0, 0x48(%r8)
xorl %ecx, %ecx
jmp 0x51c1b
movq %rax, 0x48(%r8)
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
jmp 0x51c1b
movl %ebp, -0x1c(%rsp)
movq %r12, %rdi
subq %r11, %rdi
leaq (%rsi,%r8), %r9
addq $0x4, %r9
xorl %ebx, %ebx
xorl %r8d, %r8d
xorl %esi, %esi
movl (%r9), %ebp
movl 0x4(%r9), %r15d
cmpq %r12, %r15
cmovgeq %r12, %r15
cmpq %r11, %rbp
movq %r11, %r13
cmovgq %rbp, %r13
subq %r13, %r15
js 0x51be6
cmpq %r15, %rdi
jne 0x51bd0
movzbl 0x28(%r9), %r15d
cmpl $0x64, %r15d
je 0x51bcd
cmpl $0x1, %r15d
jne 0x51bd0
incq %rbx
jmp 0x51bd0
incq %rdx
incq %r8
testq %rsi, %rsi
jne 0x51be1
movq -0x30(%rsp), %rsi
movq %rcx, 0x48(%rsi)
movl $0x1, %esi
cmpq %rcx, %r10
je 0x51bfa
incq %rcx
addq $0xa8, %r9
cmpq %rbp, %r12
jge 0x51b95
xorl %ecx, %ecx
testq %r8, %r8
setne %cl
movq -0x30(%rsp), %r8
movl $0x177, %r9d            # imm = 0x177
movq -0x8(%rsp), %r15
movq -0x10(%rsp), %r13
movl -0x1c(%rsp), %ebp
testq %rbx, %rbx
sete %sil
testq %rdx, %rdx
setg %dil
andb %sil, %dil
cmpb $0x1, %dil
movq -0x18(%rsp), %r11
jne 0x51ac8
movq -0x38(%rsp), %rcx
movq (%rcx), %rcx
addq $0x2c, %rcx
movq %rax, %rdx
cmpb $0x64, (%rcx)
jne 0x51c52
movb $0x1, (%rcx)
addq $0xa8, %rcx
decq %rdx
jne 0x51c4a
testq %rax, %rax
jle 0x51c90
movq -0x38(%rsp), %rcx
movq (%rcx), %rcx
addq $0x2d, %rcx
cmpb $0x1, -0x1(%rcx)
jne 0x51c78
movb $0x1, (%rcx)
cmpb $0x64, -0x1(%rcx)
jne 0x51c84
movw $0x1, -0x1(%rcx)
addq $0xa8, %rcx
decq %rax
jne 0x51c6f
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq