// File: /workspace/github_cpp_makefile_star2_O2/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 %r9d, 0x1c(%rsp)
movl %r8d, %ebp
movq %rdx, %r14
movq %rcx, 0x8(%rsp)
andq $0x0, (%rcx)
movq %rdi, %r12
andq $0x0, (%rdx)
movq 0x18(%rsi), %rax
movq (%rdi), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
pushq $0x4
popq %r15
xorl %ecx, %ecx
xorl %r13d, %r13d
cmpq 0x10(%r12), %r13
jge 0x28d23
movq (%r12), %rax
movb 0x28(%rax,%r15), %dl
decb %dl
cmpb $0x1, %dl
ja 0x28d14
movl (%rax,%r15), %ebx
movl 0x4(%rax,%r15), %eax
movq %rax, 0x10(%rsp)
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rax
cmpq %rdx, %rcx
jne 0x28cc8
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x8(%r14), %rdx
addl %ebx, %ebx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %ebx, (%rax,%rcx,4)
cmpq %rdx, %rsi
jne 0x28cfe
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rsi
movq 0x10(%rsp), %rcx
leal 0x3(,%rcx,2), %edx
leaq 0x1(%rsi), %rcx
movq %rcx, (%r14)
movl %edx, (%rax,%rsi,4)
incq %r13
addq $0xa8, %r15
jmp 0x28c65
movl 0x1c(%rsp), %eax
cmpl $0x2, %eax
pushq $0x1
popq %rbx
cmovgel %eax, %ebx
movq 0x10(%r14), %rdi
leaq (%rdi,%rcx,4), %rsi
callq 0x18e74
movl %ebx, %r8d
xorl %r15d, %r15d
xorl %eax, %eax
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
movl %ebx, 0x10(%rsp)
cmpq (%r14), %r15
jge 0x28e01
movq 0x10(%r14), %rcx
movl (%rcx,%r15,4), %ebx
movl %ebx, %r12d
andl $0x1, %r12d
negl %r12d
orl $0x1, %r12d
addl %eax, %r12d
cmpl %ebp, %eax
setge %cl
cmpl %ebp, %r12d
setl %dl
shrl %ebx
orb %cl, %dl
je 0x28d96
cmpl %ebp, %r12d
cmovgel %r13d, %ebx
cmpl %ebp, %eax
cmovll %r13d, %ebx
movl %ebx, %r13d
jmp 0x28df6
movl %ebx, %eax
subl %r13d, %eax
cmpl %r8d, %eax
jl 0x28df6
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x28ddc
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x10(%rsp), %r8d
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq %r13, %rdx
shlq $0x20, %rdx
decl %ebx
movslq %ebx, %rsi
orq %rdx, %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %rsi, (%rax,%rcx,8)
incq %r15
movl %r12d, %eax
jmp 0x28d51
movl %r13d, %eax
movq 0x20(%rsp), %rsi
movq %rsi, %rcx
subq %rax, %rcx
jle 0x28e67
movl %r8d, %edx
cmpq %rdx, %rcx
jl 0x28e67
shlq $0x20, %rax
decl %esi
movslq %esi, %rbx
orq %rax, %rbx
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x28e5c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
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