// File: /workspace/github_cpp_makefile_star2_O3/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 %rcx, %rax
movq %rdx, %r12
movq %rsi, %r15
movl $0xffffffff, (%rcx)     # imm = 0xFFFFFFFF
movl 0x24(%rsi), %r14d
movl 0x2c(%rsi), %ebp
movl %r14d, %ebx
movl %ebp, %ecx
shrl %cl, %ebx
movq %rdi, %r13
incl %ebx
cmpq %rbx, 0x80(%rsi)
movq %rax, 0x8(%rsp)
jae 0x1533a7
leaq -0x1(%rbx), %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), %r14d
movl 0x2c(%r15), %ebp
movl %r14d, %ebx
movl %ebp, %ecx
shrl %cl, %ebx
incl %ebx
movq %rbx, 0x78(%r15)
testl %ebx, %ebx
je 0x1533c5
movq 0x88(%r15), %rdi
leaq (,%rbx,8), %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 0x153475
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 0x153453
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 0x15344e
testq %r12, %r12
je 0x15342c
cmpb $0x0, (%r12,%r8)
je 0x153453
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 0x1533fb
testl %ecx, %ecx
sete %al
testl %edi, %edi
sete %dl
testq %r12, %r12
setne %sil
andb %al, %sil
orb %dl, %sil
je 0x153486
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %ecx, %ecx
je 0x1534c2
testl %ebx, %ebx
je 0x1534d9
movq 0x88(%r15), %rcx
movsd 0xde19b(%rip), %xmm1    # 0x231638
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm0
ucomisd %xmm1, %xmm0
cmoval %edx, %eax
maxsd %xmm1, %xmm0
incq %rdx
movapd %xmm0, %xmm1
cmpq %rdx, %rbx
jne 0x1534a4
jmp 0x1534e6
addl 0x28(%r15), %r14d
movl %r14d, 0x24(%r15)
movq 0x8(%rsp), %rax
movl %r14d, (%rax)
movl $0x1, %eax
jmp 0x153477
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movsd 0xde152(%rip), %xmm0    # 0x231638
movl %ebp, %ecx
shll %cl, %eax
movq 0x8(%rsp), %rcx
movl %eax, (%rcx)
movl $0x1, %eax
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x153477
movsd %xmm0, (%rcx)
jmp 0x153477