// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: select_bmer(unsigned int, unsigned long, bubble_type const*, min_cut_t*, unsigned int)
uint64_t select_bmer(uint32_t src, uint64_t k, const bubble_type* bub, min_cut_t* x, uint32_t bub_only)
{
    uint32_t beg, sink, n, *a;
    uint32_t v, d, u, i, nv, b_mer_d, j;
    asg_t *sg = bub->ug->g;
    uint64_t *p = NULL;
    asg_arc_t *av = NULL;

    memset(x->rGraphVis.a, 0, x->rGraphVis.n);
    kdq_push(uint64_t, x->q, set_dv(src , 0));
    b_mer_d = 0; 

    x->rGraphVis.a[src] = 1;
    x->bmerVis.a[src] = 1;

    while (1)
    {
        p = kdq_shift(uint64_t, x->q);
        if(!p) break;
        v = (uint32_t)(*p); d = ((uint64_t)(*p))>>32;

        v = v<<1;
        av = asg_arc_a(sg, v);
        nv = asg_arc_n(sg, v);
        for (i = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            u = av[i].v>>1;
            if(x->rGraphVis.a[u]) continue;
            x->rGraphVis.a[u] = 1;
            if(IF_HOM(u, *bub))
            {
                if(d < k) kdq_push(uint64_t, x->q, set_dv(u, d+1));
            }
            else
            {
                kdq_push(uint64_t, x->q, set_dv(u , d));
                b_mer_d = d;
                if(IF_BUB(u, *bub) && x->bmerVis.a[u] == 0)
                {
                    get_bubbles((bubble_type*)bub, bub->index[u], &beg, &sink, &a, &n, NULL);
                    for (j = 0; j < n; j++) x->bmerVis.a[(a[j]>>1)] = 1;
                }
                //must be here
                if(bub_only == 0) x->bmerVis.a[u] = 1;
            }            
        }


        v = v + 1;
        av = asg_arc_a(sg, v);
        nv = asg_arc_n(sg, v);
        for (i = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            u = av[i].v>>1;
            if(x->rGraphVis.a[u]) continue;
            x->rGraphVis.a[u] = 1;
            if(IF_HOM(u, *bub))
            {
                if(d < k) kdq_push(uint64_t, x->q, set_dv(u, d+1));
            }
            else
            {
                kdq_push(uint64_t, x->q, set_dv(u , d));
                b_mer_d = d;
                if(IF_BUB(u, *bub) && x->bmerVis.a[u] == 0)
                {
                    get_bubbles((bubble_type*)bub, bub->index[u], &beg, &sink, &a, &n, NULL);
                    for (j = 0; j < n; j++) x->bmerVis.a[(a[j]>>1)] = 1;
                }
                //must be here
                if(bub_only == 0) x->bmerVis.a[u] = 1;
            }            
        }
    }

    return b_mer_d;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, 0x14(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x30(%rsp)
movl %edi, %r13d
movq 0x10(%rdx), %rax
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0xb0(%rcx), %rdx
movq 0xc0(%rcx), %rdi
movq $0x0, 0x8(%rsp)
xorl %esi, %esi
callq 0x20e0
movq 0xf8(%r14), %rdi
movl %r13d, %r13d
movq %r13, %rsi
callq 0x14223d
movq 0xc0(%r14), %rax
movb $0x1, %cl
movb %cl, (%rax,%r13)
movq 0xf0(%r14), %rax
movb %cl, (%rax,%r13)
movq 0xf8(%r14), %rcx
movq 0x8(%rcx), %rdx
testq %rdx, %rdx
je 0x14290a
movq (%rcx), %rsi
movq %rsi, %rax
movabsq $0x3ffffffffffffff, %r8 # imm = 0x3FFFFFFFFFFFFFF
andq %r8, %rax
leaq 0x1(%rsi), %rdi
andq %r8, %rdi
movabsq $-0x400000000000000, %r8 # imm = 0xFC00000000000000
andq %r8, %rsi
shlq $0x3, %rax
addq 0x18(%rcx), %rax
andq 0x10(%rcx), %rdi
orq %rsi, %rdi
movq %rdi, (%rcx)
decq %rdx
movq %rdx, 0x8(%rcx)
jmp 0x14290c
xorl %eax, %eax
movq 0x18(%rsp), %rdx
testq %rax, %rax
je 0x142c09
movq (%rax), %rsi
movq %rsi, %rax
shrq $0x20, %rax
movq %rax, (%rsp)
leal (%rsi,%rsi), %ecx
movq 0x28(%rdx), %rax
movq %rcx, 0x40(%rsp)
movq (%rax,%rcx,8), %rax
testl %eax, %eax
movq %rsi, 0x28(%rsp)
je 0x142a95
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rbx
shlq $0x3, %rbx
addq 0x8(%rdx), %rbx
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rsi
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rsi, 0x20(%rsp)
addq %rsi, %rcx
movq %rcx, 0x38(%rsp)
movl %eax, %r12d
xorl %ebp, %ebp
leaq (,%rbp,2), %rax
addq %rbp, %rax
cmpl $0x0, 0xc(%rbx,%rax,8)
js 0x1429ac
leaq (%rbx,%rax,8), %rax
movl 0x8(%rax), %r13d
shrl %r13d
movq 0xc0(%r14), %rax
cmpb $0x0, (%rax,%r13)
je 0x1429b9
incq %rbp
cmpq %r12, %rbp
jne 0x142981
jmp 0x142a95
movb $0x1, (%rax,%r13)
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r13,4), %eax
incq %rcx
cmpq %rax, %rcx
jae 0x1429f5
movq (%rsp), %rax
cmpq 0x30(%rsp), %rax
jae 0x1429ac
movq 0xf8(%r14), %rdi
orq 0x38(%rsp), %r13
movq %r13, %rsi
callq 0x14223d
jmp 0x1429ac
movq 0xf8(%r14), %rdi
movq 0x20(%rsp), %rsi
orq %r13, %rsi
callq 0x14223d
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r13,4), %eax
incq %rcx
cmpq %rax, %rcx
jbe 0x142a67
movq 0xf0(%r14), %rcx
cmpb $0x0, (%rcx,%r13)
jne 0x142a67
movq 0x40(%r15), %rcx
movl (%rcx,%rax,4), %edx
movl 0x4(%rcx,%rax,4), %ecx
subl %edx, %ecx
addl $-0x2, %ecx
je 0x142a67
movq 0x28(%r15), %rax
leaq (%rax,%rdx,4), %rax
addq $0x8, %rax
movl %ecx, %ecx
xorl %edx, %edx
movq 0xf0(%r14), %rsi
movl (%rax,%rdx,4), %edi
shrl %edi
movb $0x1, (%rsi,%rdi)
incq %rdx
cmpq %rdx, %rcx
jne 0x142a4f
movq (%rsp), %rax
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x14(%rsp)
jne 0x1429ac
movq 0xf0(%r14), %rax
movb $0x1, (%rax,%r13)
movq (%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x1429ac
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rax
movl 0x40(%rsp), %ecx
movq 0x8(%rax,%rcx,8), %rax
testl %eax, %eax
je 0x1428b8
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rbx
shlq $0x3, %rbx
movq 0x18(%rsp), %rcx
addq 0x8(%rcx), %rbx
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movq 0x28(%rsp), %rdx
andq %rcx, %rdx
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rdx, 0x28(%rsp)
addq %rdx, %rcx
movq %rcx, 0x20(%rsp)
movl %eax, %r12d
xorl %ebp, %ebp
leaq (,%rbp,2), %rax
addq %rbp, %rax
cmpl $0x0, 0xc(%rbx,%rax,8)
js 0x142b20
leaq (%rbx,%rax,8), %rax
movl 0x8(%rax), %r13d
shrl %r13d
movq 0xc0(%r14), %rax
cmpb $0x0, (%rax,%r13)
je 0x142b2d
incq %rbp
cmpq %r12, %rbp
jne 0x142af5
jmp 0x1428b8
movb $0x1, (%rax,%r13)
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r13,4), %eax
incq %rcx
cmpq %rax, %rcx
jae 0x142b69
movq (%rsp), %rax
cmpq 0x30(%rsp), %rax
jae 0x142b20
movq 0xf8(%r14), %rdi
orq 0x20(%rsp), %r13
movq %r13, %rsi
callq 0x14223d
jmp 0x142b20
movq 0xf8(%r14), %rdi
movq 0x28(%rsp), %rsi
orq %r13, %rsi
callq 0x14223d
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r13,4), %eax
incq %rcx
cmpq %rax, %rcx
jbe 0x142bdb
movq 0xf0(%r14), %rcx
cmpb $0x0, (%rcx,%r13)
jne 0x142bdb
movq 0x40(%r15), %rcx
movl (%rcx,%rax,4), %edx
movl 0x4(%rcx,%rax,4), %ecx
subl %edx, %ecx
addl $-0x2, %ecx
je 0x142bdb
movq 0x28(%r15), %rax
leaq (%rax,%rdx,4), %rax
addq $0x8, %rax
movl %ecx, %ecx
xorl %edx, %edx
movq 0xf0(%r14), %rsi
movl (%rax,%rdx,4), %edi
shrl %edi
movb $0x1, (%rsi,%rdi)
incq %rdx
cmpq %rdx, %rcx
jne 0x142bc3
movq (%rsp), %rax
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x14(%rsp)
jne 0x142b20
movq 0xf0(%r14), %rax
movb $0x1, (%rax,%r13)
movq (%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x142b20
movl 0x8(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq