// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: get_weightest_hap_label_from_chain(ma_utg_t*, H_partition*, bubble_type*, unsigned int*, unsigned int*, unsigned int*)
void get_weightest_hap_label_from_chain(ma_utg_t *u, H_partition* hap, bubble_type* bub, 
uint32_t* max_hap_label, uint32_t* max_bid_idx, uint32_t* is_forward_first)
{
    (*max_hap_label) = (uint32_t)-1;
    if(max_bid_idx) (*max_bid_idx) = 0;
    if(is_forward_first) (*is_forward_first) = 1;

    if(u->n == 0) return;
    kv_resize(double, hap->label_buffer, (hap->label>>hap->label_shift)+1);///how many group
    hap->label_buffer.n = (hap->label>>hap->label_shift)+1;
    uint32_t i, k, m, is_ava, a_n, *x_a, x_n, x;
    uint64_t bid;
    hc_edge* a = NULL;
    for (i = 0; i < hap->label_buffer.n; i++)
    {
        hap->label_buffer.a[i] = 0;///count weight for each group
    }

    for (k = is_ava = 0; k < u->n; k++)
    {
        bid = u->a[k]>>33;
        ///bid might be bubble or non-bubble
        get_bubbles(bub, bid, NULL, NULL, &x_a, &x_n, NULL);
        for (m = 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))
                {
                    hap->label_buffer.a[hap->hap[a[i].uID]>>hap->label_shift] += a[i].weight;
                    is_ava = 1;
                }
            }
        }
    }
    
    if(is_ava == 0) return; ///totally new chain
    double max_weight;
    uint32_t max_i;
    ///select the best exsiting group to u
    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_bid_idx)
    {
        double current_weight, tot_w = 0, half_w = 0;
        for (k = 0, max_weight = -1, max_i = (uint32_t)-1; k < u->n; k++)
        {
            bid = u->a[k]>>33;
            ///bid might be bubble or non-bubble
            get_bubbles(bub, bid, NULL, NULL, &x_a, &x_n, NULL);
            for (m = 0, current_weight = 0; m < x_n; m++)
            {
                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_label(a[i].uID, *hap, *max_hap_label))
                    {
                        current_weight += a[i].weight;
                    }
                }
            }

            if(current_weight > max_weight) /// the weight of each bubble
            {
                max_weight = current_weight;
                max_i = k;
                half_w = 0;
            }
            tot_w += current_weight;
            half_w += current_weight;
        }

        (*max_bid_idx) = max_i;
        if(is_forward_first)
        {
            if(half_w >= (tot_w - half_w))
            {   
                (*is_forward_first) = 1;
            }
            else
            {
                (*is_forward_first) = 0;
            }
        }
    }

    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r14
movq %r8, %r12
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbp
movq %rcx, 0x40(%rsp)
movl $0xffffffff, (%rcx)     # imm = 0xFFFFFFFF
testq %r8, %r8
je 0x14de23
movl $0x0, (%r12)
testq %r14, %r14
je 0x14de2f
movl $0x1, (%r14)
cmpl $0x0, 0x10(%rbp)
je 0x14e1a8
movl 0x24(%r15), %eax
movb 0x2c(%r15), %cl
shrl %cl, %eax
incl %eax
cmpq %rax, 0x80(%r15)
jae 0x14dea8
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), %ebx
movl %ebx, %ecx
shrl %cl, %eax
incl %eax
movq %rax, 0x78(%r15)
movq %r13, 0x20(%rsp)
je 0x14dee0
movq 0x88(%r15), %rdi
movl %eax, %edx
shlq $0x3, %rdx
xorl %esi, %esi
movq %rax, %r13
callq 0x20e0
movq %r13, %rax
movq 0x20(%rsp), %r13
movl 0x10(%rbp), %ecx
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
je 0x14e1a8
movq %rax, 0x30(%rsp)
movq %r12, 0x18(%rsp)
movq %r14, 0x28(%rsp)
movq %rbp, 0x38(%rsp)
movq 0x18(%rbp), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%r13), %rax
movq 0x40(%r13), %rdi
addq $0x8, %rax
movq %rax, (%rsp)
xorl %r8d, %r8d
xorl %ecx, %ecx
movq 0x8(%rsp), %rax
movq (%rax,%r8,8), %rsi
shrq $0x21, %rsi
movl (%rdi,%rsi,4), %r9d
movl 0x4(%rdi,%rsi,4), %esi
subl %r9d, %esi
addl $-0x2, %esi
je 0x14dfd7
movq (%rsp), %rax
leaq (%rax,%r9,4), %r9
movq 0x30(%r15), %r10
movq 0x10(%r10), %r10
movl %esi, %r11d
xorl %r12d, %r12d
movl (%r9,%r12,4), %esi
shrl %esi
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
movl (%r10,%rsi), %r14d
testq %r14, %r14
je 0x14dfcf
movq 0x10(%r10,%rsi), %rbp
shlq $0x3, %r14
leaq (%r14,%r14,4), %r13
xorl %r14d, %r14d
cmpb $0x0, 0x18(%rbp,%r14)
js 0x14dfc6
movq 0x10(%r15), %rsi
movl 0x8(%rbp,%r14), %edx
movl 0x1c(%r15), %eax
orl 0x18(%r15), %eax
movl (%rsi,%rdx,4), %esi
orl 0x20(%r15), %eax
testl %esi, %eax
je 0x14dfc6
movsd (%rbp,%r14), %xmm0
movq 0x88(%r15), %rax
movl %ebx, %ecx
shrl %cl, %esi
addsd (%rax,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
movl $0x1, %ecx
addq $0x28, %r14
cmpq %r14, %r13
jne 0x14df81
incq %r12
cmpq %r11, %r12
jne 0x14df5a
incq %r8
cmpq 0x10(%rsp), %r8
jne 0x14df23
testl %ecx, %ecx
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r8
movq 0x38(%rsp), %r9
movq 0x30(%rsp), %rax
je 0x14e1a8
testl %eax, %eax
je 0x14e03b
movq 0x88(%r15), %rcx
movl %eax, %edx
movsd 0xd7522(%rip), %xmm0    # 0x225538
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %esi, %eax
maxsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
cmpq %rsi, %rdx
jne 0x14e01d
jmp 0x14e040
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %ebx, %ecx
shll %cl, %eax
movq 0x40(%rsp), %rcx
movl %eax, (%rcx)
testq %rdi, %rdi
je 0x14e1a8
movl 0x10(%r9), %ecx
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
je 0x14e17b
movq 0x18(%r9), %rcx
movq %rcx, 0x8(%rsp)
movq 0x28(%r8), %rcx
movq 0x40(%r8), %r9
addq $0x8, %rcx
movq %rcx, (%rsp)
movsd 0xd74b1(%rip), %xmm2    # 0x225538
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorpd %xmm1, %xmm1
xorl %r10d, %r10d
xorpd %xmm0, %xmm0
movq 0x8(%rsp), %rcx
movq (%rcx,%r10,8), %rcx
shrq $0x21, %rcx
movl (%r9,%rcx,4), %edi
movl 0x4(%r9,%rcx,4), %ecx
subl %edi, %ecx
xorpd %xmm3, %xmm3
addl $-0x2, %ecx
je 0x14e142
movq (%rsp), %rdx
leaq (%rdx,%rdi,4), %r11
movq 0x30(%r15), %rdi
movq 0x10(%rdi), %rbx
movl %ecx, %r14d
xorl %r12d, %r12d
movl (%r11,%r12,4), %ecx
shrl %ecx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movl (%rbx,%rcx), %edi
testq %rdi, %rdi
je 0x14e13a
movq 0x10(%rbx,%rcx), %r13
shlq $0x3, %rdi
leaq (%rdi,%rdi,4), %rbp
xorl %r8d, %r8d
cmpb $0x0, 0x18(%r13,%r8)
js 0x14e131
movq 0x10(%r15), %rcx
movl 0x8(%r13,%r8), %edi
movl 0x1c(%r15), %edx
orl 0x18(%r15), %edx
movl (%rcx,%rdi,4), %edi
orl 0x20(%r15), %edx
testl %edi, %edx
je 0x14e131
movb 0x2c(%r15), %cl
shrl %cl, %edi
movl %eax, %edx
shrl %cl, %edx
cmpl %edx, %edi
jne 0x14e131
addsd (%r13,%r8), %xmm3
addq $0x28, %r8
cmpq %r8, %rbp
jne 0x14e0f8
incq %r12
cmpq %r14, %r12
jne 0x14e0d2
ucomisd %xmm2, %xmm3
movapd %xmm3, %xmm4
maxsd %xmm2, %xmm4
cmoval %r10d, %esi
cmpltsd %xmm3, %xmm2
andnpd %xmm1, %xmm2
addsd %xmm3, %xmm0
movapd %xmm2, %xmm1
addsd %xmm3, %xmm1
incq %r10
movapd %xmm4, %xmm2
cmpq 0x10(%rsp), %r10
jne 0x14e097
jmp 0x14e188
xorpd %xmm0, %xmm0
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorpd %xmm1, %xmm1
movq 0x18(%rsp), %rax
movl %esi, (%rax)
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
je 0x14e1a8
subsd %xmm1, %xmm0
xorl %eax, %eax
ucomisd %xmm0, %xmm1
setae %al
movl %eax, (%rcx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq