// File: /workspace/github_cpp_makefile_star2_O3/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 0x80(%rsi), %r13
testq %r13, %r13
je 0x158707
movsd 0xd9197(%rip), %xmm0    # 0x231638
xorl %eax, %eax
movl $0xffffffff, -0x14(%rsp) # imm = 0xFFFFFFFF
movapd 0xe2d1d(%rip), %xmm2    # 0x23b1d0
movl $0xffffffff, -0x18(%rsp) # imm = 0xFFFFFFFF
movapd %xmm0, %xmm1
movq %rsi, -0x10(%rsp)
cmpb $0x0, (%r8,%rax)
je 0x1584d7
incl %eax
cmpq %rax, %r13
ja 0x1584c4
jmp 0x158702
movq 0x40(%rsi), %r9
movl (%r9,%rax,4), %r10d
movl 0x4(%r9,%rax,4), %ebp
subl %r10d, %ebp
movl %ebp, %ebx
addl $-0x2, %ebx
je 0x1585e2
movq 0x28(%rsi), %r9
leaq (%r9,%r10,4), %r14
addq $0x8, %r14
xorpd %xmm3, %xmm3
xorl %r15d, %r15d
xorpd %xmm4, %xmm4
movl %r15d, %r9d
movl (%r14,%r9,4), %r10d
shrl %r10d
cmpb $0x0, (%rcx,%r10)
jne 0x1585a4
movq 0x28(%rdi), %r9
movq (%r9,%r10,8), %r11
testl %r11d, %r11d
je 0x158595
movq 0x10(%rdi), %r9
movq %r11, %r10
shrq $0x20, %r10
movl %r11d, %r11d
leaq (%r10,%r10,2), %r10
shlq $0x4, %r10
addq %r10, %r9
addq $0x20, %r9
shlq $0x4, %r11
leaq (%r11,%r11,2), %r11
xorl %r12d, %r12d
movl -0xc(%r9,%r12), %r10d
cmpb $0x0, (%rcx,%r10)
movsd (%r9,%r12), %xmm6
movapd %xmm6, %xmm7
xorpd %xmm2, %xmm7
movapd %xmm7, %xmm5
cmpnlesd %xmm6, %xmm5
andpd %xmm5, %xmm7
andnpd %xmm6, %xmm5
orpd %xmm7, %xmm5
je 0x158588
addsd %xmm5, %xmm4
jmp 0x15858c
addsd %xmm5, %xmm3
addq $0x30, %r12
cmpq %r12, %r11
jne 0x158553
incl %r15d
cmpl %ebx, %r15d
jne 0x158509
movl %ebx, %r15d
addl $-0x3, %ebp
cmpl %r15d, %ebp
jae 0x1585ec
ucomisd %xmm1, %xmm4
leal (%rax,%rax), %r9d
movl -0x18(%rsp), %r10d
cmoval %r9d, %r10d
movl %r10d, -0x18(%rsp)
maxsd %xmm1, %xmm4
movapd %xmm4, %xmm1
ucomisd %xmm0, %xmm3
jbe 0x1584cb
movapd %xmm3, %xmm0
movl %r9d, -0x14(%rsp)
jmp 0x1584cb
movq %r13, -0x8(%rsp)
jmp 0x1586d7
movq %r13, -0x8(%rsp)
testl %ebx, %ebx
je 0x1586d7
movl %ebx, %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
movl (%r14,%r12,4), %r13d
movl %r13d, %esi
shrl %esi
cmpb $0x0, (%rcx,%rsi)
je 0x158617
incl %ebp
jmp 0x1586c9
movq 0x28(%rdi), %r9
movq (%r9,%rsi,8), %rsi
testl %esi, %esi
je 0x158696
movq 0x10(%rdi), %r9
movq %rsi, %r10
shrq $0x20, %r10
movl %esi, %esi
leaq (%r10,%r10,2), %r10
shlq $0x4, %r10
addq %r10, %r9
addq $0x20, %r9
shlq $0x4, %rsi
leaq (%rsi,%rsi,2), %rsi
xorpd %xmm3, %xmm3
xorl %r10d, %r10d
xorpd %xmm4, %xmm4
movl -0xc(%r9,%r10), %r11d
cmpb $0x0, (%rcx,%r11)
movsd (%r9,%r10), %xmm6
movapd %xmm6, %xmm7
xorpd %xmm2, %xmm7
movapd %xmm7, %xmm5
cmpnlesd %xmm6, %xmm5
andpd %xmm5, %xmm7
andnpd %xmm6, %xmm5
orpd %xmm7, %xmm5
je 0x158687
addsd %xmm5, %xmm4
jmp 0x15868b
addsd %xmm5, %xmm3
addq $0x30, %r10
cmpq %r10, %rsi
jne 0x158652
jmp 0x15869e
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
orl $0x1, %r13d
ucomisd %xmm1, %xmm4
movl -0x18(%rsp), %esi
cmoval %r13d, %esi
movl %esi, -0x18(%rsp)
maxsd %xmm1, %xmm4
movapd %xmm4, %xmm1
ucomisd %xmm0, %xmm3
jbe 0x1586c9
movapd %xmm3, %xmm0
movl %r13d, -0x14(%rsp)
incq %r12
cmpq %r15, %r12
jne 0x158601
jmp 0x1586d9
xorl %ebp, %ebp
cmpl %ebx, %ebp
jne 0x1586f3
movb $0x1, (%r8,%rax)
movq -0x10(%rsp), %rsi
movq 0x80(%rsi), %r13
jmp 0x1584cb
movq -0x10(%rsp), %rsi
movq -0x8(%rsp), %r13
jmp 0x1584cb
incq %r13
jmp 0x158729
movl $0x1, %r13d
movl $0xffffffff, -0x18(%rsp) # imm = 0xFFFFFFFF
movsd 0xd8f1b(%rip), %xmm1    # 0x231638
movapd %xmm1, %xmm0
movl $0xffffffff, -0x14(%rsp) # imm = 0xFFFFFFFF
movq 0x18(%rdx), %rax
movl 0x14(%rax), %r10d
andq $0x7fffffff, %r10       # imm = 0x7FFFFFFF
je 0x158833
xorl %r8d, %r8d
movapd 0xe2a87(%rip), %xmm2    # 0x23b1d0
movl -0x14(%rsp), %edx
cmpb $0x0, (%rcx,%r8)
jne 0x158760
movq (%rsi), %rax
movl (%rax,%r8,4), %eax
cmpq %rax, %r13
jbe 0x158781
movapd %xmm1, %xmm3
movapd %xmm0, %xmm4
movl %edx, %eax
incq %r8
movl %eax, %edx
movapd %xmm4, %xmm0
movapd %xmm3, %xmm1
cmpq %r10, %r8
jne 0x15874d
jmp 0x158837
movq 0x28(%rdi), %rax
movq (%rax,%r8,8), %rax
testl %eax, %eax
je 0x158801
movq 0x10(%rdi), %rbx
movq %rax, %r9
shrq $0x20, %r9
movl %eax, %r11d
leaq (%r9,%r9,2), %rax
shlq $0x4, %rax
addq %rbx, %rax
addq $0x20, %rax
shlq $0x4, %r11
leaq (%r11,%r11,2), %rbx
xorpd %xmm4, %xmm4
xorl %r9d, %r9d
xorpd %xmm3, %xmm3
movl -0xc(%rax,%r9), %r11d
cmpb $0x0, (%rcx,%r11)
movsd (%rax,%r9), %xmm6
movapd %xmm6, %xmm7
xorpd %xmm2, %xmm7
movapd %xmm7, %xmm5
cmpnlesd %xmm6, %xmm5
andpd %xmm5, %xmm7
andnpd %xmm6, %xmm5
orpd %xmm7, %xmm5
je 0x1587f2
addsd %xmm5, %xmm3
jmp 0x1587f6
addsd %xmm5, %xmm4
addq $0x30, %r9
cmpq %r9, %rbx
jne 0x1587bd
jmp 0x158809
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
ucomisd %xmm1, %xmm3
leal 0x1(%r8,%r8), %eax
movl -0x18(%rsp), %r9d
cmoval %eax, %r9d
movl %r9d, -0x18(%rsp)
maxsd %xmm1, %xmm3
ucomisd %xmm0, %xmm4
jbe 0x158764
jmp 0x15876a
movl -0x14(%rsp), %eax
movl -0x18(%rsp), %ecx
cmpl $-0x1, %ecx
cmovnel %ecx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq