// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: get_weightest_node(kv_u_trans_t*, bubble_type*, ma_ug_t*, signed char*, unsigned char*)
uint32_t get_weightest_node(kv_u_trans_t *ta, bubble_type* bub, ma_ug_t* ug, int8_t *s, uint8_t *vis)
{
    double w_a, w_n, max_w_a, max_w_n;
    uint32_t i, occ, k, m, v, *a = NULL, a_n, e_n, max_w_a_i, max_w_n_i;
    u_trans_t *e = NULL;
    max_w_a = max_w_n = -1; max_w_a_i = max_w_n_i = (uint32_t)-1;
    for (i = 0; i < bub->f_bub; i++)
    {
        get_bubbles(bub, i, NULL, NULL, &a, &a_n, NULL);
        if(vis[i]) continue;
        for (k = 0, w_a = w_n = 0; k < a_n; k++)
        {
            v = a[k]>>1;
            if(s[v] != 0) break; 
            e = u_trans_a(*ta, v);
            e_n = u_trans_n(*ta, v);
            for (m = 0; m < e_n; m++)
            {
                if(s[e[m].tn] != 0)
                {
                    w_a += (e[m].nw>=0?e[m].nw:-e[m].nw);
                }
                else
                {
                    w_n += (e[m].nw>=0?e[m].nw:-e[m].nw);
                }
            }
        }

        if(k >= a_n && a_n > 0)//unset whole bubble
        {
            if(w_a > max_w_a)
            {
                max_w_a_i = i<<1;
                max_w_a = w_a;
            }

            if(w_n > max_w_n)
            {
                max_w_n_i = i<<1;
                max_w_n = w_n;
            }
        }
        else
        {
            for (k = occ = 0; k < a_n; k++)
            {
                v = a[k]>>1;
                if(s[v] != 0)
                {
                    occ++;
                    continue;
                } 
                
                e = u_trans_a(*ta, v);
                e_n = u_trans_n(*ta, v);
                for (m = 0, w_a = w_n = 0; m < e_n; m++)
                {
                    if(s[e[m].tn] != 0)
                    {
                        w_a += (e[m].nw>=0?e[m].nw:-e[m].nw);
                    }
                    else
                    {
                        w_n += (e[m].nw>=0?e[m].nw:-e[m].nw);
                    }
                }

                if(w_a > max_w_a)
                {
                    max_w_a_i = (v<<1)+1;
                    max_w_a = w_a;
                }

                if(w_n > max_w_n)
                {
                    max_w_n_i = (v<<1)+1;
                    max_w_n = w_n;
                }
            }

            if(occ == a_n) vis[i] = 1;
        }
    }

    for (i = 0; i < ug->g->n_seq; i++)
    {
        if(s[i] != 0 || (IF_BUB(i, *bub))) continue;
        v = i;
        e = u_trans_a(*ta, v);
        e_n = u_trans_n(*ta, v);
        for (m = 0, w_a = w_n = 0; m < e_n; m++)
        {
            if(s[e[m].tn] != 0)
            {
                w_a += (e[m].nw>=0?e[m].nw:-e[m].nw);
            }
            else
            {
                w_n += (e[m].nw>=0?e[m].nw:-e[m].nw);
            }
        }

        if(w_a > max_w_a)
        {
            max_w_a_i = (i<<1)+1;
            max_w_a = w_a;
        }

        if(w_n > max_w_n)
        {
            max_w_n_i = (i<<1)+1;
            max_w_n = w_n;
        }
    }
    
    if(max_w_a_i != (uint32_t)-1) return max_w_a_i;
    return max_w_n_i;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, -0x8(%rsp)
movq 0x80(%rsi), %rbx
testq %rbx, %rbx
je 0x151f7c
movsd 0xd3810(%rip), %xmm1    # 0x225538
xorl %r10d, %r10d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movapd 0xdd288(%rip), %xmm0    # 0x22efc0
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movapd %xmm1, %xmm2
movq %rsi, -0x10(%rsp)
cmpb $0x0, (%r8,%r10)
je 0x151d5d
movapd %xmm2, %xmm4
movapd %xmm1, %xmm3
movl %edx, %eax
jmp 0x151f5d
movl %edx, -0x14(%rsp)
movq 0x28(%rsi), %rax
movq 0x40(%rsi), %rsi
movl (%rsi,%r10,4), %r9d
movl 0x4(%rsi,%r10,4), %ebp
leaq (%rax,%r9,4), %rbx
addq $0x8, %rbx
subl %r9d, %ebp
movl %ebp, %eax
addl $-0x2, %eax
je 0x151e2a
xorpd %xmm3, %xmm3
xorl %r14d, %r14d
xorpd %xmm4, %xmm4
movl %r14d, %esi
movl (%rbx,%rsi,4), %esi
shrl %esi
cmpb $0x0, (%rcx,%rsi)
jne 0x151e35
movq 0x28(%rdi), %r9
movq (%r9,%rsi,8), %rsi
testl %esi, %esi
je 0x151e19
movq 0x10(%rdi), %r9
movq %rsi, %r15
shrq $0x20, %r15
movl %esi, %esi
leaq (%r15,%r15,2), %r15
shlq $0x4, %r15
addq %r9, %r15
addq $0x20, %r15
shlq $0x4, %rsi
leaq (%rsi,%rsi,2), %r9
xorl %r12d, %r12d
movl -0xc(%r15,%r12), %esi
cmpb $0x0, (%rcx,%rsi)
movsd (%r15,%r12), %xmm6
movapd %xmm6, %xmm7
xorpd %xmm0, %xmm7
movapd %xmm7, %xmm5
cmpnlesd %xmm6, %xmm5
andpd %xmm5, %xmm7
andnpd %xmm6, %xmm5
orpd %xmm7, %xmm5
je 0x151e0c
addsd %xmm5, %xmm4
jmp 0x151e10
addsd %xmm5, %xmm3
addq $0x30, %r12
cmpq %r12, %r9
jne 0x151dd8
incl %r14d
cmpl %eax, %r14d
jne 0x151d93
movl %eax, %r14d
jmp 0x151e35
xorl %r14d, %r14d
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
addl $-0x3, %ebp
cmpl %r14d, %ebp
jae 0x151e5c
ucomisd %xmm2, %xmm4
leal (%r10,%r10), %eax
cmoval %eax, %r13d
maxsd %xmm2, %xmm4
ucomisd %xmm1, %xmm3
jbe 0x151f50
jmp 0x151f58
testl %eax, %eax
je 0x151f41
movl %eax, %r14d
xorl %r15d, %r15d
xorl %ebp, %ebp
movl (%rbx,%r15,4), %r12d
movl %r12d, %esi
shrl %esi
cmpb $0x0, (%rcx,%rsi)
je 0x151e82
incl %ebp
jmp 0x151f33
movl %r13d, %edx
movq 0x28(%rdi), %r9
movq (%r9,%rsi,8), %rsi
testl %esi, %esi
je 0x151f05
movq 0x10(%rdi), %r9
movq %rsi, %r13
shrq $0x20, %r13
movl %esi, %esi
leaq (%r13,%r13,2), %r13
shlq $0x4, %r13
addq %r9, %r13
addq $0x20, %r13
shlq $0x4, %rsi
leaq (%rsi,%rsi,2), %r9
xorpd %xmm3, %xmm3
xorl %esi, %esi
xorpd %xmm4, %xmm4
movl -0xc(%r13,%rsi), %r11d
cmpb $0x0, (%rcx,%r11)
movsd (%r13,%rsi), %xmm6
movapd %xmm6, %xmm7
xorpd %xmm0, %xmm7
movapd %xmm7, %xmm5
cmpnlesd %xmm6, %xmm5
andpd %xmm5, %xmm7
andnpd %xmm6, %xmm5
orpd %xmm7, %xmm5
je 0x151ef6
addsd %xmm5, %xmm4
jmp 0x151efa
addsd %xmm5, %xmm3
addq $0x30, %rsi
cmpq %rsi, %r9
jne 0x151ec0
jmp 0x151f0d
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
orl $0x1, %r12d
ucomisd %xmm2, %xmm4
movl %edx, %r13d
cmoval %r12d, %r13d
maxsd %xmm2, %xmm4
movapd %xmm4, %xmm2
ucomisd %xmm1, %xmm3
jbe 0x151f33
movapd %xmm3, %xmm1
movl %r12d, -0x14(%rsp)
incq %r15
cmpq %r14, %r15
jne 0x151e6c
jmp 0x151f43
xorl %ebp, %ebp
cmpl %eax, %ebp
jne 0x151f4c
movb $0x1, (%r8,%r10)
movapd %xmm2, %xmm4
movapd %xmm1, %xmm3
movl -0x14(%rsp), %eax
movq -0x10(%rsp), %rsi
incl %r10d
movq 0x80(%rsi), %rbx
movl %eax, %edx
movapd %xmm3, %xmm1
movapd %xmm4, %xmm2
cmpq %r10, %rbx
ja 0x151d47
jmp 0x151f93
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movsd 0xd35ae(%rip), %xmm4    # 0x225538
movapd %xmm4, %xmm3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq -0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
movl 0x14(%rdx), %edx
andq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
je 0x152096
incq %rbx
xorl %r8d, %r8d
movapd 0xdd006(%rip), %xmm0    # 0x22efc0
cmpb $0x0, (%rcx,%r8)
jne 0x151fcd
movq (%rsi), %r9
movl (%r9,%r8,4), %r9d
cmpq %r9, %rbx
jbe 0x151fea
movapd %xmm4, %xmm1
movapd %xmm3, %xmm2
incq %r8
movapd %xmm2, %xmm3
movapd %xmm1, %xmm4
cmpq %rdx, %r8
jne 0x151fba
jmp 0x152096
movq 0x28(%rdi), %r9
movq (%r9,%r8,8), %r11
testl %r11d, %r11d
je 0x15206b
movq 0x10(%rdi), %r9
movq %r11, %r10
shrq $0x20, %r10
movl %r11d, %r11d
leaq (%r10,%r10,2), %r10
shlq $0x4, %r10
addq %r9, %r10
addq $0x20, %r10
shlq $0x4, %r11
leaq (%r11,%r11,2), %r9
xorpd %xmm2, %xmm2
xorl %r14d, %r14d
xorpd %xmm1, %xmm1
movl -0xc(%r10,%r14), %r11d
cmpb $0x0, (%rcx,%r11)
movsd (%r10,%r14), %xmm6
movapd %xmm6, %xmm7
xorpd %xmm0, %xmm7
movapd %xmm7, %xmm5
cmpnlesd %xmm6, %xmm5
andpd %xmm5, %xmm7
andnpd %xmm6, %xmm5
orpd %xmm7, %xmm5
je 0x15205c
addsd %xmm5, %xmm1
jmp 0x152060
addsd %xmm5, %xmm2
addq $0x30, %r14
cmpq %r14, %r9
jne 0x152027
jmp 0x152073
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm4, %xmm1
leaq 0x1(%r8,%r8), %r9
cmoval %r9d, %r13d
maxsd %xmm4, %xmm1
ucomisd %xmm3, %xmm2
jbe 0x151fd1
movl %r9d, %eax
jmp 0x151fd5
cmpl $-0x1, %r13d
cmovnel %r13d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq