// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_naive_topocut_aux_sec(usg_t*, unsigned int, int)
int usg_naive_topocut_aux_sec(usg_t *g, uint32_t v0, int max_ext)
{
    int32_t n_ext = 0; usg_arc_t *av; uint32_t w = (uint32_t)-1, v = v0, nv, i, kv, tip[2] = {0}; 
    v = v0;
    while (1) {
        av = usg_arc_a(g, v); nv = usg_arc_n(g, v);
        for (i = kv = 0; i < nv; i++) {
            if (av[i].del) continue;
            kv++; w = av[i].v;
        }
        n_ext += g->a[v>>1].occ;
        // if((v0>>1) == 306) {
        //     fprintf(stderr, "[M::%s::] v>>1::%u, v&1::%u, n_ext::%d\n", __func__, v>>1, v&1, n_ext);
        // }
        if(kv != 1) {
            if(kv == 0) tip[0] = 1;
            break;
        }
        v = w;

        av = usg_arc_a(g, v^1); nv = usg_arc_n(g, v^1);
        for (i = kv = 0; i < nv; i++) {
            if (av[i].del) continue;
            kv++; 
        }
        if(kv != 1) break;
        if(v == v0) return 0;///circle, it is ok to remove it
    }
    
    v = v0^1;
    while (1) {
        av = usg_arc_a(g, v); nv = usg_arc_n(g, v);
        for (i = kv = 0; i < nv; i++) {
            if (av[i].del) continue;
            kv++; w = av[i].v;
        }
        n_ext += g->a[v>>1].occ;
        // if((v0>>1) == 306) {
        //     fprintf(stderr, "[M::%s::] v>>1::%u, v&1::%u, n_ext::%d\n", __func__, v>>1, v&1, n_ext);
        // }
        if(kv != 1) {
            if(kv == 0) tip[1] = 1;
            break;
        }
        v = w;

        av = usg_arc_a(g, v^1); nv = usg_arc_n(g, v^1);
        for (i = kv = 0; i < nv; i++) {
            if (av[i].del) continue;
            kv++; 
        }
        if(kv != 1) break;
        if(v == (v0^1)) return 0;///circle, it is ok to remove it
    }

    n_ext -= g->a[v0>>1].occ;
    // if((v0>>1) == 306) {
    //     fprintf(stderr, "[M::%s::] n_ext::%d, tip[0]::%u, tip[1]::%u\n", __func__, n_ext, tip[0], tip[1]);
    // }
    if((tip[0] || tip[1]) && (n_ext >= max_ext)) return n_ext;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
movq (%rdi), %rax
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl %esi, %r9d
movl %r9d, %r8d
shrl %r8d
andl $0x1, %r9d
imulq $0x78, %r8, %r8
leaq (%rax,%r8), %r10
leaq (%r9,%r9,2), %r11
movl 0x10(%r10,%r11,8), %r9d
testq %r9, %r9
je 0x21495f
movq 0x20(%r10,%r11,8), %r11
shlq $0x5, %r9
xorl %ebx, %ebx
xorl %r10d, %r10d
cmpl $0x0, 0xc(%r11,%rbx)
js 0x2148e4
incl %r10d
movl 0x8(%r11,%rbx), %edi
addq $0x20, %rbx
cmpq %rbx, %r9
jne 0x2148d4
addl 0x4(%rax,%r8), %ecx
movb $0x1, %r8b
cmpl $0x1, %r10d
jne 0x214958
movl %edi, %r9d
notl %r9d
movl %edi, %r10d
shrl %r10d
andl $0x1, %r9d
imulq $0x78, %r10, %r10
addq %rax, %r10
leaq (%r9,%r9,2), %r11
movl 0x10(%r10,%r11,8), %r9d
testq %r9, %r9
je 0x214967
movq 0x20(%r10,%r11,8), %r10
shlq $0x5, %r9
xorl %r11d, %r11d
xorl %ebx, %ebx
movl 0xc(%r10,%r11), %ebp
notl %ebp
shrl $0x1f, %ebp
addl %ebp, %ebx
addq $0x20, %r11
cmpq %r11, %r9
jne 0x21492e
cmpl $0x1, %ebx
jne 0x214967
movl %edi, %r9d
cmpl %esi, %edi
jne 0x2148a2
jmp 0x214a22
testl %r10d, %r10d
je 0x214964
jmp 0x214967
addl 0x4(%rax,%r8), %ecx
xorl %r8d, %r8d
movl %esi, %r9d
xorl $0x1, %r9d
movl %r9d, %r11d
movl %r11d, %r10d
shrl %r10d
andl $0x1, %r11d
imulq $0x78, %r10, %r10
leaq (%rax,%r10), %rbx
leaq (%r11,%r11,2), %r14
movl 0x10(%rbx,%r14,8), %r11d
testq %r11, %r11
je 0x214a2c
movq 0x20(%rbx,%r14,8), %r14
shlq $0x5, %r11
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpl $0x0, 0xc(%r14,%r15)
js 0x2149b2
incl %ebx
movl 0x8(%r14,%r15), %edi
addq $0x20, %r15
cmpq %r15, %r11
jne 0x2149a3
addl 0x4(%rax,%r10), %ecx
movb $0x1, %r10b
cmpl $0x1, %ebx
jne 0x214a26
movl %edi, %r11d
notl %r11d
movl %edi, %ebx
shrl %ebx
andl $0x1, %r11d
imulq $0x78, %rbx, %rbx
addq %rax, %rbx
leaq (%r11,%r11,2), %r14
movl 0x10(%rbx,%r14,8), %r11d
testq %r11, %r11
je 0x214a34
movq 0x20(%rbx,%r14,8), %rbx
shlq $0x5, %r11
xorl %r14d, %r14d
xorl %ebp, %ebp
movl 0xc(%rbx,%r14), %r15d
notl %r15d
shrl $0x1f, %r15d
addl %r15d, %ebp
addq $0x20, %r14
cmpq %r14, %r11
jne 0x2149f9
cmpl $0x1, %ebp
jne 0x214a34
movl %edi, %r11d
cmpl %r9d, %edi
jne 0x214971
xorl %eax, %eax
jmp 0x214a51
testl %ebx, %ebx
je 0x214a31
jmp 0x214a34
addl 0x4(%rax,%r10), %ecx
xorl %r10d, %r10d
shrl %esi
imulq $0x78, %rsi, %rsi
subl 0x4(%rax,%rsi), %ecx
xorl %eax, %eax
cmpl %edx, %ecx
cmovll %eax, %ecx
testb %r10b, %r10b
cmovel %ecx, %eax
testb %r8b, %r8b
cmovel %ecx, %eax
popq %rbx
popq %r14
popq %r15
popq %rbp
retq