// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: collect_no_cov_regions(overlap_region_alloc*, All_reads*, kvec_t_u32_warp*, kvec_t_u64_warp*, int, int)
void collect_no_cov_regions(overlap_region_alloc* overlap_list, All_reads* R_INF, 
kvec_t_u32_warp* b, kvec_t_u64_warp* r, int min_dp, int min_len)
{
    b->a.n = r->a.n = 0;
    ///if(overlap_list->length == 0) return;
    long long i = 0, xLen = Get_READ_LENGTH((*R_INF), overlap_list->list[0].x_id);
    uint32_t qs, qe;
    uint64_t tmp;
    int dp, old_dp, s_start = 0, s_end = 0;
    ///at least 1
    if(min_len < 1) min_len = 1;

    
    for (i = 0; i < (long long)overlap_list->length; i++)
    {
        if (overlap_list->list[i].is_match != 1 && overlap_list->list[i].is_match != 2) continue;

        qs = overlap_list->list[i].x_pos_s;
        qe = overlap_list->list[i].x_pos_e + 1;
        kv_push(uint32_t, b->a, qs<<1);
        kv_push(uint32_t, b->a, qe<<1|1);
    }


    ///we can identify the qs and qe by the 0-th bit
    radix_sort_b32(b->a.a, b->a.a + b->a.n);

    for (i = 0, dp = 0; i < (long long)b->a.n; ++i) 
    {
        old_dp = dp;
        //if a[j] is qe
        if (b->a.a[i]&1) --dp;
        else ++dp;
        /**
        min_dp is the coverage drop threshold
        there are two cases: 
            1. old_dp = dp + 1 (b.a[j] is qe); 2. old_dp = dp - 1 (b.a[j] is qs);
        **/
        if (old_dp < min_dp && dp >= min_dp) ///old_dp < dp, b.a[j] is qs
        { 
            ///case 2, a[j] is qs
            s_end = b->a.a[i]>>1;
            ///at least 1
            if(s_end-s_start >= min_len)
            {
                tmp = s_start; tmp = tmp << 32; tmp = tmp | (uint64_t)(s_end-1);
                kv_push(uint64_t, r->a, tmp);
            }
        }
        else if (old_dp >= min_dp && dp < min_dp) ///old_dp > min_dp, b.a[j] is qe
        {
            s_start = b->a.a[i]>>1;
        }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, %ebp
movq %rdx, %r14
xorl %r8d, %r8d
movq %rcx, 0x10(%rsp)
movq %r8, (%rcx)
movq %r8, (%rdx)
movq 0x18(%rsi), %rdx
movq (%rdi), %rsi
movq 0x10(%rdi), %rax
movl (%rsi), %esi
movq (%rdx,%rsi,8), %rcx
movq %rcx, 0x20(%rsp)
cmpl $0x2, %r9d
movl $0x1, %ecx
cmovgel %r9d, %ecx
movl %ecx, 0xc(%rsp)
testq %rax, %rax
jle 0x57dd1
movl $0x4, %r12d
xorl %r8d, %r8d
xorl %r13d, %r13d
movq %rdi, 0x18(%rsp)
movq (%rdi), %rdx
movb 0x28(%rdx,%r12), %sil
decb %sil
cmpb $0x1, %sil
ja 0x57dbe
movl (%rdx,%r12), %r15d
movl 0x4(%rdx,%r12), %ebx
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rax
cmpq %rdx, %r8
jne 0x57d6a
leaq (%r8,%r8), %rsi
testq %r8, %r8
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %r8
movq 0x8(%r14), %rdx
addl %r15d, %r15d
leaq 0x1(%r8), %rsi
movq %rsi, (%r14)
movl %r15d, (%rax,%r8,4)
cmpq %rdx, %rsi
jne 0x57da4
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rsi
movq 0x18(%rsp), %rdi
leal 0x3(,%rbx,2), %edx
leaq 0x1(%rsi), %r8
movq %r8, (%r14)
movl %edx, (%rax,%rsi,4)
movq 0x10(%rdi), %rax
incq %r13
addq $0xa8, %r12
cmpq %rax, %r13
jl 0x57d14
movq 0x10(%r14), %rdi
leaq (%rdi,%r8,4), %rsi
callq 0x45106
movq (%r14), %rax
xorl %r15d, %r15d
testq %rax, %rax
jle 0x57eb4
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %r8d
movl %r12d, %ecx
movq 0x10(%r14), %rdx
movl (%rdx,%r13,4), %ebx
movl %ebx, %r12d
andl $0x1, %r12d
negl %r12d
orl $0x1, %r12d
addl %ecx, %r12d
cmpl %ebp, %ecx
setge %dl
cmpl %ebp, %r12d
setl %sil
shrl %ebx
orb %dl, %sil
je 0x57e3e
cmpl %ebp, %r12d
cmovgel %r15d, %ebx
cmpl %ebp, %ecx
cmovll %r15d, %ebx
movl %ebx, %r15d
jmp 0x57ea3
movl %ebx, %ecx
subl %r15d, %ecx
cmpl %r8d, %ecx
jl 0x57ea3
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x57e86
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %r8d
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq %r15, %rdx
shlq $0x20, %rdx
decl %ebx
movslq %ebx, %rsi
orq %rdx, %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %rsi, (%rax,%rcx,8)
movq (%r14), %rax
incq %r13
cmpq %rax, %r13
jl 0x57dfd
movl %r15d, %r15d
jmp 0x57ebe
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %r8d
movq 0x20(%rsp), %rdx
movq %rdx, %rax
subq %r15, %rax
jle 0x57f26
movl %r8d, %ecx
cmpq %rcx, %rax
jl 0x57f26
shlq $0x20, %r15
decl %edx
movslq %edx, %rbx
orq %r15, %rbx
movq (%rdi), %rcx
cmpq 0x8(%rdi), %rcx
jne 0x57f17
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%rdi)
movq 0x10(%rdi), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
jmp 0x57f1b
movq 0x10(%rdi), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %rbx, (%rax,%rcx,8)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq