// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: asg_arc_cut_bub_links(asg_t*, asg64_v*, float, float, float, unsigned int, unsigned long, ma_hit_t_alloc*, R_to_U*, int)
void asg_arc_cut_bub_links(asg_t *g, asg64_v *in, float len_rat, float sec_len_rat, float ou_rat, uint32_t is_ou, uint64_t check_dist, ma_hit_t_alloc *rev, R_to_U* rI, int32_t max_ext)
{
    asg64_v tx = {0,0,0}, *b = NULL;
    uint32_t v, w, t, k, i, n_vtx = g->n_seq<<1, nv, nw, kv, kw, kol, bn, me, mu, cnt = 0, sec_check;
    asg_arc_t *av, *aw, *ref;
    buf_t x; memset(&x, 0, sizeof(x)); x.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t));

    if(in) b = in;
    else b = &tx;
	b->n = 0;

    for (v = 0; v < n_vtx; ++v) {
        if (g->seq[v>>1].del) continue;
        if(g->seq_vis[v] == 0) {
            av = asg_arc_a(g, v); nv = asg_arc_n(g, v);
            if (nv < 2) continue;

            for (i = kv = kol = 0; i < nv; ++i) {
                if(av[i].del) continue;
                kv++; kol += av[i].ol; 
            }
            if(kv < 2) continue;//must have at least one exact and one inexact

            kv_push(uint64_t, *b, ((((uint64_t)(kol))<<32) | v));  
        }
    }

    if(rev && rI) memset(g->seq_vis, 0, g->n_seq*2*sizeof(uint8_t));
    radix_sort_srt64(b->a, b->a + b->n); bn = b->n;
    for (k = 0; k < bn; k++) {
        v = (uint32_t)b->a[k];
        if (g->seq[v>>1].del) continue;
        nv = asg_arc_n(g, v); av = asg_arc_a(g, v);
        if (nv < 2 || get_arcs(g, v, NULL, 0) < 2) continue;

        for (i = 0, b->n = bn, sec_check = 0; i < nv; i++) {
            if (av[i].del) continue;

            w = av[i].v^1; nw = asg_arc_n(g, w); aw = asg_arc_a(g, w);
            if(nw < 2) break;

            for (t = kw = 0, me = mu = (uint32_t)-1; t < nw; t++) {
                if(aw[t].del) continue;
                kw++; 
                if(aw[t].v == (v^1)) continue;//note: me is the shortest edge except aw[t], so here is continue
                if(aw[t].ol < me) me = aw[t].ol;
                if(aw[t].ou < mu) mu = aw[t].ou;
                kv_push(uint64_t, *b, ((uint64_t)(aw-g->arc+t)));                
            }
            if(kw < 2) break;

            if(av[i].ol > me*len_rat && av[i].ol > me*sec_len_rat) break;
            if(av[i].ol > me*len_rat) sec_check++;
            if(is_ou && av[i].ou > mu*ou_rat) break;
        }

        if(i < nv) continue;

        if(sec_check) {
            for (i = 0, ref = NULL; i < nv; i++) {//forward
                if (av[i].del) continue;
                if(!ref) {
                    ref = &(av[i]);
                } else {
                    if(trans_path_check(ref->v, av[i].v, g, rev, rI, max_ext, b)!=1) break;
                }
            }

            if(i < nv) {
                if (b->n < bn + 2) continue;///less than two edges
                for (i = bn, ref = NULL; i < b->n; i++) {
                    if(g->arc[b->a[i]].del) continue;
                    if(get_arcs(g, g->arc[b->a[i]].ul>>32, NULL, 0)!=2) break;
                    if(!ref) {
                        ref = &(g->arc[b->a[i]]);
                    } else {
                        if(trans_path_check(ref->v, g->arc[b->a[i]].v, g, rev, rI, max_ext, b)!=1) break;
                    }
                }
                if(i < b->n) continue;
            }
        }

        if(if_false_bub_links(v, g, &x, b, bn, check_dist)) {
            for (i = 0; i < nv; ++i) {
                if (av[i].del) continue;
                av[i].del = 1; asg_arc_del(g, av[i].v^1, (av[i].ul>>32)^1, 1);
            }
            cnt++;
        }
    }

    // stats_sysm(g);
    if(!in) free(tx.a);
    free(x.a); free(x.S.a); free(x.T.a); free(x.b.a); free(x.e.a);
    if(cnt > 0) asg_cleanup(g);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x98(%rsp)
movl %edx, 0x54(%rsp)
movss %xmm2, 0x50(%rsp)
movss %xmm1, 0x4c(%rsp)
movss %xmm0, 0x58(%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
xorps %xmm0, %xmm0
leaq 0x120(%rsp), %r15
movaps %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movl 0x14(%rdi), %r14d
addl %r14d, %r14d
movups %xmm0, 0x110(%rsp)
movups %xmm0, 0x100(%rsp)
movups %xmm0, 0xf0(%rsp)
movups %xmm0, 0xe0(%rsp)
movups %xmm0, 0xd0(%rsp)
movups %xmm0, 0xc0(%rsp)
pushq $0x1c
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %r15, %r9
testq %rbx, %rbx
movq %rbx, 0x90(%rsp)
cmovneq %rbx, %r9
movq %rax, 0xb8(%rsp)
andq $0x0, (%r9)
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %r9, 0x8(%rsp)
cmpq %r14, %r15
je 0x1abdc0
movq 0x20(%rbp), %rax
movl %r15d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1abd16
movq 0x30(%rbp), %rax
cmpb $0x0, (%rax,%r15)
je 0x1abd1b
incq %r15
jmp 0x1abcf3
movq 0x28(%rbp), %rax
movq (%rax,%r15,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1abd16
movq 0x8(%rbp), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
imulq $0x18, %rax, %rax
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
addq $0xc, %rcx
xorl %edx, %edx
xorl %esi, %esi
xorl %r12d, %r12d
cmpq %rdx, %rax
je 0x1abd6d
movl (%rcx,%rdx), %edi
xorl %r8d, %r8d
testl %edi, %edi
cmovsl %ebx, %edi
setns %r8b
addl %r8d, %esi
addl %edi, %r12d
addq $0x18, %rdx
jmp 0x1abd4d
cmpl $0x2, %esi
jb 0x1abd16
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x1abda9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
shlq $0x20, %r12
orq %r15, %r12
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
movq %r12, (%rax,%rcx,8)
jmp 0x1abd16
cmpq $0x0, 0x28(%rsp)
sete %al
cmpq $0x0, 0x30(%rsp)
sete %cl
orb %al, %cl
jne 0x1abdeb
movq 0x30(%rbp), %rdi
movl 0x14(%rbp), %edx
addl %edx, %edx
xorl %esi, %esi
callq 0x20e0
movq 0x8(%rsp), %r9
movq 0x10(%r9), %rdi
movq (%r9), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movl %eax, %edx
movq %rdx, 0x68(%rsp)
movq %rax, 0x60(%rsp)
addl $0x2, %eax
movq %rax, 0x88(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x1c(%rsp)
movq %rbp, 0x40(%rsp)
cmpq 0x68(%rsp), %r12
je 0x1ac29c
movq 0x10(%rcx), %r13
movq (%r13,%r12,8), %rsi
movq 0x20(%rbp), %rax
movl %esi, %edx
shrl %edx
cmpl $0x0, (%rax,%rdx,8)
js 0x1abe7d
movq 0x28(%rbp), %rax
movl %esi, %edx
movq (%rax,%rdx,8), %rbx
testl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
je 0x1abe7d
movq 0x8(%rbp), %r14
movq %rbp, %rdi
movq %rsi, 0x10(%rsp)
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5422
cmpl $0x2, %eax
jae 0x1abe82
movq 0x8(%rsp), %rcx
incq %r12
jmp 0x1abe2a
movq %rbx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
movq %rax, 0xa0(%rsp)
leaq (%r14,%rax), %r15
movq 0x8(%rsp), %rcx
movq 0x68(%rsp), %rax
movq %rax, (%rcx)
movl %ebx, %eax
movq %rax, 0x38(%rsp)
xorl %esi, %esi
movq %r14, %r8
xorl %r9d, %r9d
movq 0x10(%rsp), %rdi
movq %r14, 0xa8(%rsp)
movq %r13, %r10
movq %r12, 0x78(%rsp)
movq %r15, 0x70(%rsp)
cmpq 0x38(%rsp), %rsi
je 0x1ac0b7
imulq $0x18, %rsi, %r11
cmpl $0x0, 0xc(%r15,%r11)
js 0x1ac0af
addq %r15, %r11
movl 0x8(%r11), %eax
xorq $0x1, %rax
movq 0x28(%rbp), %rdx
movq (%rdx,%rax,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1abe7d
movq %r11, 0xb0(%rsp)
movl %r9d, 0x5c(%rsp)
movq %rsi, 0x80(%rsp)
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%r8,%rdx), %r11
movl %eax, %r12d
leaq (%r8,%rdx), %r14
addq $0x10, %r14
xorl %ebx, %ebx
xorl %ebp, %ebp
pushq $-0x1
popq %rax
movl %eax, %r13d
cmpq %rbx, %r12
je 0x1ac009
movl -0x4(%r14), %edx
testl %edx, %edx
js 0x1abf5f
incl %ebp
movl -0x8(%r14), %esi
xorl %edi, %esi
cmpl $0x1, %esi
jne 0x1abf67
movl %eax, %r15d
jmp 0x1abffa
cmpl %r13d, %edx
cmovbl %edx, %r13d
movzwl (%r14), %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
cmpl %r15d, %eax
cmovbl %eax, %r15d
movq (%rcx), %r9
cmpq 0x8(%rcx), %r9
jne 0x1abfde
leaq (%r9,%r9), %rsi
testq %r9, %r9
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rcx)
shlq $0x3, %rsi
movq %r10, %rdi
movl %ebp, 0x20(%rsp)
movq %r12, %rbp
movl %r13d, %r12d
movq %r11, %r13
callq 0x2370
movq %r13, %r11
movl %r12d, %r13d
movq %rbp, %r12
movl 0x20(%rsp), %ebp
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rcx
movq %rax, %r10
movq %rax, 0x10(%rcx)
movq 0x40(%rsp), %rax
movq 0x8(%rax), %r8
movq (%rcx), %r9
movq %r11, %rax
subq %r8, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
addq %rbx, %rax
leaq 0x1(%r9), %rdx
movq %rdx, (%rcx)
movq %rax, (%r10,%r9,8)
incq %rbx
addq $0x18, %r14
movl %r15d, %eax
jmp 0x1abf41
cmpl $0x2, %ebp
jb 0x1ac127
movq 0xb0(%rsp), %r11
movl 0xc(%r11), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
cvtsi2ss %edx, %xmm0
movl %r13d, %edx
cvtsi2ss %rdx, %xmm2
movaps %xmm2, %xmm1
mulss 0x58(%rsp), %xmm1
ucomiss %xmm1, %xmm0
movq 0x40(%rsp), %rbp
movq 0x78(%rsp), %r12
jbe 0x1ac058
mulss 0x4c(%rsp), %xmm2
ucomiss %xmm2, %xmm0
ja 0x1abe7d
xorl %edx, %edx
ucomiss %xmm1, %xmm0
seta %dl
movl 0x5c(%rsp), %r9d
addl %edx, %r9d
cmpl $0x0, 0x54(%rsp)
je 0x1ac0a2
movzwl 0x10(%r11), %edx
andl $0x3fff, %edx           # imm = 0x3FFF
cvtsi2ss %edx, %xmm0
movl %eax, %eax
cvtsi2ss %rax, %xmm1
mulss 0x50(%rsp), %xmm1
ucomiss %xmm1, %xmm0
movq 0x70(%rsp), %r15
movq 0x80(%rsp), %rsi
jbe 0x1ac0af
jmp 0x1abe7d
movq 0x70(%rsp), %r15
movq 0x80(%rsp), %rsi
incq %rsi
jmp 0x1abecf
testl %r9d, %r9d
je 0x1ac20c
movq 0x38(%rsp), %rbx
xorl %r14d, %r14d
subq $0x1, %rbx
jb 0x1ac20c
cmpl $0x0, 0xc(%r15)
js 0x1ac11b
movq %r15, %rdx
testq %r14, %r14
je 0x1ac11e
movl 0x8(%r14), %edi
movl 0x8(%r15), %esi
movq %rcx, (%rsp)
movq %rbp, %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movl 0x170(%rsp), %r9d
callq 0x1a7e3d
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rcx
movq %r14, %rdx
cmpl $0x1, %eax
je 0x1ac11e
jmp 0x1ac136
movq %r14, %rdx
addq $0x18, %r15
movq %rdx, %r14
jmp 0x1ac0c8
movq 0x40(%rsp), %rbp
movq 0x78(%rsp), %r12
jmp 0x1abe7d
movq (%rcx), %r14
cmpq 0x88(%rsp), %r14
jb 0x1abe7d
movq 0x60(%rsp), %rbx
movq $0x0, 0x20(%rsp)
movl %ebx, %ebx
cmpq %rbx, %r14
jbe 0x1ac203
movq %r12, %r15
movq 0x8(%rbp), %r12
movq 0x10(%rcx), %rax
imulq $0x18, (%rax,%rbx,8), %r13
cmpl $0x0, 0xc(%r12,%r13)
js 0x1ac1da
movl 0x4(%r12,%r13), %esi
movq %rbp, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5422
cmpl $0x2, %eax
jne 0x1ac1f6
movq 0x20(%rsp), %rax
testq %rax, %rax
movq 0x8(%rsp), %rcx
je 0x1ac1df
movl 0x8(%rax), %edi
movl 0x8(%r12,%r13), %esi
movq %rcx, (%rsp)
movq %rbp, %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movl 0x170(%rsp), %r9d
callq 0x1a7e3d
movq 0x8(%rsp), %rcx
movq (%rcx), %r14
cmpl $0x1, %eax
movq %r15, %r12
movq 0x10(%rsp), %rdi
je 0x1ac1ef
jmp 0x1ac203
movq %r15, %r12
jmp 0x1ac1ef
addq %r13, %r12
movq %r12, 0x20(%rsp)
movq %r15, %r12
movq 0x10(%rsp), %rdi
incl %ebx
jmp 0x1ac155
movq 0x8(%rsp), %rcx
movq %r15, %r12
movq 0x10(%rsp), %rdi
cmpq %rbx, %r14
ja 0x1abe7d
movq %rbp, %rsi
leaq 0xb8(%rsp), %rdx
movq 0x60(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x1ab907
testl %eax, %eax
je 0x1abe78
imulq $0x18, 0x38(%rsp), %rbx
movq 0xa8(%rsp), %rax
movq 0xa0(%rsp), %rcx
leaq (%rax,%rcx), %r14
addq $0xc, %r14
xorl %r15d, %r15d
movq 0x8(%rsp), %rcx
cmpq %r15, %rbx
je 0x1ac293
movl (%r14,%r15), %eax
testl %eax, %eax
js 0x1ac28d
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%r14,%r15)
movl -0x8(%r14,%r15), %edx
movl -0x4(%r14,%r15), %esi
xorl $0x1, %esi
xorl $0x1, %edx
movq %rbp, %rdi
pushq $0x1
popq %rcx
callq 0x1abbde
movq 0x8(%rsp), %rcx
addq $0x18, %r15
jmp 0x1ac257
incl 0x1c(%rsp)
jmp 0x1abe7d
cmpq $0x0, 0x90(%rsp)
jne 0x1ac2b4
movq 0x130(%rsp), %rdi
callq 0x2220
movq 0xb8(%rsp), %rdi
callq 0x2220
movq 0xd0(%rsp), %rdi
callq 0x2220
movq 0xe8(%rsp), %rdi
callq 0x2220
movq 0x100(%rsp), %rdi
callq 0x2220
movq 0x118(%rsp), %rdi
callq 0x2220
cmpl $0x0, 0x1c(%rsp)
je 0x1ac304
movq %rbp, %rdi
callq 0x694eb
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq