// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rdx, 0x20(%rsp)
movq %rsi, %r12
movq %rdi, 0x8(%rsp)
movq %rcx, 0x28(%rsp)
orl $-0x1, (%rcx)
testq %r8, %r8
je 0x10d146
andl $0x0, (%r8)
movq %r8, 0x18(%rsp)
testq %r9, %r9
je 0x10d157
movl $0x1, (%r9)
movq %r9, 0x10(%rsp)
movq 0x8(%rsp), %rax
cmpl $0x0, 0x10(%rax)
je 0x10d46e
movl 0x24(%r12), %eax
movl 0x2c(%r12), %ecx
shrl %cl, %eax
incl %eax
cmpq %rax, 0x80(%r12)
jae 0x10d1ee
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(%r12)
movq 0x88(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x88(%r12)
movl 0x24(%r12), %eax
movl 0x2c(%r12), %ecx
shrl %cl, %eax
incl %eax
movq %rax, 0x78(%r12)
xorl %edx, %edx
cmpq %rdx, %rax
je 0x10d20c
movq 0x88(%r12), %rsi
andq $0x0, (%rsi,%rdx,8)
incq %rdx
jmp 0x10d1f5
movq 0x8(%rsp), %rdx
movl 0x10(%rdx), %edx
movq %rdx, 0x30(%rsp)
xorl %esi, %esi
pushq $0x1
popq %rdi
xorl %r8d, %r8d
cmpq 0x30(%rsp), %rsi
je 0x10d2ed
movq 0x8(%rsp), %rdx
movq 0x18(%rdx), %r9
movq (%r9,%rsi,8), %r9
movq 0x20(%rsp), %rdx
movq 0x28(%rdx), %r10
movq 0x40(%rdx), %r11
shrq $0x21, %r9
movl (%r11,%r9,4), %ebx
movl 0x4(%r11,%r9,4), %r9d
leaq (%r10,%rbx,4), %r10
addq $0x8, %r10
subl %ebx, %r9d
addl $-0x2, %r9d
xorl %r11d, %r11d
cmpq %r9, %r11
je 0x10d2e5
movl (%r10,%r11,4), %ebx
shrl %ebx
movq 0x30(%r12), %r14
movq 0x10(%r14), %r14
imulq $0x30, %rbx, %rbx
movl (%r14,%rbx), %r13d
movq 0x10(%r14,%rbx), %rbx
imulq $0x28, %r13, %r14
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0x10d2e0
cmpb $0x0, 0x18(%rbx,%rbp)
js 0x10d2da
movq 0x10(%r12), %r13
movl 0x8(%rbx,%rbp), %r15d
movl 0x1c(%r12), %edx
orl 0x18(%r12), %edx
movl (%r13,%r15,4), %r13d
orl 0x20(%r12), %edx
testl %r13d, %edx
je 0x10d2da
movsd (%rbx,%rbp), %xmm0
movq 0x88(%r12), %rdx
shrl %cl, %r13d
addsd (%rdx,%r13,8), %xmm0
movsd %xmm0, (%rdx,%r13,8)
movl %edi, %r8d
addq $0x28, %rbp
jmp 0x10d28c
incq %r11
jmp 0x10d265
incq %rsi
jmp 0x10d221
testl %r8d, %r8d
je 0x10d46e
pushq $-0x1
popq %rdx
movsd 0xd627f(%rip), %xmm0    # 0x1e3580
xorl %esi, %esi
cmpq %rsi, %rax
je 0x10d329
movq 0x88(%r12), %rdi
movsd (%rdi,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %esi, %edx
maxsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
jmp 0x10d303
shll %cl, %edx
movq 0x28(%rsp), %rax
movl %edx, (%rax)
cmpq $0x0, 0x18(%rsp)
je 0x10d46e
movq 0x8(%rsp), %rax
movl 0x10(%rax), %eax
xorpd %xmm0, %xmm0
pushq $-0x1
popq %rsi
movsd 0xd622b(%rip), %xmm2    # 0x1e3580
xorl %edi, %edi
xorpd %xmm1, %xmm1
cmpq %rax, %rdi
je 0x10d44b
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%rdi,8), %rcx
movq 0x20(%rsp), %r8
movq 0x28(%r8), %r9
movq 0x40(%r8), %r8
shrq $0x21, %rcx
movl (%r8,%rcx,4), %r10d
movl 0x4(%r8,%rcx,4), %r8d
leaq (%r9,%r10,4), %r9
addq $0x8, %r9
subl %r10d, %r8d
addl $-0x2, %r8d
xorpd %xmm3, %xmm3
xorl %r10d, %r10d
cmpq %r8, %r10
je 0x10d41b
movl (%r9,%r10,4), %ecx
shrl %ecx
movq 0x30(%r12), %r11
movq 0x10(%r11), %r11
imulq $0x30, %rcx, %rcx
movl (%r11,%rcx), %ebx
movq 0x10(%r11,%rcx), %r11
imulq $0x28, %rbx, %rbp
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x10d416
cmpb $0x0, 0x18(%r11,%rbx)
js 0x10d410
movq 0x10(%r12), %rcx
movl 0x8(%r11,%rbx), %r14d
movl 0x1c(%r12), %r15d
orl 0x18(%r12), %r15d
movl (%rcx,%r14,4), %r14d
orl 0x20(%r12), %r15d
testl %r14d, %r15d
je 0x10d410
movb 0x2c(%r12), %cl
shrl %cl, %r14d
movl %edx, %r15d
shrl %cl, %r15d
cmpl %r15d, %r14d
jne 0x10d410
addsd (%r11,%rbx), %xmm3
addq $0x28, %rbx
jmp 0x10d3c8
incq %r10
jmp 0x10d3a1
ucomisd %xmm2, %xmm3
movapd %xmm3, %xmm4
cmoval %edi, %esi
maxsd %xmm2, %xmm4
cmpltsd %xmm3, %xmm2
andnpd %xmm1, %xmm2
addsd %xmm3, %xmm0
movapd %xmm2, %xmm1
addsd %xmm3, %xmm1
incq %rdi
movapd %xmm4, %xmm2
jmp 0x10d35b
movq 0x18(%rsp), %rax
movl %esi, (%rax)
cmpq $0x0, 0x10(%rsp)
je 0x10d46e
subsd %xmm1, %xmm0
xorl %eax, %eax
ucomisd %xmm0, %xmm1
setae %al
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq