// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: weight_kv_u_trans(ha_ug_index*, kvec_pe_hit*, hc_links*, bubble_type*, kv_u_trans_t*, trans_idx*, int)
void weight_kv_u_trans(ha_ug_index* idx, kvec_pe_hit* hits, hc_links* link, bubble_type* bub, 
kv_u_trans_t *ta, trans_idx* dis, int sc_weight)
{
    uint64_t k, i, shif = 64 - idx->uID_bits, beg, end, t_d;
    u_trans_t *e1 = NULL, *e2 = NULL;
    long double weight;
    double *sw = NULL;
    u_trans_t *p = NULL;
    uint32_t is_cc, ii1, ii2;

    for (i = 0, ta->idx.n = ta->n = 0; i < link->a.n; i++)
    {
        for (k = 0; k < link->a.a[i].e.n; k++)
        {
            if(link->a.a[i].e.a[k].del) continue;
            if(IF_HOM(i, *bub)) continue;
            if(IF_HOM(link->a.a[i].e.a[k].uID, *bub)) continue;
            if(i == link->a.a[i].e.a[k].uID) continue;

            kv_pushp(u_trans_t, *ta, &p);
            memset(p, 0, sizeof(u_trans_t));
            p->qn = i; p->tn = link->a.a[i].e.a[k].uID;
            p->nw = 0; p->occ = 0;
        }
    }
    kt_u_trans_t_idx(ta, idx->ug->g->n_seq);
    if(sc_weight) {
        k = ta->n*3;
        MALLOC(sw, k);
        for (i = 0; i < k; i++) sw[i] = 0;
    }


    for (k = 0; k < hits->a.n; ++k) 
    {
        beg = ((hits->a.a[k].s<<1)>>shif);
        end = ((hits->a.a[k].e<<1)>>shif);

        if(beg == end) continue;
        if(IF_HOM(beg, *bub)) continue;
        if(IF_HOM(end, *bub)) continue;
        if(is_hom_hit(hits->a.a[k])) continue;
        
        t_d = get_hic_distance(&(hits->a.a[k]), link, idx, &is_cc);
        // if(t_d == (uint64_t)-1) continue;
        // if(t_d == (uint64_t)-1 && is_cc == 0) continue;
        // if(t_d == (uint64_t)-1 && !dis) continue;

        get_u_trans_spec_idx(ta, beg, end, &e1, NULL, &ii1);
        get_u_trans_spec_idx(ta, end, beg, &e2, NULL, &ii2);

        if(e1 == NULL || e2 == NULL) continue;
        weight = 1;
        if(dis) weight = get_trans_weight_advance(idx, t_d, dis);
        
        if(sc_weight){
            i = hic_sc_type(idx, hits, k);
            if(i == 0){
                e1->nw -= weight; e2->nw -= weight;
            }
            else{
                i--;
                sw[(ii1*3)+i] -= weight;
                sw[(ii2*3)+i] -= weight;
            }
        } else{
            e1->nw -= weight; e2->nw -= weight; 
        }
        e1->occ++; e2->occ++;
    }    

    if(sc_weight) {
        for (i = 0; i < ta->n; ++i){
            if(ta->a[i].nw > sw[(i*3)]) ta->a[i].nw = sw[(i*3)];
            if(ta->a[i].nw > sw[(i*3)+1]) ta->a[i].nw = sw[(i*3)+1];
            if(ta->a[i].nw > sw[(i*3)+2]) ta->a[i].nw = sw[(i*3)+2];
            ta->a[i].nw *= 2/**4**/;
        }
        free(sw);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, 0x60(%rsp)
movq %r8, %rbx
movq %rcx, %r15
movq %rsi, 0x38(%rsp)
movl $0x40, %eax
movq %rdi, 0x20(%rsp)
subq 0x18(%rdi), %rax
movq %rax, 0x68(%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, (%r8)
movq %rax, 0x18(%r8)
movq (%rdx), %rax
testq %rax, %rax
movq %rdx, 0x18(%rsp)
je 0x15deaa
movq 0x10(%rdx), %rcx
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
leaq (%r14,%r14,2), %r12
shlq $0x4, %r12
cmpq $0x0, (%rcx,%r12)
je 0x15de9e
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x10(%rcx,%r12), %rsi
cmpb $0x0, 0x18(%rsi,%rbp)
js 0x15de8a
movq (%r15), %r8
movq 0x80(%r15), %rax
movl (%r8,%r14,4), %edi
incq %rax
cmpq %rdi, %rax
jb 0x15de8a
movl 0x8(%rsi,%rbp), %esi
cmpq %rsi, %r14
je 0x15de8a
movl (%r8,%rsi,4), %esi
cmpq %rsi, %rax
jb 0x15de8a
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x15de4c
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbx)
shlq $0x4, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
callq 0x2370
xorpd %xmm0, %xmm0
movq 0x18(%rsp), %rdx
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
leaq (%rcx,%rcx,2), %rdi
shlq $0x4, %rdi
movupd %xmm0, (%rax,%rdi)
movupd %xmm0, 0x20(%rax,%rdi)
movupd %xmm0, 0x10(%rax,%rdi)
movl %r14d, 0x8(%rax,%rdi)
movq 0x10(%rdx), %rcx
movq 0x10(%rcx,%r12), %rsi
movl 0x8(%rsi,%rbp), %esi
movl %esi, 0x14(%rax,%rdi)
movl $0x0, 0x18(%rax,%rdi)
incq %r13
addq $0x28, %rbp
cmpq (%rcx,%r12), %r13
jb 0x15ddcb
movq (%rdx), %rax
incq %r14
cmpq %rax, %r14
jb 0x15ddb3
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %rbx, %rdi
callq 0xc4edd
cmpl $0x0, 0x110(%rsp)
je 0x15df0a
movq (%rbx), %r14
leaq (,%r14,8), %rax
leaq (%rax,%rax,2), %rdi
callq 0x2280
testq %r14, %r14
movq %rax, 0x40(%rsp)
je 0x15df13
leaq (%r14,%r14,2), %rdx
cmpq $0x1, %rdx
adcq $0x0, %rdx
shlq $0x3, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
jmp 0x15df13
movq $0x0, 0x40(%rsp)
movq 0x18(%rsp), %rsi
movq 0x38(%rsp), %rax
movq (%rax), %r12
testq %r12, %r12
je 0x15e2c3
xorl %r13d, %r13d
fld1
fldl 0xd368c(%rip)           # 0x2315c0
fstpt 0xcc(%rsp)
flds 0xdd333(%rip)           # 0x23b274
fstpt 0xc0(%rsp)
fchs
fstpt 0xb4(%rsp)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
movq %r13, %rax
shlq $0x5, %rax
movq (%rdi,%rax), %r14
addq %r14, %r14
movq 0x68(%rsp), %rcx
shrq %cl, %r14
movq 0x8(%rdi,%rax), %rbp
addq %rbp, %rbp
shrq %cl, %rbp
cmpq %rbp, %r14
je 0x15e2b7
movq (%r15), %rdx
movq 0x80(%r15), %rcx
movl (%rdx,%r14,4), %r8d
incq %rcx
cmpq %r8, %rcx
jb 0x15e2b7
movl (%rdx,%rbp,4), %edx
cmpq %rdx, %rcx
jb 0x15e2b7
addq %rax, %rdi
cmpq $-0x1, 0x10(%rdi)
je 0x15e2b7
movq 0x20(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
callq 0x14a518
movq %rax, 0x48(%rsp)
movq %rbx, %rdi
movl %r14d, %esi
movl %ebp, %edx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
leaq 0x5c(%rsp), %r9
callq 0x15db21
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
leaq 0x28(%rsp), %rcx
xorl %r8d, %r8d
leaq 0x58(%rsp), %r9
callq 0x15db21
cmpq $0x0, 0x30(%rsp)
je 0x15e0d0
cmpq $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rsi
je 0x15e2b7
fld1
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x15e20c
fstp %st(0)
fldt 0xcc(%rsp)
movq 0x48(%rsp), %rdx
cmpq $-0x1, %rdx
je 0x15e197
fstp %st(0)
cmpq %rdx, 0x18(%rax)
jbe 0x15e0da
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x15e07d
movq (%rax), %rax
addq $0x20, %rax
cmpq %rdx, -0x18(%rax)
jbe 0x15e074
cmpq %rdx, -0x20(%rax)
jbe 0x15e13a
addq $0x28, %rax
decq %rcx
jne 0x15e064
movq 0x20(%rsp), %rcx
fldt 0x80(%rcx)
movq %rdx, 0x90(%rsp)
xorl %eax, %eax
testq %rdx, %rdx
sets %al
fildll 0x90(%rsp)
leaq 0xdd192(%rip), %rdx     # 0x23b238
fadds (%rdx,%rax,4)
fldt 0xa0(%rcx)
fdivrp %st, %st(1)
fmulp %st, %st(1)
fldt 0x90(%rcx)
faddp %st, %st(1)
fstpl 0x88(%rsp)
movsd 0x88(%rsp), %xmm0
jmp 0x15e187
movq 0x18(%rsp), %rsi
jmp 0x15e2b7
movq 0x20(%rsp), %rcx
fldt 0x80(%rcx)
movq %rdx, 0xa8(%rsp)
xorl %eax, %eax
testq %rdx, %rdx
sets %al
fildll 0xa8(%rsp)
leaq 0xdd135(%rip), %rdx     # 0x23b238
fadds (%rdx,%rax,4)
fldt 0xa0(%rcx)
fdivrp %st, %st(1)
fmulp %st, %st(1)
fldt 0x90(%rcx)
faddp %st, %st(1)
fstpl 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
fldl 0x98(%rsp)
jmp 0x15e197
movq (%rax), %rcx
movq %rcx, %xmm1
movq 0xd3136(%rip), %xmm2    # 0x231280
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0xd313a(%rip), %xmm3    # 0x231290
subpd %xmm3, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addq -0x8(%rax), %rcx
addsd %xmm1, %xmm0
movq %rcx, %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
subpd %xmm3, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
movsd %xmm0, 0x80(%rsp)
fldl 0x80(%rsp)
fldz
fucomi %st(1), %st
fxch %st(1)
fcmovnbe %st(1), %st
fstp %st(1)
faddl 0xdd019(%rip)           # 0x23b1c0
fldt 0xc0(%rsp)
fxch %st(1)
fucomi %st(1), %st
fcmovnbe %st(1), %st
fstp %st(1)
faddl 0xdd084(%rip)           # 0x23b240
fld1
fdivp %st, %st(1)
fldt 0xb4(%rsp)
faddp %st, %st(1)
fstpt (%rsp)
callq 0x20b0
fmuls 0xdd0a1(%rip)           # 0x23b278
fldl 0xdd06b(%rip)           # 0x23b248
fucomi %st(1), %st
fxch %st(1)
fcmovnbe %st(1), %st
fstp %st(1)
fldl 0xdd065(%rip)           # 0x23b250
fxch %st(1)
fucomi %st(1), %st
fcmovnbe %st(1), %st
fstp %st(1)
fstpl 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
fldl 0x70(%rsp)
movq 0x18(%rsp), %rsi
cmpl $0x0, 0x110(%rsp)
je 0x15e26e
fstpt 0x48(%rsp)
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %r13, %rdx
callq 0x15dc01
testl %eax, %eax
je 0x15e288
movl %eax, %eax
movl 0x5c(%rsp), %ecx
leal (%rcx,%rcx,2), %ecx
addq %rax, %rcx
decq %rcx
movq 0x40(%rsp), %rdx
fldt 0x48(%rsp)
fld %st(0)
fsubrl (%rdx,%rcx,8)
fstpl (%rdx,%rcx,8)
movl 0x58(%rsp), %ecx
leal (%rcx,%rcx,2), %ecx
leaq -0x1(%rax,%rcx), %rax
fsubrl (%rdx,%rax,8)
fstpl (%rdx,%rax,8)
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
jmp 0x15e2a4
movq 0x30(%rsp), %rax
fld %st(0)
fsubrl 0x20(%rax)
fstpl 0x20(%rax)
movq 0x28(%rsp), %rcx
fsubrl 0x20(%rcx)
fstpl 0x20(%rcx)
jmp 0x15e2a9
movq 0x30(%rsp), %rax
fldt 0x48(%rsp)
fld %st(0)
fsubrl 0x20(%rax)
fstpl 0x20(%rax)
movq 0x28(%rsp), %rcx
fsubrl 0x20(%rcx)
fstpl 0x20(%rcx)
movq 0x18(%rsp), %rsi
incl 0x18(%rax)
incl 0x18(%rcx)
movq 0x38(%rsp), %rax
movq (%rax), %r12
incq %r13
cmpq %r12, %r13
jb 0x15df51
cmpl $0x0, 0x110(%rsp)
je 0x15e318
movq (%rbx), %rax
testq %rax, %rax
movq 0x40(%rsp), %rdi
je 0x15e313
movq 0x10(%rbx), %rcx
movl $0x10, %edx
movsd -0x10(%rdi,%rdx), %xmm0
minsd (%rcx,%rdx,2), %xmm0
movsd -0x8(%rdi,%rdx), %xmm1
minsd %xmm0, %xmm1
movsd (%rdi,%rdx), %xmm0
minsd %xmm1, %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,2)
addq $0x18, %rdx
decq %rax
jne 0x15e2e3
callq 0x2220
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq