// File: chhylp123[P]hifiasm/hic.cpp
// Function: get_interval_weight(ha_ug_index*, hc_links*, trans_idx*, pe_hit*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)
double get_interval_weight(ha_ug_index* idx, hc_links* link, trans_idx* dis, 
pe_hit *hits, uint32_t occ, uint32_t qid, uint32_t qs, uint32_t qe, uint32_t tid, uint32_t ts, uint32_t te)
{
    int64_t s_idx = 0, e_idx = (int64_t)occ - 1, m_idx = 0;
    uint32_t m_uid = (uint32_t)-1;
    while (s_idx <= e_idx)
    {
        m_idx = s_idx + (e_idx - s_idx)/2;
        m_uid = ((hits[m_idx].e<<1)>>(64 - idx->uID_bits)); 
        if (m_uid == tid)
            break;
        if (m_uid < tid)
            s_idx = m_idx + 1;
        else
            e_idx = m_idx - 1;
    }
    if(m_uid != tid) return 0;
    

    uint32_t k, s_uid, s_beg, s_end, e_uid, e_beg, e_end;
    uint64_t t_d;
    double w, weight;
    w = 0;
    for (k = m_idx; k < occ; k++)///all hits of qid
    {   
        interpr_hit(idx, hits[k].s, hits[k].len>>32, &s_uid, &s_beg, &s_end);
        if(s_uid != qid) continue;
        if(!(qs <= s_beg && qe >= s_end)) continue;

        interpr_hit(idx, hits[k].e, (uint32_t)hits[k].len, &e_uid, &e_beg, &e_end);
        if(e_uid != tid) break;
        if(!(ts <= e_beg && te >= e_end)) continue;

        t_d = get_hic_distance(&hits[k], link, idx, NULL);
        if(t_d == (uint64_t)-1) continue;

        weight = 1;
        if(dis) weight = get_trans_weight_advance(idx, t_d, dis);

        w += weight;
    }

    for (m_idx -= 1; m_idx >= 0; m_idx--)
    {
        k = m_idx;
        interpr_hit(idx, hits[k].s, hits[k].len>>32, &s_uid, &s_beg, &s_end);
        if(s_uid != qid) continue;
        if(!(qs <= s_beg && qe >= s_end)) continue;

        interpr_hit(idx, hits[k].e, (uint32_t)hits[k].len, &e_uid, &e_beg, &e_end);
        if(e_uid != tid) break;
        if(!(ts <= e_beg && te >= e_end)) continue;

        t_d = get_hic_distance(&hits[k], link, idx, NULL);
        if(t_d == (uint64_t)-1) continue;

        weight = 1;
        if(dis) weight = get_trans_weight_advance(idx, t_d, dis);

        w += weight;
    }
    return w;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl 0x30(%rbp), %eax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl -0x2c(%rbp), %eax
subq $0x1, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jg 0x20217a
movq -0x38(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x40(%rbp), %rax
subq -0x38(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x88(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq -0x10(%rbp), %rdx
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shrq %cl, %rax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x20(%rbp), %eax
jne 0x202153
jmp 0x20217a
movl -0x4c(%rbp), %eax
cmpl 0x20(%rbp), %eax
jae 0x202169
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x202175
movq -0x48(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2020e4
movl -0x4c(%rbp), %eax
cmpl 0x20(%rbp), %eax
je 0x20218f
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x20242f
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x48(%rbp), %rax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x2022d7
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
movl %eax, %edx
leaq -0x54(%rbp), %rcx
leaq -0x58(%rbp), %r8
leaq -0x5c(%rbp), %r9
callq 0x201880
movl -0x54(%rbp), %eax
cmpl -0x30(%rbp), %eax
je 0x2021f5
jmp 0x2022c9
movl 0x10(%rbp), %eax
cmpl -0x58(%rbp), %eax
ja 0x202205
movl 0x18(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jae 0x20220a
jmp 0x2022c9
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq -0x60(%rbp), %rcx
leaq -0x64(%rbp), %r8
leaq -0x68(%rbp), %r9
callq 0x201880
movl -0x60(%rbp), %eax
cmpl 0x20(%rbp), %eax
je 0x202252
jmp 0x2022d7
movl 0x28(%rbp), %eax
cmpl -0x64(%rbp), %eax
ja 0x202262
movl 0x30(%rbp), %eax
cmpl -0x68(%rbp), %eax
jae 0x202264
jmp 0x2022c9
movq -0x28(%rbp), %rdi
movl -0x50(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1db730
movq %rax, -0x70(%rbp)
cmpq $-0x1, -0x70(%rbp)
jne 0x202290
jmp 0x2022c9
movsd 0x17fd68(%rip), %xmm0   # 0x382000
movsd %xmm0, -0x80(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2022ba
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x2148a0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
addsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x20219e
movq -0x48(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jl 0x202425
movq -0x48(%rbp), %rax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
movl %eax, %edx
leaq -0x54(%rbp), %rcx
leaq -0x58(%rbp), %r8
leaq -0x5c(%rbp), %r9
callq 0x201880
movl -0x54(%rbp), %eax
cmpl -0x30(%rbp), %eax
je 0x202340
jmp 0x202414
movl 0x10(%rbp), %eax
cmpl -0x58(%rbp), %eax
ja 0x202350
movl 0x18(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jae 0x202355
jmp 0x202414
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq -0x60(%rbp), %rcx
leaq -0x64(%rbp), %r8
leaq -0x68(%rbp), %r9
callq 0x201880
movl -0x60(%rbp), %eax
cmpl 0x20(%rbp), %eax
je 0x20239d
jmp 0x202425
movl 0x28(%rbp), %eax
cmpl -0x64(%rbp), %eax
ja 0x2023ad
movl 0x30(%rbp), %eax
cmpl -0x68(%rbp), %eax
jae 0x2023af
jmp 0x202414
movq -0x28(%rbp), %rdi
movl -0x50(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1db730
movq %rax, -0x70(%rbp)
cmpq $-0x1, -0x70(%rbp)
jne 0x2023db
jmp 0x202414
movsd 0x17fc1d(%rip), %xmm0   # 0x382000
movsd %xmm0, -0x80(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x202405
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x2148a0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
addsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2022e3
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)