// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %r9, 0x50(%rsp)
movq %r8, %rbx
movq %rdi, 0x28(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0x18(%rsp)
movq %rcx, %r12
andq $0x0, 0x10(%rsp)
andq $0x0, (%r8)
movq %rsi, (%rsp)
andq $0x0, 0x18(%r8)
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
movq %rdx, 0x48(%rsp)
cmpq (%rdx), %r15
jae 0x117999
movq 0x10(%rdx), %rcx
imulq $0x30, %r15, %r14
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq (%rcx,%r14), %r13
jae 0x117991
leaq (%rcx,%r14), %rax
movq 0x10(%rax), %rsi
cmpb $0x0, 0x18(%rsi,%rbp)
js 0x117985
movq (%r12), %r8
movq 0x80(%r12), %rax
movl (%r8,%r15,4), %edi
incq %rax
cmpq %rdi, %rax
jb 0x117985
movl 0x8(%rsi,%rbp), %esi
cmpq %rsi, %r15
je 0x117985
movl (%r8,%rsi,4), %esi
cmpq %rsi, %rax
jb 0x117985
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x11794e
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
callq 0x2370
xorpd %xmm0, %xmm0
movq 0x48(%rsp), %rdx
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
imulq $0x30, %rcx, %rdi
movupd %xmm0, (%rax,%rdi)
movupd %xmm0, 0x20(%rax,%rdi)
movupd %xmm0, 0x10(%rax,%rdi)
movl %r15d, 0x8(%rax,%rdi)
movq 0x10(%rdx), %rcx
movq 0x10(%rcx,%r14), %rsi
movl 0x8(%rsi,%rbp), %esi
movl %esi, 0x14(%rax,%rdi)
andl $0x0, 0x18(%rax,%rdi)
incq %r13
addq $0x28, %rbp
jmp 0x1178c8
incq %r15
jmp 0x1178b2
pushq $0x40
popq %r15
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %rbx, %rdi
callq 0x88a31
cmpl $0x0, 0xa0(%rsp)
je 0x1179ef
movq (%rbx), %rax
leaq (%rax,%rax,2), %r14
imulq $0x18, %rax, %rdi
callq 0x2280
movq %rax, %r13
xorl %eax, %eax
movq (%rsp), %rdx
movq %r15, %rcx
cmpq %rax, %r14
je 0x1179f9
andq $0x0, (%r13,%rax,8)
incq %rax
jmp 0x1179df
xorl %r13d, %r13d
movq (%rsp), %rdx
movq %r15, %rcx
subq 0x8(%rsp), %rcx
xorl %ebp, %ebp
xorl %r8d, %r8d
movq %rcx, 0x20(%rsp)
cmpq (%rdx), %r8
jae 0x117bd5
movq 0x10(%rdx), %rdi
movq (%rdi,%rbp), %r15
addq %r15, %r15
shrq %cl, %r15
movq 0x8(%rdi,%rbp), %r14
addq %r14, %r14
shrq %cl, %r14
cmpq %r14, %r15
je 0x117bc9
movq (%r12), %rsi
movq 0x80(%r12), %rax
movl (%rsi,%r15,4), %edx
incq %rax
cmpq %rdx, %rax
movq (%rsp), %rdx
jb 0x117bc9
movl (%rsi,%r14,4), %esi
cmpq %rsi, %rax
jb 0x117bc9
cmpq $-0x1, 0x10(%rdi,%rbp)
je 0x117bc9
addq %rbp, %rdi
movq 0x48(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0x64(%rsp), %rcx
movq %r8, 0x8(%rsp)
callq 0x102875
movq %rax, 0x30(%rsp)
movq %rbx, %rdi
movl %r15d, %esi
movl %r14d, %edx
leaq 0x18(%rsp), %rcx
xorl %r8d, %r8d
leaq 0x44(%rsp), %r9
callq 0x117663
movq %rbx, %rdi
movl %r14d, %esi
movl %r15d, %edx
leaq 0x10(%rsp), %rcx
xorl %r8d, %r8d
leaq 0x40(%rsp), %r9
callq 0x117663
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %rcx
movq (%rsp), %rdx
cmpq $0x0, 0x18(%rsp)
je 0x117bc9
cmpq $0x0, 0x10(%rsp)
je 0x117bc9
fld1
movq 0x50(%rsp), %rdx
testq %rdx, %rdx
je 0x117b1a
fstp %st(0)
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x1216b6
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %rcx
movsd %xmm0, 0x58(%rsp)
fldl 0x58(%rsp)
cmpl $0x0, 0xa0(%rsp)
je 0x117b7b
fstpt 0x30(%rsp)
movq 0x28(%rsp), %rdi
movq (%rsp), %rsi
movq %r8, %rdx
callq 0x117727
testl %eax, %eax
je 0x117b99
imull $0x3, 0x44(%rsp), %ecx
movl %eax, %eax
addq %rax, %rcx
decq %rcx
fldt 0x30(%rsp)
fld %st(0)
fsubrl (%r13,%rcx,8)
fstpl (%r13,%rcx,8)
imull $0x3, 0x40(%rsp), %ecx
addq %rcx, %rax
decq %rax
fsubrl (%r13,%rax,8)
fstpl (%r13,%rax,8)
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rsi
jmp 0x117bb5
movq 0x18(%rsp), %rax
fld %st(0)
fsubrl 0x20(%rax)
fstpl 0x20(%rax)
movq 0x10(%rsp), %rsi
fsubrl 0x20(%rsi)
fstpl 0x20(%rsi)
movq (%rsp), %rdx
jmp 0x117bc3
movq 0x18(%rsp), %rax
fldt 0x30(%rsp)
fld %st(0)
fsubrl 0x20(%rax)
fstpl 0x20(%rax)
movq 0x10(%rsp), %rsi
fsubrl 0x20(%rsi)
fstpl 0x20(%rsi)
movq (%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r8
incl 0x18(%rax)
incl 0x18(%rsi)
incq %r8
addq $0x20, %rbp
jmp 0x117a08
cmpl $0x0, 0xa0(%rsp)
je 0x117c28
movq (%rbx), %rax
pushq $0x10
popq %rcx
subq $0x1, %rax
jb 0x117c20
movq 0x10(%rbx), %rdx
movsd -0x10(%r13,%rcx), %xmm0
movsd -0x8(%r13,%rcx), %xmm1
minsd (%rdx,%rcx,2), %xmm0
minsd %xmm0, %xmm1
movsd (%r13,%rcx), %xmm0
minsd %xmm1, %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, (%rdx,%rcx,2)
addq $0x18, %rcx
jmp 0x117be5
movq %r13, %rdi
callq 0x2220
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq