// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_ovlp_cov(ma_utg_t*, unsigned long*, asg_t*, long, long, long*, long*, unsigned long)
uint64_t get_ovlp_cov(ma_utg_t *in, uint64_t *cc, asg_t *sg, int64_t s, int64_t e, int64_t *itk, int64_t *itl, uint64_t reflen)
{
    int64_t k, l, n = in->n; uint64_t rid, o, ol; int64_t qs, qe, os, oe;
    k = (*itk); l = (*itl); o = ol = 0;
    if(k >= n) {
        k = n-1; l = ((int64_t)(in->len))-((int64_t)(sg->seq[k].len));
    }
    if(k < 0 || l < 0) {
        k = 0; l = 0;
    }    
    

    for (; k > 0; k--) {
        rid = in->a[k]>>33;
        qs = l; qe = l + sg->seq[rid].len;
        if(qe <= s) break;
        l -= (int64_t)((uint32_t)in->a[k]);
    }
    
    for (; k < n; k++) {
        rid = in->a[k]>>33;
        qs = l; qe = l + sg->seq[rid].len;
        if(qs >= e) break;
        l += (uint32_t)in->a[k];
        if(qe <= s) continue;

        os = MAX(qs, s); oe = MIN(qe, e);
        if(oe <= os) continue;
        assert(oe > os);
        o += (((double)(oe-os))/((double)(sg->seq[rid].len)))*(cc[rid]);
        ol += oe - os;
    }

    (*itk) = k; (*itl) = l;
    o = ((ol)?(o/ol):(0));
    return o*reflen;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, %r12
movq %rsi, -0x8(%rsp)
movq 0x38(%rsp), %rcx
movl 0x10(%rdi), %ebx
movq %r9, -0x10(%rsp)
movq (%r9), %rax
cmpq %rbx, %rax
jge 0x8ac43
movq (%rcx), %r11
jmp 0x8ac62
leaq -0x1(%rbx), %rax
movl (%rdi), %r11d
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andq %r9, %r11
movq 0x20(%rdx), %r10
movl -0x8(%r10,%rbx,8), %r10d
andq %r9, %r10
subq %r10, %r11
xorl %r9d, %r9d
movq %rax, %r10
orq %r11, %r10
cmovsq %r9, %r11
cmovsq %r9, %rax
movq %rax, %r15
sarq $0x3f, %r15
andq %rax, %r15
testq %rax, %rax
jle 0x8acb6
movq 0x18(%rdi), %r9
movq (%r9,%rax,8), %r9
movq 0x20(%rdx), %r10
movq %r9, %r14
shrq $0x21, %r14
movl (%r10,%r14,8), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
addq %r11, %r10
cmpq %r12, %r10
jle 0x8acb3
movl %r9d, %r9d
subq %r9, %r11
decq %rax
jmp 0x8ac7d
movq %rax, %r15
cmpq %rbx, %r15
movq %rbx, %rsi
cmovgq %r15, %rsi
xorl %eax, %eax
movsd 0x1585a6(%rip), %xmm0   # 0x1e3270
movapd 0x1585ae(%rip), %xmm1   # 0x1e3280
movsd 0x1585be(%rip), %xmm2   # 0x1e3298
xorl %r9d, %r9d
cmpq %rbx, %r15
jge 0x8adab
cmpq %r8, %r11
jge 0x8ada8
movq 0x18(%rdi), %r10
movq (%r10,%r15,8), %r10
movq %r10, %rbp
shrq $0x21, %rbp
movq 0x20(%rdx), %r14
movl (%r14,%rbp,8), %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq (%r11,%r13), %r14
cmpq %r12, %r14
jle 0x8ad9a
cmpq %r12, %r11
movq %r12, %rcx
cmovgq %r11, %rcx
cmpq %r8, %r14
cmovgeq %r8, %r14
subq %rcx, %r14
jle 0x8ad9a
cvtsi2sd %r14, %xmm3
cvtsi2sd %r13d, %xmm4
divsd %xmm4, %xmm3
movq -0x8(%rsp), %rcx
movsd (%rcx,%rbp,8), %xmm4
unpcklps %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0],xmm4[1],xmm0[1]
subpd %xmm1, %xmm4
movapd %xmm4, %xmm5
unpckhpd %xmm4, %xmm5            # xmm5 = xmm5[1],xmm4[1]
addsd %xmm4, %xmm5
mulsd %xmm3, %xmm5
movq %rax, %xmm3
punpckldq %xmm0, %xmm3    # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
subpd %xmm1, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
addsd %xmm5, %xmm4
cvttsd2si %xmm4, %rcx
movq %rcx, %r13
subsd %xmm2, %xmm4
cvttsd2si %xmm4, %rax
sarq $0x3f, %r13
andq %r13, %rax
orq %rcx, %rax
addq %r14, %r9
movl %r10d, %ecx
addq %rcx, %r11
incq %r15
jmp 0x8acdd
movq %r15, %rsi
movq -0x10(%rsp), %rcx
movq %rsi, (%rcx)
movq 0x38(%rsp), %rcx
movq %r11, (%rcx)
testq %r9, %r9
je 0x8adc7
xorl %edx, %edx
divq %r9
jmp 0x8adc9
xorl %eax, %eax
imulq 0x40(%rsp), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq