// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rcx, 0x60(%rsp)
movq %rdi, %r11
movq (%rdi), %rcx
movl %esi, %edi
shrl %edi
imulq $0x78, %rdi, %rax
cmpb $0x0, 0x70(%rcx,%rax)
je 0x21a246
xorl %r14d, %r14d
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r10
movl %esi, %ebp
movl %esi, %edx
andl $0x1, %edx
addq %rax, %rcx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %eax
xorl %r14d, %r14d
testq %rax, %rax
je 0x21a234
movq 0x20(%rcx,%rdx,8), %rcx
shlq $0x5, %rax
xorl %edx, %edx
movl 0xc(%rcx,%rdx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %r14d
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x21a26e
cmpl $0x2, %r14d
jb 0x21a231
xorl %r14d, %r14d
movq %r14, 0x50(%r8)
movq %r14, 0x38(%r8)
movq %r14, 0x20(%r8)
movq %r14, 0x8(%r8)
movq (%r8), %rax
movl %ebp, %ecx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax,%rdx)
movl $0x0, 0x14(%rax,%rdx)
cmpq %r14, 0x10(%r8)
movq %r8, 0x30(%rsp)
movq %r10, 0x48(%rsp)
movq %rdi, 0x20(%rsp)
jne 0x21a300
movq $0x2, 0x10(%r8)
movq 0x18(%r8), %rdi
movl $0x8, %esi
movq %r11, %r15
callq 0x2370
movq 0x20(%rsp), %rdi
movq %r15, %r11
movq 0x48(%rsp), %r10
movq 0x30(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rax
movq 0x18(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r8)
movl %ebp, (%rcx,%rax,4)
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl $0x0, (%rsp)
movq %rcx, 0x40(%rsp)
movq 0x18(%r8), %rcx
movq (%r8), %rax
movq 0x8(%r8), %rdx
leaq -0x1(%rdx), %rsi
movq %rsi, 0x8(%r8)
movl -0x4(%rcx,%rdx,4), %ebx
movl %ebx, %ecx
shrl %ecx
movl %ebx, %edx
andl $0x1, %edx
imulq $0x78, %rcx, %rcx
addq (%r11), %rcx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %r15d
xorl %esi, %esi
movq %r15, 0x38(%rsp)
testq %r15, %r15
setne %bpl
je 0x21a652
movq $0x0, 0x28(%rsp)
leaq (%rbx,%rbx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
movq %rbx, 0x58(%rsp)
addq %rbx, %rsi
movl 0x4(%rax,%rsi), %esi
movq 0x20(%rcx,%rdx,8), %r12
xorl %r15d, %r15d
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rcx
movl %esi, 0x4(%rsp)
movq %r12, 0x8(%rsp)
movq %r15, %rax
shlq $0x5, %rax
cmpl $0x0, 0xc(%r12,%rax)
js 0x21a632
addq %r12, %rax
movl 0x8(%rax), %ebx
movl %ebx, %r13d
shrl %r13d
cmpl %edi, %r13d
jne 0x21a3d3
movl $0x9, %ebx
jmp 0x21a62e
movq %rbx, 0x18(%rsp)
xorl %r12d, %r12d
testb $0x1, %dl
je 0x21a3e3
movl (%rax), %r12d
addl %esi, %r12d
movl $0x6, %ebx
cmpq %r10, %r12
ja 0x21a3fb
movq 0x38(%r8), %rax
cmpq 0x60(%rsp), %rax
jbe 0x21a405
movq 0x8(%rsp), %r12
jmp 0x21a62e
movq 0x18(%rsp), %r9
leaq (%r9,%r9,8), %rcx
leaq (%rcx,%rcx,2), %rbx
addq %r9, %rbx
addq (%r8), %rbx
cmpl $0x0, 0x18(%rbx)
js 0x21a4e8
movq 0x40(%r8), %rcx
cmpq %rcx, %rax
jne 0x21a46d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x40(%r8)
movq 0x48(%r8), %rdi
shlq $0x2, %rsi
movq %r11, 0x50(%rsp)
callq 0x2370
movl 0x4(%rsp), %esi
movq 0x20(%rsp), %rdi
movq 0x50(%rsp), %r11
movq 0x48(%rsp), %r10
movq 0x30(%rsp), %r8
movq %rax, 0x48(%r8)
movq 0x38(%r8), %rax
movq 0x48(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x38(%r8)
movq 0x18(%rsp), %rdx
movl %edx, (%rcx,%rax,4)
movq 0x58(%rsp), %rax
movl %eax, (%rbx)
orb $-0x80, 0x1b(%rbx)
movl %r12d, 0x4(%rbx)
movl %edx, %eax
shrl %eax
notl %edx
imulq $0x78, %rax, %rcx
addq (%r11), %rcx
andl $0x1, %edx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %eax
testq %rax, %rax
je 0x21a4f9
movq 0x20(%rcx,%rdx,8), %rdx
shlq $0x5, %rax
xorl %esi, %esi
xorl %ecx, %ecx
movq 0x8(%rsp), %r12
movl 0xc(%rdx,%rsi), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %ecx
addq $0x20, %rsi
cmpq %rsi, %rax
jne 0x21a4c3
orl $0x80000000, %ecx       # imm = 0x80000000
movq 0x20(%rsp), %rdi
movl 0x4(%rsp), %esi
jmp 0x21a503
cmpl 0x4(%rbx), %r12d
jae 0x21a4f2
movl %r12d, 0x4(%rbx)
movq 0x8(%rsp), %r12
jmp 0x21a50e
movl $0x80000000, %ecx       # imm = 0x80000000
movq 0x8(%rsp), %r12
movl %ecx, 0x18(%rbx)
incl (%rsp)
movq 0x40(%rsp), %rdx
movl 0x18(%rbx), %eax
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x18(%rbx)
xorl %ebx, %ebx
testl %ecx, %ecx
jne 0x21a629
movq 0x18(%rsp), %rax
imulq $0x78, %r13, %rcx
addq (%r11), %rcx
andl $0x1, %eax
leaq (%rax,%rax,2), %rdx
movl 0x10(%rcx,%rdx,8), %eax
testq %rax, %rax
je 0x21a5f6
movq 0x20(%rcx,%rdx,8), %rcx
shlq $0x5, %rax
xorl %edx, %edx
xorl %esi, %esi
movl 0xc(%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x21a55b
testl %esi, %esi
movq 0x20(%rsp), %rdi
movl 0x4(%rsp), %esi
je 0x21a5f6
movq 0x10(%r8), %rax
cmpq %rax, 0x8(%r8)
jne 0x21a5c9
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r8)
movq 0x18(%r8), %rdi
shlq $0x2, %rsi
movq %r11, %r13
callq 0x2370
movl 0x4(%rsp), %esi
movq 0x20(%rsp), %rdi
movq %r13, %r11
movq 0x48(%rsp), %r10
movq 0x8(%rsp), %r12
movq 0x30(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rax
movq 0x18(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r8)
movq 0x18(%rsp), %rdx
movl %edx, (%rcx,%rax,4)
movl 0x14(%rsp), %eax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rdx
jmp 0x21a618
movl $0x1, %eax
cmpl $0x0, 0x28(%rsp)
movq 0x40(%rsp), %rdx
je 0x21a618
movl $0x1, %eax
movq %rax, 0x28(%rsp)
movl $0x9, %ebx
jmp 0x21a629
decl (%rsp)
movq 0x18(%rsp), %rcx
movl %ecx, 0x14(%rsp)
movq %rax, 0x28(%rsp)
movq 0x38(%rsp), %rcx
testl %ebx, %ebx
jne 0x21a644
incq %r15
cmpq %rcx, %r15
setb %bpl
jne 0x21a3a5
jmp 0x21a649
cmpl $0x6, %ebx
jne 0x21a69d
cmpl $0x1, 0x28(%rsp)
sete %sil
testb %sil, %sil
jne 0x21a684
testb $0x1, %bpl
jne 0x21a6a0
movq 0x8(%r8), %rax
testq %rax, %rax
je 0x21a6a0
movb $0x1, %cl
cmpq $0x1, %rax
jne 0x21a324
cmpl $0x0, (%rsp)
jne 0x21a324
movl $0x1, %r14d
jmp 0x21a6a0
xorl %r14d, %r14d
movl 0x14(%rsp), %ebx
cmpl $-0x1, %ebx
je 0x21a6a0
cmpl $0x0, (%rsp)
jne 0x21a6a0
cmpq $0x0, 0x8(%r8)
je 0x21a6eb
xorl %r14d, %r14d
cmpq $0x0, 0x38(%r8)
je 0x21a234
movl $0x1, %eax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
movq (%r8), %rdx
movq 0x48(%r8), %rsi
movl (%rsi,%rcx,4), %ecx
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rcx, %rsi
movups %xmm0, 0x4(%rdx,%rsi)
movl $0x0, 0x14(%rdx,%rsi)
andb $0x7f, 0x1b(%rdx,%rsi)
movl %eax, %ecx
incl %eax
cmpq %rcx, 0x38(%r8)
ja 0x21a6b5
jmp 0x21a234
cmpq $0x0, 0x10(%r8)
jne 0x21a711
movq $0x2, 0x10(%r8)
movq 0x18(%r8), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x30(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rax
movq 0x18(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%r8)
movl %ebx, (%rcx,%rax,4)
jmp 0x21a67c