// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x30(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x24(%rcx), %edx
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %edx
movl %edx, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x1ed367
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x2, %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x4, %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x8, %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x10, %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x88(%rax)
jmp 0x1ed369
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x78(%rax)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x78(%rcx), %rax
jae 0x1ed3c5
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movl -0x3c(%rbp), %ecx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1ed397
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x58(%rbp), %r8
leaq -0x5c(%rbp), %r9
movq %rcx, %rdx
movq $0x0, (%rsp)
callq 0x1c4b20
movl $0x0, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jae 0x1ed56d
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rax), %rax
movl -0x60(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rax), %rax
movl -0x60(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jae 0x1ed55d
movq -0x68(%rbp), %rax
movl -0x3c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movb 0x18(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x1ed483
jmp 0x1ed54f
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movl -0x3c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movq -0x18(%rbp), %rcx
movl 0x18(%rcx), %ecx
movq -0x18(%rbp), %rdx
orl 0x1c(%rdx), %ecx
movq -0x18(%rbp), %rdx
orl 0x20(%rdx), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1ed548
cmpq $0x0, -0x28(%rbp)
je 0x1ed4f1
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x68(%rbp), %rdx
movl -0x3c(%rbp), %esi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
movl 0x8(%rdx), %edx
movl (%rcx,%rdx,4), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x1ed4f1
jmp 0x1ed54f
movq -0x68(%rbp), %rax
movl -0x3c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x68(%rbp), %rdx
movl -0x3c(%rbp), %esi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
movl 0x8(%rdx), %edx
movl (%rcx,%rdx,4), %edx
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %edx
movl %edx, %ecx
movl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl $0x1, -0x44(%rbp)
movl $0x1, -0x48(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1ed45a
jmp 0x1ed55f
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1ed3fe
cmpq $0x0, -0x28(%rbp)
je 0x1ed586
cmpl $0x0, -0x44(%rbp)
jne 0x1ed586
movl $0x0, -0x4(%rbp)
jmp 0x1ed66a
cmpl $0x0, -0x48(%rbp)
jne 0x1ed598
movl $0x0, -0x4(%rbp)
jmp 0x1ed66a
cmpl $0x0, -0x44(%rbp)
jne 0x1ed5ce
cmpl $0x0, -0x48(%rbp)
jbe 0x1ed5ce
movq -0x18(%rbp), %rax
movl 0x28(%rax), %ecx
movq -0x18(%rbp), %rax
addl 0x24(%rax), %ecx
movl %ecx, 0x24(%rax)
movq -0x18(%rbp), %rax
movl 0x24(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x1ed66a
movl $0x0, -0x3c(%rbp)
movsd 0x194c9b(%rip), %xmm0   # 0x382278
movsd %xmm0, -0x70(%rbp)
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x78(%rcx), %rax
jae 0x1ed63b
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movl -0x3c(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x70(%rbp), %xmm0
jbe 0x1ed62e
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movl -0x3c(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x1ed630
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1ed5e9
movl -0x74(%rbp), %eax
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shll %cl, %eax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x38(%rbp)
je 0x1ed663
movsd -0x70(%rbp), %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)