// File: /workspace/github_cpp_makefile_star2_O2/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, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r13
movq %rdi, 0x10(%rsp)
movl 0xa0(%rsp), %r11d
movq 0xb0(%rsp), %r10
movl 0xa8(%rsp), %r14d
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
movq %r9, (%rsp)
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
cmpq %r11, %rbp
je 0x1181fb
movq (%r13), %rdx
movq 0x80(%r13), %rcx
movl (%rdx,%r14,4), %eax
incq %rcx
cmpq %rax, %rcx
jb 0x1181f3
imulq $0x30, %rbp, %r15
movl 0x14(%r9,%r15), %eax
movl (%rdx,%rax,4), %edx
cmpq %rdx, %rcx
jb 0x1181f3
cmpl %r14d, %eax
je 0x117fec
movq 0x28(%r10), %rcx
movq (%rcx,%r14,8), %rdx
movq (%rcx,%rax,8), %rdi
cmpl %edx, %edi
movl %edx, %esi
cmovbl %edi, %esi
movl %eax, %ecx
cmovbl %r14d, %ecx
cmovbq %rdi, %rdx
testl %esi, %esi
je 0x1181f3
movq 0x10(%r10), %rdi
shrq $0x20, %rdx
movl %esi, %esi
imulq $0x30, %rsi, %rsi
addq $0x30, %rsi
imulq $0x30, %rdx, %rdx
addq %rdi, %rdx
addq $0x14, %rdx
xorl %edi, %edi
leaq 0x30(%rdi), %r8
cmpq %r8, %rsi
je 0x1181f3
cmpl %ecx, (%rdx,%rdi)
movq %r8, %rdi
jne 0x117fd7
movsd %xmm0, 0x8(%rsp)
addq %r9, %r15
movq 0x18(%rsp), %r9
movq 0x28(%r9), %rcx
movq 0x40(%r9), %rdx
movsd (%rdx,%r14,8), %xmm0
movsd 0xcb260(%rip), %xmm1    # 0x1e3270
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xcb265(%rip), %xmm2    # 0x1e3280
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movapd %xmm3, 0x40(%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(%r15), %r10d
movl 0x10(%r15), %r11d
movl %r11d, %edx
subl %r10d, %edx
cvtsi2sd %rdx, %xmm2
addsd %xmm0, %xmm1
movq 0x10(%rsp), %rdi
movq (%rdi), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rax,8), %esi
andl %ebx, %esi
cvtsi2sd %esi, %xmm0
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd 0xcb21b(%rip), %xmm0    # 0x1e32a0
maxsd %xmm2, %xmm0
movapd %xmm0, 0x30(%rsp)
movq (%rcx,%r14,8), %r8
movq %r8, %rcx
shrq $0x1b, %rcx
andq $-0x20, %rcx
addq 0x10(%r9), %rcx
movl (%rdx,%r14,8), %r12d
andl %ebx, %r12d
subq $0x8, %rsp
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl %r14d, %r9d
pushq %r11
pushq %r10
pushq %rax
pushq %r12
pushq $0x0
callq 0x117c37
movapd 0x60(%rsp), %xmm3
movapd 0x70(%rsp), %xmm2
addq $0x30, %rsp
ucomisd 0xcb488(%rip), %xmm0    # 0x1e3568
movapd %xmm2, %xmm5
cmplesd %xmm3, %xmm5
jne 0x1180ed
jnp 0x118105
movapd %xmm5, %xmm1
andpd %xmm2, %xmm1
movapd %xmm5, %xmm2
andnpd %xmm3, %xmm2
orpd %xmm1, %xmm2
divsd %xmm2, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movl 0x14(%r15), %r9d
cmpl %r14d, %r9d
jne 0x118138
movl 0xa0(%rsp), %r9d
movq %r9, %r11
movq 0xb0(%rsp), %r10
movq (%rsp), %r9
jmp 0x1181f3
movapd %xmm5, 0x50(%rsp)
movsd %xmm0, 0x8(%rsp)
movq 0x18(%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(%r15), %eax
movl 0x10(%r15), %r10d
movq 0x10(%rsp), %rdi
movq (%rdi), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%r14,8), %r11d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %r11d
subq $0x8, %rsp
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
pushq %r11
pushq $0x0
pushq %r14
pushq %r10
pushq %rax
callq 0x117c37
addq $0x30, %rsp
ucomisd 0xcb3bc(%rip), %xmm0    # 0x1e3568
jne 0x1181b0
jnp 0x1181ce
movapd 0x40(%rsp), %xmm1
movapd 0x50(%rsp), %xmm2
andpd %xmm2, %xmm1
andnpd 0x30(%rsp), %xmm2
orpd %xmm1, %xmm2
divsd %xmm2, %xmm0
movl 0xa0(%rsp), %r9d
movq %r9, %r11
movq 0xb0(%rsp), %r10
movq (%rsp), %r9
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
incq %rbp
jmp 0x117f55
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq