// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/ecovlp.cpp
// Function: gen_hap_dc_cov(asg64_v*, asg64_v*, ma_hit_t_alloc*, All_reads*, unsigned long, long, double, UC_Read*, UC_Read*, cc_v*, unsigned long)
uint64_t gen_hap_dc_cov(asg64_v *be, asg64_v *ba, ma_hit_t_alloc *paf, All_reads *rref, uint64_t wl, int64_t occ_exact, double occ_exact_rate, UC_Read* qu, UC_Read* tu, cc_v *sc, uint64_t rid)
{
    ma_hit_t *z; be->n = ba->n = 0; uint64_t k, s, e, vn, m; asg64_v *v = NULL;
    int64_t dp, old_dp, st = 0, ed, ql = qu->length, qs, qe, ff;
    for (k = 0; k < paf->length; k++) {
        z = &(paf->buffer[k]);

        // if(rid == 16) {
        //     fprintf(stderr, "[M::%s]\tqn::%u::%.*s\ttn::%u::%.*s\t%c\tq::[%u,%u)\tq::[%u,%u)\tel::%u\n", __func__, 
        //         (uint32_t)(z->qns>>32), (int)Get_NAME_LENGTH(R_INF, (uint32_t)(z->qns>>32)), Get_NAME(R_INF, (uint32_t)(z->qns>>32)), z->tn, (int)Get_NAME_LENGTH(R_INF, z->tn), Get_NAME(R_INF, z->tn), "+-"[z->rev],
        //         (uint32_t)z->qns, z->qe, z->ts, z->te, z->el);
        // }
        
        if((z->el) && (quick_exact_match(z, rref, qu, tu, sc))) {
            s = ((uint32_t)(z->qns)); e = z->qe;
            kv_push(uint64_t, (*be), (s<<1));
            kv_push(uint64_t, (*be), (e<<1)|1);
            z->el = 1;
            // fprintf(stderr, "-gmm-[M::%s]\tqn::%u::%.*s\ttn::%u::%.*s\t%c\tq::[%u,%u)\tq::[%u,%u)\n", __func__, 
            //     (uint32_t)(z->qns>>32), (int)Get_NAME_LENGTH(R_INF, (uint32_t)(z->qns>>32)), Get_NAME(R_INF, (uint32_t)(z->qns>>32)), z->tn, (int)Get_NAME_LENGTH(R_INF, z->tn), Get_NAME(R_INF, z->tn), "+-"[z->rev],
            //     (uint32_t)z->qns, z->qe, z->ts, z->te);
        } else {
            s = ((uint32_t)(z->qns)); e = z->qe;
            kv_push(uint64_t, (*ba), (s<<1));
            kv_push(uint64_t, (*ba), (e<<1)|1);
            z->el = 0;
            // fprintf(stderr, "-gum-[M::%s]\tqn::%u::%.*s\ttn::%u::%.*s\t%c\tq::[%u,%u)\tq::[%u,%u)\n", __func__, 
            //     (uint32_t)(z->qns>>32), (int)Get_NAME_LENGTH(R_INF, (uint32_t)(z->qns>>32)), Get_NAME(R_INF, (uint32_t)(z->qns>>32)), z->tn, (int)Get_NAME_LENGTH(R_INF, z->tn), Get_NAME(R_INF, z->tn), "+-"[z->rev],
            //     (uint32_t)z->qns, z->qe, z->ts, z->te);
        }
        /**
        s = ((uint32_t)(z->qns)); e = z->qe;
        // sc->a[z->tn]
        if(z->el) {
            // if((z->qns>>32) == 75 && z->tn == 59) {
            if(quick_exact_match(z, rref, qu, tu, sc)) {
                s = ((uint32_t)(z->qns)); e = z->qe;
                // fprintf(stderr, "-mm-[M::%s]\tqn::%u::%.*s\ttn::%u::%.*s\t%c\n", __func__, 
                // (uint32_t)(z->qns>>32), (int)Get_NAME_LENGTH(R_INF, (uint32_t)(z->qns>>32)), Get_NAME(R_INF, (uint32_t)(z->qns>>32)), z->tn, (int)Get_NAME_LENGTH(R_INF, z->tn), Get_NAME(R_INF, z->tn), "+-"[z->rev]);
            } else {
                s = ((uint32_t)(z->qns)); e = z->qe;
                // fprintf(stderr, "-um-[M::%s]\tqn::%u::%.*s\ttn::%u::%.*s\t%c\n", __func__, 
                // (uint32_t)(z->qns>>32), (int)Get_NAME_LENGTH(R_INF, (uint32_t)(z->qns>>32)), Get_NAME(R_INF, (uint32_t)(z->qns>>32)), z->tn, (int)Get_NAME_LENGTH(R_INF, z->tn), Get_NAME(R_INF, z->tn), "+-"[z->rev]);
            }
            // }
        } 
        **/
    }


    v = be; vn = v->n;
    radix_sort_ec64(v->a, v->a+v->n);
    for (k = 0, dp = 0, st = ed = 0; k < vn; ++k) {
        old_dp = dp;
        ///if a[j] is qe
        if (v->a[k]&1) --dp;
        else ++dp;

        ed = v->a[k]>>1;
        if(ed > st) {
            m = st; m <<= 32; m |= ((uint64_t)ed);
            kv_push(uint64_t, (*v), m); 
            kv_push(uint64_t, (*v), old_dp); 
            if((old_dp + 1) < occ_exact) return 0;///+1 for self
        }
        st = ed;
    }
    ed = ql; old_dp = dp;
    if(ed > st) {
        m = st; m <<= 32; m |= ((uint64_t)ed);
        kv_push(uint64_t, (*v), m); 
        kv_push(uint64_t, (*v), old_dp); 
        if((old_dp + 1) < occ_exact) return 0;///+1 for self
    }

    for (k = vn, m = 0; k < v->n; k++) {
        v->a[m++] = v->a[k];
    }
    v->n = m;


    v = ba; vn = v->n;
    radix_sort_ec64(v->a, v->a+v->n);
    for (k = 0, dp = 0, st = ed = 0; k < vn; ++k) {
        old_dp = dp;
        ///if a[j] is qe
        if (v->a[k]&1) --dp;
        else ++dp;

        ed = v->a[k]>>1;
        if(ed > st) {
            m = st; m <<= 32; m |= ((uint64_t)ed);
            kv_push(uint64_t, (*v), m); 
            kv_push(uint64_t, (*v), old_dp); 
        }
        st = ed;
    }
    ed = ql; old_dp = dp;
    if(ed > st) {
        m = st; m <<= 32; m |= ((uint64_t)ed);
        kv_push(uint64_t, (*v), m); 
        kv_push(uint64_t, (*v), old_dp); 
    }

    for (k = vn, m = 0; k < v->n; k++) {
        v->a[m++] = v->a[k];
    }
    v->n = m;
    






    ///debug
    // v = be; 
    // for (k = 0; k < v->n; k += 2) {
    //     fprintf(stderr, "-be-[M::%s]\tq::[%lu,%u)\tocc::%lu\n", __func__, v->a[k]>>32, (uint32_t)v->a[k], v->a[k+1]);
    // }

    // v = ba; 
    // for (k = 0; k < v->n; k += 2) {
    //     fprintf(stderr, "-ba-[M::%s]\tq::[%lu,%u)\tocc::%lu\n", __func__, v->a[k]>>32, (uint32_t)v->a[k], v->a[k+1]);
    // }









    int64_t ke = 0, ka = 0, cc[2];
    qs = 0; qe = wl; qe = ((qe<=ql)?qe:ql);
    for (; qs < ql; ) {
        cc[0] = cal_cov_re(be, &ke, qs, qe); 
        cc[1] = cal_cov_re(ba, &ka, qs, qe);

        // fprintf(stderr, "[M::%s]\tq::[%ld,%ld)\tcc[0]::%ld\tcc[1]::%ld\n", __func__, qs, qe, cc[0], cc[1]);

        ff = 0;
        if((cc[0]) && (cc[0] > cc[1])) {
            cc[0] += qe - qs;///+(qe - qs) for self
            cc[1] += cc[0];
            if(cc[0] > (cc[1]*occ_exact_rate)) {
                ff = 1;
            }
        }

        if(ff == 0) return 0;
        // fprintf(stderr, "[M::%s-beg]\tq::[%ld,%ld)\tcc[0]::%ld\tcc[1]::%ld\n", __func__, qs, qe, cc[0], cc[1]);
        qs += wl; qe += wl; qe = ((qe<=ql)?qe:ql);
    }


    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm0, 0x38(%rsp)
movq %r9, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdi, %r8
movq 0x90(%rsp), %rcx
xorl %eax, %eax
movq %rax, (%rsi)
movq %rax, (%rdi)
movq 0x8(%rcx), %rax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0xc(%rdx)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x8(%rsp)
je 0x1829e3
movq %rsi, %r15
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %rdx, 0x10(%rsp)
movq (%rdx), %r13
cmpb $0x0, 0x20(%r13,%rbx)
je 0x182910
leaq (%rbx,%r13), %rdi
movq 0x40(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq 0x98(%rsp), %rcx
movq 0xa0(%rsp), %r8
callq 0x182573
movq 0x8(%rsp), %r8
testl %eax, %eax
je 0x182910
movq (%r13,%rbx), %r14
movl 0x8(%r13,%rbx), %ebp
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x1828da
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
addq %r14, %r14
movabsq $0x1fffffffe, %rax      # imm = 0x1FFFFFFFE
andq %rax, %r14
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
movq %r14, (%rdi,%rax,8)
movq 0x8(%r8), %rax
movb $0x1, %r15b
movq %r8, %r14
cmpq %rax, (%r8)
je 0x182981
jmp 0x1829a7
movq (%r13,%rbx), %r14
movl 0x8(%r13,%rbx), %ebp
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x18294d
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r15)
addq %r14, %r14
movabsq $0x1fffffffe, %rax      # imm = 0x1FFFFFFFE
andq %rax, %r14
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r15)
movq %r14, (%rdi,%rax,8)
movq 0x8(%r15), %rax
movq %r15, %rcx
xorl %r15d, %r15d
movq %rcx, %r14
cmpq %rax, (%rcx)
jne 0x1829a7
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r14)
leaq 0x1(,%rbp,2), %rax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rax, (%rdx,%rcx,8)
movb %r15b, 0x20(%r13,%rbx)
incq %r12
movq 0x10(%rsp), %rdx
movl 0xc(%rdx), %eax
addq $0x28, %rbx
cmpq %rax, %r12
movq 0x20(%rsp), %r15
jb 0x18285f
movq (%r8), %rbx
movq 0x10(%r8), %rdi
leaq (%rdi,%rbx,8), %rsi
callq 0x178fc2
movq %rbx, %r9
testq %rbx, %rbx
movq %rbx, 0x10(%rsp)
je 0x182ae9
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x8(%rsp), %r8
movq 0x10(%r8), %rdi
movq (%rdi,%rbp,8), %r14
leaq -0x1(%r12), %rbx
leaq 0x1(%r12), %r15
testb $0x1, %r14b
cmoveq %r15, %rbx
shrq %r14
cmpq %r13, %r14
jbe 0x182ad5
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x182a6b
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
shlq $0x20, %r13
orq %r14, %r13
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
movq %r13, (%rdi,%rax,8)
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x182ab8
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
movq %r12, (%rcx,%rax,8)
cmpq 0x28(%rsp), %r15
jl 0x182ba7
incq %rbp
movq %r14, %r13
movq %rbx, %r12
cmpq %rbp, %r9
jne 0x182a11
jmp 0x182af3
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rax
cmpq %r14, %rax
movq 0x20(%rsp), %r15
jle 0x182bae
shlq $0x20, %r14
orq %rax, %r14
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x182b45
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
movq %r14, (%rdi,%rax,8)
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x182b8b
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
movq %rbx, (%rcx,%rax,8)
incq %rbx
cmpq 0x28(%rsp), %rbx
jge 0x182bae
xorl %eax, %eax
jmp 0x182e77
cmpq (%r8), %r9
jae 0x182bda
movq 0x10(%r8), %rax
leaq (%rax,%r9,8), %rcx
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
leaq 0x1(%rsi), %rdx
movq %rdi, (%rax,%rsi,8)
leaq (%r9,%rsi), %rdi
incq %rdi
movq %rdx, %rsi
cmpq (%r8), %rdi
jb 0x182bbd
jmp 0x182bdc
xorl %edx, %edx
movq %rdx, (%r8)
movq (%r15), %rbx
movq 0x10(%r15), %rdi
leaq (%rdi,%rbx,8), %rsi
callq 0x178fc2
movq %rbx, %r9
testq %rbx, %rbx
movq %rbx, 0x10(%rsp)
je 0x182cd6
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x8(%rsp), %r8
movq 0x10(%r15), %rdi
movq (%rdi,%rbp,8), %r13
movq %r13, %rbx
shrq %rbx
cmpq %r14, %rbx
jbe 0x182cb7
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x182c58
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r15)
shlq $0x20, %r14
orq %rbx, %r14
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r15)
movq %r14, (%rdi,%rax,8)
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x182ca5
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r15)
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %r12, (%rcx,%rax,8)
andl $0x1, %r13d
negq %r13
orq $0x1, %r13
addq %r13, %r12
incq %rbp
movq %rbx, %r14
cmpq %rbp, %r9
jne 0x182c0d
jmp 0x182ce0
xorl %r12d, %r12d
xorl %ebx, %ebx
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r13
cmpq %rbx, %r13
movq 0x30(%rsp), %r14
jle 0x182d8a
shlq $0x20, %rbx
orq %r13, %rbx
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x182d32
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r15)
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r15)
movq %rbx, (%rdi,%rax,8)
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x182d78
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r15)
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %r12, (%rcx,%rax,8)
cmpq (%r15), %r9
jae 0x182db6
movq 0x10(%r15), %rax
leaq (%rax,%r9,8), %rcx
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
leaq 0x1(%rsi), %rdx
movq %rdi, (%rax,%rsi,8)
leaq (%r9,%rsi), %rdi
incq %rdi
movq %rdx, %rsi
cmpq (%r15), %rdi
jb 0x182d99
jmp 0x182db8
xorl %edx, %edx
movq %rdx, (%r15)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x48(%rsp)
testq %r13, %r13
jle 0x182e68
cmpq %r14, %r13
movq %r14, %r12
cmovlq %r13, %r12
movq $0x0, 0x10(%rsp)
leaq 0x48(%rsp), %rbp
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %r8, %rdi
leaq 0x50(%rsp), %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x182764
movq %rax, %r13
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x182764
testq %r13, %r13
je 0x182e72
cmpq %rax, %r13
jle 0x182e72
addq %r12, %r13
leaq (%r15,%r13), %rcx
addq %r15, %rax
addq %r13, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x38(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x182e72
addq %r14, %rbx
addq %r14, %r12
movq 0x18(%rsp), %rax
cmpq %rax, %r12
cmovgeq %rax, %r12
subq %r14, %r15
cmpq %rax, %rbx
movq 0x8(%rsp), %r8
jl 0x182ded
movl $0x1, %eax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq