// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, %r12
movq %rdx, %r13
movq %r8, 0x10(%rsp)
orl $-0x1, (%r8)
movq %rsi, %r15
movl 0x24(%rsi), %esi
movl 0x2c(%r15), %ecx
movl %esi, %edx
shrl %cl, %edx
movq %rdi, %rbp
incl %edx
cmpq %rdx, 0x80(%r15)
movq %r9, 0x20(%rsp)
jae 0x10c070
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x80(%r15)
movq 0x88(%r15), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, 0x88(%r15)
movl 0x24(%r15), %esi
movl 0x2c(%r15), %ecx
movl %esi, %edx
shrl %cl, %edx
incl %edx
movq %rdx, 0x78(%r15)
xorl %eax, %eax
cmpq %rax, %rdx
je 0x10c08c
movq 0x88(%r15), %rdi
andq $0x0, (%rdi,%rax,8)
incq %rax
jmp 0x10c076
movl %esi, 0xc(%rsp)
movq %rdx, 0x18(%rsp)
movq 0x28(%r13), %rdi
movq 0x40(%r13), %rax
movl (%rax,%rbp,4), %r8d
movl 0x4(%rax,%rbp,4), %eax
leaq (%rdi,%r8,4), %rdi
addq $0x8, %rdi
subl %r8d, %eax
addl $-0x2, %eax
xorl %r8d, %r8d
pushq $0x1
popq %r10
xorl %r9d, %r9d
xorl %r11d, %r11d
cmpq %rax, %r8
je 0x10c152
movl (%rdi,%r8,4), %ebx
shrl %ebx
movq 0x30(%r15), %r14
movq 0x10(%r14), %r14
imulq $0x30, %rbx, %rbx
movl (%r14,%rbx), %ebp
movq 0x10(%r14,%rbx), %r13
imulq $0x28, %rbp, %rbp
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x10c14a
cmpb $0x0, 0x18(%r13,%rbx)
js 0x10c144
movq 0x10(%r15), %r14
movl 0x8(%r13,%rbx), %esi
movl 0x1c(%r15), %edx
orl 0x18(%r15), %edx
movl (%r14,%rsi,4), %r14d
orl 0x20(%r15), %edx
testl %r14d, %edx
je 0x10c141
testq %r12, %r12
je 0x10c121
cmpb $0x0, (%r12,%r14)
je 0x10c144
shrl %cl, %r14d
movsd (%r13,%rbx), %xmm0
movq 0x88(%r15), %rdx
addsd (%rdx,%r14,8), %xmm0
movsd %xmm0, (%rdx,%r14,8)
movl %r10d, %r9d
movl %r10d, %r11d
addq $0x28, %rbx
jmp 0x10c0ea
incq %r8
jmp 0x10c0c0
testq %r12, %r12
setne %al
testl %r9d, %r9d
sete %dl
andb %al, %dl
testl %r11d, %r11d
sete %sil
xorl %eax, %eax
orb %dl, %sil
jne 0x10c1dc
testl %r9d, %r9d
je 0x10c1c6
pushq $-0x1
popq %rax
movsd 0xd7402(%rip), %xmm0    # 0x1e3580
xorl %esi, %esi
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %r8
cmpq %rsi, %r8
je 0x10c1af
movq 0x88(%r15), %rdx
movsd (%rdx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %esi, %eax
maxsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
jmp 0x10c18a
shll %cl, %eax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
pushq $0x1
popq %rax
testq %rdi, %rdi
je 0x10c1dc
movsd %xmm0, (%rdi)
jmp 0x10c1dc
movl 0xc(%rsp), %ecx
addl 0x28(%r15), %ecx
movl %ecx, 0x24(%r15)
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq