// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: get_weightest_hap_label_from_bubble(unsigned long, H_partition*, bubble_type*, unsigned char*, unsigned int*, double*)
uint32_t get_weightest_hap_label_from_bubble(uint64_t bid, H_partition* hap, bubble_type* bub, 
uint8_t* hap_label_flag, uint32_t* max_hap_label, double* max_hap_weight)
{
    (*max_hap_label) = (uint32_t)-1;

    kv_resize(double, hap->label_buffer, (hap->label>>hap->label_shift)+1);
    hap->label_buffer.n = (hap->label>>hap->label_shift)+1;
    uint32_t i, m, is_set_ava, is_unset_ava, a_n, *x_a, x_n, x;
    hc_edge* a = NULL;
    for (i = 0; i < hap->label_buffer.n; i++)
    {
        hap->label_buffer.a[i] = 0;
    }

    ///bid might be bubble or non-bubble
    get_bubbles(bub, bid, NULL, NULL, &x_a, &x_n, NULL);
    for (m = is_set_ava = is_unset_ava = 0; m < x_n; m++)
    {
        ///x is uid
        x = x_a[m]>>1;
        a_n = hap->link->a.a[x].e.n;
        a = hap->link->a.a[x].e.a;
        for (i = 0; i < a_n; i++)
        {
            if(a[i].del) continue;
            if(is_hap_set(a[i].uID, *hap))
            {
                if(hap_label_flag && hap_label_flag[hap->hap[a[i].uID]] == 0) continue; ///not at current chain
                hap->label_buffer.a[hap->hap[a[i].uID]>>hap->label_shift] += a[i].weight;
                is_set_ava = 1;
            }
            is_unset_ava = 1;
        }
    }
    if(hap_label_flag && is_set_ava == 0) return 0; ///no connection in current chain
    
    if(is_unset_ava == 0) return 0; ///no any connection
    if(is_set_ava == 0 && is_unset_ava > 0) ///update hap->label
    {
        hap->label += hap->label_add;
        (*max_hap_label) = hap->label;
        return 1;
    }
    
    double max_weight;
    uint32_t max_i;
    for (i = 0, max_weight = -1, max_i = (uint32_t)-1; i < hap->label_buffer.n; i++)
    {
        if(hap->label_buffer.a[i] > max_weight)
        {
            max_weight = hap->label_buffer.a[i];
            max_i = i;
        }
    }

    (*max_hap_label) = max_i<<hap->label_shift;
    if(max_hap_weight) (*max_hap_weight) = max_weight;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x10(%rsp)
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %r15
movq %r8, 0x18(%rsp)
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
movl 0x24(%rsi), %eax
movb 0x2c(%rsi), %cl
shrl %cl, %eax
movq %rdi, %rbx
incl %eax
cmpq %rax, 0x80(%rsi)
jae 0x14cfbb
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x80(%r15)
movq 0x88(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x88(%r15)
movl 0x24(%r15), %eax
movl 0x2c(%r15), %ebp
movl %eax, 0xc(%rsp)
movl %ebp, %ecx
shrl %cl, %eax
incl %eax
movq %rax, 0x20(%rsp)
movq %rax, 0x78(%r15)
je 0x14cfee
movq 0x88(%r15), %rdi
movl 0x20(%rsp), %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x40(%r13), %rcx
movl (%rcx,%rbx,4), %eax
movl 0x4(%rcx,%rbx,4), %ecx
subl %eax, %ecx
addl $-0x2, %ecx
je 0x14d0c3
movq 0x28(%r13), %rdx
leaq (%rdx,%rax,4), %rax
addq $0x8, %rax
movq 0x30(%r15), %rdx
movq 0x10(%rdx), %rdx
movl %ecx, %esi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %ecx, %ecx
movl (%rax,%rdi,4), %r9d
shrl %r9d
leaq (%r9,%r9,2), %r9
shlq $0x4, %r9
movl (%rdx,%r9), %r10d
testq %r10, %r10
je 0x14d0aa
movq 0x10(%rdx,%r9), %r9
shlq $0x3, %r10
leaq (%r10,%r10,4), %r10
xorl %r11d, %r11d
cmpb $0x0, 0x18(%r9,%r11)
js 0x14d0a1
movq 0x10(%r15), %rbx
movl 0x8(%r9,%r11), %r13d
movl 0x1c(%r15), %r14d
orl 0x18(%r15), %r14d
movl (%rbx,%r13,4), %ebx
orl 0x20(%r15), %r14d
testl %ebx, %r14d
je 0x14d09b
testq %r12, %r12
je 0x14d07b
cmpb $0x0, (%r12,%rbx)
je 0x14d0a1
movl %ebp, %ecx
shrl %cl, %ebx
movsd (%r9,%r11), %xmm0
movq 0x88(%r15), %rcx
addsd (%rcx,%rbx,8), %xmm0
movsd %xmm0, (%rcx,%rbx,8)
movl $0x1, %ecx
movl $0x1, %r8d
addq $0x28, %r11
cmpq %r11, %r10
jne 0x14d049
incq %rdi
cmpq %rsi, %rdi
jne 0x14d021
testl %ecx, %ecx
sete %cl
testl %r8d, %r8d
sete %al
jmp 0x14d0c7
movb $0x1, %cl
movb $0x1, %al
testq %r12, %r12
setne %dl
andb %cl, %dl
orb %al, %dl
xorl %eax, %eax
testb $0x1, %dl
jne 0x14d169
testb %cl, %cl
je 0x14d0fa
movl 0xc(%rsp), %ecx
addl 0x28(%r15), %ecx
movl %ecx, 0x24(%r15)
movq 0x18(%rsp), %rax
movl %ecx, (%rax)
movl $0x1, %eax
jmp 0x14d169
movq 0x20(%rsp), %rax
testl %eax, %eax
je 0x14d13e
movq 0x88(%r15), %rcx
movl %eax, %edx
movsd 0xd8424(%rip), %xmm1    # 0x225538
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %esi, %esi
movq 0x10(%rsp), %rdi
movsd (%rcx,%rsi,8), %xmm0
ucomisd %xmm1, %xmm0
cmoval %esi, %eax
maxsd %xmm1, %xmm0
incq %rsi
movapd %xmm0, %xmm1
cmpq %rsi, %rdx
jne 0x14d120
jmp 0x14d150
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movsd 0xd83ed(%rip), %xmm0    # 0x225538
movq 0x10(%rsp), %rdi
movl %ebp, %ecx
shll %cl, %eax
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
movl $0x1, %eax
testq %rdi, %rdi
je 0x14d169
movsd %xmm0, (%rdi)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq