// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: get_hits_weight(ha_ug_index*, bubble_type*, kvec_pe_hit*, hc_links*, trans_idx*, u_trans_t*, unsigned int, unsigned int, kv_u_trans_t*)
double get_hits_weight(ha_ug_index* idx, bubble_type* bub, kvec_pe_hit* hits, hc_links* link, trans_idx* dis, 
u_trans_t *t_a, uint32_t t_n, uint32_t qid, kv_u_trans_t *ta_idx)
{
    /****************************may have bugs********************************/
    ///need to record self hits
    ///if(u_trans_n(*ta_idx, qid) == 0) return 0;///no hit bridging qid
    /****************************may have bugs********************************/
    uint32_t k, i, x_n, y;
    u_trans_t *x_a = NULL;
    double occ_q, occ_t;
    double w, i_w;
    for (k = 0, w = 0; k < t_n; k++)
    {
        if(IF_HOM(qid, *bub)) continue;
        if(IF_HOM(t_a[k].tn, *bub)) continue;
        /****************************may have bugs********************************/
        if(qid != t_a[k].tn)
        {
            x_n = u_trans_n(*ta_idx, qid); x_a = u_trans_a(*ta_idx, qid); y = t_a[k].tn;
            if(u_trans_n(*ta_idx, t_a[k].tn) < x_n)
            {
                x_n = u_trans_n(*ta_idx, t_a[k].tn); x_a = u_trans_a(*ta_idx, t_a[k].tn); y = qid;
            }
            if(x_n == 0) continue; 

            for (i = 0; i < x_n; i++)
            {
                if(x_a[i].tn == y) break;
            }
            if(i >= x_n) continue; ///no hit bridging tn and qid
        }
        /****************************may have bugs********************************/
        occ_q = hits->occ.a[qid];
        occ_t = hits->occ.a[t_a[k].tn] * ((double)(t_a[k].te - t_a[k].ts) / (double)(idx->ug->g->seq[t_a[k].tn].len));
        if(occ_t < 1) occ_t = 1;

        ///q--->t hits
        i_w = get_interval_weight(idx, link, dis, hits->a.a + (hits->idx.a[qid]>>32),
        (uint32_t)(hits->idx.a[qid]), qid, 0, idx->ug->g->seq[qid].len, t_a[k].tn, 
        t_a[k].ts, t_a[k].te);
        if(i_w != 0) i_w /= (MIN(occ_q, occ_t));
        w += i_w;
        if(t_a[k].tn == qid) continue;

        ///t--->q hits
        i_w = get_interval_weight(idx, link, dis, hits->a.a + (hits->idx.a[t_a[k].tn]>>32),
        (uint32_t)(hits->idx.a[t_a[k].tn]), t_a[k].tn, t_a[k].ts, t_a[k].te, 
        qid, 0, idx->ug->g->seq[qid].len);    
        if(i_w != 0) i_w /= (MIN(occ_q, occ_t));
        w += i_w;
    }
    return w;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movl 0xa0(%rsp), %eax
testl %eax, %eax
je 0x15ecf7
movq %rsi, %r13
movq 0xb0(%rsp), %r8
movl 0xa8(%rsp), %r12d
movl %r12d, %r15d
movl %eax, %r10d
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
movq %r9, 0x10(%rsp)
movq %r10, 0x8(%rsp)
movq (%r13), %rdx
movq 0x80(%r13), %rcx
movl (%rdx,%r15,4), %eax
incq %rcx
cmpq %rax, %rcx
jb 0x15ece9
leaq (,%rbp,2), %rbx
addq %rbp, %rbx
shlq $0x4, %rbx
movl 0x14(%r9,%rbx), %eax
movl (%rdx,%rax,4), %edx
cmpq %rdx, %rcx
jb 0x15ece9
cmpl %r12d, %eax
jne 0x15ebd4
movsd %xmm0, 0x18(%rsp)
addq %r9, %rbx
movq 0x28(%rsp), %r9
movq 0x28(%r9), %rcx
movq 0x40(%r9), %rdx
movsd (%rdx,%r15,8), %xmm0
movsd 0xd27de(%rip), %xmm1    # 0x231280
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xd27e3(%rip), %xmm2    # 0x231290
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movapd %xmm3, 0x50(%rsp)
movsd (%rdx,%rax,8), %xmm0
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
movl 0xc(%rbx), %r10d
movl 0x10(%rbx), %r11d
movl %r11d, %edx
subl %r10d, %edx
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
addsd %xmm0, %xmm1
movq 0x20(%rsp), %rdi
movq (%rdi), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rax,8), %esi
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
andl %r14d, %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd 0xd27ec(%rip), %xmm0    # 0x231310
maxsd %xmm2, %xmm0
movapd %xmm0, 0x40(%rsp)
movq (%rcx,%r15,8), %r8
movq %r8, %rcx
shrq $0x1b, %rcx
andq $-0x20, %rcx
addq 0x10(%r9), %rcx
movq %r12, %r9
movl (%rdx,%r15,8), %r12d
andl %r14d, %r12d
subq $0x8, %rsp
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r9, %r14
pushq %r11
pushq %r10
pushq %rax
pushq %r12
pushq $0x0
callq 0x15e32a
addq $0x30, %rsp
ucomisd 0xd2aa2(%rip), %xmm0    # 0x231618
jne 0x15eb7a
jnp 0x15eba3
movapd 0x50(%rsp), %xmm3
movapd %xmm3, %xmm1
movapd 0x40(%rsp), %xmm4
cmplesd %xmm4, %xmm1
movapd %xmm1, %xmm2
andpd %xmm3, %xmm2
andnpd %xmm4, %xmm1
orpd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movl 0x14(%rbx), %r9d
movq %r14, %r12
cmpl %r12d, %r9d
jne 0x15ec33
movq 0x10(%rsp), %r9
movq 0xb0(%rsp), %r8
movq 0x8(%rsp), %r10
jmp 0x15ece9
movq 0x28(%r8), %rcx
movq (%rcx,%r15,8), %rdx
movq (%rcx,%rax,8), %rdi
cmpl %edx, %edi
movl %edx, %esi
cmovbl %edi, %esi
movl %eax, %ecx
cmovbl %r12d, %ecx
cmovbq %rdi, %rdx
testl %esi, %esi
je 0x15ece9
movq 0x10(%r8), %rdi
shrq $0x20, %rdx
movl %esi, %esi
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
addq %rdi, %rdx
addq $0x14, %rdx
shlq $0x4, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
cmpl %ecx, (%rdx,%rdi)
je 0x15ea7e
addq $0x30, %rdi
cmpq %rdi, %rsi
jne 0x15ec1c
jmp 0x15ece9
movsd %xmm0, 0x18(%rsp)
movq 0x28(%rsp), %rdx
movq 0x28(%rdx), %rax
movq (%rax,%r9,8), %r8
movq %r8, %rcx
shrq $0x1b, %rcx
andq $-0x20, %rcx
addq 0x10(%rdx), %rcx
movl 0xc(%rbx), %eax
movl 0x10(%rbx), %r10d
movq 0x20(%rsp), %rdi
movq (%rdi), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%r15,8), %r11d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %r11d
subq $0x8, %rsp
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
pushq %r11
pushq $0x0
pushq %r12
pushq %r10
pushq %rax
callq 0x15e32a
addq $0x30, %rsp
ucomisd 0xd2978(%rip), %xmm0    # 0x231618
jne 0x15eca4
jnp 0x15ecc9
movapd 0x50(%rsp), %xmm2
movapd %xmm2, %xmm1
movapd 0x40(%rsp), %xmm3
cmplesd %xmm3, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm3, %xmm1
orpd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq 0x10(%rsp), %r9
movq 0xb0(%rsp), %r8
movq 0x8(%rsp), %r10
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
incq %rbp
cmpq %r10, %rbp
jne 0x15ea3a
jmp 0x15ecfb
xorpd %xmm0, %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq