// File: /workspace/github_cpp_makefile_star2_O3/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, %r11
movq %rsi, -0x8(%rsp)
movq 0x38(%rsp), %rcx
movl 0x10(%rdi), %esi
movq %r9, -0x10(%rsp)
movq (%r9), %rax
cmpq %rsi, %rax
jge 0xc7172
movq (%rcx), %r15
jmp 0xc7190
leaq -0x1(%rsi), %rax
movl (%rdi), %r15d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andq %r10, %r15
movq 0x20(%rdx), %rbx
movl -0x8(%rbx,%rsi,8), %ebx
andq %r10, %rbx
subq %rbx, %r15
movq %rcx, %r9
xorl %ebx, %ebx
movq %rax, %r10
orq %r15, %r10
cmovsq %rbx, %r15
cmovnsq %rax, %rbx
testq %rbx, %rbx
jle 0xc71e8
movq 0x18(%rdi), %r14
movq 0x20(%rdx), %r12
xorl %ebx, %ebx
movq (%r14,%rax,8), %r10
movq %r10, %r13
shrq $0x21, %r13
movl (%r12,%r13,8), %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
addq %r15, %r13
cmpq %r11, %r13
jle 0xc71e5
movl %r10d, %r10d
subq %r10, %r15
leaq -0x1(%rax), %r10
cmpq $0x1, %rax
movq %r10, %rax
jg 0xc71b2
jmp 0xc71e8
movq %rax, %rbx
movq 0x40(%rsp), %rcx
cmpq %rsi, %rbx
jge 0xc7304
movq 0x18(%rdi), %r12
movq 0x20(%rdx), %rdx
xorl %edi, %edi
movsd 0x16a078(%rip), %xmm0   # 0x231280
movapd 0x16a080(%rip), %xmm1   # 0x231290
movsd 0x16a0f0(%rip), %xmm2   # 0x231308
xorl %eax, %eax
movq %r15, %r13
cmpq %r8, %r15
jge 0xc72e8
movq (%r12,%rbx,8), %r10
movq %r10, %r15
shrq $0x21, %r15
movl (%rdx,%r15,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
leaq (%r14,%r13), %rbp
cmpq %r11, %rbp
jle 0xc72d0
cmpq %r11, %r13
movq %r11, %rcx
cmovgq %r13, %rcx
cmpq %r8, %rbp
cmovgeq %r8, %rbp
subq %rcx, %rbp
jle 0xc72d0
xorps %xmm3, %xmm3
cvtsi2sd %rbp, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %r14d, %xmm4
divsd %xmm4, %xmm3
movq -0x8(%rsp), %rcx
movsd (%rcx,%r15,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, %r14
subsd %xmm2, %xmm4
cvttsd2si %xmm4, %rax
sarq $0x3f, %r14
andq %r14, %rax
orq %rcx, %rax
addq %rbp, %rdi
movl %r10d, %r15d
addq %r13, %r15
incq %rbx
cmpq %rbx, %rsi
jne 0xc721a
movq %r15, %r13
movq %rsi, %rbx
movq -0x10(%rsp), %rcx
movq %rbx, (%rcx)
movq %r13, (%r9)
testq %rdi, %rdi
movq 0x40(%rsp), %rcx
je 0xc730f
xorl %edx, %edx
divq %rdi
jmp 0xc7311
movq -0x10(%rsp), %rax
movq %rbx, (%rax)
movq %r15, (%r9)
xorl %eax, %eax
imulq %rcx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq