// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: get_weightest_uid(unsigned int*, unsigned int, H_partition*, unsigned char*, unsigned int*)
uint32_t get_weightest_uid(uint32_t* a, uint32_t n, H_partition* hap, uint8_t* hap_label_flag, 
uint32_t* max_hap_label)
{
    double cur_w0, cur_w1, max_weight;
    uint32_t j, max_i, is_ava, hap_label;

    for (j = is_ava = 0, max_i = (uint32_t)-1; j < n; j++)
    {
        if(is_hap_set(a[j]>>1, *hap)) ///avoid repeat phasing
        {
            continue;
        }
        
        get_weightest_hap_label_from_uid(a[j]>>1, hap, hap_label_flag, &hap_label, NULL);

        if(hap_label == (uint32_t)-1)
        {
            continue;
        }

        if(get_related_weight(a[j]>>1, hap, &cur_w0, &cur_w1, &hap_label) > 0)
        {
            if(max_i == (uint32_t)-1)
            {
                max_i = j;
                max_weight = cur_w0 + cur_w1;
                (*max_hap_label) = hap_label;
            }
            else if((cur_w0 + cur_w1) > max_weight)
            {
                max_i = j;
                max_weight = cur_w0 + cur_w1;
                (*max_hap_label) = hap_label;
            }
            is_ava++;
        }
    }

    ///no useful unitig
    if(is_ava == 0)
    {
        for (j = is_ava = 0, max_i = (uint32_t)-1; j < n; j++)
        {
            if(is_hap_set(a[j]>>1, *hap)) 
            {
                continue;
            }
            
            if(get_weightest_hap_label_from_uid(a[j]>>1, hap, NULL, &hap_label, NULL)==0) continue;

            if(get_related_weight(a[j]>>1, hap, &cur_w0, &cur_w1, &hap_label) > 0)
            {
                if(max_i == (uint32_t)-1)
                {
                    max_i = j;
                    max_weight = cur_w0 + cur_w1;
                    (*max_hap_label) = hap_label;
                }
                else if((cur_w0 + cur_w1) > max_weight)
                {
                    max_i = j;
                    max_weight = cur_w0 + cur_w1;
                    (*max_hap_label) = hap_label;
                }
                is_ava++;
            }
        }
    }
    if(max_i == (uint32_t)-1) return max_i;

    return a[max_i]>>1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x30(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, %r15
movq %rdi, %rbx
testl %esi, %esi
je 0x14dd06
movl %esi, 0x2c(%rsp)
movl %esi, %r14d
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %ebp, %ebp
leaq 0x8(%rsp), %r12
movl $0x0, 0xc(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x10(%r15), %rax
movl (%rbx,%rbp,4), %edi
shrl %edi
movl 0x1c(%r15), %edx
orl 0x18(%r15), %edx
orl 0x20(%r15), %edx
testl %edx, (%rax,%rdi,4)
jne 0x14dcec
movq %r15, %rsi
movq 0x40(%rsp), %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x14cd15
movl 0x8(%rsp), %r13d
cmpl $-0x1, %r13d
je 0x14dcec
movl (%rbx,%rbp,4), %edi
shrl %edi
movq %r15, %rsi
leaq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r12, %rbx
movq %r12, %r8
callq 0x14c1ed
testl %eax, %eax
je 0x14dce4
movsd 0x20(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
cmpl $-0x1, 0x4(%rsp)
je 0x14dcce
ucomisd 0x10(%rsp), %xmm0
jbe 0x14dce0
movq 0x30(%rsp), %rax
movl %r13d, (%rax)
movsd %xmm0, 0x10(%rsp)
movl %ebp, 0x4(%rsp)
incl 0xc(%rsp)
movq %rbx, %r12
movq 0x38(%rsp), %rbx
incq %rbp
cmpq %rbp, %r14
jne 0x14dc5a
cmpl $0x0, 0xc(%rsp)
sete %al
movl 0x2c(%rsp), %esi
jmp 0x14dd10
movb $0x1, %al
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
testb %al, %al
je 0x14ddc8
testl %esi, %esi
je 0x14ddc0
movl %esi, %ebp
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %r12d, %r12d
leaq 0x8(%rsp), %r14
leaq 0x18(%rsp), %r13
movq 0x10(%r15), %rax
movl (%rbx,%r12,4), %edi
shrl %edi
movl 0x1c(%r15), %ecx
orl 0x18(%r15), %ecx
orl 0x20(%r15), %ecx
testl %ecx, (%rax,%rdi,4)
je 0x14dd5c
incq %r12
cmpq %r12, %rbp
jne 0x14dd37
jmp 0x14ddc8
movq %r15, %rsi
xorl %edx, %edx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x14cd15
testl %eax, %eax
je 0x14dd52
movl (%rbx,%r12,4), %edi
shrl %edi
movq %r15, %rsi
leaq 0x20(%rsp), %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x14c1ed
testl %eax, %eax
je 0x14dd52
movsd 0x20(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
cmpl $-0x1, 0x4(%rsp)
je 0x14dda8
ucomisd 0x10(%rsp), %xmm0
jbe 0x14dd52
movl 0x8(%rsp), %eax
movq 0x30(%rsp), %rcx
movl %eax, (%rcx)
movsd %xmm0, 0x10(%rsp)
movl %r12d, 0x4(%rsp)
jmp 0x14dd52
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movl 0x4(%rsp), %eax
cmpl $-0x1, %eax
je 0x14ddda
movl %eax, %eax
movl (%rbx,%rax,4), %eax
shrl %eax
jmp 0x14dddf
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq