// File: /workspace/github_cpp_makefile_star2_O3/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)
testl %esi, %esi
je 0x1543a0
movq %rdx, %r15
movq %rdi, %rbx
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 %rdi, 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 0x1542cf
movq %r15, %rsi
movq 0x40(%rsp), %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x1532f8
movl 0x8(%rsp), %r13d
cmpl $-0x1, %r13d
je 0x1542cf
movq %rbx, %rax
movq %r14, %rbx
movl (%rax,%rbp,4), %edi
shrl %edi
movq %r15, %rsi
leaq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r12, %r14
movq %r12, %r8
callq 0x15284f
testl %eax, %eax
je 0x1542c4
movsd 0x20(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
cmpl $-0x1, 0x4(%rsp)
je 0x1542ae
ucomisd 0x10(%rsp), %xmm0
jbe 0x1542c0
movq 0x30(%rsp), %rax
movl %r13d, (%rax)
movsd %xmm0, 0x10(%rsp)
movl %ebp, 0x4(%rsp)
incl 0xc(%rsp)
movq %r14, %r12
movq %rbx, %r14
movq 0x38(%rsp), %rbx
incq %rbp
cmpq %rbp, %r14
jne 0x154230
cmpl $0x0, 0xc(%rsp)
je 0x1542fb
movl 0x4(%rsp), %eax
cmpl $-0x1, %eax
je 0x1543a0
movl %eax, %eax
movl (%rbx,%rax,4), %eax
shrl %eax
jmp 0x1543a5
cmpl $0x0, 0x2c(%rsp)
je 0x1543a0
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %ebp, %ebp
leaq 0x8(%rsp), %r12
leaq 0x18(%rsp), %r13
movq 0x10(%r15), %rax
movl (%rbx,%rbp,4), %edi
shrl %edi
movl 0x1c(%r15), %ecx
orl 0x18(%r15), %ecx
orl 0x20(%r15), %ecx
testl %ecx, (%rax,%rdi,4)
je 0x15433e
incq %rbp
cmpq %rbp, %r14
jne 0x15431a
jmp 0x1542e2
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x1532f8
testl %eax, %eax
je 0x154334
movl (%rbx,%rbp,4), %edi
shrl %edi
movq %r15, %rsi
leaq 0x20(%rsp), %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x15284f
testl %eax, %eax
je 0x154334
movsd 0x20(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
cmpl $-0x1, 0x4(%rsp)
je 0x154389
ucomisd 0x10(%rsp), %xmm0
jbe 0x154334
movl 0x8(%rsp), %eax
movq 0x30(%rsp), %rcx
movl %eax, (%rcx)
movsd %xmm0, 0x10(%rsp)
movl %ebp, 0x4(%rsp)
jmp 0x154334
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq