// File: /workspace/github_cpp_makefile_star2_O2/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)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movsd 0xd1d47(%rip), %xmm2    # 0x1e3580
movapd 0xdb87f(%rip), %xmm0    # 0x1ed0c0
movapd %xmm2, %xmm1
movl %r13d, %eax
movq %rsi, -0x10(%rsp)
movq %r8, -0x18(%rsp)
movl %r10d, %r10d
movq 0x80(%rsi), %r11
cmpq %r10, %r11
jbe 0x111a5b
cmpb $0x0, (%r8,%r10)
je 0x11187b
movapd %xmm2, %xmm3
movapd %xmm1, %xmm4
movl %eax, %edx
jmp 0x111a49
movq 0x28(%rsi), %rdx
movq 0x40(%rsi), %r9
movl (%r9,%r10,4), %ebx
movl 0x4(%r9,%r10,4), %r14d
leaq (%rdx,%rbx,4), %r11
addq $0x8, %r11
subl %ebx, %r14d
leal -0x2(%r14), %r8d
xorl %ebp, %ebp
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
cmpl %r8d, %ebp
je 0x111924
movl %ebp, %esi
movl (%r11,%rsi,4), %esi
shrl %esi
cmpb $0x0, (%rcx,%rsi)
jne 0x111927
movq 0x10(%rdi), %r9
movq 0x28(%rdi), %r15
movq (%r15,%rsi,8), %rsi
movl %esi, %r15d
shrq $0x20, %rsi
imulq $0x30, %r15, %r15
imulq $0x30, %rsi, %rsi
addq %rsi, %r9
addq $0x20, %r9
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x111920
movl -0xc(%r9,%r12), %esi
cmpb $0x0, (%rcx,%rsi)
movsd (%r9,%r12), %xmm6
movapd %xmm6, %xmm7
xorpd %xmm0, %xmm7
movapd %xmm7, %xmm5
cmpnlesd %xmm6, %xmm5
andpd %xmm5, %xmm7
andnpd %xmm6, %xmm5
orpd %xmm7, %xmm5
je 0x111916
addsd %xmm5, %xmm3
jmp 0x11191a
addsd %xmm5, %xmm4
addq $0x30, %r12
jmp 0x1118dd
incl %ebp
jmp 0x1118a5
movl %r8d, %ebp
addl $-0x3, %r14d
cmpl %ebp, %r14d
jae 0x11194f
ucomisd %xmm2, %xmm3
leal (%r10,%r10), %edx
cmoval %edx, %r13d
maxsd %xmm2, %xmm3
ucomisd %xmm1, %xmm4
jbe 0x111a39
jmp 0x111a3f
movl %r8d, %r14d
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq %r14, %r15
je 0x111a15
movl (%r11,%r15,4), %r12d
movl %r12d, %esi
shrl %esi
cmpb $0x0, (%rcx,%rsi)
je 0x111976
incl %ebp
jmp 0x111a0d
movl %r13d, %ebx
movq 0x10(%rdi), %r9
movq 0x28(%rdi), %r13
movq (%r13,%rsi,8), %rsi
movl %esi, %r13d
shrq $0x20, %rsi
imulq $0x30, %r13, %r13
imulq $0x30, %rsi, %rsi
addq %rsi, %r9
addq $0x20, %r9
xorpd %xmm3, %xmm3
xorl %esi, %esi
xorpd %xmm4, %xmm4
cmpq %rsi, %r13
je 0x1119e9
movl -0xc(%r9,%rsi), %edx
cmpb $0x0, (%rcx,%rdx)
movsd (%r9,%rsi), %xmm6
movapd %xmm6, %xmm7
xorpd %xmm0, %xmm7
movapd %xmm7, %xmm5
cmpnlesd %xmm6, %xmm5
andpd %xmm5, %xmm7
andnpd %xmm6, %xmm5
orpd %xmm7, %xmm5
je 0x1119df
addsd %xmm5, %xmm3
jmp 0x1119e3
addsd %xmm5, %xmm4
addq $0x30, %rsi
jmp 0x1119a6
orl $0x1, %r12d
ucomisd %xmm2, %xmm3
movl %ebx, %r13d
cmoval %r12d, %r13d
maxsd %xmm2, %xmm3
movapd %xmm3, %xmm2
ucomisd %xmm1, %xmm4
jbe 0x111a0d
movapd %xmm4, %xmm1
movl %r12d, %eax
incq %r15
jmp 0x111957
cmpl %r8d, %ebp
jne 0x111a35
movq -0x18(%rsp), %r8
movb $0x1, (%r8,%r10)
movapd %xmm2, %xmm3
movapd %xmm1, %xmm4
movl %eax, %edx
movq -0x10(%rsp), %rsi
jmp 0x111a49
movapd %xmm2, %xmm3
movapd %xmm1, %xmm4
movl %eax, %edx
movq -0x10(%rsp), %rsi
movq -0x18(%rsp), %r8
incl %r10d
movapd %xmm3, %xmm2
movapd %xmm4, %xmm1
movl %edx, %eax
jmp 0x111852
movq -0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
movl 0x14(%rdx), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %r8d, %r8d
cmpq %r14, %r8
je 0x111b34
cmpb $0x0, (%rcx,%r8)
jne 0x111afd
movq (%rsi), %rdx
movl (%rdx,%r8,4), %edx
cmpq %rdx, %r11
jae 0x111afd
movq 0x10(%rdi), %rdx
movq 0x28(%rdi), %r9
movq (%r9,%r8,8), %r9
movl %r9d, %r10d
shrq $0x20, %r9
imulq $0x30, %r10, %r10
imulq $0x30, %r9, %r9
addq %rdx, %r9
addq $0x20, %r9
xorpd %xmm3, %xmm3
xorl %ebx, %ebx
xorpd %xmm4, %xmm4
cmpq %rbx, %r10
je 0x111b1b
movl -0xc(%r9,%rbx), %edx
cmpb $0x0, (%rcx,%rdx)
movsd (%r9,%rbx), %xmm6
movapd %xmm6, %xmm7
xorpd %xmm0, %xmm7
movapd %xmm7, %xmm5
cmpnlesd %xmm6, %xmm5
andpd %xmm5, %xmm7
andnpd %xmm6, %xmm5
orpd %xmm7, %xmm5
je 0x111af3
addsd %xmm5, %xmm3
jmp 0x111af7
addsd %xmm5, %xmm4
addq $0x30, %rbx
jmp 0x111aba
movapd %xmm2, %xmm3
movapd %xmm1, %xmm4
movl %eax, %r9d
incq %r8
movapd %xmm3, %xmm2
movapd %xmm4, %xmm1
movl %r9d, %eax
jmp 0x111a72
ucomisd %xmm2, %xmm3
leal 0x1(%r8,%r8), %r9d
cmoval %r9d, %r13d
maxsd %xmm2, %xmm3
ucomisd %xmm1, %xmm4
jbe 0x111b01
jmp 0x111b08
cmpl $-0x1, %r13d
cmovnel %r13d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq