// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %eax
movl %eax, -0x30(%rbp)
leaq -0x44(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x2100
movl -0x14(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x34f903
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34f8db
jmp 0x34f8f8
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x34f8b8
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x1, -0x3c(%rbp)
je 0x34f939
cmpl $0x0, -0x3c(%rbp)
jne 0x34f934
movl $0x1, -0x44(%rbp)
jmp 0x34fa0f
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x34f9ee
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34f9da
jmp 0x34f9e3
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x34f9b7
cmpl $0x1, -0x3c(%rbp)
je 0x34f9f6
jmp 0x34fa0f
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x34fa0a
movl $0x0, -0x4(%rbp)
jmp 0x34fc20
jmp 0x34f84c
movl -0x14(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x34facf
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34faa7
jmp 0x34fac4
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x34fa84
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x1, -0x3c(%rbp)
je 0x34fb05
cmpl $0x0, -0x3c(%rbp)
jne 0x34fb00
movl $0x1, -0x40(%rbp)
jmp 0x34fbdd
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x34fbba
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34fba6
jmp 0x34fbaf
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x34fb83
cmpl $0x1, -0x3c(%rbp)
je 0x34fbc2
jmp 0x34fbdd
movl -0x30(%rbp), %eax
movl -0x14(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0x34fbd8
movl $0x0, -0x4(%rbp)
jmp 0x34fc20
jmp 0x34fa18
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0x34fc09
cmpl $0x0, -0x40(%rbp)
je 0x34fc19
movl -0x1c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jl 0x34fc19
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x34fc20
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)