// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_pair_purge_coverage(ma_utg_t*, long long, long long, ma_utg_t*, long long, long long, unsigned int, asg_t*, hap_cov_t*)
uint64_t get_pair_purge_coverage(ma_utg_t *xReads, long long xPosBeg, long long xPosEnd, 
ma_utg_t *yReads, long long yPosBeg, long long yPosEnd, uint32_t rev, asg_t *read_g, hap_cov_t *cov)
{
    long long offset, r_beg, r_end, i_beg, i_end, ovlp, IdxBeg, IdxEnd;
    uint64_t i, rId, uCov, uLen;
    ma_utg_t *x = NULL;
    uCov = uLen = 0;
    if(rev) 
    {
        yPosBeg = yReads->len - yPosBeg - 1;
        yPosEnd = yReads->len - yPosEnd - 1;
        offset = yPosBeg; yPosBeg = yPosEnd; yPosEnd = offset;
    }
    

    IdxBeg = IdxEnd = -1; 
    x = xReads; i_beg = xPosBeg; i_end = xPosEnd;
    for (i = 0, offset = 0; i < x->n; i++)
    {
        rId = x->a[i]>>33;
        r_beg = offset; r_end = offset + (long long)(read_g->seq[rId].len) - 1;
        offset += (uint32_t)x->a[i];
        ovlp = (long long)(MIN(r_end, i_end)) - (long long)(MAX(r_beg, i_beg)) + 1;
        if(ovlp <= 0 || ovlp < read_g->seq[rId].len * 0.8)
        {
            if(IdxBeg != -1 && IdxEnd != -1) break;
            continue;
        }

        if(IdxBeg == -1) IdxBeg = i;
        IdxEnd = i;
    }
    if(IdxBeg != -1 && IdxEnd != -1)
    {
        for (i = IdxBeg; (long long)i <= IdxEnd; i++)
        {
            rId = x->a[i]>>33;
            uCov += cov->cov[rId];
            uLen += cov->read_g->seq[rId].len;
        }
    }    



    IdxBeg = IdxEnd = -1; 
    x = yReads; i_beg = yPosBeg; i_end = yPosEnd;
    for (i = 0, offset = 0; i < x->n; i++)
    {
        rId = x->a[i]>>33;
        r_beg = offset; r_end = offset + (long long)(read_g->seq[rId].len) - 1;
        offset += (uint32_t)x->a[i];
        ovlp = (long long)(MIN(r_end, i_end)) - (long long)(MAX(r_beg, i_beg)) + 1;
        if(ovlp <= 0 || ovlp < read_g->seq[rId].len * 0.8)
        {
            if(IdxBeg != -1 && IdxEnd != -1) break;
            continue;
        }

        if(IdxBeg == -1) IdxBeg = i;
        IdxEnd = i;
    }
    if(IdxBeg != -1 && IdxEnd != -1)
    {
        for (i = IdxBeg; (long long)i <= IdxEnd; i++)
        {
            rId = x->a[i]>>33;
            uCov += cov->cov[rId];
            uLen += cov->read_g->seq[rId].len;
        }
    }

    return (uLen == 0? 0 : uCov / uLen);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %r10
cmpl $0x0, 0x38(%rsp)
movq %rcx, -0x8(%rsp)
je 0xc8183
movl (%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
notq %r8
addq %rax, %r8
notq %r9
movq %r9, %rsi
addq %rax, %rsi
movq %r8, %r9
movq %rsi, %r8
movq 0x40(%rsp), %rbp
movl 0x10(%rdi), %ecx
movabsq $-0x1, %rbx
xorl %r15d, %r15d
movsd 0x11b360(%rip), %xmm0   # 0x1e3500
movq %rbx, %r14
xorl %r12d, %r12d
cmpq %r12, %rcx
je 0xc8238
movq 0x18(%rdi), %r11
movq (%r11,%r12,8), %r13
movq %rbp, %rax
movq 0x20(%rbp), %r11
movq %r13, %rbp
shrq $0x21, %rbp
movl (%r11,%rbp,8), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq (%r15,%rbp), %r11
decq %r11
cmpq %rdx, %r11
cmovgeq %rdx, %r11
cmpq %r10, %r15
movq %r10, %rsi
cmovgq %r15, %rsi
subq %rsi, %r11
js 0xc8202
incq %r11
cvtsi2sd %r11, %xmm1
cvtsi2sd %ebp, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jbe 0xc821c
cmpq $-0x1, %rbx
setne %sil
cmpq $-0x1, %r14
setne %r11b
testb %r11b, %sil
movq %rax, %rbp
je 0xc822a
jmp 0xc8238
cmpq $-0x1, %rbx
cmoveq %r12, %rbx
movq %r12, %r14
movq %rax, %rbp
movl %r13d, %esi
addq %rsi, %r15
incq %r12
jmp 0xc81a6
cmpq $-0x1, %rbx
setne %al
cmpq $-0x1, %r14
setne %dl
andb %al, %dl
xorl %eax, %eax
xorl %esi, %esi
cmpb $0x1, %dl
jne 0xc8291
movq 0x48(%rsp), %r15
movq -0x8(%rsp), %rcx
cmpq %r14, %rbx
jg 0xc8296
movq 0x18(%rdi), %rdx
movq (%rdx,%rbx,8), %rdx
shrq $0x21, %rdx
movq 0x8(%r15), %r10
movq 0x30(%r15), %r11
movl (%r10,%rdx,4), %r10d
addq %r10, %rax
movq 0x20(%r11), %r10
movl (%r10,%rdx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq %rdx, %rsi
incq %rbx
jmp 0xc825b
movq -0x8(%rsp), %rcx
movl 0x10(%rcx), %ebx
movabsq $-0x1, %rdx
xorl %r14d, %r14d
movq %rdx, %rdi
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0xc8337
movq 0x18(%rcx), %r10
movq (%r10,%r15,8), %r12
movq 0x20(%rbp), %r10
movq %r12, %r11
shrq $0x21, %r11
movl (%r10,%r11,8), %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq (%r14,%r13), %r11
decq %r11
cmpq %r9, %r11
cmovgeq %r9, %r11
cmpq %r8, %r14
movq %r8, %r10
cmovgq %r14, %r10
subq %r10, %r11
js 0xc8307
incq %r11
cvtsi2sd %r11, %xmm1
cvtsi2sd %r13d, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jbe 0xc831e
cmpq $-0x1, %rdx
setne %r10b
cmpq $-0x1, %rdi
setne %r11b
testb %r11b, %r10b
je 0xc8329
jmp 0xc8337
cmpq $-0x1, %rdx
cmoveq %r15, %rdx
movq %r15, %rdi
movl %r12d, %r10d
addq %r10, %r14
incq %r15
jmp 0xc82ac
cmpq $-0x1, %rdx
setne %r8b
cmpq $-0x1, %rdi
setne %r9b
andb %r8b, %r9b
cmpb $0x1, %r9b
jne 0xc838c
movq 0x48(%rsp), %r11
cmpq %rdi, %rdx
jg 0xc838c
movq 0x18(%rcx), %r8
movq (%r8,%rdx,8), %r8
shrq $0x21, %r8
movq 0x8(%r11), %r9
movq 0x30(%r11), %r10
movl (%r9,%r8,4), %r9d
addq %r9, %rax
movq 0x20(%r10), %r9
movl (%r9,%r8,8), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
addq %r8, %rsi
incq %rdx
jmp 0xc8355
testq %rsi, %rsi
je 0xc8398
xorl %edx, %edx
divq %rsi
jmp 0xc839a
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq