// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: iter_contain_g(R_to_U*, flex_asg_t*, unsigned int, asg64_v*, asg64_v*, float, unsigned int)
uint32_t iter_contain_g(R_to_U* rI, flex_asg_t *fg, uint32_t v0, asg64_v *b, asg64_v *st, float ou_rat, uint32_t only_trans_nn)
{
    uint32_t v, w = (uint32_t)-1, x, i, kv, kw, ulen, cnt = 0, st_n, m, is_purge; 
    asg_arc_t *s; flex_asg_e_retrive_t rr;
    b->n = st->n = ulen = 0; kv_push(uint64_t, *st, v0);
    while (st->n) {
        v = kv_pop(*st); kv = kw = (uint32_t)-1; ulen = 0;
        if(fg->g->seq_vis[v>>1]) continue;
        while ((!(fg->g->seq_vis[v>>1])) && (is_contain_r((*rI), (v>>1)))) {///push a unitig
            kv = get_flex_arcs(fg, v, &w, 1); ulen++;
            kv_push(uint64_t, *b, v); 
            if(kv == 1) {
                flex_arcs0(s, (*fg), w);
                w = s->v;
                kw = get_flex_arcs(fg, w^1, NULL, 0);
                if(kw == 1) v = w;
                else break;
            } else {
                break;
            }
        }
        // if((v0>>1) == 12321 || (v0>>1) == 12334) {
        //     fprintf(stderr, "0[M::%s] v0>>1::%u(%c), b->n::%u, ulen::%u\n", 
        //     __func__, (v0>>1), "+-"[v0&1], (uint32_t)b->n, ulen);
        // }   
        if((!ulen) || (fg->g->seq_vis[v>>1]) || (!(is_contain_r((*rI), (v>>1))))) {
            for (i = b->n - ulen; i < b->n; i++) {
                fg->g->seq_vis[b->a[i]>>1] = 1; b->a[i] |= ((uint64_t)0x100000000);
            }
            continue;
        }
        // fprintf(stderr, "1[M::%s] v0>>1::%u(%c), b->n::%u, ulen::%u\n", 
        //     __func__, (v0>>1), "+-"[v0&1], (uint32_t)b->n, ulen);
        
        for (i = 0; i < b->n; i++) {
            if(b->a[i]&(0x100000000)) continue;
            fg->g->seq_vis[b->a[i]>>1] = 2;
        }
        // fprintf(stderr, "2[M::%s] v0>>1::%u(%c), b->n::%u, ulen::%u\n", 
        //     __func__, (v0>>1), "+-"[v0&1], (uint32_t)b->n, ulen);
        for (i = 0, st_n = st->n; i < b->n; i++) {
            if(b->a[i]&(0x100000000)) continue;
            if(detect_tip2(fg, b->a[i]>>1, st, ou_rat)) break;
        }
        if(i >= b->n) is_purge = 1;
        else is_purge = 0;
        // fprintf(stderr, "3[M::%s] v0>>1::%u(%c), b->n::%u, ulen::%u\n", 
        //     __func__, (v0>>1), "+-"[v0&1], (uint32_t)b->n, ulen);
        for (i = m = st_n; i < st->n; i++) {
            if(fg->g->seq_vis[st->a[i]]&128) fg->g->seq_vis[st->a[i]] -= 128;
            ///append edges to all nodes, instead of non-contained only
            // if(is_contain_r((*rI), (st->a[i]>>1))) continue;
            st->a[m++] = st->a[i];
        }
        st->n = m;
        // if((v0>>1) == 12321 || (v0>>1) == 12334) {
        //     fprintf(stderr, "4[M::%s] v0>>1::%u(%c), b->n::%u, i::%u\n", 
        //         __func__, (v0>>1), "+-"[v0&1], (uint32_t)b->n, i);
        // }
        if(is_purge && only_trans_nn) {
            is_purge = append_trans_check(fg, st->a + st_n, st->n-st_n);
        }
        
        if(is_purge) {
            for (i = 0; i < b->n; i++) {
                x = ((uint32_t)b->a[i])>>1;
                // if((v0>>1) == 12321 || (v0>>1) == 12334) {
                //     fprintf(stderr, "del::[M::%s] x::%u, seq_vis::%u\n", __func__, x, fg->g->seq_vis[x]);
                // }
                if(fg->g->seq_vis[x]&2) {
                    asg_seq_del(fg->g, x); cnt++;
                }
                fg->g->seq_vis[x] = 0;
            }
            append_notrans_e(fg, st->a + st_n, st->n-st_n, b);
            st->n = st_n;
            return cnt;
        }
        for (i = 0; i < b->n; i++) {
            if(b->a[i]&(0x100000000)) continue;
            fg->g->seq_vis[b->a[i]>>1] = 1;
        }
        st->n = st_n;

        rr.i[0] = 0; rr.i[1] = (uint32_t)-1;
        while(1) {
            s = iter_flex_asg(fg, &rr, v);
            if(!s) break;
            if(s->del) continue;
            if(fg->g->seq_vis[s->v>>1]) continue;
            if(!is_contain_r((*rI), (s->v>>1))) continue;
            kv_push(uint64_t, *st, s->v); 
        }
    }
    for (i = 0; i < b->n; i++) fg->g->seq_vis[((uint32_t)b->a[i])>>1] = 0;
    return cnt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, 0x20(%rsp)
movss %xmm0, 0x24(%rsp)
movq %r8, %r15
movq %rcx, %rbx
movl %edx, %ebp
orl $-0x1, 0x4(%rsp)
movq %rsi, %r14
andq $0x0, (%r8)
movq %rdi, 0x8(%rsp)
andq $0x0, (%rcx)
movq (%r8), %rcx
pushq $0x2
popq %rax
cmpq 0x8(%r8), %rcx
jne 0x1ab08c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
jmp 0x1ab090
movq 0x10(%r15), %rax
movl %ebp, %edx
leaq 0x1(%rcx), %rsi
movq %r15, 0x10(%rsp)
movq %rsi, (%r15)
movq %rdx, (%rax,%rcx,8)
leaq 0x58(%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
testq %rax, %rax
je 0x1ab427
movq 0x10(%rsi), %rcx
leaq -0x1(%rax), %rdx
movq %rdx, (%rsi)
movq -0x8(%rcx,%rax,8), %rbp
movq 0x30(%r14), %rax
movq 0x30(%rax), %rax
movl %ebp, %ecx
shrl %ecx
cmpb $0x0, (%rax,%rcx)
jne 0x1ab0ab
xorl %r15d, %r15d
movl %ebp, %r13d
movq 0x30(%r14), %rax
movq 0x30(%rax), %rax
movl %ebp, %r12d
shrl %r12d
cmpb $0x0, (%rax,%r12)
jne 0x1ab1c3
movq 0x8(%rsp), %rax
cmpq %r12, (%rax)
jbe 0x1ab1c3
movq 0x8(%rax), %rax
cmpl $0x0, (%rax,%r12,4)
js 0x1ab1c3
movq %r14, %rdi
movl %r13d, %esi
leaq 0x4(%rsp), %rdx
pushq $0x1
popq %rcx
callq 0x1a5473
movl %eax, %ebp
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x1ab161
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movl %r13d, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
cmpl $0x1, %ebp
jne 0x1ab1c0
movl 0x4(%rsp), %eax
testl %eax, %eax
js 0x1ab186
movq 0x30(%r14), %rcx
addq $0x8, %rcx
jmp 0x1ab190
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movl 0x8(%rcx,%rax), %ebp
movl %ebp, 0x4(%rsp)
movl %ebp, %esi
xorl $0x1, %esi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5473
incl %r15d
cmpl $0x1, %eax
je 0x1ab0e1
jmp 0x1ab1c3
incl %r15d
testl %r15d, %r15d
je 0x1ab22c
movq 0x30(%r14), %rax
movq 0x30(%rax), %rax
cmpb $0x0, (%rax,%r12)
jne 0x1ab22c
movq 0x8(%rsp), %rax
cmpq %r12, (%rax)
jbe 0x1ab22c
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
cmpl $0x0, (%rax,%r12,4)
js 0x1ab22c
xorl %eax, %eax
movq 0x10(%rsp), %rbp
movabsq $0x100000000, %rsi      # imm = 0x100000000
movl %eax, %eax
movq (%rbx), %rcx
cmpq %rax, %rcx
jbe 0x1ab26f
movq 0x10(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
testq %rsi, %rcx
jne 0x1ab228
movq 0x30(%r14), %rdx
movq 0x30(%rdx), %rdx
shrq %rcx
movb $0x2, (%rdx,%rcx)
incl %eax
jmp 0x1ab202
movq (%rbx), %rcx
movl %ecx, %eax
subl %r15d, %eax
movabsq $0x100000000, %rsi      # imm = 0x100000000
movl %eax, %eax
cmpq %rax, %rcx
jbe 0x1ab0ab
movq 0x30(%r14), %rcx
movq 0x30(%rcx), %rcx
movq 0x10(%rbx), %rdx
movq (%rdx,%rax,8), %rdx
shrq %rdx
movb $0x1, (%rcx,%rdx)
movq 0x10(%rbx), %rcx
orq %rsi, (%rcx,%rax,8)
incl %eax
movq (%rbx), %rcx
jmp 0x1ab23e
movq (%rbp), %r12
movl %r12d, %eax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
movl %r15d, %r15d
cmpq %r15, %rcx
jbe 0x1ab2ee
movq 0x10(%rbx), %rax
movq (%rax,%r15,8), %rsi
movabsq $0x100000000, %rax      # imm = 0x100000000
testq %rax, %rsi
jne 0x1ab2b8
shrq %rsi
movq %r14, %rdi
movq %rbp, %rdx
movss 0x24(%rsp), %xmm0
callq 0x1aa3da
movq (%rbx), %rcx
testl %eax, %eax
jne 0x1ab2ee
incl %r15d
jmp 0x1ab27e
movq 0x30(%r14), %rax
movq 0x30(%rax), %rdi
movq 0x10(%rbp), %rsi
movq (%rsi,%rdx,8), %rax
movb (%rdi,%rax), %r8b
testb %r8b, %r8b
jns 0x1ab2e6
andb $0x7f, %r8b
movb %r8b, (%rdi,%rax)
movq 0x10(%rbp), %rsi
movq (%rsi,%rdx,8), %rax
leal 0x1(%rdx), %r12d
movq %rax, (%rsi,%rdx,8)
movl %r12d, %edx
cmpq %rdx, (%rbp)
ja 0x1ab2bd
xorl %eax, %eax
cmpq %r15, %rcx
seta %sil
setbe %cl
cmpl $0x0, 0x20(%rsp)
sete %dil
movq %rdx, (%rbp)
orb %sil, %dil
movabsq $0x100000000, %r15      # imm = 0x100000000
leaq 0x30(%rsp), %r12
jne 0x1ab342
movq 0x18(%rsp), %rax
leaq (,%rax,8), %rsi
addq 0x10(%rbp), %rsi
subq %rax, %rdx
movq %r14, %rdi
callq 0x1aadf4
jmp 0x1ab344
movb %cl, %al
testl %eax, %eax
jne 0x1ab44f
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, (%rbx)
jbe 0x1ab375
movq 0x10(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
testq %r15, %rcx
jne 0x1ab371
movq 0x30(%r14), %rdx
movq 0x30(%rdx), %rdx
shrq %rcx
movb $0x1, (%rdx,%rcx)
incl %eax
jmp 0x1ab34e
movq 0x18(%rsp), %rax
movq %rax, (%rbp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x30(%rsp)
movq %r14, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x1aa378
testq %rax, %rax
je 0x1ab0ab
movq %rax, %rbp
cmpl $0x0, 0xc(%rax)
js 0x1ab38d
movq 0x30(%r14), %rax
movq 0x30(%rax), %rdx
movl 0x8(%rbp), %ecx
movl %ecx, %eax
shrl %eax
cmpb $0x0, (%rdx,%rax)
jne 0x1ab38d
movq 0x8(%rsp), %rdx
cmpq %rax, (%rdx)
jbe 0x1ab38d
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %rdx
cmpl $0x0, (%rdx,%rax,4)
js 0x1ab38d
movq 0x10(%rsp), %r15
movq (%r15), %rdx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rdx
jne 0x1ab415
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movl 0x8(%rbp), %ecx
movq (%r15), %rdx
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r15)
movq %rcx, (%rax,%rdx,8)
jmp 0x1ab38d
xorl %ebp, %ebp
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, (%rbx)
jbe 0x1ab4bc
movq 0x30(%r14), %rcx
movq 0x30(%rcx), %rcx
movq 0x10(%rbx), %rdx
movl (%rdx,%rax,8), %edx
shrl %edx
movb $0x0, (%rcx,%rdx)
incl %eax
jmp 0x1ab42b
xorl %r15d, %r15d
xorl %ebp, %ebp
movl %r15d, %r15d
cmpq %r15, (%rbx)
jbe 0x1ab492
movq 0x10(%rbx), %rax
movl (%rax,%r15,8), %r12d
shrl %r12d
movq 0x30(%r14), %rdi
movq 0x30(%rdi), %rax
testb $0x2, (%rax,%r12)
je 0x1ab488
movl %r12d, %esi
callq 0x1a606e
incl %ebp
movq 0x30(%r14), %rax
movq 0x30(%rax), %rax
movb $0x0, (%rax,%r12)
incl %r15d
jmp 0x1ab454
movq 0x18(%rsp), %r12
leaq (,%r12,8), %rsi
movq 0x10(%rsp), %r15
addq 0x10(%r15), %rsi
movq (%r15), %rdx
subq %r12, %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1aa9fa
movq %r12, (%r15)
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq