// File: /workspace/github_cpp_makefile_star2_O3/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, 0x40(%rsp)
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x18(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
movq 0x90(%rsp), %rcx
xorl %eax, %eax
movq %rax, (%rsi)
movq %rax, (%rdi)
movq 0x8(%rcx), %rbx
cmpl $0x0, 0xc(%rdx)
movq %rdi, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
je 0x18a11e
xorl %r12d, %r12d
xorl %ebx, %ebx
movq %rdx, (%rsp)
movq (%rdx), %r13
cmpb $0x0, 0x20(%r13,%r12)
je 0x189f45
leaq (%r12,%r13), %rdi
movq 0x18(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq 0x98(%rsp), %rcx
movq 0xa0(%rsp), %r8
callq 0x189b65
testl %eax, %eax
je 0x189f45
movq (%r13,%r12), %r15
movl 0x8(%r13,%r12), %eax
movq %rax, 0x20(%rsp)
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0x189ed7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
addq %r15, %r15
movabsq $0x1fffffffe, %rdx      # imm = 0x1FFFFFFFE
andq %rdx, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
movq %r15, (%rax,%rcx,8)
movq (%rbp), %rcx
cmpq 0x8(%rbp), %rcx
jne 0x189f25
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
movq 0x20(%rsp), %rdx
leaq 0x1(,%rdx,2), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbp)
movq %rdx, (%rax,%rcx,8)
movb $0x1, %al
jmp 0x189fe8
movq (%r13,%r12), %rbp
movl 0x8(%r13,%r12), %r15d
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x189f83
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
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 0x189fce
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
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), %rbp
leaq 0x1(,%r15,2), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movq %rdx, (%rax,%rcx,8)
xorl %eax, %eax
movb %al, 0x20(%r13,%r12)
incq %rbx
movq (%rsp), %rdx
movl 0xc(%rdx), %eax
addq $0x28, %r12
cmpq %rax, %rbx
jb 0x189e55
movq (%rbp), %rbx
movq 0x10(%rbp), %rdi
leaq 0x10(%rbp), %r12
leaq (%rdi,%rbx,8), %rsi
callq 0x17fc92
testq %rbx, %rbx
je 0x18a137
movq %rbx, %r9
movq %r12, 0x38(%rsp)
movq 0x10(%rbp), %rax
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %rbx, 0x18(%rsp)
movq 0x8(%rsp), %rbx
movq (%rax,%r12,8), %rbp
leaq -0x1(%r13), %rsi
leaq 0x1(%r13), %rcx
testb $0x1, %bpl
cmoveq %rcx, %rsi
shrq %rbp
cmpq %r15, %rbp
jbe 0x18a100
movq %rcx, 0x20(%rsp)
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
movq %rsi, (%rsp)
jne 0x18a09a
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
shlq $0x20, %r15
orq %rbp, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r15, (%rax,%rcx,8)
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x18a0dc
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movq 0x18(%rsp), %r9
movq (%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r13, (%rax,%rcx,8)
movq 0x20(%rsp), %rcx
cmpq 0x30(%rsp), %rcx
jl 0x18a1ed
incq %r12
movq %rbp, %r15
movq %rsi, %r13
cmpq %r12, %r9
jne 0x18a041
movq 0x10(%rsp), %rbx
movq 0x38(%rsp), %r12
jmp 0x18a143
leaq 0x10(%rbp), %r12
movq 0x10(%rbp), %rdi
movq %rdi, %rsi
callq 0x17fc92
xorl %r9d, %r9d
xorl %esi, %esi
xorl %ebp, %ebp
jmp 0x18a143
xorl %r9d, %r9d
xorl %esi, %esi
xorl %ebp, %ebp
movq 0x10(%rsp), %rbx
cmpq %rbp, %rbx
jle 0x18a1f4
shlq $0x20, %rbp
orq %rbx, %rbp
movq 0x8(%rsp), %rbx
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x18a194
movq %rsi, %r13
movq %r9, %r15
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
jmp 0x18a19a
movq %rsi, %r13
movq %r9, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %rbp, (%rax,%rcx,8)
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x18a1d5
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movq %r15, %r9
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r13, (%rax,%rcx,8)
incq %r13
cmpq 0x30(%rsp), %r13
jge 0x18a1f4
xorl %eax, %eax
jmp 0x18a48e
movq 0x8(%rsp), %r8
cmpq (%r8), %r9
jae 0x18a225
movq (%r12), %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 0x18a208
jmp 0x18a227
xorl %edx, %edx
movq %rdx, (%r8)
movq (%r14), %rbx
movq 0x10(%r14), %rdi
leaq (%rdi,%rbx,8), %rsi
callq 0x17fc92
movq %rbx, %r8
testq %rbx, %rbx
movq %rbx, (%rsp)
je 0x18a309
movq 0x10(%r14), %rax
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq (%rax,%rbp,8), %r13
movq %r13, %rbx
shrq %rbx
cmpq %r15, %rbx
jbe 0x18a2ea
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x18a299
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
shlq $0x20, %r15
orq %rbx, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r15, (%rax,%rcx,8)
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x18a2db
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq (%rsp), %r8
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r12, (%rax,%rcx,8)
andl $0x1, %r13d
negq %r13
orq $0x1, %r13
addq %r13, %r12
incq %rbp
movq %rbx, %r15
cmpq %rbp, %r8
jne 0x18a256
jmp 0x18a30e
xorl %r12d, %r12d
xorl %ebx, %ebx
movq 0x10(%rsp), %r15
cmpq %rbx, %r15
jle 0x18a3a1
shlq $0x20, %rbx
orq %r15, %rbx
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x18a357
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rbx, (%rax,%rcx,8)
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x18a392
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq (%rsp), %r8
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r12, (%rax,%rcx,8)
cmpq (%r14), %r8
jae 0x18a3cd
movq 0x10(%r14), %rax
leaq (%rax,%r8,8), %rcx
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
leaq 0x1(%rsi), %rdx
movq %rdi, (%rax,%rsi,8)
leaq (%r8,%rsi), %rdi
incq %rdi
movq %rdx, %rsi
cmpq (%r14), %rdi
jb 0x18a3b0
jmp 0x18a3cf
xorl %edx, %edx
movq %rdx, (%r14)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x48(%rsp)
testq %r15, %r15
jle 0x18a481
movq 0x28(%rsp), %rbx
cmpq %rbx, %r15
cmovlq %r15, %rbx
movq $0x0, (%rsp)
leaq 0x48(%rsp), %rbp
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x189d6b
movq %rax, %r13
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x189d6b
testq %r13, %r13
je 0x18a48a
cmpq %rax, %r13
jle 0x18a48a
addq %rbx, %r13
leaq (%r15,%r13), %rcx
addq %r15, %rax
addq %r13, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x40(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x18a48a
movq 0x28(%rsp), %rax
addq %rax, %r12
addq %rax, %rbx
movq 0x10(%rsp), %rcx
cmpq %rcx, %rbx
cmovgeq %rcx, %rbx
subq %rax, %r15
cmpq %rcx, %r12
jl 0x18a406
movl $0x1, %eax
movq %rax, (%rsp)
movq (%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq