// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %rax
movq %rcx, %r14
movq %rdx, %r12
orl $-0x1, (%rcx)
movq %rsi, %r15
movl 0x24(%rsi), %esi
movl 0x2c(%r15), %ecx
movl %esi, %edx
shrl %cl, %edx
movq %rdi, %r13
incl %edx
cmpq %rdx, 0x80(%r15)
movq %r8, (%rsp)
jae 0x10be9e
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x80(%r15)
movq 0x88(%r15), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, 0x88(%r15)
movl 0x24(%r15), %esi
movl 0x2c(%r15), %ecx
movl %esi, %edx
shrl %cl, %edx
incl %edx
movq %rdx, 0x78(%r15)
xorl %eax, %eax
cmpq %rax, %rdx
je 0x10beba
movq 0x88(%r15), %rdi
andq $0x0, (%rdi,%rax,8)
incq %rax
jmp 0x10bea4
movq 0x30(%r15), %rax
movq 0x10(%rax), %rax
imulq $0x30, %r13, %rdi
movl (%rax,%rdi), %r8d
movq 0x10(%rax,%rdi), %rax
imulq $0x28, %r8, %rdi
xorl %r8d, %r8d
pushq $0x1
popq %r10
xorl %r9d, %r9d
xorl %r11d, %r11d
cmpq %r8, %rdi
je 0x10bf40
cmpb $0x0, 0x18(%rax,%r8)
js 0x10bf3a
movq 0x10(%r15), %r13
movl 0x8(%rax,%r8), %ebp
movl 0x1c(%r15), %ebx
orl 0x18(%r15), %ebx
movl (%r13,%rbp,4), %r13d
orl 0x20(%r15), %ebx
testl %r13d, %ebx
je 0x10bf37
testq %r12, %r12
je 0x10bf18
cmpb $0x0, (%r12,%r13)
je 0x10bf3a
shrl %cl, %r13d
movsd (%rax,%r8), %xmm0
movq 0x88(%r15), %r9
addsd (%r9,%r13,8), %xmm0
movsd %xmm0, (%r9,%r13,8)
movl %r10d, %r9d
movl %r10d, %r11d
addq $0x28, %r8
jmp 0x10bee0
testq %r12, %r12
setne %al
testl %r9d, %r9d
sete %dil
andb %al, %dil
testl %r11d, %r11d
sete %r8b
xorl %eax, %eax
orb %dil, %r8b
jne 0x10bfbb
testl %r9d, %r9d
je 0x10bfad
pushq $-0x1
popq %rax
movsd 0xd7612(%rip), %xmm0    # 0x1e3580
xorl %esi, %esi
movq (%rsp), %r8
cmpq %rsi, %rdx
je 0x10bf99
movq 0x88(%r15), %rdi
movsd (%rdi,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %esi, %eax
maxsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
jmp 0x10bf74
shll %cl, %eax
movl %eax, (%r14)
pushq $0x1
popq %rax
testq %r8, %r8
je 0x10bfbb
movsd %xmm0, (%r8)
jmp 0x10bfbb
addl 0x28(%r15), %esi
movl %esi, 0x24(%r15)
movl %esi, (%r14)
pushq $0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq