// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: rd_hm_bub(asg_t*, asg_t*, unsigned int, unsigned long, buf_t*)
uint64_t rd_hm_bub(asg_t *g, asg_t *ref, uint32_t v0, uint64_t max_dist, buf_t *b)
{
    uint32_t i0, i1, n_pending = 0, is_first = 1, n_tips, tip_end; uint64_t n_pop = 0;
    uint32_t v, w, d, nv0, nv1, l, x, i; asg_arc_t *av0, *av1; binfo_t *t;
    if (g->seq[v0>>1].del) return 0; // already deleted
    nv0 = g?get_real_length(g, v0, NULL):0; 
    nv1 = ref?get_real_length(ref, v0, NULL):0;
    // if((v0 == 4386) && (!ref)) {
    //     fprintf(stderr, "[M::%s] v0>>1::%u(v0&1::%u), nv0::%u, nv1::%u\n", __func__, v0>>1, v0&1, 
    //     nv0, nv1);
    // }
    if((nv0+nv1)<2) return 0;
    // if(v0 >= (ref->n_seq<<1)) {
    //     fprintf(stderr, "[M::%s] v0::%u, ref->n_seq::%u\n", __func__, v0, ref->n_seq);
    // }
    b->S.n = b->T.n = b->b.n = b->e.n = 0;
    b->a[v0].c = b->a[v0].d = b->a[v0].m = b->a[v0].nc = b->a[v0].np = 0;
    ///b->S is the nodes with all incoming edges visited
    kv_push(uint32_t, b->S, v0);
    n_tips = 0; tip_end = (uint32_t)-1;

    do {
        ///v is a node that all incoming edges have been visited
        ///d is the distance from v0 to v
        v = kv_pop(b->S); d = b->a[v].d;
        nv0 = 0; av0 = NULL; nv1 = 0; av1 = NULL; i0 = i1 = 0;
        if(g) {
            nv0 = asg_arc_n(g, v); av0 = asg_arc_a(g, v);
        }
        if(ref) {
            nv1 = asg_arc_n(ref, v); av1 = asg_arc_a(ref, v);
        }
        // if((v0 == 4386) && (!ref)) {
        //     fprintf(stderr, "[M::%s] v0>>1::%u(v0&1::%u), v>>1::%u(v&1::%u)\n", __func__, v0>>1, v0&1, v>>1, v&1);
        // }
        ///all out-edges of v
        for (i0 = 0; i0 < nv0; ++i0) { // loop through v's neighbors
            if (av0[i0].del) continue;
            w = av0[i0].v; l = (uint32_t)av0[i0].ul; t = &b->a[w];
            // if((v0 == 4386) && (!ref)) {
            //     fprintf(stderr, "[M::%s] v0>>1::%u(v0&1::%u), w>>1::%u(w&1::%u)\n", 
            //     __func__, v0>>1, v0&1, w>>1, w&1);
            // }
            if ((w>>1) == (v0>>1)) goto pop_rd_hm_bub;
            if(is_first) l = 0;
            if (d + l > max_dist) break; // too far
            ///unvisited node
            if (t->s == 0) { // this vertex has never been visited
                kv_push(uint32_t, b->b, w); // save it for revert
                t->p = v, t->s = 1, t->d = d + l;
                t->r = (g?get_real_length(g, w^1, NULL):0)+(ref?get_real_length(ref, w^1, NULL):0);
                ++n_pending;
            } else { // visited before
                ///the shortest path
                if (d + l < t->d) t->d = d + l; // update dist
            }
            // if((v0 == 4386) && (!ref) && ((w>>1) == 2597)) {
            //     fprintf(stderr, "[M::%s] w>>1::%u(w&1::%u), t->r::%u\n", 
            //     __func__, w>>1, w&1, t->r);
            // }
            //if all incoming edges of w have visited
            //push it to b->S
            if (--(t->r) == 0) {
                x = (g?get_real_length(g, w, NULL):0)+(ref?get_real_length(ref, w, NULL):0);
                // if((v0 == 4386) && (!ref) && ((w>>1) == 2597)) {
                //     fprintf(stderr, "[M::%s] w>>1::%u(w&1::%u), t->r::%u, x::%u, b->S.n::%u\n", 
                //     __func__, w>>1, w&1, t->r, x, (uint32_t)b->S.n);
                // }
                if(x > 0) {
                    // if((v0 == 4386) && (!ref)) {
                    //     fprintf(stderr, "+[M::%s] v0>>1::%u(v0&1::%u), push_w>>1::%u(w&1::%u)\n", 
                    //     __func__, v0>>1, v0&1, w>>1, w&1);
                    //     uint32_t m;
                    //     for (m = 0; m < b->S.n; m++) {
                    //         fprintf(stderr, "+[M::%s] m>>1::%u(m&1::%u)\n", __func__, b->S.a[m]>>1, b->S.a[m]&1);
                    //     }
                    // }
                    kv_push(uint32_t, b->S, w);
                } else {
                    ///at most one tip
                    if(n_tips != 0) goto pop_rd_hm_bub;
                    n_tips++; tip_end = w;
                }
                --n_pending;
            }
        }
        if (i0 >= nv0) {
            for (i1 = 0; i1 < nv1; ++i1) { // loop through v's neighbors
                if (av1[i1].del) continue;
                w = av1[i1].v; l = (uint32_t)av1[i1].ul; t = &b->a[w];
                if ((w>>1) == (v0>>1)) goto pop_rd_hm_bub;
                if(is_first) l = 0;
                if (d + l > max_dist) break; // too far
                ///unvisited node
                if (t->s == 0) { // this vertex has never been visited
                    kv_push(uint32_t, b->b, w); // save it for revert
                    t->p = v, t->s = 1, t->d = d + l;
                    t->r = (g?get_real_length(g, w^1, NULL):0)+(ref?get_real_length(ref, w^1, NULL):0);
                    ++n_pending;
                } else { // visited before
                    ///the shortest path
                    if (d + l < t->d) t->d = d + l; // update dist
                }

                //if all incoming edges of w have visited
                //push it to b->S
                if (--(t->r) == 0) {
                    x = (g?get_real_length(g, w, NULL):0)+(ref?get_real_length(ref, w, NULL):0);
                    if(x > 0) {
                        // if((v0 == 4386) && (!ref)) {
                        //     fprintf(stderr, "-[M::%s] v0>>1::%u(v0&1::%u), push_w>>1::%u(w&1::%u)\n", 
                        //     __func__, v0>>1, v0&1, w>>1, w&1);
                        // }
                        kv_push(uint32_t, b->S, w);
                    } else {
                        ///at most one tip
                        if(n_tips != 0) goto pop_rd_hm_bub;
                        n_tips++; tip_end = w;
                    }
                    --n_pending;
                }
            }
        }

        is_first = 0;
        //if found a tip
        if(n_tips == 1) {
            if(tip_end != (uint32_t)-1 && n_pending == 0 && b->S.n == 0) {
                // if((v0 == 4386) && (!ref)) {
                //     fprintf(stderr, ">[M::%s] v0>>1::%u(v0&1::%u), push_w>>1::%u(w&1::%u)\n", 
                //     __func__, v0>>1, v0&1, tip_end>>1, tip_end&1);
                // }
                kv_push(uint32_t, b->S, tip_end);
                break;
            } else {
                goto pop_rd_hm_bub;
            }
        }
        // if((v0 == 4386) && (!ref)) {
        //     fprintf(stderr, "[M::%s] v0>>1::%u(v0&1::%u), b->S.n::%u, n_pending::%u\n", 
        //             __func__, v0>>1, v0&1, (uint32_t)b->S.n, n_pending);
        // }
        ///if i < nv, that means (d + l > max_dist)
        if (i0 < nv0 || i1 < nv1 || b->S.n == 0) goto pop_rd_hm_bub;
    } while (b->S.n > 1 || n_pending);
    n_pop = 1;
    pop_rd_hm_bub:
    for (i = 0; i < b->b.n; ++i) { // clear the states of visited vertices
        t = &b->a[b->b.a[i]];
        t->s = t->c = t->d = t->m = t->nc = t->np = 0;
    }
    return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x60(%rsp)
movl %edx, %ebp
movq 0x20(%rdi), %rax
shrl %edx
xorl %ebx, %ebx
movq %rdx, 0x68(%rsp)
cmpl $0x0, (%rax,%rdx,8)
js 0xa6cd2
movq %r8, %r13
movq %rsi, %rcx
movq %rdi, 0x18(%rsp)
movl %ebp, %esi
xorl %edx, %edx
movq %rcx, (%rsp)
callq 0xc2e7a
movq (%rsp), %r8
movl %eax, %r14d
testq %r8, %r8
je 0xa6cca
movq %r8, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xc2e7a
movq (%rsp), %r8
movl %eax, %ebx
addl %r14d, %ebx
cmpl $0x2, %ebx
jae 0xa6cd9
xorl %eax, %eax
jmp 0xa7286
andq $0x0, 0x50(%r13)
andq $0x0, 0x38(%r13)
andq $0x0, 0x20(%r13)
andq $0x0, 0x8(%r13)
movq (%r13), %rax
movl %ebp, %ecx
imulq $0x1c, %rcx, %rcx
xorps %xmm0, %xmm0
andl $0x0, 0x14(%rax,%rcx)
movups %xmm0, 0x4(%rax,%rcx)
xorl %eax, %eax
cmpq %rax, 0x10(%r13)
je 0xa6d14
movq 0x18(%r13), %rax
xorl %ecx, %ecx
jmp 0xa6d34
movq $0x2, 0x10(%r13)
movq 0x18(%r13), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq (%rsp), %r8
movq %rax, 0x18(%r13)
movq 0x8(%r13), %rcx
movl %ebp, (%rax,%rcx,4)
incq %rcx
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
movq $0x0, 0x28(%rsp)
xorl %r15d, %r15d
movq $0x0, 0x48(%rsp)
movq %r13, 0x50(%rsp)
movq (%r13), %rax
movq 0x18(%r13), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%r13)
movl -0x4(%rdx,%rcx,4), %r9d
testq %r8, %r8
je 0xa6d93
movq 0x28(%r8), %rcx
movq (%rcx,%r9,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq 0x8(%r8), %rdx
movq %rdx, 0x10(%rsp)
jmp 0xa6d9e
xorl %ecx, %ecx
movq $0x0, 0x10(%rsp)
imulq $0x1c, %r9, %rdx
movq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq 0x28(%rdi), %rdi
movq %r9, 0x58(%rsp)
movq (%rdi,%r9,8), %rdi
movl %edi, %r9d
movq %r9, 0x70(%rsp)
shrq $0x20, %rdi
movl 0x4(%rax,%rdx), %eax
movl %eax, 0x34(%rsp)
movl %ecx, %eax
movq %rax, 0x40(%rsp)
imulq $0x18, %rdi, %rax
leaq (%rsi,%rax), %rbx
addq $0xc, %rbx
xorl %esi, %esi
movl $0x0, 0x8(%rsp)
cmpq %rsi, 0x70(%rsp)
je 0xa6fde
cmpl $0x0, (%rbx)
js 0xa6fd2
movl -0x4(%rbx), %ebp
movl %ebp, %eax
shrl %eax
cmpl 0x68(%rsp), %eax
je 0xa7252
xorl %r14d, %r14d
testb $0x1, 0x48(%rsp)
je 0xa6e1c
movl -0xc(%rbx), %r14d
addl 0x34(%rsp), %r14d
cmpq 0x60(%rsp), %r14
ja 0xa71d3
imulq $0x1c, %rbp, %r12
addq (%r13), %r12
movl 0x18(%r12), %eax
testl %eax, %eax
movq %rsi, 0x38(%rsp)
js 0xa6f04
movq 0x38(%r13), %rcx
movq 0x48(%r13), %rax
cmpq 0x40(%r13), %rcx
movl %r15d, 0x24(%rsp)
jne 0xa6e7f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x48(%r13)
movq 0x38(%r13), %rcx
movq 0x18(%rsp), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%r13)
movl %ebp, (%rax,%rcx,4)
movq 0x58(%rsp), %rax
movl %eax, (%r12)
orb $-0x80, 0x1b(%r12)
movl %r14d, 0x4(%r12)
movl %ebp, %r15d
xorl $0x1, %r15d
xorl %r14d, %r14d
movl %r15d, %esi
xorl %edx, %edx
callq 0xc2e7a
movl %eax, %r13d
movq (%rsp), %r8
testq %r8, %r8
je 0xa6ed7
movq %r8, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0xc2e7a
movq (%rsp), %r8
movl %eax, %r14d
addl %r13d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl 0x18(%r12), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
andl %ecx, %eax
orl %r14d, %eax
movl 0x24(%rsp), %r15d
incl %r15d
movq 0x50(%rsp), %r13
movq 0x38(%rsp), %rsi
jmp 0xa6f10
cmpl 0x4(%r12), %r14d
jae 0xa6f10
movl %r14d, 0x4(%r12)
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x18(%r12)
testl %ecx, %ecx
jne 0xa6fd2
movl %r15d, %r12d
xorl %r15d, %r15d
movq 0x18(%rsp), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xc2e7a
movq (%rsp), %r8
movl %eax, %r14d
testq %r8, %r8
je 0xa6f60
movq %r8, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xc2e7a
movq (%rsp), %r8
movl %eax, %r15d
addl %r15d, %r14d
jne 0xa6f84
movl %ebp, 0xc(%rsp)
cmpl $0x0, 0x8(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x8(%rsp)
movl %r12d, %r15d
movq 0x38(%rsp), %rsi
je 0xa6fcf
jmp 0xa7252
movq 0x8(%r13), %rcx
movq 0x18(%r13), %rax
cmpq 0x10(%r13), %rcx
movl %r12d, %r15d
jne 0xa6fbf
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r8
movq %rax, 0x18(%r13)
movq 0x8(%r13), %rcx
movq 0x38(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
movl %ebp, (%rax,%rcx,4)
decl %r15d
incq %rsi
addq $0x18, %rbx
jmp 0xa6de9
movq 0x10(%rsp), %rsi
addq $0xc, %rsi
xorl %r12d, %r12d
cmpq %r12, 0x40(%rsp)
je 0xa71e0
cmpl $0x0, (%rsi)
js 0xa71c7
movl -0x4(%rsi), %ebp
movl %ebp, %eax
shrl %eax
cmpl 0x68(%rsp), %eax
je 0xa7252
xorl %r14d, %r14d
testb $0x1, 0x48(%rsp)
je 0xa701d
movl -0xc(%rsi), %r14d
addl 0x34(%rsp), %r14d
cmpq 0x60(%rsp), %r14
ja 0xa71e9
movq %rsi, 0x10(%rsp)
imulq $0x1c, %rbp, %rbx
addq (%r13), %rbx
movl 0x18(%rbx), %eax
testl %eax, %eax
js 0xa70f7
movq 0x38(%r13), %rcx
movq 0x48(%r13), %rax
cmpq 0x40(%r13), %rcx
movl %r15d, 0x24(%rsp)
jne 0xa707e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x48(%r13)
movq 0x38(%r13), %rcx
movq 0x18(%rsp), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%r13)
movl %ebp, (%rax,%rcx,4)
movq 0x58(%rsp), %rax
movl %eax, (%rbx)
orb $-0x80, 0x1b(%rbx)
movl %r14d, 0x4(%rbx)
movl %ebp, %r15d
xorl $0x1, %r15d
xorl %r14d, %r14d
movl %r15d, %esi
xorl %edx, %edx
callq 0xc2e7a
movl %eax, %r13d
movq (%rsp), %r8
testq %r8, %r8
je 0xa70d1
movq %r8, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0xc2e7a
movq (%rsp), %r8
movl %eax, %r14d
addl %r13d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl 0x18(%rbx), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
andl %ecx, %eax
orl %r14d, %eax
movl 0x24(%rsp), %r15d
incl %r15d
movq 0x50(%rsp), %r13
jmp 0xa7101
cmpl 0x4(%rbx), %r14d
jae 0xa7101
movl %r14d, 0x4(%rbx)
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x18(%rbx)
testl %ecx, %ecx
je 0xa7122
movq 0x10(%rsp), %rsi
jmp 0xa71c7
movl %r15d, %ebx
xorl %r15d, %r15d
movq 0x18(%rsp), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xc2e7a
movq (%rsp), %r8
movl %eax, %r14d
testq %r8, %r8
je 0xa7155
movq %r8, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xc2e7a
movq (%rsp), %r8
movl %eax, %r15d
addl %r15d, %r14d
jne 0xa7179
movl %ebp, 0xc(%rsp)
cmpl $0x0, 0x8(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x8(%rsp)
movl %ebx, %r15d
movq 0x10(%rsp), %rsi
je 0xa71c4
jmp 0xa7252
movq 0x8(%r13), %rcx
movq 0x18(%r13), %rax
cmpq 0x10(%r13), %rcx
movl %ebx, %r15d
jne 0xa71b4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r8
movq %rax, 0x18(%r13)
movq 0x8(%r13), %rcx
movq 0x10(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
movl %ebp, (%rax,%rcx,4)
decl %r15d
incq %r12
addq $0x18, %rsi
jmp 0xa6fea
cmpq 0x70(%rsp), %rsi
setb %al
xorl %r12d, %r12d
jmp 0xa71eb
xorl %eax, %eax
movq 0x40(%rsp), %r12
jmp 0xa71eb
xorl %eax, %eax
cmpl $0x1, 0x8(%rsp)
je 0xa722b
cmpl 0x40(%rsp), %r12d
setb %cl
orb %cl, %al
jne 0xa7252
movq 0x8(%r13), %rcx
testq %rcx, %rcx
je 0xa7252
movb $0x1, %al
movq %rax, 0x48(%rsp)
cmpq $0x1, %rcx
jne 0xa6d5b
testl %r15d, %r15d
jne 0xa6d5b
pushq $0x1
popq %rax
movq %rax, 0x28(%rsp)
jmp 0xa7252
movq $0x0, 0x28(%rsp)
movl 0xc(%rsp), %esi
cmpl $-0x1, %esi
je 0xa7252
testl %r15d, %r15d
jne 0xa7252
cmpq $0x0, 0x8(%r13)
je 0xa7295
movq $0x0, 0x28(%rsp)
xorl %eax, %eax
xorps %xmm0, %xmm0
movl %eax, %eax
cmpq %rax, 0x38(%r13)
jbe 0xa7281
movq (%r13), %rcx
movq 0x48(%r13), %rdx
movl (%rdx,%rax,4), %edx
imulq $0x1c, %rdx, %rdx
movups %xmm0, 0x4(%rcx,%rdx)
andl $0x0, 0x14(%rcx,%rdx)
andb $0x7f, 0x1b(%rcx,%rdx)
incl %eax
jmp 0xa7257
movq 0x28(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x10(%r13)
movq 0x18(%r13), %rax
je 0xa72a4
xorl %ecx, %ecx
jmp 0xa72c3
movq $0x2, 0x10(%r13)
pushq $0x8
popq %rsi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %esi
movq %rax, 0x18(%r13)
movq 0x8(%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
movl %esi, (%rax,%rcx,4)
jmp 0xa7221