// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rsi, -0x8(%rsp)
movq 0x38(%rsp), %rax
movl 0x10(%rdi), %esi
movq %r9, -0x10(%rsp)
movq (%r9), %r11
cmpq %rsi, %r11
jge 0xc63ca
movq %rax, %r9
movq (%rax), %r14
jmp 0xc63eb
movq %rax, %r9
leaq -0x1(%rsi), %r11
movl (%rdi), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r14
movq 0x20(%rdx), %r10
movl -0x8(%r10,%rsi,8), %r10d
andq %rax, %r10
subq %r10, %r14
xorl %eax, %eax
movq %r11, %r10
orq %r14, %r10
cmovsq %rax, %r14
cmovsq %rax, %r11
testq %r11, %r11
jle 0xc643c
movq 0x18(%rdi), %rax
movq 0x20(%rdx), %r15
movq (%rax,%r11,8), %r10
movq %r10, %r12
shrq $0x21, %r12
movl (%r15,%r12,8), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
addq %r14, %r12
cmpq %rbx, %r12
jle 0xc643c
movl %r10d, %r10d
subq %r10, %r14
leaq -0x1(%r11), %r10
cmpq $0x1, %r11
movq %r10, %r11
jg 0xc6408
xorl %r11d, %r11d
cmpq %rsi, %r11
jge 0xc6535
movq 0x18(%rdi), %r15
movq 0x20(%rdx), %rdx
xorl %edi, %edi
movsd 0x15ede9(%rip), %xmm0   # 0x225240
movapd 0x15edf1(%rip), %xmm1   # 0x225250
movsd 0x15ee01(%rip), %xmm2   # 0x225268
xorl %eax, %eax
movq %r14, %r12
cmpq %r8, %r14
jge 0xc653b
movq (%r15,%r11,8), %r14
movq %r14, %rbp
shrq $0x21, %rbp
movl (%rdx,%rbp,8), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
leaq (%r12,%r10), %r13
cmpq %rbx, %r13
jle 0xc651e
cmpq %rbx, %r12
movq %rbx, %rcx
cmovgq %r12, %rcx
cmpq %r8, %r13
cmovgeq %r8, %r13
subq %rcx, %r13
jle 0xc651e
xorps %xmm3, %xmm3
cvtsi2sd %r13, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %r10d, %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, %r10
subsd %xmm2, %xmm4
cvttsd2si %xmm4, %rax
sarq $0x3f, %r10
andq %r10, %rax
orq %rcx, %rax
addq %r13, %rdi
movl %r14d, %r14d
addq %r12, %r14
incq %r11
cmpq %r11, %rsi
jne 0xc6469
movq %rsi, %r11
jmp 0xc653e
xorl %eax, %eax
xorl %edi, %edi
jmp 0xc653e
movq %r12, %r14
movq 0x40(%rsp), %rcx
movq -0x10(%rsp), %rdx
movq %r11, (%rdx)
movq %r14, (%r9)
testq %rdi, %rdi
je 0xc655a
xorl %edx, %edx
divq %rdi
jmp 0xc655c
xorl %eax, %eax
imulq %rcx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq