// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: adjust_weight_kv_u_trans(ha_ug_index*, kvec_pe_hit*, hc_links*, bubble_type*, kv_u_trans_t*, kv_u_trans_t*, trans_idx*)
void adjust_weight_kv_u_trans(ha_ug_index* idx, kvec_pe_hit* hits, hc_links* link, bubble_type* bub, 
kv_u_trans_t *ta, kv_u_trans_t *ref, trans_idx* dis)
{
    u_trans_t *a = NULL, *p = NULL;
    uint32_t n, k, i, m, qn, tn;
    uint8_t *vis = NULL; CALLOC(vis, idx->ug->g->n_seq);
    double w;

    for (i = m = 0; i < ta->n; i++)
    {
        if(ta->a[i].del) continue;
        if(IF_HOM(ta->a[i].qn, *bub)) continue;
        if(IF_HOM(ta->a[i].tn, *bub)) continue;
        ta->a[m] = ta->a[i];
        if(ta->a[m].nw != 0)
        {
            ta->a[m].nw /= (double)(MIN(hits->occ.a[ta->a[m].qn], hits->occ.a[ta->a[m].tn]));
        }
        /*******************************for debug************************************/
        // trans_checking_pass(bub, ref, ta->a[i].qn, ta->a[i].tn);
        // if(trans_checking_pass(bub, ref, ta->a[i].qn, ta->a[i].tn)) ta->a[m].nw = 0;
        /*******************************for debug************************************/
        
        m++;
    }
    ta->n = m;
    
    for (k = 0; k < ta->idx.n; k++)///all nodes
    {
        if(IF_HOM(k, *bub)) continue;
        a = u_trans_a(*ta, k);
        n = u_trans_n(*ta, k);
        ///for each pair qn, tn
        ///count hic pairs between (qn, tn^) and (qn^1, tn)
        for (i = 0, vis[k] = 1; i < n; i++)
        {
            vis[a[i].tn] = 1;
            if(a[i].qn == a[i].tn) continue;
            if(IF_HOM(a[i].qn, *bub)) continue;
            if(IF_HOM(a[i].tn, *bub)) continue;
            ///(qn, tn^)
            a[i].nw += get_hits_weight(idx, bub, hits, link, dis, 
                                    u_trans_a(*ref, a[i].tn), u_trans_n(*ref, a[i].tn), a[i].qn, ta);
            ///(qn^1, tn)
            a[i].nw += get_hits_weight(idx, bub, hits, link, dis, 
                                    u_trans_a(*ref, a[i].qn), u_trans_n(*ref, a[i].qn), a[i].tn, ta);
        }
        
        
        for (i = 0; i < ta->idx.n; i++)///all edges
        {
            qn = k; tn = i; w = 0;
            if(IF_HOM(qn, *bub)) continue;
            if(IF_HOM(tn, *bub)) continue;
            if(vis[tn]) continue;
            if(qn == tn) continue;
            ///(qn, tn^)
            w += get_hits_weight(idx, bub, hits, link, dis, 
                                u_trans_a(*ref, tn), u_trans_n(*ref, tn), qn, ta);
            ///(qn^1, tn)
            w += get_hits_weight(idx, bub, hits, link, dis, 
                                u_trans_a(*ref, qn), u_trans_n(*ref, qn), tn, ta);
            if(w == 0) continue;
            kv_pushp(u_trans_t, *ta, &p);
            memset(p, 0, sizeof(u_trans_t));///extra edges
            p->nw = w; p->qn = qn; p->tn = tn;
        }

        // for (i = 0; i < u_trans_n(*ref, k); i++)///only trans edges
        // {
        //     qn = k; tn = (u_trans_a(*ref, k))[i].tn; w = 0;
        //     if(IF_HOM(qn, *bub)) continue;
        //     if(IF_HOM(tn, *bub)) continue;
        //     if(vis[tn]) continue;
        //     if(qn == tn) continue;
        //     ///(qn, tn^)
        //     w += get_hits_weight(idx, hits, link, dis, 
        //                         u_trans_a(*ref, tn), u_trans_n(*ref, tn), qn, ta);
        //     ///(qn^1, tn)
        //     w += get_hits_weight(idx, hits, link, dis, 
        //                         u_trans_a(*ref, qn), u_trans_n(*ref, qn), tn, ta);
        //     if(w == 0) continue;
        //     kv_pushp(u_trans_t, *ta, &p);
        //     memset(p, 0, sizeof(u_trans_t));///extra edges
        //     p->nw = w; p->qn = qn; p->tn = tn;
        // }

        for (i = 0, vis[k] = 0; i < n; i++) vis[a[i].tn] = 0;
    }

    // fprintf(stderr, "------ta->n=%u\n", (uint32_t)ta->n);

    for (i = m = 0; i < ta->n; i++)
    {
        if(ta->a[i].nw == 0 || ta->a[i].del) continue;
        ta->a[m] = ta->a[i];
        m++;
    }
    ta->n = m;

    free(vis);
    kt_u_trans_t_idx(ta, idx->ug->g->n_seq);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x50(%rsp)
movq %r8, %r15
movq %rcx, %r13
movq %rdx, 0x28(%rsp)
movq %rsi, (%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movq (%rdi), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, %rbx
xorl %eax, %eax
cmpq $0x0, (%r15)
je 0x158560
movl $0x1, %ecx
xorpd %xmm0, %xmm0
movq 0xccdd6(%rip), %xmm1    # 0x225240
movapd 0xccdde(%rip), %xmm2    # 0x225250
xorl %edx, %edx
movq 0x10(%r15), %rsi
leaq (%rdx,%rdx,2), %rdi
shlq $0x4, %rdi
cmpb $0x0, 0x28(%rsi,%rdi)
js 0x158551
addq %rsi, %rdi
movq (%r13), %r8
movq 0x80(%r13), %rdx
movl 0x8(%rdi), %r9d
movl (%r8,%r9,4), %r9d
incq %rdx
cmpq %r9, %rdx
jb 0x158551
movl 0x14(%rdi), %r9d
movl (%r8,%r9,4), %r8d
cmpq %r8, %rdx
jb 0x158551
movl %eax, %edx
movq %rdx, %r8
shlq $0x4, %r8
leaq (%r8,%r8,2), %r8
movups (%rdi), %xmm3
movupd 0x10(%rdi), %xmm4
movups 0x20(%rdi), %xmm5
movups %xmm5, 0x20(%rsi,%r8)
movupd %xmm4, 0x10(%rsi,%r8)
movups %xmm3, (%rsi,%r8)
movq 0x10(%r15), %rsi
movsd 0x20(%rsi,%r8), %xmm3
ucomisd %xmm0, %xmm3
jne 0x1584fc
jnp 0x15854f
addq %rsi, %r8
movq (%rsp), %rdi
movq 0x40(%rdi), %rdi
movl 0x8(%r8), %r9d
movl 0x14(%r8), %r8d
movq (%rdi,%r9,8), %r9
movq (%rdi,%r8,8), %rdi
cmpq %rdi, %r9
cmovbq %r9, %rdi
movq %rdi, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
movsd 0x20(%rsi,%rdx), %xmm3
divsd %xmm4, %xmm3
movsd %xmm3, 0x20(%rsi,%rdx)
incl %eax
movl %ecx, %edx
incl %ecx
cmpq %rdx, (%r15)
ja 0x158474
movl %eax, %eax
movq %rax, (%r15)
cmpq $0x0, 0x18(%r15)
je 0x1588cb
xorl %ebp, %ebp
movq %rbx, 0x48(%rsp)
movq %r15, 0x8(%rsp)
movq (%r13), %rax
movq 0x80(%r13), %rcx
movl (%rax,%rbp,4), %eax
incq %rcx
cmpq %rax, %rcx
jb 0x1588bf
movq 0x10(%r15), %rax
movq %rax, 0x38(%rsp)
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %rcx
movq %rcx, %rax
shrq $0x20, %rax
movq %rax, 0x30(%rsp)
movb $0x1, (%rbx,%rbp)
movq %rcx, 0x40(%rsp)
testl %ecx, %ecx
je 0x1586ec
movl 0x40(%rsp), %eax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movq 0x38(%rsp), %rdx
leaq (%rdx,%rcx), %r14
addq $0x20, %r14
shlq $0x4, %rax
leaq (%rax,%rax,2), %r12
xorl %r15d, %r15d
movl -0xc(%r14,%r15), %ecx
movb $0x1, (%rbx,%rcx)
movl -0x18(%r14,%r15), %eax
cmpl %ecx, %eax
je 0x1586df
movq (%r13), %rsi
movq 0x80(%r13), %rdx
movl (%rsi,%rax,4), %edi
incq %rdx
cmpq %rdi, %rdx
jb 0x1586df
movl (%rsi,%rcx,4), %esi
cmpq %rsi, %rdx
jb 0x1586df
movq 0x50(%rsp), %rbx
movq 0x28(%rbx), %rdx
movq (%rdx,%rcx,8), %r10
movq %r10, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
shlq $0x4, %r9
addq 0x10(%rbx), %r9
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x20(%rsp), %r8
pushq 0x10(%rsp)
pushq %rax
pushq %r10
callq 0x1580c4
addq $0x20, %rsp
addsd (%r14,%r15), %xmm0
movsd %xmm0, (%r14,%r15)
movq 0x28(%rbx), %rax
movl -0x18(%r14,%r15), %ecx
movl -0xc(%r14,%r15), %r10d
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
shlq $0x4, %r9
addq 0x10(%rbx), %r9
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x50(%rsp), %rbx
movq %r13, %rsi
movq 0x8(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x20(%rsp), %r8
pushq 0x10(%rsp)
pushq %r10
pushq %rax
callq 0x1580c4
addq $0x20, %rsp
addsd (%r14,%r15), %xmm0
movsd %xmm0, (%r14,%r15)
addq $0x30, %r15
cmpq %r15, %r12
jne 0x1585eb
movq 0x8(%rsp), %r15
cmpq $0x0, 0x18(%r15)
je 0x15887d
xorl %r14d, %r14d
movq (%r13), %rcx
movq 0x80(%r13), %rax
movl (%rcx,%rbp,4), %edx
incq %rax
cmpq %rdx, %rax
jb 0x158870
movl (%rcx,%r14,4), %ecx
cmpq %rcx, %rax
jb 0x158870
cmpl %r14d, %ebp
je 0x158870
cmpb $0x0, (%rbx,%r14)
jne 0x158870
movq 0x50(%rsp), %r12
movq 0x28(%r12), %rax
movq (%rax,%r14,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
shlq $0x4, %r9
addq 0x10(%r12), %r9
subq $0x8, %rsp
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
movq 0x30(%rsp), %r15
movq %r15, %rcx
movq 0x20(%rsp), %r8
pushq 0x10(%rsp)
pushq %rbp
pushq %rax
callq 0x1580c4
addq $0x20, %rsp
pxor %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x28(%r12), %rax
movq (%rax,%rbp,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
shlq $0x4, %r9
addq 0x10(%r12), %r9
subq $0x8, %rsp
movq %rbx, %rdi
movq 0x50(%rsp), %rbx
movq %r13, %rsi
movq 0x8(%rsp), %rdx
movq %r15, %rcx
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %r8
pushq %r15
pushq %r14
pushq %rax
callq 0x1580c4
addq $0x20, %rsp
addsd 0x20(%rsp), %xmm0
ucomisd 0xccd24(%rip), %xmm0    # 0x225518
jne 0x1587f8
jnp 0x158870
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x158836
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
movsd %xmm0, 0x20(%rsp)
callq 0x2370
movsd 0x20(%rsp), %xmm0
movq %rax, 0x10(%r15)
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
pxor %xmm1, %xmm1
movdqu %xmm1, 0x20(%rcx,%rax)
movdqu %xmm1, (%rcx,%rax)
movdqu %xmm1, 0x10(%rcx,%rax)
movsd %xmm0, 0x20(%rcx,%rax)
movl %ebp, 0x8(%rcx,%rax)
movl %r14d, 0x14(%rcx,%rax)
incl %r14d
cmpq %r14, 0x18(%r15)
ja 0x1586ff
movb $0x0, (%rbx,%rbp)
movq 0x40(%rsp), %rax
testl %eax, %eax
je 0x1588bf
movl %eax, %ecx
movq 0x30(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq 0x38(%rsp), %rdx
addq %rdx, %rax
addq $0x14, %rax
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
movl (%rax,%rdx), %esi
movb $0x0, (%rbx,%rsi)
addq $0x30, %rdx
cmpq %rdx, %rcx
jne 0x1588af
incl %ebp
cmpq %rbp, 0x18(%r15)
ja 0x15857a
xorl %eax, %eax
cmpq $0x0, (%r15)
je 0x158937
movl $0x1, %ecx
xorpd %xmm0, %xmm0
xorl %esi, %esi
movq 0x10(%r15), %rdx
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
movsd 0x20(%rdx,%rsi), %xmm1
ucomisd %xmm0, %xmm1
jne 0x1588f8
jnp 0x15892c
addq %rdx, %rsi
cmpb $0x0, 0x28(%rsi)
js 0x15892c
movl %eax, %edi
leaq (%rdi,%rdi,2), %rdi
shlq $0x4, %rdi
movupd (%rsi), %xmm1
movupd 0x10(%rsi), %xmm2
movupd 0x20(%rsi), %xmm3
movupd %xmm3, 0x20(%rdx,%rdi)
movupd %xmm2, 0x10(%rdx,%rdi)
movupd %xmm1, (%rdx,%rdi)
incl %eax
movl %ecx, %esi
incl %ecx
cmpq %rsi, (%r15)
ja 0x1588de
movl %eax, %eax
movq %rax, (%r15)
movq %rbx, %rdi
callq 0x2220
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %r15, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc405a