// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x70(%rax)
je 0x36cb4c
movq $0x0, -0x8(%rbp)
jmp 0x36d1ec
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x37db40
cmpl $0x2, %eax
jae 0x36cb6e
movq $0x0, -0x8(%rbp)
jmp 0x36d1ec
movq -0x30(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x10(%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x14(%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0xc(%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x4(%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x36cc88
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x36cc47
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0x88(%rbp)
jmp 0x36cc55
movl $0x2, %eax
movq %rax, -0x88(%rbp)
jmp 0x36cc55
movq -0x88(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x14(%rbp), %edx
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x8(%rsi)
movl %edx, (%rax,%rcx,4)
movl $0x0, -0x40(%rbp)
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $-0x1, %rsi
movq %rsi, 0x8(%rdx)
movl -0x4(%rax,%rcx,4), %eax
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x54(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x54(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x54(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x36d03e
movq -0x68(%rbp), %rax
movl -0x34(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x36cd7f
jmp 0x36d030
movq -0x68(%rbp), %rax
movl -0x34(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0x68(%rbp), %rax
movl -0x34(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x70(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x6c(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl -0x6c(%rbp), %eax
shrl %eax
movl -0x14(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0x36cdcf
jmp 0x36d158
cmpl $0x0, -0x3c(%rbp)
je 0x36cddc
movl $0x0, -0x70(%rbp)
movl -0x5c(%rbp), %eax
addl -0x70(%rbp), %eax
movl %eax, %eax
cmpq -0x20(%rbp), %rax
jbe 0x36cdef
jmp 0x36d03e
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x28(%rbp), %rax
jbe 0x36ce02
jmp 0x36d03e
movq -0x78(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x36cf0e
jmp 0x36ce17
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jne 0x36ce89
movq -0x30(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x36ce48
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rax
shlq %rax
movq %rax, -0x90(%rbp)
jmp 0x36ce56
movl $0x2, %eax
movq %rax, -0x90(%rbp)
jmp 0x36ce56
movq -0x90(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x48(%rax)
movl -0x6c(%rbp), %edx
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x30(%rbp), %rsi
movq 0x38(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x38(%rsi)
movl %edx, (%rax,%rcx,4)
movl -0x54(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, (%rax)
movq -0x78(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%rax)
movl -0x5c(%rbp), %ecx
addl -0x70(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rdi
movl -0x6c(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x37db40
movl %eax, %edx
movq -0x78(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, 0x18(%rax)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x36cf2c
movl -0x5c(%rbp), %eax
addl -0x70(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x36cf2a
movl -0x5c(%rbp), %ecx
addl -0x70(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x36cf2c
movq -0x78(%rbp), %rcx
movl 0x18(%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl $-0x1, %eax
movl 0x18(%rcx), %edx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %eax, %edx
movl %edx, 0x18(%rcx)
cmpl $0x0, %eax
jne 0x36d02e
movq -0x10(%rbp), %rdi
movl -0x6c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x37db40
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jbe 0x36d00b
jmp 0x36cf76
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x36cfe8
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x36cfa7
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0x98(%rbp)
jmp 0x36cfb5
movl $0x2, %eax
movq %rax, -0x98(%rbp)
jmp 0x36cfb5
movq -0x98(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x6c(%rbp), %edx
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x8(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x36d025
cmpl $0x0, -0x40(%rbp)
je 0x36d016
jmp 0x36d158
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x36d030
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x36cd55
movl $0x0, -0x3c(%rbp)
cmpl $0x1, -0x40(%rbp)
jne 0x36d10b
cmpl $-0x1, -0x44(%rbp)
je 0x36d109
cmpl $0x0, -0x38(%rbp)
jne 0x36d109
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x36d109
jmp 0x36d074
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x36d0e6
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x36d0a5
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0x36d0b3
movl $0x2, %eax
movq %rax, -0xa0(%rbp)
jmp 0x36d0b3
movq -0xa0(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x44(%rbp), %edx
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rsi
movq 0x8(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x8(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x36d150
jmp 0x36d158
movl -0x34(%rbp), %eax
cmpl -0x58(%rbp), %eax
jb 0x36d11e
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x36d120
jmp 0x36d158
jmp 0x36d122
movq -0x30(%rbp), %rcx
movb $0x1, %al
cmpq $0x1, 0x8(%rcx)
movb %al, -0xa1(%rbp)
ja 0x36d142
cmpl $0x0, -0x38(%rbp)
setne %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x36ccb7
movq $0x1, -0x50(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x36d1e4
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x80(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x80(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x80(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x80(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x80(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, 0x18(%rax)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x36d15f
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)