// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
cmpq $0x0, -0x28(%rbp)
je 0x1ef06e
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
cmpq $0x0, -0x30(%rbp)
je 0x1ef07f
movq -0x30(%rbp), %rax
movl $0x1, (%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x1ef08e
jmp 0x1ef6da
jmp 0x1ef090
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x24(%rcx), %edx
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %edx
movl %edx, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x1ef1da
movq -0x10(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
shrq %rcx
movq -0x10(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x2, %rcx
movq -0x10(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x4, %rcx
movq -0x10(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x8, %rcx
movq -0x10(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x10, %rcx
movq -0x10(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
jmp 0x1ef1dc
movq -0x10(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x78(%rcx), %rax
jae 0x1ef238
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movl -0x34(%rbp), %ecx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ef20a
movl $0x0, -0x40(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1ef3dc
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x38(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x50(%rbp), %r8
leaq -0x54(%rbp), %r9
movq %rcx, %rdx
movq $0x0, (%rsp)
callq 0x1c4b20
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0x1ef3cc
movq -0x50(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rax), %rax
movl -0x58(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rax), %rax
movl -0x58(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x1ef3bc
movq -0x68(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movb 0x18(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x1ef31d
jmp 0x1ef3ae
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
movq -0x10(%rbp), %rdx
orl 0x1c(%rdx), %ecx
movq -0x10(%rbp), %rdx
orl 0x20(%rdx), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1ef3ac
movq -0x68(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x68(%rbp), %rdx
movl -0x34(%rbp), %esi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
movl 0x8(%rdx), %edx
movl (%rcx,%rdx,4), %edx
movq -0x10(%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, -0x40(%rbp)
jmp 0x1ef3ae
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ef2f4
jmp 0x1ef3be
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1ef298
jmp 0x1ef3ce
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1ef246
cmpl $0x0, -0x40(%rbp)
jne 0x1ef3e7
jmp 0x1ef6da
movl $0x0, -0x34(%rbp)
movsd 0x192e82(%rip), %xmm0   # 0x382278
movsd %xmm0, -0x70(%rbp)
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x78(%rcx), %rax
jae 0x1ef454
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movl -0x34(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x70(%rbp), %xmm0
jbe 0x1ef447
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movl -0x34(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x1ef449
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ef402
movl -0x74(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shll %cl, %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x1ef6d8
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
movl $0x0, -0x38(%rbp)
movsd 0x192de0(%rip), %xmm0   # 0x382278
movsd %xmm0, -0x70(%rbp)
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movl -0x38(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1ef690
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x38(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x50(%rbp), %r8
leaq -0x54(%rbp), %r9
movq %rcx, %rdx
movq $0x0, (%rsp)
callq 0x1c4b20
movl $0x0, -0x3c(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0x1ef631
movq -0x50(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rax), %rax
movl -0x58(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rax), %rax
movl -0x58(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x1ef621
movq -0x68(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movb 0x18(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x1ef583
jmp 0x1ef613
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
movq -0x10(%rbp), %rdx
orl 0x1c(%rdx), %ecx
movq -0x10(%rbp), %rdx
orl 0x20(%rdx), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1ef611
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %eax
movq -0x20(%rbp), %rcx
movl (%rcx), %edx
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jne 0x1ef611
movq -0x68(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
addsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
jmp 0x1ef613
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ef55a
jmp 0x1ef623
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1ef4fe
movsd -0x80(%rbp), %xmm0
ucomisd -0x70(%rbp), %xmm0
jbe 0x1ef658
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x74(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x80(%rbp), %xmm0
addsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x80(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1ef4a4
movl -0x74(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x30(%rbp)
je 0x1ef6d6
movsd -0x90(%rbp), %xmm0
movsd -0x88(%rbp), %xmm1
subsd -0x90(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1ef6ca
movq -0x30(%rbp), %rax
movl $0x1, (%rax)
jmp 0x1ef6d4
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
jmp 0x1ef6d6
jmp 0x1ef6d8
jmp 0x1ef6da
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)