// File: /workspace/github_cpp_makefile_star2_O3/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, 0x38(%rsp)
movl %r8d, %ebp
movl %ecx, %r14d
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
movq 0x30(%rdi), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x80(%rsi)
je 0x153919
xorl %ebx, %ebx
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
movsd 0xdde7e(%rip), %xmm0    # 0x231638
movsd %xmm0, 0x20(%rsp)
movl $0xffffffff, 0x1c(%rsp) # imm = 0xFFFFFFFF
movq 0x28(%r12), %rax
movq 0x40(%r12), %rcx
movl (%rcx,%rbx,4), %esi
movl 0x4(%rcx,%rbx,4), %edx
leaq (%rax,%rsi,4), %rax
addq $0x8, %rax
subl %esi, %edx
movq $0x0, 0x8(%rsp)
movl $0x0, %ecx
addl $-0x2, %edx
je 0x153824
movl %edx, %esi
xorl %ecx, %ecx
testl %r14d, %r14d
je 0x15381a
movl (%rax,%rcx,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 0x153824
incq %rcx
cmpq %rcx, %rsi
jne 0x1537fa
jmp 0x15382c
cmpl %edx, %ecx
jne 0x1538f1
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
testl %ebp, %ebp
je 0x153865
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x38(%rsp), %rcx
leaq 0x4(%rsp), %r8
leaq 0x8(%rsp), %r9
callq 0x15350d
testl %eax, %eax
je 0x1538f1
movsd 0x8(%rsp), %xmm0
jmp 0x1538d7
testl %edx, %edx
je 0x1538f1
movq 0x30(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl %edx, %edx
xorpd %xmm0, %xmm0
xorl %esi, %esi
xorl %edi, %edi
movl (%rax,%rsi,4), %r8d
shrl %r8d
leaq (%r8,%r8,2), %r9
shlq $0x4, %r9
movq (%rcx,%r9), %r8
testq %r8, %r8
je 0x1538cb
addq %rcx, %r9
movq 0x10(%r9), %r9
movl $0x1, %r10d
cmpb $0x0, 0x18(%r9)
js 0x1538bc
addsd (%r9), %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0x1, %edi
movl %r10d, %r11d
addq $0x28, %r9
incl %r10d
cmpq %r11, %r8
ja 0x1538a5
incq %rsi
cmpq %rdx, %rsi
jne 0x153880
testl %edi, %edi
je 0x1538f1
ucomisd 0x20(%rsp), %xmm0
jbe 0x1538f1
movl 0x4(%rsp), %eax
movl %eax, 0x18(%rsp)
movl %ebx, 0x1c(%rsp)
movsd %xmm0, 0x20(%rsp)
incl %ebx
cmpq %rbx, 0x80(%r12)
ja 0x1537c8
cmpq $0x0, (%r15)
jne 0x153941
movl 0x1c(%rsp), %esi
cmpl $-0x1, %esi
jne 0x153a9a
jmp 0x153c29
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
cmpq $0x0, (%r15)
je 0x153c29
movsd 0xddd05(%rip), %xmm0    # 0x231638
movsd %xmm0, 0x20(%rsp)
movl $0xffffffff, 0x1c(%rsp) # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movsd 0xddce5(%rip), %xmm1    # 0x231638
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x40(%rsp)
movq (%r12), %rax
movq 0x80(%r12), %rcx
movl (%rax,%rbx,4), %eax
incq %rcx
cmpq %rax, %rcx
jne 0x153a61
testl %r14d, %r14d
je 0x153996
movq 0x10(%r13), %rax
movl 0x1c(%r13), %ecx
orl 0x18(%r13), %ecx
orl 0x20(%r13), %ecx
testl %ecx, (%rax,%rbx,4)
jne 0x153a61
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
testl %ebp, %ebp
je 0x1539d4
movsd %xmm1, 0x28(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq 0x38(%rsp), %rdx
leaq 0x4(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0x1532f8
testl %eax, %eax
je 0x153a3a
movsd 0x8(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
jmp 0x153a48
movq $0x0, 0x8(%rsp)
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rcx
leaq (%rbx,%rbx,2), %rdx
shlq $0x4, %rdx
movq (%rcx,%rdx), %rax
testq %rax, %rax
je 0x153a61
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
xorpd %xmm0, %xmm0
xorl %edi, %edi
movb $0x1, %sil
xorl %edx, %edx
incl %edx
leaq (%rdi,%rdi,4), %rdi
cmpb $0x0, 0x18(%rcx,%rdi,8)
jns 0x153a21
movl %edx, %edi
incl %edx
cmpq %rdi, %rax
ja 0x153a0b
jmp 0x153a42
leaq (%rcx,%rdi,8), %rsi
addsd (%rsi), %xmm0
movsd %xmm0, 0x8(%rsp)
movl %edx, %edi
xorl %esi, %esi
cmpq %rdi, %rax
ja 0x153a09
jmp 0x153a48
movsd 0x28(%rsp), %xmm1
jmp 0x153a61
testb $0x1, %sil
jne 0x153a61
ucomisd %xmm1, %xmm0
jbe 0x153a61
movl 0x4(%rsp), %eax
movl %eax, 0x14(%rsp)
movl %ebx, %eax
movq %rax, 0x40(%rsp)
movapd %xmm0, %xmm1
incl %ebx
movq (%r15), %rax
cmpq %rbx, %rax
ja 0x15395d
movl 0x1c(%rsp), %esi
cmpl $-0x1, %esi
setne %cl
movq 0x40(%rsp), %rdi
cmpl $-0x1, %edi
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x153a95
ucomisd 0x20(%rsp), %xmm1
jbe 0x153a9a
jmp 0x153ab6
cmpl $-0x1, %esi
je 0x153ab1
addl %esi, %esi
movl 0x18(%rsp), %ecx
movq 0x80(%rsp), %rax
movl %ecx, (%rax)
movl %esi, %eax
jmp 0x153c2e
cmpl $-0x1, %edi
je 0x153ae9
testq %rax, %rax
sete %al
leal (%rdi,%rdi), %ecx
movsd %xmm1, 0x8(%rsp)
movq 0x80(%rsp), %rdx
movl 0x14(%rsp), %esi
movl %esi, (%rdx)
cmpl $-0x2, %ecx
setne %cl
orb %al, %cl
je 0x153af2
leal 0x1(,%rdi,2), %eax
jmp 0x153c2e
testq %rax, %rax
je 0x153c29
xorl %ebx, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x28(%rsp)
movsd 0xddb32(%rip), %xmm0    # 0x231638
movsd %xmm0, 0x20(%rsp)
movq (%r12), %rax
movq 0x80(%r12), %rcx
movl (%rax,%rbx,4), %eax
incq %rcx
cmpq %rax, %rcx
jbe 0x153c04
testl %r14d, %r14d
je 0x153b45
movq 0x10(%r13), %rax
movl 0x1c(%r13), %ecx
orl 0x18(%r13), %ecx
orl 0x20(%r13), %ecx
testl %ecx, (%rax,%rbx,4)
jne 0x153c04
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
testl %ebp, %ebp
je 0x153b7b
movq %rbx, %rdi
movq %r13, %rsi
movq 0x38(%rsp), %rdx
leaq 0x4(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0x1532f8
testl %eax, %eax
je 0x153c04
movsd 0x8(%rsp), %xmm0
jmp 0x153be7
movq $0x0, 0x8(%rsp)
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rcx
leaq (%rbx,%rbx,2), %rdx
shlq $0x4, %rdx
movq (%rcx,%rdx), %rax
testq %rax, %rax
je 0x153c04
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
xorpd %xmm0, %xmm0
xorl %edi, %edi
movb $0x1, %sil
xorl %edx, %edx
incl %edx
leaq (%rdi,%rdi,4), %rdi
cmpb $0x0, 0x18(%rcx,%rdi,8)
jns 0x153bc8
movl %edx, %edi
incl %edx
cmpq %rdi, %rax
ja 0x153bb2
jmp 0x153be1
leaq (%rcx,%rdi,8), %rsi
addsd (%rsi), %xmm0
movsd %xmm0, 0x8(%rsp)
movl %edx, %edi
xorl %esi, %esi
cmpq %rdi, %rax
ja 0x153bb0
jmp 0x153be7
testb $0x1, %sil
jne 0x153c04
ucomisd 0x20(%rsp), %xmm0
jbe 0x153c04
movl 0x4(%rsp), %eax
movl %eax, 0x14(%rsp)
movl %ebx, %eax
movq %rax, 0x28(%rsp)
movsd %xmm0, 0x20(%rsp)
incl %ebx
cmpq %rbx, (%r15)
ja 0x153b0c
movq 0x28(%rsp), %rax
cmpl $-0x1, %eax
je 0x153c29
leal 0x1(,%rax,2), %esi
movl 0x14(%rsp), %ecx
jmp 0x153aa0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq