// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movl %r8d, 0x24(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x50(%rsp)
movl %edi, %ebx
movq 0x10(%rdx), %rax
movq 0x18(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0xb0(%rcx), %rdx
movq 0xc0(%rcx), %rdi
xorl %r13d, %r13d
xorl %esi, %esi
callq 0x20e0
movq 0xf8(%r14), %rdi
movl %ebx, %ebx
movq %rbx, %rsi
callq 0x1005f4
movq 0xc0(%r14), %rax
movb $0x1, %cl
movb %cl, (%rax,%rbx)
movq 0xf0(%r14), %rax
movb %cl, (%rax,%rbx)
movq %r15, 0x40(%rsp)
movq 0xf8(%r14), %rdi
callq 0x100741
testq %rax, %rax
je 0x101423
movq (%rax), %rdx
movq %rdx, %rsi
leal (%rdx,%rdx), %edi
movq 0x38(%rsp), %rcx
movq 0x28(%rcx), %rax
movq %rdi, 0x60(%rsp)
movq (%rax,%rdi,8), %rax
movl %eax, %r12d
shrq $0x20, %rax
imulq $0x18, %rax, %rbx
addq 0x8(%rcx), %rbx
shrq $0x20, %rsi
movq %rsi, 0x18(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
andq %rax, %rdx
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rdx, 0x58(%rsp)
addq %rdx, %rax
movq %rax, 0x48(%rsp)
xorl %ebp, %ebp
movq %r13, %rsi
cmpq %r12, %rbp
je 0x1012d7
imulq $0x18, %rbp, %rax
cmpl $0x0, 0xc(%rbx,%rax)
js 0x1011e2
addq %rbx, %rax
movl 0x8(%rax), %r13d
shrl %r13d
movq 0xc0(%r14), %rax
cmpb $0x0, (%rax,%r13)
je 0x1011e7
incq %rbp
jmp 0x1011b6
movb $0x1, (%rax,%r13)
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r13,4), %eax
incq %rcx
cmpq %rax, %rcx
jae 0x10122f
movq 0x18(%rsp), %rax
cmpq 0x50(%rsp), %rax
jae 0x1011e2
movq 0xf8(%r14), %rdi
orq 0x48(%rsp), %r13
movq %rsi, %r15
movq %r13, %rsi
callq 0x1005f4
movq %r15, %rsi
movq 0x40(%rsp), %r15
jmp 0x1011e2
movq 0xf8(%r14), %rdi
movq 0x58(%rsp), %rsi
orq %r13, %rsi
callq 0x1005f4
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r13,4), %esi
incq %rcx
cmpq %rsi, %rcx
jbe 0x101267
movq 0xf0(%r14), %rax
cmpb $0x0, (%rax,%r13)
je 0x101291
movq 0x18(%rsp), %rax
movl %eax, %esi
cmpl $0x0, 0x24(%rsp)
jne 0x1011e2
movq 0xf0(%r14), %rax
movb $0x1, (%rax,%r13)
movq 0x18(%rsp), %rax
movl %eax, %esi
jmp 0x1011e2
andq $0x0, (%rsp)
movq %r15, %rdi
leaq 0x34(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x14(%rsp), %r9
callq 0xf6ab5
xorl %eax, %eax
movl 0x14(%rsp), %ecx
cmpq %rcx, %rax
jae 0x101267
movq 0xf0(%r14), %rcx
movq 0x28(%rsp), %rdx
movl (%rdx,%rax,4), %edx
shrl %edx
movb $0x1, (%rcx,%rdx)
incq %rax
jmp 0x1012b4
movq 0x38(%rsp), %rdx
movq 0x28(%rdx), %rax
movl 0x60(%rsp), %ecx
movq 0x8(%rax,%rcx,8), %rax
movl %eax, %ebx
shrq $0x20, %rax
imulq $0x18, %rax, %r12
addq 0x8(%rdx), %r12
xorl %ebp, %ebp
cmpq %rbx, %rbp
je 0x10141b
imulq $0x18, %rbp, %rax
cmpl $0x0, 0xc(%r12,%rax)
js 0x101326
addq %r12, %rax
movl 0x8(%rax), %r13d
shrl %r13d
movq 0xc0(%r14), %rax
cmpb $0x0, (%rax,%r13)
je 0x10132b
incq %rbp
jmp 0x1012f9
movb $0x1, (%rax,%r13)
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r13,4), %eax
incq %rcx
cmpq %rax, %rcx
jae 0x101373
movq 0x18(%rsp), %rax
cmpq 0x50(%rsp), %rax
jae 0x101326
movq 0xf8(%r14), %rdi
orq 0x48(%rsp), %r13
movq %rsi, %r15
movq %r13, %rsi
callq 0x1005f4
movq %r15, %rsi
movq 0x40(%rsp), %r15
jmp 0x101326
movq 0xf8(%r14), %rdi
movq 0x58(%rsp), %rsi
orq %r13, %rsi
callq 0x1005f4
movq (%r15), %rax
movq 0x80(%r15), %rcx
movl (%rax,%r13,4), %esi
incq %rcx
cmpq %rsi, %rcx
jbe 0x1013ab
movq 0xf0(%r14), %rax
cmpb $0x0, (%rax,%r13)
je 0x1013d5
movq 0x18(%rsp), %rax
movl %eax, %esi
cmpl $0x0, 0x24(%rsp)
jne 0x101326
movq 0xf0(%r14), %rax
movb $0x1, (%rax,%r13)
movq 0x18(%rsp), %rax
movl %eax, %esi
jmp 0x101326
andq $0x0, (%rsp)
movq %r15, %rdi
leaq 0x34(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x14(%rsp), %r9
callq 0xf6ab5
xorl %eax, %eax
movl 0x14(%rsp), %ecx
cmpq %rcx, %rax
jae 0x1013ab
movq 0xf0(%r14), %rcx
movq 0x28(%rsp), %rdx
movl (%rdx,%rax,4), %edx
shrl %edx
movb $0x1, (%rcx,%rdx)
incq %rax
jmp 0x1013f8
movq %rsi, %r13
jmp 0x101145
movl %r13d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq