// File: /workspace/github_cpp_makefile_star2_O1/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), %r11
leaq (%r9,%r9,2), %r9
movl 0x10(%r11,%r9,8), %r10d
testq %r10, %r10
je 0x20957b
movq 0x20(%r11,%r9,8), %r11
shlq $0x5, %r10
xorl %ebx, %ebx
xorl %r9d, %r9d
cmpl $0x0, 0xc(%r11,%rbx)
js 0x209570
incl %r9d
movl 0x8(%r11,%rbx), %edi
addq $0x20, %rbx
cmpq %rbx, %r10
jne 0x209560
jmp 0x20957e
xorl %r9d, %r9d
addl 0x4(%rax,%r8), %ecx
movb $0x1, %r8b
cmpl $0x1, %r9d
jne 0x2095e9
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 0x2095f1
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 0x2095bf
cmpl $0x1, %ebx
jne 0x2095f1
movl %edi, %r9d
cmpl %esi, %edi
jne 0x209532
jmp 0x2096b0
testl %r9d, %r9d
jne 0x2095f1
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), %r14
leaq (%r11,%r11,2), %r11
movl 0x10(%r14,%r11,8), %ebx
testq %rbx, %rbx
je 0x209645
movq 0x20(%r14,%r11,8), %r14
shlq $0x5, %rbx
xorl %r15d, %r15d
xorl %r11d, %r11d
cmpl $0x0, 0xc(%r14,%r15)
js 0x20963a
incl %r11d
movl 0x8(%r14,%r15), %edi
addq $0x20, %r15
cmpq %r15, %rbx
jne 0x20962a
jmp 0x209648
xorl %r11d, %r11d
addl 0x4(%rax,%r10), %ecx
movb $0x1, %r10b
cmpl $0x1, %r11d
jne 0x2096b4
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 0x2096bc
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 0x209687
cmpl $0x1, %ebp
jne 0x2096bc
movl %edi, %r11d
cmpl %r9d, %edi
jne 0x2095fb
xorl %eax, %eax
jmp 0x2096d9
testl %r11d, %r11d
jne 0x2096bc
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