// File: /workspace/github_cpp_makefile_star2_O3/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, %r9
movq %rdi, %r11
movq (%rdi), %rcx
movl %esi, %edi
shrl %edi
imulq $0x78, %rdi, %rax
cmpb $0x0, 0x70(%rcx,%rax)
je 0x225787
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 0x225775
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 0x2257af
cmpl $0x2, %r14d
jb 0x225772
xorl %eax, %eax
movq %rax, 0x50(%r8)
movq %rax, 0x38(%r8)
movq %rax, 0x20(%r8)
movq %rax, 0x8(%r8)
movq (%r8), %rcx
movl %ebp, %edx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rcx,%rsi)
movl $0x0, 0x14(%rcx,%rsi)
cmpq %rax, 0x10(%r8)
movq %r8, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
je 0x225814
movq 0x18(%r8), %rax
xorl %ecx, %ecx
jmp 0x22584e
movq $0x2, 0x10(%r8)
movq 0x18(%r8), %rdi
movl $0x8, %esi
movq %r9, %rbx
movq %r10, %r14
movq %r11, %r15
callq 0x2370
movq 0x18(%rsp), %rdi
movq %r15, %r11
movq %r14, %r10
movq %rbx, %r9
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
movl %ebp, (%rax,%rcx,4)
incq %rcx
xorl %r14d, %r14d
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq $0x0, 0x38(%rsp)
xorl %ebx, %ebx
movq %r9, 0x50(%rsp)
movq %r10, 0x48(%rsp)
movq %r11, 0x40(%rsp)
movq (%r8), %rax
movq 0x18(%r8), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%r8)
movl -0x4(%rdx,%rcx,4), %r15d
movl %r15d, %ecx
shrl %ecx
movl %r15d, %edx
imulq $0x78, %rcx, %rcx
addq (%r11), %rcx
andl $0x1, %edx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %esi
movq %rsi, 0x60(%rsp)
testq %rsi, %rsi
je 0x225b21
movl %ebx, 0x14(%rsp)
leaq (%r15,%r15,8), %rsi
leaq (%rsi,%rsi,2), %rsi
movq %r15, 0x58(%rsp)
addq %r15, %rsi
movl 0x4(%rax,%rsi), %eax
movl %eax, 0x34(%rsp)
movq 0x20(%rcx,%rdx,8), %rdx
xorl %r12d, %r12d
movl $0x0, 0x10(%rsp)
movq %rdx, 0x28(%rsp)
movq %r12, %rax
shlq $0x5, %rax
cmpl $0x0, 0xc(%rdx,%rax)
js 0x225b08
addq %rdx, %rax
movl 0x8(%rax), %ebp
movl %ebp, %r13d
shrl %r13d
cmpl %edi, %r13d
je 0x225b6f
xorl %ebx, %ebx
testb $0x1, 0x38(%rsp)
je 0x225918
movl (%rax), %ebx
addl 0x34(%rsp), %ebx
cmpq %r10, %rbx
ja 0x225b4a
movq 0x38(%r8), %rcx
cmpq %r9, %rcx
ja 0x225b4a
leaq (,%rbp,8), %rax
addq %rbp, %rax
leaq (%rax,%rax,2), %r15
addq %rbp, %r15
addq (%r8), %r15
movl 0x18(%r15), %eax
testl %eax, %eax
js 0x225a02
movq 0x48(%r8), %rax
cmpq 0x40(%r8), %rcx
jne 0x22599e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %rdi
movq 0x40(%rsp), %r11
movq 0x48(%rsp), %r10
movq 0x50(%rsp), %r9
movq 0x20(%rsp), %r8
movq %rax, 0x48(%r8)
movq 0x38(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%r8)
movl %ebp, (%rax,%rcx,4)
movq 0x58(%rsp), %rax
movl %eax, (%r15)
orb $-0x80, 0x1b(%r15)
movl %ebx, 0x4(%r15)
movl %ebp, %ecx
notl %ecx
imulq $0x78, %r13, %rax
addq (%r11), %rax
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rdx
movl 0x10(%rax,%rdx,8), %ecx
testq %rcx, %rcx
je 0x225a0e
movq 0x20(%rax,%rdx,8), %rdx
shlq $0x5, %rcx
xorl %esi, %esi
xorl %eax, %eax
movl 0xc(%rdx,%rsi), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %eax
addq $0x20, %rsi
cmpq %rsi, %rcx
jne 0x2259e2
orl $0x80000000, %eax       # imm = 0x80000000
movq 0x18(%rsp), %rdi
jmp 0x225a13
cmpl 0x4(%r15), %ebx
jae 0x225a1c
movl %ebx, 0x4(%r15)
jmp 0x225a1c
movl $0x80000000, %eax       # imm = 0x80000000
incl 0x14(%rsp)
movq 0x28(%rsp), %rdx
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x18(%r15)
testl %ecx, %ecx
jne 0x225b08
movl %r13d, %eax
movl %ebp, %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 0x225aec
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 0x225a65
testl %esi, %esi
movq 0x18(%rsp), %rdi
je 0x225aec
movq 0x8(%r8), %rcx
movq 0x18(%r8), %rax
cmpq 0x10(%r8), %rcx
jne 0x225ad4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r9, %rbx
movq %r10, %r15
movq %r11, %r13
callq 0x2370
movq 0x18(%rsp), %rdi
movq %r13, %r11
movq %r15, %r10
movq %rbx, %r9
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
movl %ebp, (%rax,%rcx,4)
movl 0xc(%rsp), %eax
movl %eax, %ebp
movq 0x28(%rsp), %rdx
jmp 0x225b00
cmpl $0x0, 0x10(%rsp)
movl $0x1, 0x10(%rsp)
movq 0x28(%rsp), %rdx
jne 0x225b6f
decl 0x14(%rsp)
movl %ebp, 0xc(%rsp)
incq %r12
cmpq 0x60(%rsp), %r12
jne 0x2258e6
cmpl $0x1, 0x10(%rsp)
movl 0x14(%rsp), %ebx
je 0x225b55
movq 0x8(%r8), %rcx
testq %rcx, %rcx
je 0x225b6f
cmpq $0x1, %rcx
setne %al
testl %ebx, %ebx
setne %dl
orb %al, %dl
movb $0x1, %al
movq %rax, 0x38(%rsp)
jne 0x225879
jmp 0x225bfa
cmpl $0x1, 0x10(%rsp)
movl 0x14(%rsp), %ebx
jne 0x225b6c
xorl %r14d, %r14d
movl 0xc(%rsp), %esi
cmpl $-0x1, %esi
je 0x225b6f
testl %ebx, %ebx
jne 0x225b6f
cmpq $0x0, 0x8(%r8)
je 0x225bba
xorl %r14d, %r14d
cmpq $0x0, 0x38(%r8)
je 0x225775
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 0x225b84
jmp 0x225775
cmpq $0x0, 0x10(%r8)
movq 0x18(%r8), %rax
je 0x225bc9
xorl %ecx, %ecx
jmp 0x225bef
movq $0x2, 0x10(%r8)
movl $0x8, %esi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
movl %esi, (%rax,%rcx,4)
movl $0x1, %r14d
jmp 0x225b6f