// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x1eeedd
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl -0x4c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq -0x20(%rbp), %rcx
movl 0x18(%rcx), %ecx
movq -0x20(%rbp), %rdx
orl 0x1c(%rdx), %ecx
movq -0x20(%rbp), %rdx
orl 0x20(%rdx), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1eee16
jmp 0x1eeecf
movq -0x10(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, %eax
movl %eax, %edi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x58(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x1ecdc0
cmpl $-0x1, -0x58(%rbp)
jne 0x1eee47
jmp 0x1eeecf
movq -0x10(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %edi
shrl %edi
movq -0x20(%rbp), %rsi
leaq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rcx
leaq -0x58(%rbp), %r8
callq 0x1eb960
cmpl $0x0, %eax
jbe 0x1eeecd
cmpl $-0x1, -0x50(%rbp)
jne 0x1eee93
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
movsd -0x38(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movl -0x58(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
jmp 0x1eeec4
movsd -0x38(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
ucomisd -0x48(%rbp), %xmm0
jbe 0x1eeec2
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
movsd -0x38(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movl -0x58(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
jmp 0x1eeec4
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1eeecf
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1eedd0
cmpl $0x0, -0x54(%rbp)
jne 0x1ef009
movl $0x0, -0x54(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x1ef007
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl -0x4c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq -0x20(%rbp), %rcx
movl 0x18(%rcx), %ecx
movq -0x20(%rbp), %rdx
orl 0x1c(%rdx), %ecx
movq -0x20(%rbp), %rdx
orl 0x20(%rdx), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1eef42
jmp 0x1eeff9
movq -0x10(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, %eax
movl %eax, %edi
movq -0x20(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r8d
leaq -0x58(%rbp), %rcx
movq %r8, %rdx
callq 0x1ecdc0
cmpl $0x0, %eax
jne 0x1eef71
jmp 0x1eeff9
movq -0x10(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %edi
shrl %edi
movq -0x20(%rbp), %rsi
leaq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rcx
leaq -0x58(%rbp), %r8
callq 0x1eb960
cmpl $0x0, %eax
jbe 0x1eeff7
cmpl $-0x1, -0x50(%rbp)
jne 0x1eefbd
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
movsd -0x38(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movl -0x58(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
jmp 0x1eefee
movsd -0x38(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
ucomisd -0x48(%rbp), %xmm0
jbe 0x1eefec
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
movsd -0x38(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movl -0x58(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
jmp 0x1eefee
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1eeff9
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1eeefc
jmp 0x1ef009
cmpl $-0x1, -0x50(%rbp)
jne 0x1ef017
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1ef026
movq -0x10(%rbp), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop