// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %r9, 0x48(%rsp)
movl %r8d, 0x34(%rsp)
movl %ecx, %r14d
movq %rdx, 0x60(%rsp)
movq %rdi, %r13
movq 0x30(%rdi), %rbp
movsd 0xd7366(%rip), %xmm0    # 0x1e3580
movsd %xmm0, 0x38(%rsp)
pushq $-0x1
popq %rax
xorl %r15d, %r15d
pushq $0x1
popq %r12
movl %eax, 0x44(%rsp)
movl %eax, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
movl %r15d, %ebx
cmpq %rbx, 0x80(%rsi)
jbe 0x10c371
andq $0x0, (%rsp)
movq %rsi, %rdi
movq %rbx, %rsi
leaq 0x74(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r8
leaq 0x54(%rsp), %r9
callq 0xf6ab5
andq $0x0, 0x20(%rsp)
movl 0x54(%rsp), %eax
movq 0x68(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x10c2aa
testl %r14d, %r14d
je 0x10c2a1
movl (%rcx,%rdx,4), %esi
shrl %esi
movq 0x10(%r13), %rdi
movl 0x1c(%r13), %r8d
orl 0x18(%r13), %r8d
orl 0x20(%r13), %r8d
testl %r8d, (%rdi,%rsi,4)
jne 0x10c2a6
incq %rdx
jmp 0x10c27c
cmpl %edx, %eax
jne 0x10c2e6
orl $-0x1, 0x14(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x10c2ed
movq %rbx, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq 0x48(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x20(%rsp), %r9
callq 0x10bfca
testl %eax, %eax
je 0x10c2e6
movsd 0x20(%rsp), %xmm0
movq 0x18(%rsp), %rsi
jmp 0x10c34e
movq 0x18(%rsp), %rsi
jmp 0x10c369
xorl %edx, %edx
xorpd %xmm0, %xmm0
xorl %esi, %esi
cmpq %rax, %rdx
je 0x10c345
movl (%rcx,%rdx,4), %edi
shrl %edi
movq 0x10(%rbp), %r8
imulq $0x30, %rdi, %r9
leaq (%r8,%r9), %rdi
movq (%r8,%r9), %r8
xorl %r9d, %r9d
movl %r9d, %r9d
cmpq %r9, %r8
jbe 0x10c340
movq 0x10(%rdi), %r10
imulq $0x28, %r9, %r11
cmpb $0x0, 0x18(%r10,%r11)
js 0x10c33b
addq %r11, %r10
addsd (%r10), %xmm0
movsd %xmm0, 0x20(%rsp)
movl %r12d, %esi
incl %r9d
jmp 0x10c312
incq %rdx
jmp 0x10c2f5
testl %esi, %esi
movq 0x18(%rsp), %rsi
je 0x10c369
ucomisd 0x38(%rsp), %xmm0
jbe 0x10c369
movl 0x14(%rsp), %eax
movl %eax, 0x30(%rsp)
movl %r15d, 0x44(%rsp)
movsd %xmm0, 0x38(%rsp)
incl %r15d
jmp 0x10c237
movsd 0xd7207(%rip), %xmm1    # 0x1e3580
pushq $-0x1
popq %r15
xorl %r12d, %r12d
pushq $0x1
popq %rbx
movl %r15d, 0x2c(%rsp)
movl %r12d, %edi
movq 0x60(%rsp), %rax
movq (%rax), %rax
cmpq %rdi, %rax
jbe 0x10c48e
movq (%rsi), %rax
movq 0x80(%rsi), %rcx
movl (%rax,%rdi,4), %eax
incq %rcx
cmpq %rax, %rcx
jne 0x10c486
testl %r14d, %r14d
je 0x10c3d3
movq 0x10(%r13), %rax
movl 0x1c(%r13), %ecx
orl 0x18(%r13), %ecx
orl 0x20(%r13), %ecx
testl %ecx, (%rax,%rdi,4)
jne 0x10c486
orl $-0x1, 0x14(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x10c413
movsd %xmm1, 0x58(%rsp)
movq %r13, %rsi
movq 0x48(%rsp), %rdx
leaq 0x14(%rsp), %rcx
leaq 0x20(%rsp), %r8
callq 0x10be02
testl %eax, %eax
je 0x10c47b
movsd 0x20(%rsp), %xmm0
movq 0x18(%rsp), %rsi
movsd 0x58(%rsp), %xmm1
jmp 0x10c464
andq $0x0, 0x20(%rsp)
movq 0x10(%rbp), %rcx
imulq $0x30, %rdi, %rdx
leaq (%rcx,%rdx), %rax
movq (%rcx,%rdx), %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
xorl %esi, %esi
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0x10c45b
movq 0x10(%rax), %rdi
imulq $0x28, %rdx, %r8
cmpb $0x0, 0x18(%rdi,%r8)
js 0x10c457
addq %r8, %rdi
addsd (%rdi), %xmm0
movsd %xmm0, 0x20(%rsp)
movl %ebx, %esi
incl %edx
jmp 0x10c431
testl %esi, %esi
movq 0x18(%rsp), %rsi
je 0x10c486
ucomisd %xmm1, %xmm0
jbe 0x10c486
movl 0x14(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl %r12d, %r15d
movapd %xmm0, %xmm1
jmp 0x10c486
movq 0x18(%rsp), %rsi
movsd 0x58(%rsp), %xmm1
incl %r12d
jmp 0x10c388
movl 0x44(%rsp), %esi
cmpl $-0x1, %esi
setne %cl
cmpl $-0x1, %r15d
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x10c4b0
ucomisd 0x38(%rsp), %xmm1
jbe 0x10c4b5
jmp 0x10c4c6
cmpl $-0x1, %esi
je 0x10c4c0
addl %esi, %esi
movl 0x30(%rsp), %ecx
jmp 0x10c60e
cmpl $-0x1, %r15d
je 0x10c4e7
leal (%r15,%r15), %ecx
movsd %xmm1, 0x20(%rsp)
movq 0xb0(%rsp), %rdx
movl 0x2c(%rsp), %esi
movl %esi, (%rdx)
cmpl $-0x2, %ecx
jne 0x10c61a
movsd 0xd7091(%rip), %xmm0    # 0x1e3580
movsd %xmm0, 0x38(%rsp)
pushq $-0x1
popq %r15
xorl %r12d, %r12d
pushq $0x1
popq %rbx
movq 0x18(%rsp), %rdx
movl %r12d, %edi
cmpq %rdi, %rax
jbe 0x10c5fc
movq (%rdx), %rax
movq 0x80(%rdx), %rcx
movl (%rax,%rdi,4), %eax
incq %rcx
cmpq %rax, %rcx
jbe 0x10c5ec
testl %r14d, %r14d
je 0x10c547
movq 0x10(%r13), %rax
movl 0x1c(%r13), %ecx
orl 0x18(%r13), %ecx
orl 0x20(%r13), %ecx
testl %ecx, (%rax,%rdi,4)
jne 0x10c5ec
orl $-0x1, 0x14(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x10c57b
movq %r13, %rsi
movq 0x48(%rsp), %rdx
leaq 0x14(%rsp), %rcx
leaq 0x20(%rsp), %r8
callq 0x10be02
testl %eax, %eax
je 0x10c5e7
movsd 0x20(%rsp), %xmm0
movq 0x18(%rsp), %rdx
jmp 0x10c5cc
andq $0x0, 0x20(%rsp)
movq 0x10(%rbp), %rcx
imulq $0x30, %rdi, %rdx
leaq (%rcx,%rdx), %rax
movq (%rcx,%rdx), %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
xorl %esi, %esi
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0x10c5c3
movq 0x10(%rax), %rdi
imulq $0x28, %rdx, %r8
cmpb $0x0, 0x18(%rdi,%r8)
js 0x10c5bf
addq %r8, %rdi
addsd (%rdi), %xmm0
movsd %xmm0, 0x20(%rsp)
movl %ebx, %esi
incl %edx
jmp 0x10c599
testl %esi, %esi
movq 0x18(%rsp), %rdx
je 0x10c5ec
ucomisd 0x38(%rsp), %xmm0
jbe 0x10c5ec
movl 0x14(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl %r12d, %r15d
movsd %xmm0, 0x38(%rsp)
jmp 0x10c5ec
movq 0x18(%rsp), %rdx
incl %r12d
movq 0x60(%rsp), %rax
movq (%rax), %rax
jmp 0x10c504
cmpl $-0x1, %r15d
je 0x10c624
leal 0x1(,%r15,2), %esi
movl 0x2c(%rsp), %ecx
movq 0xb0(%rsp), %rax
movl %ecx, (%rax)
jmp 0x10c627
leal 0x1(,%r15,2), %esi
jmp 0x10c627
pushq $-0x1
popq %rsi
movl %esi, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq