// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rcx, %r14
movl %edx, 0x54(%rsp)
movl %esi, %r12d
movq %rdi, 0x18(%rsp)
movq 0x8(%rdi), %r8
addq %r8, %r8
testq %rcx, %rcx
je 0x1c603c
andq $0x0, (%r14)
andl $-0x2, %r8d
xorl %ebp, %ebp
leaq 0x48(%rsp), %r13
xorl %r15d, %r15d
movq $0x0, 0x28(%rsp)
movq $0x0, (%rsp)
movq %r14, 0x38(%rsp)
movl %r12d, 0x44(%rsp)
movq %r8, 0x10(%rsp)
cmpq %r8, %rbp
je 0x1c61f8
movq 0x18(%rsp), %rax
movq (%rax), %rbx
movl %ebp, %eax
shrl %eax
imulq $0x78, %rax, %rax
cmpb $0x0, 0x70(%rbx,%rax)
jne 0x1c61f0
movl %ebp, %ecx
notl %ecx
andl $0x1, %ecx
leaq (%rbx,%rax), %rdx
imulq $0x18, %rcx, %rsi
movq 0x20(%rsi,%rdx), %rcx
movl 0x10(%rsi,%rdx), %edx
shlq $0x5, %rdx
addq $0x20, %rdx
xorl %esi, %esi
leaq 0x20(%rsi), %rdi
cmpq %rdi, %rdx
je 0x1c60c8
cmpl $0x0, 0xc(%rcx,%rsi)
movq %rdi, %rsi
js 0x1c60b0
jmp 0x1c61f0
movq %r15, 0x8(%rsp)
xorl %r14d, %r14d
cmpb $0x0, 0x71(%rbx,%rax)
setne %r14b
movl 0x4(%rbx,%rax), %r15d
movq %rbp, %rsi
subl $0x1, %r12d
jb 0x1c611b
xorl $0x1, %esi
movq 0x18(%rsp), %rdi
movq %r13, %rdx
callq 0x1c6687
testl %eax, %eax
jne 0x1c611b
movq 0x48(%rsp), %rsi
movl %esi, %eax
shrl %eax
imulq $0x78, %rax, %rax
addl 0x4(%rbx,%rax), %r15d
cmpb $0x0, 0x71(%rbx,%rax)
pushq $0x1
popq %rax
cmovnel %eax, %r14d
jmp 0x1c60e1
movl 0x44(%rsp), %r12d
cmpl %r12d, %r15d
ja 0x1c6164
testl %r14d, %r14d
movq 0x10(%rsp), %r8
jne 0x1c615a
movq 0x38(%rsp), %r14
testq %r14, %r14
je 0x1c6175
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x1c61b4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
jmp 0x1c6198
movq 0x38(%rsp), %r14
jmp 0x1c61eb
movq 0x38(%rsp), %r14
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %r8
jmp 0x1c61f0
movq 0x8(%rsp), %rax
cmpq 0x28(%rsp), %rax
jne 0x1c61c1
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x28(%rsp)
movq (%rsp), %rdi
shlq $0x3, %rsi
callq 0x2370
testq %r14, %r14
je 0x1c61c7
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x10(%rsp), %r8
jmp 0x1c61b8
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x1c61e0
movq (%rsp), %rax
jmp 0x1c61d0
movq %rax, (%rsp)
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
incq %rdx
movq %rdx, 0x8(%rsp)
shlq $0x20, %r15
orq %rbp, %r15
movq %r15, (%rax,%rcx,8)
movq 0x8(%rsp), %r15
incq %rbp
jmp 0x1c606a
movq (%rsp), %rdi
movq %r15, %rax
testq %r14, %r14
je 0x1c620b
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
xorl %ebx, %ebx
movl $0x0, 0x40(%rsp)
movl %ebx, %ebx
testq %r14, %r14
je 0x1c6234
cmpq %rbx, (%r14)
jbe 0x1c6645
movq 0x10(%r14), %rax
jmp 0x1c6241
movq (%rsp), %rax
cmpq %rbx, %r15
jbe 0x1c663c
movq (%rax,%rbx,8), %r13
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl %r13d, %ecx
shrl %ecx
imulq $0x78, %rcx, %rbp
cmpb $0x0, 0x70(%rax,%rbp)
jne 0x1c6635
movl %r13d, %ecx
notl %ecx
andl $0x1, %ecx
addq %rbp, %rax
imulq $0x18, %rcx, %rdx
movq 0x20(%rdx,%rax), %rcx
movl 0x10(%rdx,%rax), %eax
shlq $0x5, %rax
addq $0x20, %rax
xorl %edx, %edx
leaq 0x20(%rdx), %rsi
cmpq %rsi, %rax
je 0x1c629b
cmpl $0x0, 0xc(%rcx,%rdx)
movq %rsi, %rdx
js 0x1c6283
jmp 0x1c6635
testq %r14, %r14
movq %rbx, 0x10(%rsp)
je 0x1c62cf
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x1c62f0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
movq %rcx, 0x30(%rsp)
movl %ecx, 0x24(%rsp)
jmp 0x1c632c
cmpq 0x28(%rsp), %r15
jne 0x1c62ff
testq %r15, %r15
je 0x1c630f
leaq (%r15,%r15), %rsi
movq %r15, 0x30(%rsp)
movl %r15d, 0x24(%rsp)
movq %rsi, 0x28(%rsp)
jmp 0x1c6328
movq 0x10(%r14), %rax
movq %rcx, 0x30(%rsp)
movl %ecx, 0x24(%rsp)
jmp 0x1c6341
movl %r15d, 0x24(%rsp)
movq %r15, 0x30(%rsp)
movq (%rsp), %rax
jmp 0x1c6353
movq $0x0, 0x30(%rsp)
pushq $0x2
popq %rsi
movq %rsi, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movq (%rsp), %rdi
shlq $0x3, %rsi
callq 0x2370
testq %r14, %r14
je 0x1c634f
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq %r15, 0x8(%rsp)
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x1c635f
movq %rax, (%rsp)
movq %r15, %rcx
leaq 0x1(%r15), %rdx
movq %rdx, 0x8(%rsp)
movl %r13d, %edx
movq %rdx, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
movq (%rax), %r14
xorl %r15d, %r15d
cmpb $0x0, 0x71(%r14,%rbp)
setne %r15b
movl 0x4(%r14,%rbp), %ebp
subl $0x1, %r12d
jb 0x1c6470
xorl $0x1, %r13d
movq 0x18(%rsp), %rdi
movl %r13d, %esi
leaq 0x48(%rsp), %rdx
callq 0x1c6687
testl %eax, %eax
jne 0x1c6470
movq 0x48(%rsp), %r13
movl %r13d, %eax
shrl %eax
imulq $0x78, %rax, %rbx
addl 0x4(%r14,%rbx), %ebp
movq 0x38(%rsp), %r14
testq %r14, %r14
je 0x1c63e6
movq (%r14), %rdx
cmpq 0x8(%r14), %rdx
jne 0x1c6425
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
jmp 0x1c6409
movq 0x8(%rsp), %rax
cmpq 0x28(%rsp), %rax
jne 0x1c6435
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x28(%rsp)
movq (%rsp), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x48(%rsp), %rcx
testq %r14, %r14
je 0x1c643e
movq %rax, 0x10(%r14)
movq (%r14), %rdx
jmp 0x1c642c
movq 0x10(%r14), %rax
movq %r13, %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
jmp 0x1c6452
movq %r13, %rcx
movq (%rsp), %rax
jmp 0x1c6442
movq %rax, (%rsp)
movq 0x8(%rsp), %rsi
movq %rsi, %rdx
incq %rsi
movq %rsi, 0x8(%rsp)
movq %rcx, (%rax,%rdx,8)
movq 0x18(%rsp), %rax
movq (%rax), %r14
cmpb $0x0, 0x71(%r14,%rbx)
pushq $0x1
popq %rax
cmovnel %eax, %r15d
jmp 0x1c6380
movl 0x44(%rsp), %r12d
cmpl %r12d, %ebp
ja 0x1c64c9
testl %r15d, %r15d
movq 0x38(%rsp), %r14
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %r13
jne 0x1c64bf
cmpl $0x0, 0x54(%rsp)
movq 0x8(%rsp), %r15
movl 0x24(%rsp), %ebp
je 0x1c64e2
movl %r13d, %eax
testq %r14, %r14
je 0x1c65c7
movq (%r14), %rcx
cmpq %rcx, %rax
jae 0x1c65ed
movq 0x10(%r14), %rdx
jmp 0x1c65d3
movq 0x8(%rsp), %r15
jmp 0x1c6625
movq 0x38(%rsp), %r14
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %r13
jmp 0x1c6625
movl %ebp, %ebx
movl %ebx, %eax
incl %ebx
testq %r14, %r14
je 0x1c64fb
movq (%r14), %rcx
cmpq %rbx, %rcx
jbe 0x1c6528
movq 0x10(%r14), %rcx
jmp 0x1c6504
movq (%rsp), %rcx
cmpq %rbx, %r15
jbe 0x1c653c
movl (%rcx,%rax,8), %esi
movl (%rcx,%rbx,8), %edx
movq 0x18(%rsp), %rdi
callq 0x1dee3b
testq %rax, %rax
je 0x1c6668
cmpl $0x1, 0x10(%rax)
jbe 0x1c64e4
jmp 0x1c6620
cmpq %rax, %rcx
jbe 0x1c65b7
movq 0x10(%r14), %rcx
movq 0x10(%rsp), %rbx
jmp 0x1c654a
movq (%rsp), %rcx
cmpq %rax, %r15
movq 0x10(%rsp), %rbx
jbe 0x1c65c4
movq (%rsp), %rdx
testq %r14, %r14
je 0x1c6557
movq 0x10(%r14), %rdx
movq 0x18(%rsp), %rsi
movq (%rsi), %rsi
movq (%rcx,%rax,8), %rcx
movl %ecx, %edi
shrl %edi
andl $0x1, %ecx
imulq $0x78, %rdi, %rdi
addq %rsi, %rdi
imulq $0x18, %rcx, %rcx
movq 0x20(%rcx,%rdi), %rcx
movq (%rdx,%rax,8), %rax
movl %eax, %edx
shrl %edx
andl $0x1, %eax
imulq $0x78, %rdx, %rdx
addq %rsi, %rdx
imulq $0x18, %rax, %rax
movl 0x10(%rax,%rdx), %eax
shlq $0x5, %rax
xorl %edx, %edx
cmpq %rdx, %rax
je 0x1c649e
cmpl $0x0, 0xc(%rcx,%rdx)
js 0x1c65b1
cmpl $0x1, 0x10(%rcx,%rdx)
ja 0x1c6625
addq $0x20, %rdx
jmp 0x1c659a
movl %r13d, %eax
movq 0x10(%rsp), %rbx
jmp 0x1c64ad
movl %r13d, %eax
movq (%rsp), %rdx
movq %r15, %rcx
cmpq %r15, %rax
jae 0x1c65ed
movl -0x8(%rdx,%rcx,8), %esi
movq 0x18(%rsp), %rdi
movsd 0x1cfa4(%rip), %xmm0    # 0x1e3588
callq 0x1c5f30
testl %eax, %eax
je 0x1c6625
movl %ebp, %ebx
testq %r14, %r14
je 0x1c65ff
cmpq %rbx, (%r14)
jbe 0x1c661c
movq 0x10(%r14), %rax
jmp 0x1c6608
movq (%rsp), %rax
cmpq %rbx, %r15
jbe 0x1c661c
movl (%rax,%rbx,8), %esi
shrl %esi
movq 0x18(%rsp), %rdi
callq 0x1c676d
leal 0x1(%rbx), %ebp
jmp 0x1c65ed
incl 0x40(%rsp)
movq 0x10(%rsp), %rbx
movl %r13d, %eax
testq %r14, %r14
je 0x1c6632
movq %rax, (%r14)
jmp 0x1c6635
movq %rax, %r15
incl %ebx
jmp 0x1c621e
movq (%rsp), %rdi
callq 0x2220
movl 0x40(%rsp), %ebx
testl %ebx, %ebx
je 0x1c6657
movq 0x18(%rsp), %rdi
callq 0x1c5ce9
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bf49(%rip), %rdi     # 0x1e25b8
leaq 0x30e63(%rip), %rsi     # 0x1f74d9
leaq 0x33e1c(%rip), %rcx     # 0x1fa499
movl $0x2a16, %edx           # imm = 0x2A16
callq 0x2110