// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x88(%rbp)
cmpl $0x0, 0x10(%rbp)
je 0x170aec
movq -0x20(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x28(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x30(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $-0x1, -0x70(%rbp)
movq $-0x1, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x78(%rbp), %rax
movq -0x98(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0x170c80
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq -0x80(%rbp), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x170bbb
movq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x170bc6
movq -0x58(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x170beb
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x170bf6
movq -0x50(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rax
movq -0xb0(%rbp), %rcx
subq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jle 0x170c46
cvtsi2sdq -0x60(%rbp), %xmm1
movq 0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2sd %eax, %xmm0
movsd 0x2115f4(%rip), %xmm2   # 0x382230
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x170c58
cmpq $-0x1, -0x68(%rbp)
je 0x170c56
cmpq $-0x1, -0x70(%rbp)
je 0x170c56
jmp 0x170c80
jmp 0x170c6f
cmpq $-0x1, -0x68(%rbp)
jne 0x170c67
movq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x170b27
cmpq $-0x1, -0x68(%rbp)
je 0x170d18
cmpq $-0x1, -0x70(%rbp)
je 0x170d18
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
jg 0x170d16
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x80(%rbp)
movq 0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq 0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x170c9e
jmp 0x170d18
movq $-0x1, -0x70(%rbp)
movq $-0x1, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x78(%rbp), %rax
movq -0x98(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0x170eac
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq -0x80(%rbp), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x170de7
movq -0x48(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x170df2
movq -0x58(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x170e17
movq -0x40(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0x170e22
movq -0x50(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rcx
subq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jle 0x170e72
cvtsi2sdq -0x60(%rbp), %xmm1
movq 0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2sd %eax, %xmm0
movsd 0x2113c8(%rip), %xmm2   # 0x382230
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x170e84
cmpq $-0x1, -0x68(%rbp)
je 0x170e82
cmpq $-0x1, -0x70(%rbp)
je 0x170e82
jmp 0x170eac
jmp 0x170e9b
cmpq $-0x1, -0x68(%rbp)
jne 0x170e93
movq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x170d53
cmpq $-0x1, -0x68(%rbp)
je 0x170f44
cmpq $-0x1, -0x70(%rbp)
je 0x170f44
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
jg 0x170f42
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x80(%rbp)
movq 0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq 0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x170eca
jmp 0x170f44
cmpq $0x0, -0x90(%rbp)
jne 0x170f59
xorl %eax, %eax
movq %rax, -0xd0(%rbp)
jmp 0x170f72
movq -0x88(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x90(%rbp)
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop