// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movsd %xmm0, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %rbp
movq %rsi, %r14
movq 0x80(%rsp), %rax
andq $0x0, (%rsi)
andq $0x0, (%rdi)
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %rdi, (%rsp)
movl 0xc(%rbp), %eax
cmpq %rax, %r12
jae 0x1454aa
movq (%rbp), %r13
cmpb $0x0, 0x20(%r13,%rbx)
je 0x1453eb
leaq (%rbx,%r13), %rdi
movq 0x10(%rsp), %rsi
movq 0x80(%rsp), %rdx
movq 0x88(%rsp), %rcx
movq 0x90(%rsp), %r8
callq 0x14503a
movq (%rsp), %rdi
testl %eax, %eax
je 0x1453eb
movq (%r13,%rbx), %r15
movl 0x8(%r13,%rbx), %eax
movq %rax, 0x8(%rsp)
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x14537f
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 (%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
addq %r15, %r15
movabsq $0x1fffffffe, %rdx      # imm = 0x1FFFFFFFE
andq %rdx, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %r15, (%rax,%rcx,8)
movq (%rdi), %rcx
cmpq 0x8(%rdi), %rcx
jne 0x1453cc
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 (%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq 0x8(%rsp), %rdx
leaq 0x1(,%rdx,2), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
movq %rdx, (%rax,%rcx,8)
movb $0x1, %al
jmp 0x145499
movq %rbp, %r15
movq (%r13,%rbx), %rbp
movl 0x8(%r13,%rbx), %eax
movq %rax, 0x8(%rsp)
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x14542f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
addq %rbp, %rbp
movabsq $0x1fffffffe, %rdx      # imm = 0x1FFFFFFFE
andq %rdx, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rbp, (%rax,%rcx,8)
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x145478
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq (%rsp), %rdi
movq %r15, %rbp
movq 0x8(%rsp), %rdx
leaq 0x1(,%rdx,2), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rdx, (%rax,%rcx,8)
xorl %eax, %eax
movb %al, 0x20(%r13,%rbx)
incq %r12
addq $0x28, %rbx
jmp 0x1452ec
movq (%rdi), %rbx
movq (%rsp), %rax
movq 0x10(%rax), %rdi
leaq (%rdi,%rbx,8), %rsi
callq 0x13d044
movq %rbx, %rsi
movq (%rsp), %rdi
xorl %r13d, %r13d
xorl %edx, %edx
xorl %ebx, %ebx
movq %rsi, 0x10(%rsp)
cmpq %r13, %rsi
je 0x1455a7
movq 0x10(%rdi), %rax
movq (%rax,%r13,8), %rbp
leaq -0x1(%rdx), %r12
leaq 0x1(%rdx), %r15
testb $0x1, %bpl
cmoveq %r15, %r12
shrq %rbp
cmpq %rbx, %rbp
jbe 0x145599
movq (%rdi), %rcx
cmpq 0x8(%rdi), %rcx
movq %rdx, 0x8(%rsp)
jne 0x145535
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 (%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
shlq $0x20, %rbx
orq %rbp, %rbx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %rbx, (%rax,%rcx,8)
movq (%rdi), %rcx
cmpq 0x8(%rdi), %rcx
jne 0x145579
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 (%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r8
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %r8, (%rax,%rcx,8)
cmpq 0x28(%rsp), %r15
jl 0x145651
incq %r13
movq %r12, %rdx
movq %rbp, %rbx
jmp 0x1454d1
movq 0x18(%rsp), %rax
cmpq %rbx, %rax
jle 0x145658
shlq $0x20, %rbx
orq %rax, %rbx
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x1455f7
movq %rdx, %r15
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 (%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
jmp 0x1455fa
movq %rdx, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %rbx, (%rax,%rcx,8)
movq (%rdi), %rcx
cmpq 0x8(%rdi), %rcx
jne 0x145637
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 (%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq 0x10(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %r15, (%rax,%rcx,8)
incq %r15
cmpq 0x28(%rsp), %r15
jge 0x145658
xorl %eax, %eax
jmp 0x1458b8
xorl %eax, %eax
cmpq (%rdi), %rsi
jae 0x145673
movq 0x10(%rdi), %rcx
movq (%rcx,%rsi,8), %rdx
movq %rdx, (%rcx,%rax,8)
incq %rax
incq %rsi
jmp 0x14565a
movq %rax, (%rdi)
movq (%r14), %r15
movq 0x10(%r14), %rdi
leaq (%rdi,%r15,8), %rsi
callq 0x13d044
xorl %r13d, %r13d
xorl %esi, %esi
xorl %ebx, %ebx
cmpq %r13, %r15
je 0x145748
movq 0x10(%r14), %rax
movq (%rax,%r13,8), %rbp
movq %rbp, %r12
shrq %r12
cmpq %rbx, %r12
jbe 0x145730
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
movq %rsi, 0x8(%rsp)
jne 0x1456e0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
shlq $0x20, %rbx
orq %r12, %rbx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rbx, (%rax,%rcx,8)
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x145720
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x8(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rsi, (%rax,%rcx,8)
andl $0x1, %ebp
negq %rbp
orq $0x1, %rbp
addq %rbp, %rsi
incq %r13
movq %r12, %rbx
jmp 0x14568d
cmpq %rbx, 0x18(%rsp)
movq (%rsp), %rdi
jle 0x1457e2
shlq $0x20, %rbx
orq 0x18(%rsp), %rbx
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x145797
movq %rsi, %r12
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0x14579a
movq %rsi, %r12
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rbx, (%rax,%rcx,8)
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x1457d3
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq (%rsp), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r12, (%rax,%rcx,8)
xorl %eax, %eax
cmpq (%r14), %r15
jae 0x1457fd
movq 0x10(%r14), %rcx
movq (%rcx,%r15,8), %rdx
movq %rdx, (%rcx,%rax,8)
incq %rax
incq %r15
jmp 0x1457e4
movq %rax, (%r14)
leaq 0x38(%rsp), %rax
andq $0x0, (%rax)
leaq 0x40(%rsp), %r13
andq $0x0, (%r13)
movq 0x20(%rsp), %rbp
movq 0x18(%rsp), %rax
cmpq %rbp, %rax
cmovlq %rax, %rbp
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpq %rax, %rbx
jge 0x1458ab
leaq 0x38(%rsp), %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0x145208
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0x145208
testq %r12, %r12
je 0x1458b3
cmpq %rax, %r12
jle 0x1458b3
addq %rbp, %r12
leaq (%r15,%r12), %rcx
addq %r15, %rax
addq %r12, %rax
cvtsi2sd %rcx, %xmm0
cvtsi2sd %rax, %xmm1
mulsd 0x30(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1458b3
movq 0x20(%rsp), %rax
addq %rax, %rbx
addq %rax, %rbp
movq 0x18(%rsp), %rcx
cmpq %rcx, %rbp
cmovgeq %rcx, %rbp
subq %rax, %r15
movq %rcx, %rax
movq (%rsp), %rdi
jmp 0x145832
pushq $0x1
popq %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq