// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: get_weightest_hap_label_from_uid(unsigned long, H_partition*, unsigned char*, unsigned int*, double*)
uint32_t get_weightest_hap_label_from_uid(uint64_t x, H_partition* hap, uint8_t* hap_label_flag, 
uint32_t* max_hap_label, double* max_hap_weight)
{
    (*max_hap_label) = (uint32_t)-1;
    kv_resize(double, hap->label_buffer, (hap->label>>hap->label_shift)+1);
    hap->label_buffer.n = (hap->label>>hap->label_shift)+1;
    uint32_t i, is_set_ava, is_unset_ava, a_n;
    hc_edge* a = NULL;
    for (i = 0; i < hap->label_buffer.n; i++)
    {
        hap->label_buffer.a[i] = 0;
    }

    is_set_ava = is_unset_ava = 0;
    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))
        {
            if(hap_label_flag && hap_label_flag[hap->hap[a[i].uID]] == 0) continue;
            hap->label_buffer.a[hap->hap[a[i].uID]>>hap->label_shift] += a[i].weight;
            is_set_ava = 1;
        }
        is_unset_ava = 1;
    }
    
    if(hap_label_flag && is_set_ava == 0) return 0;
    if(is_unset_ava == 0) return 0;
    if(is_set_ava == 0 && is_unset_ava > 0)
    {
        hap->label += hap->label_add;
        (*max_hap_label) = hap->label;
        return 1;
    }
    
    double max_weight;
    uint32_t max_i;
    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_hap_weight) (*max_hap_weight) = max_weight;
    return 1;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, 0x10(%rsp)
movq %rdx, %r12
movq %rsi, %r15
movq %rcx, 0x8(%rsp)
movl $0xffffffff, (%rcx)     # imm = 0xFFFFFFFF
movl 0x24(%rsi), %eax
movb 0x2c(%rsi), %cl
shrl %cl, %eax
movq %rdi, %r13
incl %eax
cmpq %rax, 0x80(%rsi)
jae 0x14cda9
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x80(%r15)
movq 0x88(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x88(%r15)
movl 0x24(%r15), %ebx
movl 0x2c(%r15), %ebp
movl %ebx, %r14d
movl %ebp, %ecx
shrl %cl, %r14d
incl %r14d
movq %r14, 0x78(%r15)
je 0x14cdd7
movq 0x88(%r15), %rdi
movl %r14d, %edx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x30(%r15), %rax
movq 0x10(%rax), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %edx
testq %rdx, %rdx
je 0x14ce7a
movq 0x10(%rax,%rcx), %rax
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rdx
xorl %esi, %esi
xorl %edi, %edi
xorl %ecx, %ecx
cmpb $0x0, 0x18(%rax,%rsi)
js 0x14ce65
movq 0x10(%r15), %r8
movl 0x8(%rax,%rsi), %r9d
movl 0x1c(%r15), %r10d
orl 0x18(%r15), %r10d
movl (%r8,%r9,4), %r8d
orl 0x20(%r15), %r10d
testl %r8d, %r10d
je 0x14ce60
testq %r12, %r12
je 0x14ce3e
cmpb $0x0, (%r12,%r8)
je 0x14ce65
movl %ebp, %ecx
shrl %cl, %r8d
movsd (%rax,%rsi), %xmm0
movq 0x88(%r15), %rcx
addsd (%rcx,%r8,8), %xmm0
movsd %xmm0, (%rcx,%r8,8)
movl $0x1, %ecx
movl $0x1, %edi
addq $0x28, %rsi
cmpq %rsi, %rdx
jne 0x14ce0d
testl %ecx, %ecx
sete %cl
testl %edi, %edi
sete %al
jmp 0x14ce7e
movb $0x1, %cl
movb $0x1, %al
testq %r12, %r12
setne %dl
andb %cl, %dl
orb %al, %dl
xorl %eax, %eax
testb $0x1, %dl
jne 0x14cf14
testb %cl, %cl
je 0x14cead
addl 0x28(%r15), %ebx
movl %ebx, 0x24(%r15)
movq 0x8(%rsp), %rax
movl %ebx, (%rax)
movl $0x1, %eax
jmp 0x14cf14
testl %r14d, %r14d
je 0x14cee9
movq 0x88(%r15), %rcx
movl %r14d, %edx
movsd 0xd8674(%rip), %xmm1    # 0x225538
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm0
ucomisd %xmm1, %xmm0
cmoval %esi, %eax
maxsd %xmm1, %xmm0
incq %rsi
movapd %xmm0, %xmm1
cmpq %rsi, %rdx
jne 0x14cecb
jmp 0x14cef6
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movsd 0xd8642(%rip), %xmm0    # 0x225538
movl %ebp, %ecx
shll %cl, %eax
movq 0x8(%rsp), %rcx
movl %eax, (%rcx)
movl $0x1, %eax
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x14cf14
movsd %xmm0, (%rcx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq