// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x24(%rcx), %edx
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %edx
movl %edx, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x1ecf30
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x2, %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x4, %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x8, %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
shrq $0x10, %rcx
movq -0x18(%rbp), %rax
orq 0x80(%rax), %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x88(%rax)
jmp 0x1ecf32
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x78(%rax)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x78(%rcx), %rax
jae 0x1ecf8e
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movl -0x34(%rbp), %ecx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ecf60
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rax), %rax
imulq $0x30, -0x10(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rax), %rax
imulq $0x30, -0x10(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x1ed0dc
movq -0x48(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movb 0x18(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x1ed002
jmp 0x1ed0ce
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
movl -0x34(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movq -0x18(%rbp), %rcx
movl 0x18(%rcx), %ecx
movq -0x18(%rbp), %rdx
orl 0x1c(%rdx), %ecx
movq -0x18(%rbp), %rdx
orl 0x20(%rdx), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1ed0c7
cmpq $0x0, -0x20(%rbp)
je 0x1ed070
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x48(%rbp), %rdx
movl -0x34(%rbp), %esi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
movl 0x8(%rdx), %edx
movl (%rcx,%rdx,4), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x1ed070
jmp 0x1ed0ce
movq -0x48(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x48(%rbp), %rdx
movl -0x34(%rbp), %esi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
movl 0x8(%rdx), %edx
movl (%rcx,%rdx,4), %edx
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %edx
movl %edx, %ecx
movl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl $0x1, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ecfd9
cmpq $0x0, -0x20(%rbp)
je 0x1ed0f5
cmpl $0x0, -0x38(%rbp)
jne 0x1ed0f5
movl $0x0, -0x4(%rbp)
jmp 0x1ed1d9
cmpl $0x0, -0x3c(%rbp)
jne 0x1ed107
movl $0x0, -0x4(%rbp)
jmp 0x1ed1d9
cmpl $0x0, -0x38(%rbp)
jne 0x1ed13d
cmpl $0x0, -0x3c(%rbp)
jbe 0x1ed13d
movq -0x18(%rbp), %rax
movl 0x28(%rax), %ecx
movq -0x18(%rbp), %rax
addl 0x24(%rax), %ecx
movl %ecx, 0x24(%rax)
movq -0x18(%rbp), %rax
movl 0x24(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x1ed1d9
movl $0x0, -0x34(%rbp)
movsd 0x19512c(%rip), %xmm0   # 0x382278
movsd %xmm0, -0x50(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movl -0x34(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x78(%rcx), %rax
jae 0x1ed1aa
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movl -0x34(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x50(%rbp), %xmm0
jbe 0x1ed19d
movq -0x18(%rbp), %rax
movq 0x88(%rax), %rax
movl -0x34(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0x1ed19f
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ed158
movl -0x54(%rbp), %eax
movq -0x18(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shll %cl, %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x30(%rbp)
je 0x1ed1d2
movsd -0x50(%rbp), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)