// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: get_available_com(H_partition*, bubble_type*, ma_ug_t*, unsigned int, unsigned int, unsigned char*, unsigned int*)
uint32_t get_available_com(H_partition* hap, bubble_type* bub, ma_ug_t *ug, uint32_t check_self, uint32_t check_others, 
uint8_t* hap_label_flag, uint32_t* max_hap_label)
{
    hc_links* link = hap->link;
    uint32_t beg, sink, n, *a, i, j, k, uID, max_bub_i, max_non_bub_i, max_i, is_ava;
    uint32_t hap_label, max_bub_label = (uint32_t)-1, max_non_bub_label = (uint32_t)-1;
    double w, max_bub_w, max_non_bub_w;
    max_i = (uint32_t)-1;

    for (i = 0, max_bub_w = -1, max_bub_i = (uint32_t)-1; i < bub->f_bub/**bub->s_bub**/; i++)
    {
        get_bubbles(bub, i, &beg, &sink, &a, &n, NULL);
        for (j = 0, w = 0; j < n; j++)
        {
            uID = a[j]>>1;
            if(check_self && is_hap_set(uID, *hap)) break;
        }
        if(j != n) continue;

        is_ava = 0;
        hap_label = (uint32_t)-1;
        if(check_others)
        {
            if(get_weightest_hap_label_from_bubble(i, hap, bub, 
                                                    hap_label_flag, &hap_label, &w)>0)
            {
                is_ava = 1;
            }
        }
        else
        {
            for (j = 0, w = 0, is_ava = 0; j < n; j++)
            {
                uID = a[j]>>1;
                for (k = 0; k < link->a.a[uID].e.n; k++)
                {
                    if(link->a.a[uID].e.a[k].del) continue;
                    w += link->a.a[uID].e.a[k].weight;
                    is_ava = 1;
                }
            }
        }
        
        if(is_ava == 0) continue;

        if(w > max_bub_w)
        {
            max_bub_w = w;
            max_bub_i = i;
            max_bub_label = hap_label;
        }
    }

    for (i = 0, max_non_bub_w = -1, max_non_bub_i = (uint32_t)-1; i < ug->u.n; i++)
    {
        if(IF_HET(i, *bub)/** || (bub->index[i] >= bub->s_bub && bub->index[i] < bub->f_bub)**/)
        {
            uID = i;
            is_ava = 0;
            if(check_self && is_hap_set(uID, *hap)) continue;
            hap_label = (uint32_t)-1;
            if(check_others)
            {
                if(get_weightest_hap_label_from_uid(uID, hap, hap_label_flag, &hap_label, &w)>0)
                {
                    is_ava = 1;
                }
            }
            else
            {
                for (k = 0, w = 0, is_ava = 0; k < link->a.a[uID].e.n; k++)
                {
                    if(link->a.a[uID].e.a[k].del) continue;
                    w += link->a.a[uID].e.a[k].weight;
                    is_ava = 1;
                }
            }
            
            if(is_ava == 0) continue;

            if(w > max_non_bub_w)
            {
                max_non_bub_w = w;
                max_non_bub_i = i;
                max_non_bub_label = hap_label;
            }
        }
    }

    if(max_bub_i != (uint32_t)-1 && max_non_bub_i != (uint32_t)-1)
    {
        if(max_non_bub_w > max_bub_w)
        {
            max_i = (max_non_bub_i << 1) + 1;
            w = max_non_bub_w;
            (*max_hap_label) = max_non_bub_label;
        }
        else
        {
            max_i = (max_bub_i << 1);
            w = max_bub_w;
            (*max_hap_label) = max_bub_label;
        }
    }
    else if(max_bub_i != (uint32_t)-1)
    {
        max_i = (max_bub_i << 1);
        w = max_bub_w;
        (*max_hap_label) = max_bub_label;
    }
    else if(max_non_bub_i != (uint32_t)-1)
    {
        max_i = (max_non_bub_i << 1) + 1;
        w = max_non_bub_w;
        (*max_hap_label) = max_non_bub_label;
    }

    if(max_i == (uint32_t)-1)
    {
        for (i = 0, max_non_bub_w = -1, max_non_bub_i = (uint32_t)-1; i < ug->u.n; i++)
        {
            ///if(bub->index[i] < bub->s_bub)
            if(IF_BUB(i, *bub))
            {

                uID = i;
                is_ava = 0;
                if(check_self && is_hap_set(uID, *hap)) continue;
                hap_label = (uint32_t)-1;
                if(check_others)
                {
                    if(get_weightest_hap_label_from_uid(uID, hap, hap_label_flag, &hap_label, &w)>0)
                    {
                        is_ava = 1;
                    }
                }
                else
                {
                    for (k = 0, w = 0, is_ava = 0; k < link->a.a[uID].e.n; k++)
                    {
                        if(link->a.a[uID].e.a[k].del) continue;
                        w += link->a.a[uID].e.a[k].weight;
                        is_ava = 1;
                    }
                }
                
                if(is_ava == 0) continue;

                if(w > max_non_bub_w)
                {
                    max_non_bub_w = w;
                    max_non_bub_i = i;
                    max_non_bub_label = hap_label;
                }

            }
        }

        if(max_non_bub_i != (uint32_t)-1)
        {
            max_i = (max_non_bub_i << 1) + 1;
            w = max_non_bub_w;
            (*max_hap_label) = max_non_bub_label;
        }
    }

    // if(max_i == (uint32_t)-1)
    // {
    //     fprintf(stderr, "-Cannot find!\n");
    // }
    // else if(max_i & 1)
    // {
    //     fprintf(stderr, "-utg-%uth, phasing ID: %u, w: %f, max_bub_i: %u, max_bub_w: %f, max_non_bub_i: %u, max_non_bub_w: %f\n", 
    //     max_i>>1, hap->label>>3, w, max_bub_i, max_bub_w, max_non_bub_i, max_non_bub_w);
    // }
    // else
    // {
    //     fprintf(stderr, "-bubble-%uth, phasing ID: %u, w: %f, max_bub_i: %u, max_bub_w: %f, max_non_bub_i: %u, max_non_bub_w: %f\n", 
    //     max_i>>1, hap->label>>3, w, max_bub_i, max_bub_w, max_non_bub_i, max_non_bub_w);
    // }

    return max_i;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x40(%rsp)
movl %r8d, %ebp
movl %ecx, %r14d
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
movq 0x30(%rdi), %rax
movq %rax, 0x38(%rsp)
movsd 0xd838d(%rip), %xmm1    # 0x225538
cmpq $0x0, 0x80(%rsi)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x30(%rsp)
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movapd %xmm1, %xmm2
je 0x14d336
xorl %ebx, %ebx
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movsd 0xd834f(%rip), %xmm2    # 0x225538
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
movq 0x28(%r12), %rcx
movq 0x40(%r12), %rax
movl (%rax,%rbx,4), %edx
movl 0x4(%rax,%rbx,4), %eax
leaq (%rcx,%rdx,4), %rcx
addq $0x8, %rcx
subl %edx, %eax
movq $0x0, (%rsp)
movl $0x0, %edx
addl $-0x2, %eax
je 0x14d24c
movl %eax, %esi
xorl %edx, %edx
testl %r14d, %r14d
je 0x14d242
movl (%rcx,%rdx,4), %edi
shrl %edi
movq 0x10(%r13), %r8
movl 0x1c(%r13), %r9d
orl 0x18(%r13), %r9d
orl 0x20(%r13), %r9d
testl %r9d, (%r8,%rdi,4)
jne 0x14d24c
incq %rdx
cmpq %rdx, %rsi
jne 0x14d222
movl %eax, %edx
cmpl %eax, %edx
jne 0x14d326
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
testl %ebp, %ebp
je 0x14d291
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x40(%rsp), %rcx
leaq 0xc(%rsp), %r8
movq %rsp, %r9
movsd %xmm2, 0x18(%rsp)
callq 0x14cf23
movsd 0x18(%rsp), %xmm2
movsd 0xd82a9(%rip), %xmm1    # 0x225538
jmp 0x14d307
movq $0x0, (%rsp)
testl %eax, %eax
je 0x14d305
movsd (%rsp), %xmm0
movq 0x38(%rsp), %rdx
movq 0x10(%rdx), %rdx
movl %eax, %esi
xorl %edi, %edi
xorl %eax, %eax
movl (%rcx,%rdi,4), %r8d
shrl %r8d
leaq (%r8,%r8,2), %r9
shlq $0x4, %r9
movq (%rdx,%r9), %r8
testq %r8, %r8
je 0x14d2fb
addq %rdx, %r9
movq 0x10(%r9), %r9
movl $0x1, %r10d
cmpb $0x0, 0x18(%r9)
js 0x14d2ec
addsd (%r9), %xmm0
movsd %xmm0, (%rsp)
movl $0x1, %eax
movl %r10d, %r11d
addq $0x28, %r9
incl %r10d
cmpq %r11, %r8
ja 0x14d2d6
incq %rdi
cmpq %rsi, %rdi
jne 0x14d2b1
jmp 0x14d307
xorl %eax, %eax
testl %eax, %eax
je 0x14d326
movsd (%rsp), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x14d326
movl 0xc(%rsp), %eax
movl %eax, 0x14(%rsp)
movl %ebx, 0x24(%rsp)
movapd %xmm0, %xmm2
incl %ebx
cmpq %rbx, 0x80(%r12)
ja 0x14d1f1
cmpq $0x0, (%r15)
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
je 0x14d462
xorl %ebx, %ebx
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movsd 0xd81de(%rip), %xmm1    # 0x225538
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x30(%rsp)
movsd %xmm2, 0x18(%rsp)
movq (%r12), %rax
movq 0x80(%r12), %rcx
movl (%rax,%rbx,4), %eax
incq %rcx
cmpq %rax, %rcx
jne 0x14d457
testl %r14d, %r14d
je 0x14d3a3
movq 0x10(%r13), %rax
movl 0x1c(%r13), %ecx
orl 0x18(%r13), %ecx
orl 0x20(%r13), %ecx
testl %ecx, (%rax,%rbx,4)
jne 0x14d457
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
testl %ebp, %ebp
je 0x14d3db
movq %rbx, %rdi
movq %r13, %rsi
movq 0x40(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %rsp, %r8
movsd %xmm1, 0x28(%rsp)
callq 0x14cd15
movsd 0x18(%rsp), %xmm2
movsd 0x28(%rsp), %xmm1
jmp 0x14d435
movq $0x0, (%rsp)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%rbx,%rbx,2), %rdx
shlq $0x4, %rdx
movq (%rax,%rdx), %rcx
testq %rcx, %rcx
je 0x14d433
addq %rdx, %rax
movsd (%rsp), %xmm0
movq 0x10(%rax), %rdx
xorl %eax, %eax
movl $0x1, %esi
cmpb $0x0, 0x18(%rdx)
js 0x14d424
addsd (%rdx), %xmm0
movsd %xmm0, (%rsp)
movl $0x1, %eax
movl %esi, %edi
addq $0x28, %rdx
incl %esi
cmpq %rdi, %rcx
ja 0x14d410
jmp 0x14d435
xorl %eax, %eax
testl %eax, %eax
je 0x14d457
movsd (%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14d457
movl 0xc(%rsp), %eax
movl %eax, 0x10(%rsp)
movl %ebx, %eax
movq %rax, 0x30(%rsp)
movapd %xmm0, %xmm1
incl %ebx
cmpq %rbx, (%r15)
ja 0x14d36a
movq 0x80(%rsp), %rdi
movl 0x24(%rsp), %eax
cmpl $-0x1, %eax
setne %dl
movq 0x30(%rsp), %rsi
cmpl $-0x1, %esi
setne %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x14d492
ucomisd %xmm2, %xmm1
movl 0x14(%rsp), %ecx
jbe 0x14d49b
jmp 0x14d4ad
cmpl $-0x1, %eax
movl 0x14(%rsp), %ecx
je 0x14d4a4
addl %eax, %eax
movsd %xmm2, (%rsp)
jmp 0x14d4bd
cmpl $-0x1, %esi
je 0x14d62e
leal 0x1(,%rsi,2), %eax
movsd %xmm1, (%rsp)
movl 0x10(%rsp), %ecx
movl %ecx, (%rdi)
cmpl $-0x1, %eax
jne 0x14d61f
cmpq $0x0, (%r15)
je 0x14d5d8
xorl %ebx, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x28(%rsp)
movsd 0xd8052(%rip), %xmm0    # 0x225538
movsd %xmm0, 0x18(%rsp)
movq (%r12), %rax
movq 0x80(%r12), %rcx
movl (%rax,%rbx,4), %eax
incq %rcx
cmpq %rax, %rcx
jbe 0x14d5cb
testl %r14d, %r14d
je 0x14d525
movq 0x10(%r13), %rax
movl 0x1c(%r13), %ecx
orl 0x18(%r13), %ecx
orl 0x20(%r13), %ecx
testl %ecx, (%rax,%rbx,4)
jne 0x14d5cb
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
testl %ebp, %ebp
je 0x14d54b
movq %rbx, %rdi
movq %r13, %rsi
movq 0x40(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %rsp, %r8
callq 0x14cd15
jmp 0x14d5a5
movq $0x0, (%rsp)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%rbx,%rbx,2), %rdx
shlq $0x4, %rdx
movq (%rax,%rdx), %rcx
testq %rcx, %rcx
je 0x14d5a3
addq %rdx, %rax
movsd (%rsp), %xmm0
movq 0x10(%rax), %rdx
xorl %eax, %eax
movl $0x1, %esi
cmpb $0x0, 0x18(%rdx)
js 0x14d594
addsd (%rdx), %xmm0
movsd %xmm0, (%rsp)
movl $0x1, %eax
movl %esi, %edi
addq $0x28, %rdx
incl %esi
cmpq %rdi, %rcx
ja 0x14d580
jmp 0x14d5a5
xorl %eax, %eax
testl %eax, %eax
je 0x14d5cb
movsd (%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x14d5cb
movl 0xc(%rsp), %eax
movl %eax, 0x10(%rsp)
movl %ebx, %eax
movq %rax, 0x28(%rsp)
movsd %xmm0, 0x18(%rsp)
incl %ebx
cmpq %rbx, (%r15)
ja 0x14d4ec
jmp 0x14d5f0
movsd 0xd7f58(%rip), %xmm0    # 0x225538
movsd %xmm0, 0x18(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rcx
cmpl $-0x1, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x80(%rsp), %rdx
je 0x14d61f
leal 0x1(,%rcx,2), %eax
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
movl 0x10(%rsp), %ecx
movl %ecx, (%rdx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x14d4bf