// File: /workspace/github_cpp_makefile_star2_O2/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
movl %esi, %ebp
xorl %r14d, %r14d
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
leaq 0x10(%rsp), %r12
movl $0x0, 0x14(%rsp)
movq %rbp, 0x38(%rsp)
cmpq %r14, %rbp
je 0x10d04b
movq 0x10(%r15), %rax
movl (%rbx,%r14,4), %edi
shrl %edi
movl 0x1c(%r15), %edx
orl 0x18(%r15), %edx
orl 0x20(%r15), %edx
testl %edx, (%rax,%rdi,4)
jne 0x10d043
movq %r15, %rsi
movq 0x40(%rsp), %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x10be02
movl 0x10(%rsp), %r13d
cmpl $-0x1, %r13d
je 0x10d043
movl (%rbx,%r14,4), %edi
shrl %edi
movq %r15, %rsi
leaq 0x28(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r12, %rbp
movq %r12, %r8
callq 0x10b4a6
testl %eax, %eax
je 0x10d03b
movsd 0x28(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
cmpl $-0x1, 0xc(%rsp)
je 0x10d024
ucomisd 0x18(%rsp), %xmm0
jbe 0x10d037
movq 0x30(%rsp), %rax
movl %r13d, (%rax)
movsd %xmm0, 0x18(%rsp)
movl %r14d, 0xc(%rsp)
incl 0x14(%rsp)
movq %rbp, %r12
movq 0x38(%rsp), %rbp
incq %r14
jmp 0x10cfa5
cmpl $0x0, 0x14(%rsp)
je 0x10d06b
movl 0xc(%rsp), %eax
cmpl $-0x1, %eax
je 0x10d108
movl %eax, %eax
movl (%rbx,%rax,4), %eax
shrl %eax
jmp 0x10d10b
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
xorl %r14d, %r14d
leaq 0x10(%rsp), %r12
leaq 0x20(%rsp), %r13
cmpq %r14, %rbp
je 0x10d052
movq 0x10(%r15), %rax
movl (%rbx,%r14,4), %edi
shrl %edi
movl 0x1c(%r15), %ecx
orl 0x18(%r15), %ecx
orl 0x20(%r15), %ecx
testl %ecx, (%rax,%rdi,4)
je 0x10d0a4
incq %r14
jmp 0x10d07f
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x10be02
testl %eax, %eax
je 0x10d09f
movl (%rbx,%r14,4), %edi
shrl %edi
movq %r15, %rsi
leaq 0x28(%rsp), %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x10b4a6
testl %eax, %eax
je 0x10d09f
movsd 0x28(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
cmpl $-0x1, 0xc(%rsp)
je 0x10d0f0
ucomisd 0x18(%rsp), %xmm0
jbe 0x10d09f
movl 0x10(%rsp), %eax
movq 0x30(%rsp), %rcx
movl %eax, (%rcx)
movsd %xmm0, 0x18(%rsp)
movl %r14d, 0xc(%rsp)
jmp 0x10d09f
pushq $-0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq