// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %r15
movq %r8, 0x10(%rsp)
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
movl 0x24(%rsi), %eax
movl 0x2c(%rsi), %ebp
movl %eax, %esi
movl %ebp, %ecx
shrl %cl, %esi
movq %rdi, %rbx
incl %esi
cmpq %rsi, 0x80(%r15)
jae 0x1535c0
leaq -0x1(%rsi), %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 %eax, %esi
movl %ebp, %ecx
shrl %cl, %esi
incl %esi
jmp 0x1535c4
movl %eax, 0xc(%rsp)
movq %rsi, 0x78(%r15)
testl %esi, %esi
je 0x1535e8
movq 0x88(%r15), %rdi
leaq (,%rsi,8), %rdx
movq %rsi, %r14
xorl %esi, %esi
callq 0x20e0
movq %r14, %rsi
movq 0x40(%r13), %rcx
movl (%rcx,%rbx,4), %eax
movl 0x4(%rcx,%rbx,4), %ecx
subl %eax, %ecx
addl $-0x2, %ecx
je 0x1536cf
movq %rsi, 0x18(%rsp)
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 0x1536a9
movq 0x10(%rdx,%r9), %r9
shlq $0x3, %r10
leaq (%r10,%r10,4), %r10
xorl %r11d, %r11d
cmpb $0x0, 0x18(%r9,%r11)
js 0x1536a0
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 0x15369a
testq %r12, %r12
je 0x15367a
cmpb $0x0, (%r12,%rbx)
je 0x1536a0
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 0x153648
incq %rdi
cmpq %rsi, %rdi
jne 0x153620
testl %ecx, %ecx
sete %al
testl %r8d, %r8d
sete %dl
testq %r12, %r12
setne %sil
andb %al, %sil
orb %dl, %sil
je 0x1536e0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %ecx, %ecx
je 0x153721
movq 0x18(%rsp), %rsi
testl %esi, %esi
je 0x15373b
movq 0x88(%r15), %rcx
movsd 0xddf3c(%rip), %xmm1    # 0x231638
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm0
ucomisd %xmm1, %xmm0
cmoval %edx, %eax
maxsd %xmm1, %xmm0
incq %rdx
movapd %xmm0, %xmm1
cmpq %rdx, %rsi
jne 0x153703
jmp 0x153748
movl 0xc(%rsp), %ecx
addl 0x28(%r15), %ecx
movl %ecx, 0x24(%r15)
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
movl $0x1, %eax
jmp 0x1536d1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movsd 0xddef0(%rip), %xmm0    # 0x231638
movl %ebp, %ecx
shll %cl, %eax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movl $0x1, %eax
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
je 0x1536d1
movsd %xmm0, (%rcx)
jmp 0x1536d1