// File: /workspace/github_cpp_makefile_star2_O1/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, 0x68(%rsp)
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, 0x60(%rsp)
movl $0x40, %eax
movq %rdi, 0x18(%rsp)
subq 0x18(%rdi), %rax
movq %rax, 0x20(%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, (%r8)
movq %rax, 0x18(%r8)
cmpq %rax, (%rdx)
movq %r8, 0x38(%rsp)
je 0x1574f1
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
movq 0x10(%r12), %rax
leaq (%r14,%r14,2), %rbp
shlq $0x4, %rbp
cmpq $0x0, (%rax,%rbp)
je 0x1574e4
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x10(%rax,%rbp), %rdx
cmpb $0x0, 0x18(%rdx,%r13)
js 0x1574ce
movq (%r15), %rcx
movq 0x80(%r15), %rax
movl (%rcx,%r14,4), %esi
incq %rax
cmpq %rsi, %rax
jb 0x1574ce
movl 0x8(%rdx,%r13), %edx
cmpq %rdx, %r14
je 0x1574ce
movl (%rcx,%rdx,4), %ecx
cmpq %rcx, %rax
jb 0x1574ce
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x15747e
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r8)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
xorpd %xmm0, %xmm0
movq 0x38(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movupd %xmm0, (%rcx,%rax)
movupd %xmm0, 0x20(%rcx,%rax)
movupd %xmm0, 0x10(%rcx,%rax)
movl %r14d, 0x8(%rcx,%rax)
movq 0x10(%r12), %rdx
movq 0x10(%rdx,%rbp), %rdx
movl 0x8(%rdx,%r13), %edx
movl %edx, 0x14(%rcx,%rax)
movq $0x0, 0x20(%rcx,%rax)
movl $0x0, 0x18(%rcx,%rax)
incq %rbx
movq 0x10(%r12), %rax
addq $0x28, %r13
cmpq (%rax,%rbp), %rbx
jb 0x1573f9
incq %r14
cmpq (%r12), %r14
jb 0x1573dc
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %r8, %rbx
movq %r8, %rdi
callq 0xc405a
cmpl $0x0, 0x110(%rsp)
je 0x157554
movq (%rbx), %rbx
leaq (,%rbx,8), %rax
leaq (%rax,%rax,2), %rdi
callq 0x2280
testq %rbx, %rbx
movq %rax, 0x40(%rsp)
je 0x15755d
leaq (%rbx,%rbx,2), %rdx
cmpq $0x1, %rdx
adcq $0x0, %rdx
shlq $0x3, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
jmp 0x15755d
movq $0x0, 0x40(%rsp)
movq 0x60(%rsp), %rbx
cmpq $0x0, (%rbx)
movq 0x20(%rsp), %rcx
je 0x157924
xorl %r13d, %r13d
fld1
fldl 0xcdf44(%rip)           # 0x2254c0
fstpt 0xcc(%rsp)
flds 0xd7a9b(%rip)           # 0x22f024
fstpt 0xc0(%rsp)
fchs
fstpt 0xb4(%rsp)
movq 0x10(%rbx), %rdi
movq %r13, %rax
shlq $0x5, %rax
movq (%rdi,%rax), %r14
addq %r14, %r14
shrq %cl, %r14
movq 0x8(%rdi,%rax), %rbp
addq %rbp, %rbp
shrq %cl, %rbp
cmpq %rbp, %r14
je 0x157918
movq (%r15), %rdx
movq 0x80(%r15), %r8
movl (%rdx,%r14,4), %esi
incq %r8
cmpq %rsi, %r8
jb 0x157918
movl (%rdx,%rbp,4), %edx
cmpq %rdx, %r8
jb 0x157918
addq %rax, %rdi
cmpq $-0x1, 0x10(%rdi)
je 0x157918
movq %r12, %rsi
movq 0x18(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
callq 0x143e3e
movq %rax, 0x48(%rsp)
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %esi
movl %ebp, %edx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
leaq 0x5c(%rsp), %r9
callq 0x157162
movq %rbx, %rdi
movq 0x60(%rsp), %rbx
movl %ebp, %esi
movl %r14d, %edx
leaq 0x28(%rsp), %rcx
xorl %r8d, %r8d
leaq 0x58(%rsp), %r9
callq 0x157162
movq 0x20(%rsp), %rcx
cmpq $0x0, 0x30(%rsp)
je 0x157918
cmpq $0x0, 0x28(%rsp)
je 0x157918
fld1
movq 0x68(%rsp), %rax
testq %rax, %rax
je 0x157878
fstp %st(0)
fldt 0xcc(%rsp)
movq 0x48(%rsp), %r9
cmpq $-0x1, %r9
je 0x157803
fstp %st(0)
cmpq %r9, 0x18(%rax)
jbe 0x1576ed
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x1577a5
movq (%rax), %rax
movq %rcx, %rdx
negq %rdx
addq $0x20, %rax
movb $0x1, %dil
movl $0x1, %esi
cmpq %r9, -0x18(%rax)
jbe 0x1576cd
cmpq %r9, -0x20(%rax)
jbe 0x157750
cmpq %rcx, %rsi
setb %dil
leaq (%rdx,%rsi), %r8
incq %r8
incq %rsi
addq $0x28, %rax
cmpq $0x1, %r8
jne 0x1576bd
jmp 0x1577a5
movq 0x18(%rsp), %rcx
fldt 0x80(%rcx)
movq %r9, 0xa8(%rsp)
xorl %eax, %eax
testq %r9, %r9
sets %al
fildll 0xa8(%rsp)
leaq 0xd78d2(%rip), %rdx     # 0x22efe8
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 0x157803
testb $0x1, %dil
je 0x1577a5
movq (%rax), %rcx
movq %rcx, %xmm1
movq 0xcdada(%rip), %xmm2    # 0x225240
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0xcdade(%rip), %xmm3    # 0x225250
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
jmp 0x1577f3
movq 0x18(%rsp), %rcx
fldt 0x80(%rcx)
movq %r9, 0x90(%rsp)
xorl %eax, %eax
testq %r9, %r9
sets %al
fildll 0x90(%rsp)
leaq 0xd781a(%rip), %rdx     # 0x22efe8
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
movsd %xmm0, 0x80(%rsp)
fldl 0x80(%rsp)
fldz
fucomi %st(1), %st
fxch %st(1)
fcmovnbe %st(1), %st
fstp %st(1)
faddl 0xd779d(%rip)           # 0x22efb0
fldt 0xc0(%rsp)
fxch %st(1)
fucomi %st(1), %st
fcmovnbe %st(1), %st
fstp %st(1)
faddl 0xd77c8(%rip)           # 0x22eff0
fld1
fdivp %st, %st(1)
fldt 0xb4(%rsp)
faddp %st, %st(1)
fstpt (%rsp)
callq 0x20b0
fmuls 0xd77e5(%rip)           # 0x22f028
fldl 0xd77af(%rip)           # 0x22eff8
fucomi %st(1), %st
fxch %st(1)
fcmovnbe %st(1), %st
fstp %st(1)
fldl 0xd77a9(%rip)           # 0x22f000
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 0x20(%rsp), %rcx
cmpl $0x0, 0x110(%rsp)
je 0x1578cd
fstpt 0x48(%rsp)
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x157231
testl %eax, %eax
je 0x1578e5
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
addq %rcx, %rax
decq %rax
leaq (%rdx,%rax,8), %rax
jmp 0x1578ff
movq 0x30(%rsp), %rax
fld %st(0)
fsubrl 0x20(%rax)
fstpl 0x20(%rax)
movq 0x28(%rsp), %rax
addq $0x20, %rax
jmp 0x157904
movq 0x30(%rsp), %rax
fldt 0x48(%rsp)
fld %st(0)
fsubrl 0x20(%rax)
fstpl 0x20(%rax)
movq 0x28(%rsp), %rax
addq $0x20, %rax
movq 0x20(%rsp), %rcx
fsubrl (%rax)
fstpl (%rax)
movq 0x30(%rsp), %rax
incl 0x18(%rax)
movq 0x28(%rsp), %rax
incl 0x18(%rax)
incq %r13
cmpq (%rbx), %r13
jb 0x157599
cmpl $0x0, 0x110(%rsp)
je 0x1579a6
movq 0x38(%rsp), %rcx
movq (%rcx), %rax
testq %rax, %rax
movq 0x40(%rsp), %rdi
je 0x1579a1
movq 0x10(%rcx), %rcx
movl $0x10, %edx
movsd (%rcx,%rdx,2), %xmm1
movsd -0x10(%rdi,%rdx), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x15795f
movsd %xmm0, (%rcx,%rdx,2)
movsd (%rcx,%rdx,2), %xmm1
movsd -0x8(%rdi,%rdx), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x157975
movsd %xmm0, (%rcx,%rdx,2)
movsd (%rcx,%rdx,2), %xmm1
movsd (%rdi,%rdx), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x15798a
movsd %xmm0, (%rcx,%rdx,2)
movsd (%rcx,%rdx,2), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,2)
addq $0x18, %rdx
decq %rax
jne 0x157949
callq 0x2220
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq