// File: /workspace/github_cpp_makefile_star2_O2/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, 0x40(%rsp)
movq %rsi, 0x10(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x18(%rsp)
movq (%rdi), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x8(%rsp)
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd 0xcb006(%rip), %xmm1    # 0x1e3270
movapd 0xcb00e(%rip), %xmm2    # 0x1e3280
xorl %ecx, %ecx
movl %eax, %eax
movl %ecx, %edx
cmpq %rax, (%rbx)
jbe 0x118345
movq 0x10(%rbx), %rsi
imulq $0x30, %rax, %rdi
cmpb $0x0, 0x28(%rsi,%rdi)
js 0x11833e
addq %rsi, %rdi
movq (%r13), %r9
movq 0x80(%r13), %r8
movl 0x8(%rdi), %r10d
movl (%r9,%r10,4), %r10d
incq %r8
cmpq %r10, %r8
jb 0x11833e
movl 0x14(%rdi), %r10d
movl (%r9,%r10,4), %r9d
cmpq %r9, %r8
jb 0x11833e
imulq $0x30, %rdx, %rdx
movups (%rdi), %xmm3
movupd 0x10(%rdi), %xmm4
movupd 0x20(%rdi), %xmm5
movupd %xmm5, 0x20(%rsi,%rdx)
movupd %xmm4, 0x10(%rsi,%rdx)
movups %xmm3, (%rsi,%rdx)
movq 0x10(%rbx), %rsi
movsd 0x20(%rsi,%rdx), %xmm3
ucomisd %xmm0, %xmm3
jne 0x1182f6
jnp 0x11833c
leaq (%rsi,%rdx), %rdi
movq 0x10(%rsp), %r8
movq 0x40(%r8), %r8
movl 0x8(%rdi), %r9d
movl 0x14(%rdi), %edi
movq (%r8,%r9,8), %r9
movq (%r8,%rdi,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
divsd %xmm5, %xmm3
movsd %xmm3, 0x20(%rsi,%rdx)
incl %ecx
incl %eax
jmp 0x118274
movq %rdx, (%rbx)
movq 0x18(%rbx), %rax
xorl %r10d, %r10d
movq %rbx, 0x20(%rsp)
movl %r10d, %r14d
cmpq %r14, %rax
jbe 0x11868d
movq (%r13), %rcx
movq 0x80(%r13), %rdx
movl (%rcx,%r14,4), %ecx
incq %rdx
cmpq %rcx, %rdx
jae 0x11837c
incl %r10d
jmp 0x118354
movq 0x10(%rbx), %rsi
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %rax
movl %eax, %ecx
shrq $0x20, %rax
movq 0x8(%rsp), %rdx
movb $0x1, (%rdx,%r14)
imulq $0x30, %rcx, %rbp
imulq $0x30, %rax, %rax
movq %rsi, 0x60(%rsp)
movq %rax, 0x58(%rsp)
leaq (%rsi,%rax), %r12
addq $0x20, %r12
xorl %ebx, %ebx
movq %r10, 0x38(%rsp)
movq %r14, 0x48(%rsp)
cmpq %rbx, %rbp
je 0x1184c7
movl -0xc(%r12,%rbx), %ecx
movq 0x8(%rsp), %rax
movb $0x1, (%rax,%rcx)
movl -0x18(%r12,%rbx), %eax
cmpl %ecx, %eax
je 0x1184be
movq (%r13), %rsi
movq 0x80(%r13), %rdx
movl (%rsi,%rax,4), %edi
incq %rdx
cmpq %rdi, %rdx
jb 0x1184be
movl (%rsi,%rcx,4), %esi
cmpq %rsi, %rdx
jb 0x1184be
movq 0x50(%rsp), %r14
movq 0x28(%r14), %rdx
movq (%rdx,%rcx,8), %r10
movq %r10, %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %r9
addq 0x10(%r14), %r9
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r15
pushq %r15
pushq %rax
pushq %r10
callq 0x117f09
addq $0x20, %rsp
addsd (%r12,%rbx), %xmm0
movsd %xmm0, (%r12,%rbx)
movq 0x28(%r14), %rax
movl -0x18(%r12,%rbx), %ecx
movl -0xc(%r12,%rbx), %r10d
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %r9
addq 0x10(%r14), %r9
movq 0x48(%rsp), %r14
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x30(%rsp), %r8
pushq %r15
pushq %r10
pushq %rax
callq 0x117f09
movq 0x58(%rsp), %r10
addq $0x20, %rsp
addsd (%r12,%rbx), %xmm0
movsd %xmm0, (%r12,%rbx)
addq $0x30, %rbx
jmp 0x1183be
xorl %r12d, %r12d
movq 0x20(%rsp), %rbx
movl %r12d, %ecx
movq 0x18(%rbx), %rax
cmpq %rcx, %rax
jbe 0x118655
movq (%r13), %rdx
movq 0x80(%r13), %rax
movl (%rdx,%r14,4), %esi
incq %rax
cmpq %rsi, %rax
jb 0x11864d
movl (%rdx,%rcx,4), %edx
cmpq %rdx, %rax
jb 0x11864d
cmpl %r12d, %r10d
je 0x11864d
movq 0x8(%rsp), %rax
cmpb $0x0, (%rax,%rcx)
jne 0x11864d
movq 0x50(%rsp), %rbx
movq 0x28(%rbx), %rax
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %r9
addq 0x10(%rbx), %r9
subq $0x8, %rsp
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq 0x48(%rsp), %r15
movq %r15, %rcx
movq 0x30(%rsp), %r8
pushq 0x28(%rsp)
pushq %r10
pushq %rax
callq 0x117f09
addq $0x20, %rsp
xorpd %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x28(%rbx), %rax
movq 0x48(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %r9
addq 0x10(%rbx), %r9
movq 0x20(%rsp), %rbx
subq $0x8, %rsp
movq %r14, %rdi
movq 0x50(%rsp), %r14
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq %r15, %rcx
movq 0x30(%rsp), %r8
pushq %rbx
pushq %r12
pushq %rax
callq 0x117f09
movq 0x58(%rsp), %r10
addq $0x20, %rsp
addsd 0x30(%rsp), %xmm0
ucomisd 0xcaf92(%rip), %xmm0    # 0x1e3568
jne 0x1185da
jnp 0x11864d
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x11861d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbx)
imulq $0x30, %rdx, %rsi
movq %rax, %rdi
movsd %xmm0, 0x30(%rsp)
callq 0x2370
movsd 0x30(%rsp), %xmm0
movq 0x38(%rsp), %r10
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
imulq $0x30, %rcx, %rcx
xorpd %xmm1, %xmm1
movupd %xmm1, 0x20(%rax,%rcx)
movupd %xmm1, (%rax,%rcx)
movupd %xmm1, 0x10(%rax,%rcx)
movsd %xmm0, 0x20(%rax,%rcx)
movl %r10d, 0x8(%rax,%rcx)
movl %r12d, 0x14(%rax,%rcx)
incl %r12d
jmp 0x1184cf
movq 0x8(%rsp), %rcx
movb $0x0, (%rcx,%r14)
movq 0x60(%rsp), %rcx
movq 0x58(%rsp), %rdx
addq %rdx, %rcx
addq $0x14, %rcx
xorl %edx, %edx
cmpq %rdx, %rbp
je 0x118377
movl (%rcx,%rdx), %esi
movq 0x8(%rsp), %rdi
movb $0x0, (%rdi,%rsi)
addq $0x30, %rdx
jmp 0x118672
xorl %eax, %eax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movl %eax, %eax
movl %ecx, %edx
cmpq %rax, (%rbx)
jbe 0x1186e6
movq 0x10(%rbx), %rsi
imulq $0x30, %rax, %rdi
movsd 0x20(%rsi,%rdi), %xmm1
ucomisd %xmm0, %xmm1
jne 0x1186b4
jnp 0x1186e2
addq %rsi, %rdi
cmpb $0x0, 0x28(%rdi)
js 0x1186e2
imulq $0x30, %rdx, %rdx
movupd (%rdi), %xmm1
movupd 0x10(%rdi), %xmm2
movupd 0x20(%rdi), %xmm3
movupd %xmm3, 0x20(%rsi,%rdx)
movupd %xmm2, 0x10(%rsi,%rdx)
movupd %xmm1, (%rsi,%rdx)
incl %ecx
incl %eax
jmp 0x118695
movq %rdx, (%rbx)
movq 0x8(%rsp), %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 0x88a31