// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_arc_cut_tips(usg_t*, unsigned int, unsigned int, asg64_v*)
uint32_t usg_arc_cut_tips(usg_t *g, uint32_t max_ext, uint32_t ignore_ul, asg64_v *in)
{
    asg64_v tx = {0,0,0}, *b = NULL;
    uint32_t n_vtx = g->n<<1, v, w, i, k, cnt = 0, nv, kv, pb, ff, is_telo;
    usg_arc_t *av = NULL, *p; uint64_t lw;
    if(in) b = in;
    else b = &tx;

    for (v = b->n = 0; v < n_vtx; ++v) {
        if (g->a[v>>1].del) continue;

        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++; break;
        }

        if(kv) continue;
        
        is_telo = 0; if(g->a[v>>1].telo) is_telo = 1;
        for (i = 0, w = v, kv = g->a[v>>1].occ; i < max_ext; i++) {
            if(usg_end(g, w^1, &lw)!=0) break;
            w = (uint32_t)lw; kv += g->a[w>>1].occ;
            if(g->a[w>>1].telo) is_telo = 1;
        }
        if((kv <= max_ext) && (!is_telo)) kv_push(uint64_t, *b, (((uint64_t)kv)<<32)|v);  
    }

    radix_sort_srt64(b->a, b->a + b->n);
    for (k = 0; k < b->n; k++) {
        v = (uint32_t)(b->a[k]);
        if (g->a[v>>1].del) continue;

        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++; break;
        }

        if(kv) continue;
        pb = b->n; kv_push(uint64_t, *b, v); 
        is_telo = 0; if(g->a[v>>1].telo) is_telo = 1;
        for (i = 0, w = v, kv = g->a[v>>1].occ; i < max_ext; i++) {
            if(usg_end(g, w^1, &lw)!=0) break;
            w = (uint32_t)lw; kv += g->a[w>>1].occ; kv_push(uint64_t, *b, lw);
            if(g->a[w>>1].telo) is_telo = 1;
        }
        // if((v>>1) == 308) {
        //     fprintf(stderr, "[M::%s::] v>>1::%u, v&1::%u, kv::%u\n", 
        //     __func__, v>>1, v&1, kv);
        // }


        if((kv <= max_ext) && (!is_telo)) {
            ff = 0;
            if(!ignore_ul) {///consider UL
                for (i = pb; i + 1 < b->n; i++) {
                    p = get_usg_arc(g, ((uint32_t)b->a[i]), ((uint32_t)b->a[i+1])); assert(p);
                    if(p->ou > 1) {//ignore ou == 1
                        ff = 1;
                        break;
                    }
                }

                if(ff == 0 && i < b->n) {
                    av = usg_arc_a(g, ((uint32_t)b->a[i])); nv = usg_arc_n(g, ((uint32_t)b->a[i]));
                    for (i = 0; i < nv; i++) {
                        if(av[i].del) continue;
                        if(av[i].ou > 1) {//ignore ou == 1
                            ff = 1;
                            break;
                        }
                    }
                }
            }

            // if((v>>1) == 139131) {
            //     fprintf(stderr, "+[M::%s::] v>>1::%u, v&1::%u, kv::%u, ff::%u, pb::%u, b->n::%u, w>>1::%u, w&1::%u\n", __func__, v>>1, v&1, kv,
            //     ff, pb, (uint32_t)b->n, ((uint32_t)b->a[b->n-1])>>1, ((uint32_t)b->a[b->n-1])&1);
            // }

            if((ff == 0) && (pb < b->n) && (!usg_real_tip(g, ((uint32_t)b->a[b->n-1]), 0.75))) {
                ff = 1;
            }

            // if((v>>1) == 139131) {
            //     fprintf(stderr, "-[M::%s::] v>>1::%u, v&1::%u, kv::%u, ff::%u, pb::%u, b->n::%u, w>>1::%u, w&1::%u\n", __func__, v>>1, v&1, kv,
            //     ff, pb, (uint32_t)b->n, ((uint32_t)b->a[b->n-1])>>1, ((uint32_t)b->a[b->n-1])&1);
            // }

            if(ff == 0) {
                for (i = pb; i < b->n; i++) usg_seq_del(g, ((uint32_t)b->a[i])>>1);
                cnt++;
            }

            // if((v>>1) == 139131) {
            //     fprintf(stderr, ">[M::%s::] v>>1::%u, v&1::%u, kv::%u, ff::%u, pb::%u, b->n::%u, w>>1::%u, w&1::%u, del::%u\n", __func__, v>>1, v&1, kv,
            //     ff, pb, (uint32_t)b->n, ((uint32_t)b->a[b->n-1])>>1, ((uint32_t)b->a[b->n-1])&1, 
            //     g->a[((uint32_t)b->a[b->n-1])>>1].del);
            // }
        }
        b->n = pb; 
    }

    if(!in) free(tx.a);
    if(cnt > 0) usg_cleanup(g);

    return cnt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r14
movl %edx, 0x3c(%rsp)
movl %esi, %ebp
movl 0x8(%rdi), %eax
addl %eax, %eax
testq %rcx, %rcx
je 0x213b90
movq $0x0, (%r14)
movq %rdi, 0x30(%rsp)
testl %eax, %eax
je 0x213dd4
movl %eax, %r15d
xorl %r13d, %r13d
movq $0x0, (%rsp)
movq $0x0, 0x18(%rsp)
xorl %r12d, %r12d
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl %r13d, %ecx
shrl %ecx
imulq $0x78, %rcx, %rcx
cmpb $0x0, 0x70(%rax,%rcx)
jne 0x213dc6
movl %r13d, %edx
notl %edx
andl $0x1, %edx
leaq (%rax,%rcx), %rsi
leaq (%rdx,%rdx,2), %rdi
movl 0x10(%rsi,%rdi,8), %edx
testq %rdx, %rdx
je 0x213c0b
movq 0x20(%rsi,%rdi,8), %rsi
shlq $0x5, %rdx
xorl %edi, %edi
cmpl $0x0, 0xc(%rsi,%rdi)
jns 0x213dc6
addq $0x20, %rdi
cmpq %rdi, %rdx
jne 0x213bf7
movq %r12, 0x8(%rsp)
movb 0x71(%rax,%rcx), %sil
testb %sil, %sil
setne %dl
movl 0x4(%rax,%rcx), %r12d
testl %ebp, %ebp
je 0x213d14
xorl %ecx, %ecx
testb %sil, %sil
setne %cl
xorl %edx, %edx
movl %r13d, %esi
movl %esi, %edi
shrl %edi
andl $0x1, %esi
imulq $0x78, %rdi, %r8
addq %rax, %r8
leaq (%rsi,%rsi,2), %rsi
movq 0x10(%r8,%rsi,8), %rdi
testl %edi, %edi
jle 0x213d0f
movq 0x20(%r8,%rsi,8), %rsi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
leaq 0xc(%rsi), %r9
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
xorl %r11d, %r11d
xorl %ebx, %ebx
cmpl $0x0, (%r9)
setns %bl
cmovnsl %r10d, %r8d
addl %ebx, %r11d
incq %r10
addq $0x20, %r9
cmpq %r10, %rdi
jne 0x213c6f
cmpl $0x1, %r11d
jne 0x213d0f
movslq %r8d, %rdi
shlq $0x5, %rdi
movl 0x8(%rsi,%rdi), %esi
movl %esi, %edi
notl %edi
movl %esi, %r8d
shrl %r8d
andl $0x1, %edi
imulq $0x78, %r8, %r8
addq %rax, %r8
leaq (%rdi,%rdi,2), %r9
movq 0x10(%r8,%r9,8), %rdi
testl %edi, %edi
jle 0x213d0f
movq 0x20(%r8,%r9,8), %r8
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0xc, %r8
xorl %r9d, %r9d
movl (%r8), %r10d
notl %r10d
shrl $0x1f, %r10d
addl %r10d, %r9d
addq $0x20, %r8
decq %rdi
jne 0x213ccf
cmpl $0x1, %r9d
jne 0x213d0f
movl %esi, %edi
shrl %edi
imulq $0x78, %rdi, %rdi
addl 0x4(%rax,%rdi), %r12d
cmpb $0x0, 0x71(%rax,%rdi)
movl $0x1, %edi
cmovnel %edi, %ecx
incl %edx
cmpl %ebp, %edx
jne 0x213c35
testl %ecx, %ecx
setne %dl
cmpl %ebp, %r12d
ja 0x213dc1
testb %dl, %dl
jne 0x213dc1
testq %r14, %r14
je 0x213d5d
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x213d91
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0x213d95
movq 0x18(%rsp), %rax
cmpq %rax, 0x8(%rsp)
jne 0x213d9e
leaq (%rax,%rax), %rbx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq (%rsp), %rdi
callq 0x2370
movq %rbx, 0x18(%rsp)
jmp 0x213da2
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x213db6
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
incq %rdx
movq %rax, (%rsp)
movq %rdx, 0x8(%rsp)
shlq $0x20, %r12
orq %r13, %r12
movq %r12, (%rax,%rcx,8)
movq 0x8(%rsp), %r12
incq %r13
cmpq %r15, %r13
jne 0x213bb7
jmp 0x213de8
xorl %r12d, %r12d
movq $0x0, 0x18(%rsp)
movq $0x0, (%rsp)
movq (%rsp), %r15
movq %r15, %rdi
movq %r12, %rax
testq %r14, %r14
je 0x213dfe
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1ee586
xorl %edx, %edx
movl $0x0, 0x28(%rsp)
movq 0x30(%rsp), %r13
movq %r14, 0x40(%rsp)
movl %ebp, 0x2c(%rsp)
movl %edx, %edx
testq %r14, %r14
je 0x213e35
cmpq %rdx, (%r14)
jbe 0x2142c9
movq 0x10(%r14), %rax
jmp 0x213e41
movq %r15, %rax
cmpq %rdx, %r12
jbe 0x2142c1
movq %r15, (%rsp)
movq (%rax,%rdx,8), %r15
movq (%r13), %rax
movl %r15d, %ecx
shrl %ecx
imulq $0x78, %rcx, %rbx
cmpb $0x0, 0x70(%rax,%rbx)
je 0x213e66
movq (%rsp), %r15
jmp 0x2142ba
movq %rdx, 0x20(%rsp)
movl %r15d, %ecx
notl %ecx
andl $0x1, %ecx
addq %rbx, %rax
leaq (%rcx,%rcx,2), %rdx
movl 0x10(%rax,%rdx,8), %ecx
testq %rcx, %rcx
je 0x213e9e
movq 0x20(%rax,%rdx,8), %rax
shlq $0x5, %rcx
xorl %edx, %edx
cmpl $0x0, 0xc(%rax,%rdx)
jns 0x213edb
addq $0x20, %rdx
cmpq %rdx, %rcx
jne 0x213e8e
testq %r14, %r14
je 0x213ee9
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
movq %rcx, 0x10(%rsp)
jne 0x213f18
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0x213f1c
movq (%rsp), %r15
movq 0x20(%rsp), %rdx
jmp 0x2142ba
cmpq 0x18(%rsp), %r12
jne 0x213f25
leaq (%r12,%r12), %rax
testq %r12, %r12
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, 0x18(%rsp)
leaq (,%rax,8), %rsi
movq (%rsp), %rdi
callq 0x2370
jmp 0x213f29
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x213f38
movq (%rsp), %rax
movq %r12, %rcx
movq %r12, 0x10(%rsp)
incq %r12
movq %rax, (%rsp)
movl %r15d, %edx
movq %rdx, (%rax,%rcx,8)
movq (%r13), %rax
movb 0x71(%rax,%rbx), %dl
testb %dl, %dl
setne %cl
movl 0x4(%rax,%rbx), %r11d
testl %ebp, %ebp
je 0x21411a
xorl %edi, %edi
testb %dl, %dl
setne %dil
xorl %ebx, %ebx
movl %r15d, %ecx
shrl %ecx
movl %r15d, %edx
andl $0x1, %edx
imulq $0x78, %rcx, %rcx
addq %rax, %rcx
leaq (%rdx,%rdx,2), %rsi
movq 0x10(%rcx,%rsi,8), %rdx
testl %edx, %edx
jle 0x214111
movl %edi, 0x8(%rsp)
movq 0x20(%rcx,%rsi,8), %rcx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq 0xc(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpl $0x0, (%rdi)
setns %r10b
cmovnsl %r8d, %esi
addl %r10d, %r9d
incq %r8
addq $0x20, %rdi
cmpq %r8, %rdx
jne 0x213fa4
cmpl $0x1, %r9d
jne 0x21410d
movslq %esi, %rdx
shlq $0x5, %rdx
movl 0x8(%rcx,%rdx), %ecx
movl %ecx, %edx
notl %edx
movl %ecx, %esi
shrl %esi
andl $0x1, %edx
imulq $0x78, %rsi, %rsi
addq %rax, %rsi
leaq (%rdx,%rdx,2), %rdi
movq 0x10(%rsi,%rdi,8), %rdx
testl %edx, %edx
jle 0x21410d
shlq $0x20, %r15
orq %rcx, %r15
movq 0x20(%rsi,%rdi,8), %rsi
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq $0xc, %rsi
xorl %edi, %edi
movl (%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x20, %rsi
decq %rdx
jne 0x214011
cmpl $0x1, %edi
jne 0x21410d
movq %r12, %rbp
shrl %ecx
imulq $0x78, %rcx, %r12
addl 0x4(%rax,%r12), %r11d
testq %r14, %r14
je 0x214081
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x2140be
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
movl %r11d, %r13d
callq 0x2370
movl %r13d, %r11d
movq 0x30(%rsp), %r13
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0x2140c2
movq 0x18(%rsp), %rax
cmpq %rax, %rbp
jne 0x2140cf
leaq (%rax,%rax), %r13
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r13
leaq (,%r13,8), %rsi
movq (%rsp), %rdi
movl %r11d, %r14d
callq 0x2370
movl %r14d, %r11d
movq %r13, 0x18(%rsp)
movq 0x30(%rsp), %r13
jmp 0x2140d3
movq 0x10(%r14), %rax
movl 0x8(%rsp), %edi
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x2140e1
movq (%rsp), %rax
movl 0x8(%rsp), %edi
movq %rbp, %rcx
incq %rbp
movq %rax, (%rsp)
movq %r15, (%rax,%rcx,8)
movq (%r13), %rax
cmpb $0x0, 0x71(%rax,%r12)
movl $0x1, %ecx
cmovnel %ecx, %edi
incl %ebx
cmpl 0x2c(%rsp), %ebx
movq %rbp, %r12
movq 0x40(%rsp), %r14
jne 0x213f63
jmp 0x214111
movl 0x8(%rsp), %edi
testl %edi, %edi
setne %cl
movl 0x2c(%rsp), %ebp
cmpl %ebp, %r11d
movq (%rsp), %r15
movq 0x20(%rsp), %rdx
ja 0x2142a6
testb %cl, %cl
jne 0x2142a6
movq 0x10(%rsp), %rcx
movl %ecx, %ebx
cmpl $0x0, 0x3c(%rsp)
je 0x214164
movl 0x10(%rsp), %eax
testq %r14, %r14
je 0x21424f
movq (%r14), %rdx
cmpq %rdx, %rax
jae 0x214272
movq 0x10(%r14), %rcx
jmp 0x21425a
movl %ebx, %esi
leal 0x1(%rsi), %ecx
testq %r14, %r14
je 0x21417c
movq (%r14), %rdx
cmpq %rcx, %rdx
jbe 0x2141dd
movq 0x10(%r14), %rdx
jmp 0x214184
movq %r15, %rdx
cmpq %rcx, %r12
jbe 0x2141ee
movl %esi, %esi
movq (%rdx,%rsi,8), %rsi
movl %esi, %edi
shrl %edi
andl $0x1, %esi
imulq $0x78, %rdi, %r8
addq %rax, %r8
leaq (%rsi,%rsi,2), %r9
movl 0x10(%r8,%r9,8), %esi
testq %rsi, %rsi
je 0x2142ff
movl (%rdx,%rcx,8), %edi
movq 0x20(%r8,%r9,8), %rdx
shlq $0x5, %rsi
xorl %r8d, %r8d
cmpl %edi, 0x8(%rdx,%r8)
je 0x2141ce
addq $0x20, %r8
cmpq %r8, %rsi
jne 0x2141b9
jmp 0x2142ff
cmpl $0x1, 0x10(%rdx,%r8)
movl %ecx, %esi
jbe 0x214166
jmp 0x2142a1
movl %esi, %ecx
cmpq %rcx, %rdx
jbe 0x214242
shlq $0x3, %rcx
addq 0x10(%r14), %rcx
jmp 0x2141f9
movl %esi, %ecx
cmpq %rcx, %r12
jbe 0x21424b
leaq (%r15,%rcx,8), %rcx
movq (%rcx), %rcx
movl %ecx, %edx
shrl %edx
andl $0x1, %ecx
imulq $0x78, %rdx, %rdx
addq %rdx, %rax
leaq (%rcx,%rcx,2), %rdx
movl 0x10(%rax,%rdx,8), %ecx
testq %rcx, %rcx
je 0x214142
movq 0x20(%rax,%rdx,8), %rax
shlq $0x5, %rcx
xorl %edx, %edx
cmpl $0x0, 0xc(%rax,%rdx)
js 0x214234
cmpl $0x1, 0x10(%rax,%rdx)
ja 0x2142a1
addq $0x20, %rdx
cmpq %rdx, %rcx
jne 0x214226
jmp 0x214142
movl 0x10(%rsp), %eax
jmp 0x214152
movl 0x10(%rsp), %eax
movq %r15, %rcx
movq %r12, %rdx
cmpq %r12, %rax
jae 0x214272
movl -0x8(%rcx,%rdx,8), %esi
movq %r13, %rdi
movsd 0x1d3c7(%rip), %xmm0    # 0x231630
callq 0x213a81
testl %eax, %eax
je 0x2142a1
movl %ebx, %ebx
testq %r14, %r14
je 0x214284
cmpq %rbx, (%r14)
jbe 0x21429d
movq 0x10(%r14), %rax
jmp 0x21428c
movq %r15, %rax
cmpq %rbx, %r12
jbe 0x21429d
movl (%rax,%rbx,8), %esi
shrl %esi
movq %r13, %rdi
callq 0x21431e
incl %ebx
jmp 0x214272
incl 0x28(%rsp)
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %rax
movl %eax, %eax
testq %r14, %r14
je 0x2142b7
movq %rax, (%r14)
jmp 0x2142ba
movq %rax, %r12
incl %edx
jmp 0x213e1f
movq %r15, %rdi
callq 0x2220
movl 0x28(%rsp), %ebx
testl %ebx, %ebx
je 0x2142ee
leaq 0x81480(%rip), %rax     # 0x295758
movl 0x7c(%rax), %edi
movq 0x8(%r13), %rcx
leaq -0xb03(%rip), %rsi      # 0x2137e3
movq %r13, %rdx
callq 0x108f2c
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c2b2(%rip), %rdi     # 0x2305b8
leaq 0x312fc(%rip), %rsi     # 0x245609
leaq 0x341c6(%rip), %rcx     # 0x2484da
movl $0x2a16, %edx           # imm = 0x2A16
callq 0x2110