// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_bub_identify(usg_t*, unsigned int, unsigned long, unsigned long, buf_t*)
uint64_t usg_bub_identify(usg_t *g, uint32_t v0, uint64_t max_dist, uint64_t max_occ, buf_t *b)
{
    uint32_t i, n_pending = 0, is_first = 1, n_tips, tip_end;
	uint64_t n_pop = 0; uint32_t v, nv, d, x; usg_arc_t *av;
	if (g->a[v0>>1].del) return 0; // already deleted
    if(usg_arc_occ(g, v0, NULL) < 2) return 0;
	///S saves nodes with all incoming edges visited
	b->S.n = b->T.n = b->b.n = b->e.n = 0;
	///for each node, b->a saves all related information
	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;
		nv = usg_arc_n(g, v); av = usg_arc_a(g, v);
		///why we have this assert?
		///assert(nv > 0);
		///all out-edges of v
		for (i = 0; i < nv; ++i) { // loop through v's neighbors
			/**
			p->ul: |____________31__________|__________1___________|______________32_____________|
								qn            direction of overlap       length of this node (not overlap length)
												(in the view of query)
			p->v : |___________31___________|__________1___________|
								tn             reverse direction of overlap
											(in the view of target)
			p->ol: overlap length
			**/
            ///if this edge has been deleted
			if (av[i].del) continue;
            
			uint32_t w = av[i].v, l = (uint32_t)av[i].ul; // v->w with length l
			binfo_t *t = &b->a[w];
			///that means there is a circle, directly terminate the whole bubble poping
			///if (w == v0) goto pop_reset;
            if ((w>>1) == (v0>>1)) goto usg_clean_reset;
            /****************************may have bugs********************************/
            ///important when poping at long untig graph
            if(is_first) l = 0;
            /****************************may have bugs********************************/

			///push the edge
            ///high 32-bit of g->idx[v] is the start point of v's edges
            //so here is the point of this specfic edge
			// kv_push(uint32_t, b->e, (g->idx[v]>>32) + i);
			///find a too far path? directly terminate the whole bubble poping
			if (d + l > max_dist) break; // too far
            if (b->b.n > max_occ) break; // too far

            ///if this node
			if (t->s == 0) { // this vertex has never been visited
				kv_push(uint32_t, b->b, w); // save it for revert
				///t->p is the parent node of 
				///t->s = 1 means w has been visited
				///d is len(v0->v), l is len(v->w), so t->d is len(v0->w)
				t->p = v, t->s = 1, t->d = d + l;
				///incoming edges of w
                t->r = usg_arc_occ(g, w^1, NULL);
				++n_pending;
			} else { // visited before
				if (d + l < t->d) t->d = d + l; // update dist
			}
			///assert(t->r > 0);
			//if all incoming edges of w have visited
			//push it to b->S
			if (--(t->r) == 0) {
                x = usg_arc_occ(g, w, NULL);
                /****************************may have bugs for bubble********************************/
                if(x > 0) {
                    kv_push(uint32_t, b->S, w);
                }
                else {
                    ///at most one tip
                    if(n_tips != 0) goto usg_clean_reset;
                    n_tips++; tip_end = w;
                }
                /****************************may have bugs for bubble********************************/
				--n_pending;
			}
		}
        is_first = 0;
        //if found a tip
        /****************************may have bugs for bubble********************************/
        if(n_tips == 1) {
            if(tip_end != (uint32_t)-1 && n_pending == 0 && b->S.n == 0) {
                kv_push(uint32_t, b->S, tip_end);
                break;
            } else {
                goto usg_clean_reset;
            }
        }
        /****************************may have bugs for bubble********************************/
		///if i < nv, that means (d + l > max_dist)
		if (i < nv || b->S.n == 0) goto usg_clean_reset;
	} while (b->S.n > 1 || n_pending);

    n_pop = 1;
    usg_clean_reset:

    for (i = 0; i < b->b.n; ++i) { // clear the states of visited vertices
		binfo_t *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 $0x58, %rsp
movq %rcx, %r12
movq %rdx, %r13
movq (%rdi), %rax
movl %esi, %edx
shrl %edx
imulq $0x78, %rdx, %rcx
cmpb $0x0, 0x70(%rax,%rcx)
je 0x1d6555
xorl %r14d, %r14d
jmp 0x1d6571
movq %r8, %rbx
movl %esi, %ebp
movq %rdi, %r15
movq %rdx, 0x8(%rsp)
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x1df1ab
cmpl $0x2, %eax
jae 0x1d6583
movq %r14, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andq $0x0, 0x50(%rbx)
andq $0x0, 0x38(%rbx)
andq $0x0, 0x20(%rbx)
andq $0x0, 0x8(%rbx)
movq (%rbx), %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(%rbx)
je 0x1d65bd
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0x1d65d9
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %ebp, (%rax,%rcx,4)
incq %rcx
pushq $-0x1
popq %rax
movl %eax, 0x14(%rsp)
movq $0x0, 0x40(%rsp)
xorl %r8d, %r8d
movq 0x8(%rsp), %rdi
movq %r13, 0x20(%rsp)
movq %r15, 0x28(%rsp)
movq %r12, 0x38(%rsp)
movq (%rbx), %rax
movq 0x18(%rbx), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rdx,%rcx,4), %edx
imulq $0x1c, %rdx, %rcx
movl 0x4(%rax,%rcx), %eax
movl %eax, 0x34(%rsp)
movl %edx, %eax
shrl %eax
movq %rdx, 0x48(%rsp)
movl %edx, %ecx
andl $0x1, %ecx
imulq $0x78, %rax, %rax
addq (%r15), %rax
imulq $0x18, %rcx, %rcx
movl 0x10(%rcx,%rax), %edx
movq 0x20(%rcx,%rax), %rsi
shlq $0x5, %rdx
movq %rdx, 0x50(%rsp)
xorl %r15d, %r15d
movl $0x0, 0x4(%rsp)
movq %rsi, 0x18(%rsp)
cmpq %r15, 0x50(%rsp)
je 0x1d681e
cmpl $0x0, 0xc(%rsi,%r15)
js 0x1d6815
movl 0x8(%rsi,%r15), %ebp
movl %ebp, %eax
shrl %eax
cmpl %edi, %eax
je 0x1d687a
xorl %r14d, %r14d
testb $0x1, 0x40(%rsp)
je 0x1d6696
movl (%rsi,%r15), %r14d
addl 0x34(%rsp), %r14d
cmpq %r13, %r14
ja 0x1d6855
movq 0x38(%rbx), %rdx
cmpq %r12, %rdx
ja 0x1d6855
imulq $0x1c, %rbp, %r12
addq (%rbx), %r12
movl 0x18(%r12), %ecx
testl %ecx, %ecx
js 0x1d674d
movl %r8d, %r13d
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rdx
jne 0x1d66f8
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x38(%rbx)
movl %ebp, (%rax,%rdx,4)
movq 0x48(%rsp), %rax
movl %eax, (%r12)
orb $-0x80, 0x1b(%r12)
movl %r14d, 0x4(%r12)
movl %ebp, %esi
xorl $0x1, %esi
movq 0x28(%rsp), %rdi
xorl %edx, %edx
callq 0x1df1ab
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x18(%r12), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %eax, %ecx
movl %r13d, %r8d
incl %r8d
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
jmp 0x1d6759
cmpl 0x4(%r12), %r14d
jae 0x1d6759
movl %r14d, 0x4(%r12)
leal -0x1(%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %eax, %ecx
movl %ecx, 0x18(%r12)
testl %eax, %eax
je 0x1d6781
movq 0x38(%rsp), %r12
movq 0x20(%rsp), %r13
jmp 0x1d6815
movl %r8d, %r14d
movq 0x28(%rsp), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x1df1ab
testl %eax, %eax
movq 0x38(%rsp), %r12
je 0x1d67ee
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
movq 0x20(%rsp), %r13
jne 0x1d67d4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movq 0x8(%rsp), %rdi
movl %r14d, %r8d
movq 0x18(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %ebp, (%rax,%rcx,4)
jmp 0x1d6812
movl %ebp, 0x14(%rsp)
cmpl $0x0, 0x4(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
movq 0x20(%rsp), %r13
movq 0x8(%rsp), %rdi
movl %r14d, %r8d
movq 0x18(%rsp), %rsi
jne 0x1d687a
decl %r8d
addq $0x20, %r15
jmp 0x1d6660
cmpl $0x1, 0x4(%rsp)
je 0x1d685c
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x1d687a
cmpq $0x1, %rcx
setne %al
testl %r8d, %r8d
setne %dl
orb %al, %dl
movb $0x1, %al
movq %rax, 0x40(%rsp)
movq 0x28(%rsp), %r15
jne 0x1d6606
pushq $0x1
popq %r14
jmp 0x1d687d
cmpl $0x1, 0x4(%rsp)
jne 0x1d687a
movl 0x14(%rsp), %ebp
cmpl $-0x1, %ebp
setne %al
testl %r8d, %r8d
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1d687a
cmpq $0x0, 0x8(%rbx)
je 0x1d68af
xorl %r14d, %r14d
xorl %eax, %eax
xorps %xmm0, %xmm0
movl %eax, %eax
cmpq %rax, 0x38(%rbx)
jbe 0x1d6571
movq (%rbx), %rcx
movq 0x48(%rbx), %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 0x1d6882
cmpq $0x0, 0x10(%rbx)
movq 0x18(%rbx), %rax
je 0x1d68be
xorl %ecx, %ecx
jmp 0x1d68d9
movq $0x2, 0x10(%rbx)
pushq $0x8
popq %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
pushq $0x1
popq %r14
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %ebp, (%rax,%rcx,4)
jmp 0x1d687d