// File: /workspace/github_cpp_makefile_star2_O2/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
imulq $0x18, %r9, %r9
movq 0x20(%r9,%r11), %r10
movl 0x10(%r9,%r11), %r11d
shlq $0x5, %r11
xorl %ebx, %ebx
xorl %r9d, %r9d
cmpq %rbx, %r11
je 0x1c6c9b
cmpl $0x0, 0xc(%r10,%rbx)
js 0x1c6c95
incl %r9d
movl 0x8(%r10,%rbx), %edi
addq $0x20, %rbx
jmp 0x1c6c80
addl 0x4(%rax,%r8), %ecx
movb $0x1, %r8b
cmpl $0x1, %r9d
jne 0x1c6d03
movl %edi, %r9d
notl %r9d
movl %edi, %r10d
shrl %r10d
andl $0x1, %r9d
imulq $0x78, %r10, %r10
addq %rax, %r10
imulq $0x18, %r9, %r11
movq 0x20(%r11,%r10), %r9
movl 0x10(%r11,%r10), %r10d
shlq $0x5, %r10
xorl %r11d, %r11d
xorl %ebx, %ebx
cmpq %r11, %r10
je 0x1c6cee
movl 0xc(%r9,%r11), %ebp
notl %ebp
shrl $0x1f, %ebp
addl %ebp, %ebx
addq $0x20, %r11
jmp 0x1c6cd7
cmpl $0x1, %ebx
jne 0x1c6d0b
movl %edi, %r9d
cmpl %esi, %edi
jne 0x1c6c57
jmp 0x1c6dbc
testl %r9d, %r9d
jne 0x1c6d0b
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
imulq $0x18, %r11, %r14
movq 0x20(%r14,%rbx), %r11
movl 0x10(%r14,%rbx), %r14d
shlq $0x5, %r14
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpq %r15, %r14
je 0x1c6d58
cmpl $0x0, 0xc(%r11,%r15)
js 0x1c6d52
incl %ebx
movl 0x8(%r11,%r15), %edi
addq $0x20, %r15
jmp 0x1c6d3e
addl 0x4(%rax,%r10), %ecx
movb $0x1, %r10b
cmpl $0x1, %ebx
jne 0x1c6dc0
movl %edi, %r11d
notl %r11d
movl %edi, %ebx
shrl %ebx
andl $0x1, %r11d
imulq $0x78, %rbx, %rbx
addq %rax, %rbx
imulq $0x18, %r11, %r14
movq 0x20(%r14,%rbx), %r11
movl 0x10(%r14,%rbx), %ebx
shlq $0x5, %rbx
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq %r14, %rbx
je 0x1c6dab
movl 0xc(%r11,%r14), %r15d
notl %r15d
shrl $0x1f, %r15d
addl %r15d, %ebp
addq $0x20, %r14
jmp 0x1c6d91
cmpl $0x1, %ebp
jne 0x1c6dc7
movl %edi, %r11d
cmpl %r9d, %edi
jne 0x1c6d15
xorl %eax, %eax
jmp 0x1c6de4
testl %ebx, %ebx
jne 0x1c6dc7
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