// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %r9, 0x50(%rsp)
movq %r8, %rbx
movq %rcx, %r13
movq %rdx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %rdi, 0x18(%rsp)
movq (%rdi), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, %r15
movq %rbx, 0x8(%rsp)
movq (%rbx), %rsi
xorl %eax, %eax
testq %rsi, %rsi
je 0x15ee85
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl $0x1, %edx
xorpd %xmm0, %xmm0
movq 0xd24f7(%rip), %xmm1    # 0x231280
movapd 0xd24ff(%rip), %xmm2    # 0x231290
xorl %edi, %edi
leaq (%rdi,%rdi,2), %rdi
shlq $0x4, %rdi
cmpb $0x0, 0x28(%rcx,%rdi)
js 0x15ee76
addq %rcx, %rdi
movq (%r13), %r9
movq 0x80(%r13), %r8
movl 0x8(%rdi), %r10d
movl (%r9,%r10,4), %r10d
incq %r8
cmpq %r10, %r8
jb 0x15ee76
movl 0x14(%rdi), %r10d
movl (%r9,%r10,4), %r9d
cmpq %r9, %r8
jb 0x15ee76
movl %eax, %esi
movq %rsi, %r8
shlq $0x4, %r8
leaq (%r8,%r8,2), %r8
movups (%rdi), %xmm3
movupd 0x10(%rdi), %xmm4
movupd 0x20(%rdi), %xmm5
movupd %xmm5, 0x20(%rcx,%r8)
movupd %xmm4, 0x10(%rcx,%r8)
movups %xmm3, (%rcx,%r8)
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movsd 0x20(%rcx,%r8), %xmm3
ucomisd %xmm0, %xmm3
jne 0x15ee1e
jnp 0x15ee6c
addq %rcx, %r8
movq 0x20(%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, %xmm4
punpckldq %xmm1, %xmm4    # xmm4 = xmm4[0],xmm1[0],xmm4[1],xmm1[1]
subpd %xmm2, %xmm4
movapd %xmm4, %xmm5
unpckhpd %xmm4, %xmm5            # xmm5 = xmm5[1],xmm4[1]
addsd %xmm4, %xmm5
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
divsd %xmm5, %xmm3
movsd %xmm3, 0x20(%rcx,%rsi)
incl %eax
movq 0x8(%rsp), %rsi
movq (%rsi), %rsi
movl %edx, %edi
incl %edx
cmpq %rdi, %rsi
ja 0x15ed93
movl %eax, %eax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x18(%rcx), %rcx
testq %rcx, %rcx
je 0x15f245
xorl %r12d, %r12d
movq %r15, 0x40(%rsp)
movq %r13, 0x58(%rsp)
movq (%r13), %rax
movq 0x80(%r13), %rdx
movl (%rax,%r12,4), %eax
incq %rdx
cmpq %rax, %rdx
jb 0x15f231
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x38(%rsp)
movq 0x28(%rax), %rax
movq (%rax,%r12,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
movq %rcx, 0x30(%rsp)
movb $0x1, (%r15,%r12)
movq %rax, %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
andq %rcx, %rdx
movq %rdx, 0x60(%rsp)
je 0x15f042
movq %r12, 0x10(%rsp)
movl %eax, %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 %ebx, %ebx
movl -0xc(%r14,%rbx), %ecx
movb $0x1, (%r15,%rcx)
movl -0x18(%r14,%rbx), %eax
cmpl %ecx, %eax
je 0x15f020
movq (%r13), %rsi
movq 0x80(%r13), %rdx
movl (%rsi,%rax,4), %edi
incq %rdx
cmpq %rdi, %rdx
jb 0x15f020
movl (%rsi,%rcx,4), %esi
cmpq %rsi, %rdx
jb 0x15f020
movq 0x50(%rsp), %r15
movq 0x28(%r15), %rdx
movq (%rdx,%rcx,8), %r10
movq %r10, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
shlq $0x4, %r9
addq 0x10(%r15), %r9
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x50(%rsp), %rbp
movq %rbp, %r8
pushq 0x10(%rsp)
pushq %rax
pushq %r10
callq 0x15e9e0
addq $0x20, %rsp
addsd (%r14,%rbx), %xmm0
movsd %xmm0, (%r14,%rbx)
movq 0x28(%r15), %rax
movl -0x18(%r14,%rbx), %ecx
movl -0xc(%r14,%rbx), %r10d
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
shlq $0x4, %r9
addq 0x10(%r15), %r9
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rbp, %r8
pushq 0x10(%rsp)
movq 0x50(%rsp), %r15
pushq %r10
pushq %rax
callq 0x15e9e0
addq $0x20, %rsp
addsd (%r14,%rbx), %xmm0
movsd %xmm0, (%r14,%rbx)
addq $0x30, %rbx
cmpq %rbx, %r12
jne 0x15ef2a
movq 0x8(%rsp), %rax
cmpq $0x0, 0x18(%rax)
movq 0x10(%rsp), %r12
je 0x15f1ec
xorl %r14d, %r14d
movq (%r13), %rcx
movq 0x80(%r13), %rax
movl (%rcx,%r12,4), %edx
incq %rax
cmpq %rdx, %rax
jb 0x15f1d5
movl (%rcx,%r14,4), %ecx
cmpq %rcx, %rax
jb 0x15f1d5
cmpl %r14d, %r12d
je 0x15f1d5
cmpb $0x0, (%r15,%r14)
jne 0x15f1d5
movq %r12, %rbx
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 0x20(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rbp
movq %rbp, %rdx
movq 0x30(%rsp), %rcx
movq 0x50(%rsp), %r13
movq %r13, %r8
pushq 0x10(%rsp)
pushq %rbx
pushq %rax
callq 0x15e9e0
addq $0x20, %rsp
pxor %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x28(%r12), %rax
movq (%rax,%rbx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
shlq $0x4, %r9
addq 0x10(%r12), %r9
movq %rbx, %r12
subq $0x8, %rsp
movq %r15, %rdi
movq 0x48(%rsp), %r15
movq 0x60(%rsp), %rsi
movq %rbp, %rdx
movq 0x30(%rsp), %rcx
movq %r13, %r8
movq 0x60(%rsp), %r13
pushq 0x10(%rsp)
pushq %r14
pushq %rax
callq 0x15e9e0
addq $0x20, %rsp
addsd 0x10(%rsp), %xmm0
ucomisd 0xd24d2(%rip), %xmm0    # 0x231618
jne 0x15f14e
jnp 0x15f1d5
movq 0x8(%rsp), %rdx
movq (%rdx), %rcx
movq 0x10(%rdx), %rax
cmpq 0x8(%rdx), %rcx
jne 0x15f19c
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq 0x8(%rsp), %rbx
movq %rdx, 0x8(%rbx)
shlq $0x4, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x2370
movsd 0x10(%rsp), %xmm0
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq 0x8(%rsp), %rsi
movq %rdx, (%rsi)
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
pxor %xmm1, %xmm1
movdqu %xmm1, 0x20(%rax,%rcx)
movdqu %xmm1, (%rax,%rcx)
movdqu %xmm1, 0x10(%rax,%rcx)
movsd %xmm0, 0x20(%rax,%rcx)
movl %r12d, 0x8(%rax,%rcx)
movl %r14d, 0x14(%rax,%rcx)
incl %r14d
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rcx
cmpq %r14, %rcx
ja 0x15f045
jmp 0x15f1ee
xorl %ecx, %ecx
movb $0x0, (%r15,%r12)
movq 0x60(%rsp), %rsi
testq %rsi, %rsi
je 0x15f231
movq 0x30(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq 0x38(%rsp), %rdx
addq %rdx, %rax
addq $0x14, %rax
shlq $0x4, %rsi
leaq (%rsi,%rsi,2), %rdx
xorl %esi, %esi
movl (%rax,%rsi), %edi
movb $0x0, (%r15,%rdi)
addq $0x30, %rsi
cmpq %rsi, %rdx
jne 0x15f220
incl %r12d
cmpq %r12, %rcx
ja 0x15eea7
movq 0x8(%rsp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
testq %rax, %rax
je 0x15f2bd
movl $0x1, %edx
xorpd %xmm0, %xmm0
xorl %edi, %edi
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rsi
leaq (%rdi,%rdi,2), %rdi
shlq $0x4, %rdi
movsd 0x20(%rsi,%rdi), %xmm1
ucomisd %xmm0, %xmm1
jne 0x15f276
jnp 0x15f2b2
addq %rsi, %rdi
cmpb $0x0, 0x28(%rdi)
js 0x15f2b2
movl %ecx, %eax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movupd (%rdi), %xmm1
movupd 0x10(%rdi), %xmm2
movupd 0x20(%rdi), %xmm3
movupd %xmm3, 0x20(%rsi,%rax)
movupd %xmm2, 0x10(%rsi,%rax)
movupd %xmm1, (%rsi,%rax)
incl %ecx
movq 0x8(%rsp), %rax
movq (%rax), %rax
movl %edx, %edi
incl %edx
cmpq %rdi, %rax
ja 0x15f257
movl %ecx, %ecx
movq 0x8(%rsp), %rbx
movq %rcx, (%rbx)
movq %r15, %rdi
callq 0x2220
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %rbx, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc4edd