// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
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 -0x8(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x48(%rbp), %rax
jl 0x1083dd
movq -0x48(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq -0x18(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq -0x38(%rbp), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
subq %rcx, %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x38(%rbp)
jl 0x1083eb
cmpq $0x0, -0x40(%rbp)
jge 0x1083fb
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
jmp 0x1083fd
cmpq $0x0, -0x38(%rbp)
jle 0x108482
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq -0x50(%rbp), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x20(%rbp), %rax
jg 0x108452
jmp 0x108482
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movl %eax, %ecx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1083fd
jmp 0x108484
movq -0x38(%rbp), %rax
cmpq -0x48(%rbp), %rax
jge 0x108662
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq -0x50(%rbp), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x28(%rbp), %rax
jl 0x1084e3
jmp 0x108662
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x20(%rbp), %rax
jg 0x10850c
jmp 0x108651
movq -0x68(%rbp), %rax
cmpq -0x20(%rbp), %rax
jl 0x108523
movq -0x68(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x10852e
movq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x28(%rbp), %rax
jg 0x108550
movq -0x70(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x10855b
movq -0x28(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jg 0x108575
jmp 0x108651
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jle 0x108581
jmp 0x1085a0
leaq 0x27f2dd(%rip), %rdi    # 0x387865
leaq 0x27e32e(%rip), %rsi    # 0x3868bd
movl $0x3dac, %edx           # imm = 0x3DAC
leaq 0x27f2d2(%rip), %rcx    # 0x38786d
callq 0x2130
movq -0x80(%rbp), %rax
movq -0x78(%rbp), %rcx
subq %rcx, %rax
cvtsi2sd %rax, %xmm0
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movq (%rax,%rcx,8), %xmm2
movaps 0x279a34(%rip), %xmm4   # 0x382010
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
movapd 0x279a38(%rip), %xmm3   # 0x382020
subpd %xmm3, %xmm2
movaps %xmm2, %xmm1
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
addsd %xmm1, %xmm2
movq -0x58(%rbp), %xmm1
punpckldq %xmm4, %xmm1    # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
subpd %xmm3, %xmm1
movaps %xmm1, %xmm3
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x2799ca(%rip), %xmm1   # 0x381ff8
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x80(%rbp), %rax
subq -0x78(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x108484
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, -0x60(%rbp)
je 0x108694
movq -0x58(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x60(%rbp)
movq %rax, -0x98(%rbp)
jmp 0x10869f
xorl %eax, %eax
movq %rax, -0x98(%rbp)
jmp 0x10869f
movq -0x98(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
imulq 0x18(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)