// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movl %r8d, (%rsp)
movq %rdx, %r14
xorl %eax, %eax
movq %rcx, 0x8(%rsp)
movq %rax, (%rcx)
movq %rax, (%rdx)
movq 0x18(%rsi), %rcx
movq (%rdi), %rdx
movl (%rdx), %edx
movq (%rcx,%rdx,8), %rcx
movq %rcx, 0x10(%rsp)
cmpl $0x2, %r9d
movl $0x1, %ecx
cmovgel %r9d, %ecx
movl %ecx, 0x4(%rsp)
cmpq %rax, 0x10(%rdi)
jle 0x5897f
movq %rdi, %r15
movl $0x4, %r12d
xorl %r13d, %r13d
movq (%r15), %rax
movb 0x28(%rax,%r12), %cl
decb %cl
cmpb $0x1, %cl
ja 0x5896b
movl (%rax,%r12), %ebp
movl 0x4(%rax,%r12), %ebx
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x58916
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
addl %ebp, %ebp
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r14)
movl %ebp, (%rdi,%rax,4)
movq 0x8(%r14), %rax
cmpq %rax, %rcx
jne 0x58953
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
leal 0x3(,%rbx,2), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %eax, (%rdx,%rcx,4)
incq %r13
addq $0xa8, %r12
cmpq 0x10(%r15), %r13
jl 0x588cc
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,4), %rsi
callq 0x45ada
xorl %r15d, %r15d
cmpq $0x0, (%r14)
jle 0x58a60
movl $0x2, %ebp
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x8(%rsp), %rdi
movl (%rsp), %r8d
movl %r13d, %eax
movq 0x10(%r14), %rcx
movl (%rcx,%r12,4), %ebx
movl %ebx, %r13d
andl $0x1, %r13d
negl %r13d
orl $0x1, %r13d
addl %eax, %r13d
cmpl %r8d, %eax
setge %cl
cmpl %r8d, %r13d
setl %dl
shrl %ebx
orb %cl, %dl
je 0x589f1
cmpl %r8d, %r13d
cmovgel %r15d, %ebx
cmpl %r8d, %eax
cmovll %r15d, %ebx
movl %ebx, %r15d
jmp 0x58a4f
movl %ebx, %eax
subl %r15d, %eax
cmpl 0x4(%rsp), %eax
jl 0x58a4f
movq 0x8(%rdi), %rax
cmpq %rax, (%rdi)
jne 0x58a2e
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %rbp, %rsi
movq %rsi, 0x8(%rdi)
movq 0x10(%rdi), %rdi
shlq $0x3, %rsi
callq 0x2370
movl (%rsp), %r8d
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq %r15, %rax
shlq $0x20, %rax
decl %ebx
movslq %ebx, %rcx
orq %rax, %rcx
movq (%rdi), %rax
movq 0x10(%rdi), %rdx
leaq 0x1(%rax), %rsi
movq %rsi, (%rdi)
movq %rcx, (%rdx,%rax,8)
incq %r12
cmpq (%r14), %r12
jl 0x589b0
movl %r15d, %r15d
jmp 0x58a65
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rdx
movq %rdx, %rax
subq %r15, %rax
jle 0x58acc
movl 0x4(%rsp), %ecx
cmpq %rcx, %rax
jl 0x58acc
shlq $0x20, %r15
decl %edx
movslq %edx, %rbx
orq %r15, %rbx
movq 0x8(%rdi), %rax
cmpq %rax, (%rdi)
jne 0x58aba
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rdi)
movq 0x10(%rdi), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rax
movq 0x10(%rdi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rdi)
movq %rbx, (%rcx,%rax,8)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq