// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: asg_arc_cut_complex_bub_links(asg_t*, asg64_v*, float, float, unsigned int, bub_label_t*)
void asg_arc_cut_complex_bub_links(asg_t *g, asg64_v *in, float len_rat, float ou_rat, uint32_t is_ou, bub_label_t *b_mask_t)
{
    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, me, mu, cnt = 0, bn;
    asg_arc_t *av, *aw;

    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));  
        }
    }

    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; 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;
            }
            if(kw < 2) break;

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

        if(i < nv) continue;

        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);
            kv_push(uint64_t, *b, ((uint64_t)(av-g->arc+i)));
        }
        // b->a[cnt++] = v;
    }

    if(b->n > bn) {
        asg_arc_identify_simple_bubbles_multi(g, b_mask_t, 0);
        for (k = bn, cnt = 0; k < b->n; k++) {
            // if(g->arc[b->a[k]].del) continue;
            v = g->arc[b->a[k]].ul>>32; w = g->arc[b->a[k]].v; 
            if(g->seq_vis[v] || g->seq_vis[v^1] || g->seq_vis[w] || g->seq_vis[w^1]) {
                cnt++; continue;
            }
            g->arc[b->a[k]].del = 0; asg_arc_del(g, g->arc[b->a[k]].v^1, (g->arc[b->a[k]].ul>>32)^1, 0);
        }
    }
    // stats_sysm(g);
    if(!in) free(tx.a);
    if(cnt > 0) asg_cleanup(g);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x48(%rsp)
movl %edx, 0x38(%rsp)
movss %xmm1, 0x34(%rsp)
movss %xmm0, 0x3c(%rsp)
movq %rsi, %r14
movl 0x14(%rdi), %eax
addl %eax, %eax
testq %rsi, %rsi
je 0x1ac34a
andq $0x0, (%r14)
movl %eax, %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
movq $0x0, 0x20(%rsp)
xorl %ebp, %ebp
movq %rdi, (%rsp)
cmpq %r15, %r13
je 0x1ac488
movq 0x20(%rdi), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1ac38e
movq 0x30(%rdi), %rax
cmpb $0x0, (%rax,%r13)
je 0x1ac393
incq %r13
jmp 0x1ac36b
movq 0x28(%rdi), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1ac38e
movq 0x8(%rdi), %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 %ebx, %ebx
cmpq %rdx, %rax
je 0x1ac3e7
movl (%rcx,%rdx), %r9d
xorl %r8d, %r8d
testl %r9d, %r9d
cmovsl %r12d, %r9d
setns %r8b
addl %r8d, %esi
addl %r9d, %ebx
addq $0x18, %rdx
jmp 0x1ac3c4
cmpl $0x2, %esi
jb 0x1ac38e
testq %r14, %r14
je 0x1ac412
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x1ac44f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
jmp 0x1ac434
movq 0x8(%rsp), %rax
cmpq 0x20(%rsp), %rax
jne 0x1ac45c
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x20(%rsp)
movq %rbp, %rdi
shlq $0x3, %rsi
callq 0x2370
testq %r14, %r14
je 0x1ac461
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq (%rsp), %rdi
jmp 0x1ac453
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x1ac478
movq %rbp, %rax
jmp 0x1ac468
movq %rax, %rbp
movq (%rsp), %rdi
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
incq %rdx
movq %rdx, 0x8(%rsp)
shlq $0x20, %rbx
orq %r13, %rbx
movq %rbx, (%rax,%rcx,8)
jmp 0x1ac38e
testq %r14, %r14
je 0x1ac4a5
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movl (%r14), %r15d
movq %rbp, %r13
jmp 0x1ac4c3
movq %rbp, %r13
movq 0x8(%rsp), %rbx
leaq (,%rbx,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x1a3def
movq %rbx, %r15
movl %r15d, %eax
movq %rax, 0x58(%rsp)
xorl %ecx, %ecx
movq (%rsp), %rdi
movq %r15, 0x18(%rsp)
cmpq 0x58(%rsp), %rcx
je 0x1ac777
movq %r13, %rax
testq %r14, %r14
je 0x1ac4ed
movq 0x10(%r14), %rax
movq %rcx, 0x70(%rsp)
movq (%rax,%rcx,8), %r12
movq 0x20(%rdi), %rax
movl %r12d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1ac679
movq %r13, 0x10(%rsp)
movq 0x28(%rdi), %rcx
movl %r12d, %eax
movq %rcx, 0x68(%rsp)
movq (%rcx,%rax,8), %r13
testl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
je 0x1ac540
movq 0x8(%rdi), %rbx
movl %r12d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5422
cmpl $0x2, %eax
jae 0x1ac54a
movq (%rsp), %rdi
movq 0x10(%rsp), %r13
jmp 0x1ac679
movq %r13, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
movq %rax, 0x50(%rsp)
addq %rbx, %rax
movq %rax, 0x28(%rsp)
movl %r13d, %eax
movq %rax, 0x40(%rsp)
leaq 0x10(%rbx), %rax
movq %rax, 0x60(%rsp)
xorl %edx, %edx
movq (%rsp), %rdi
movq %rbx, %r15
cmpq 0x40(%rsp), %rdx
je 0x1ac686
imulq $0x18, %rdx, %rsi
movq 0x28(%rsp), %rax
movl 0xc(%rax,%rsi), %r13d
testl %r13d, %r13d
js 0x1ac663
addq 0x28(%rsp), %rsi
movl 0x8(%rsi), %eax
xorq $0x1, %rax
movq 0x68(%rsp), %rcx
movq (%rcx,%rax,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1ac66f
movq %rax, %rcx
shrq $0x20, %rcx
movl %eax, %eax
imulq $0x18, %rax, %r9
imulq $0x18, %rcx, %r10
addq 0x60(%rsp), %r10
xorl %eax, %eax
xorl %ecx, %ecx
pushq $-0x1
popq %r8
movl %r8d, %r11d
cmpq %rax, %r9
je 0x1ac61b
movl -0x4(%r10,%rax), %ebp
testl %ebp, %ebp
js 0x1ac615
incl %ecx
movl -0x8(%r10,%rax), %ebx
xorl %r12d, %ebx
cmpl $0x1, %ebx
je 0x1ac615
cmpl %r11d, %ebp
cmovbl %ebp, %r11d
movzwl (%r10,%rax), %ebx
andl $0x3fff, %ebx           # imm = 0x3FFF
cmpl %ebx, %r8d
cmovael %ebx, %r8d
addq $0x18, %rax
jmp 0x1ac5df
cmpl $0x2, %ecx
jb 0x1ac66b
cvtsi2ss %r13d, %xmm0
movl %r11d, %eax
cvtsi2ss %rax, %xmm1
mulss 0x3c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1ac66b
cmpl $0x0, 0x38(%rsp)
movq (%rsp), %rdi
je 0x1ac663
movzwl 0x10(%rsi), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cvtsi2ss %eax, %xmm0
movl %r8d, %eax
cvtsi2ss %rax, %xmm1
mulss 0x34(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1ac66f
incq %rdx
jmp 0x1ac57c
movq (%rsp), %rdi
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %r15
movq 0x70(%rsp), %rcx
incq %rcx
jmp 0x1ac4d6
movq 0x50(%rsp), %rax
leaq (%r15,%rax), %rbx
addq $0xc, %rbx
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
cmpq %r12, 0x40(%rsp)
je 0x1ac674
movl (%rbx), %eax
testl %eax, %eax
js 0x1ac76b
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rbx)
movl -0x8(%rbx), %edx
movl -0x4(%rbx), %esi
xorl $0x1, %esi
xorl $0x1, %edx
pushq $0x1
popq %rcx
callq 0x1abbde
testq %r14, %r14
je 0x1ac6ed
movq (%r14), %rax
cmpq 0x8(%r14), %rax
jne 0x1ac726
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
jmp 0x1ac70f
movq 0x8(%rsp), %rax
cmpq 0x20(%rsp), %rax
jne 0x1ac726
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x20(%rsp)
movq %r13, %rdi
shlq $0x3, %rsi
callq 0x2370
testq %r14, %r14
je 0x1ac723
movq %rax, 0x10(%r14)
jmp 0x1ac726
movq %rax, %r13
movq (%rsp), %rdi
movq 0x28(%rsp), %rax
subq 0x8(%rdi), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
testq %r14, %r14
je 0x1ac750
movq (%r14), %rcx
movq 0x10(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
jmp 0x1ac764
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
leaq 0x1(%rdx), %rsi
movq %r13, %rdx
movq %rsi, 0x8(%rsp)
addq %r12, %rax
movq %rax, (%rdx,%rcx,8)
incq %r12
addq $0x18, %rbx
jmp 0x1ac69b
movl %r15d, %eax
testq %r14, %r14
je 0x1ac789
cmpq %rax, (%r14)
ja 0x1ac796
jmp 0x1ac886
xorl %ebx, %ebx
cmpq %rax, 0x8(%rsp)
jbe 0x1ac863
xorl %ebx, %ebx
movq 0x48(%rsp), %rsi
xorl %edx, %edx
callq 0x6ef72
movq (%rsp), %rdi
movl %r15d, %r15d
testq %r14, %r14
je 0x1ac7bf
cmpq %r15, (%r14)
jbe 0x1ac86f
movq 0x10(%r14), %rax
jmp 0x1ac7cd
movq %r13, %rax
cmpq %r15, 0x8(%rsp)
jbe 0x1ac863
movq %r13, %rcx
testq %r14, %r14
je 0x1ac7d9
movq 0x10(%r14), %rcx
imulq $0x18, (%rax,%r15,8), %rdx
movq 0x8(%rdi), %rax
movl 0x4(%rax,%rdx), %esi
movq 0x30(%rdi), %rdx
cmpb $0x0, (%rdx,%rsi)
jne 0x1ac813
xorq $0x1, %rsi
cmpb $0x0, (%rdx,%rsi)
jne 0x1ac813
imulq $0x18, (%rcx,%r15,8), %rcx
movl 0x8(%rax,%rcx), %ecx
cmpb $0x0, (%rdx,%rcx)
jne 0x1ac813
xorq $0x1, %rcx
cmpb $0x0, (%rdx,%rcx)
je 0x1ac81a
incl %ebx
incl %r15d
jmp 0x1ac7a8
movq %r13, %rcx
testq %r14, %r14
je 0x1ac826
movq 0x10(%r14), %rcx
imulq $0x18, (%rcx,%r15,8), %rcx
andl $0x7fffffff, 0xc(%rax,%rcx) # imm = 0x7FFFFFFF
movq %r13, %rax
testq %r14, %r14
je 0x1ac83f
movq 0x10(%r14), %rax
movq 0x8(%rdi), %rcx
imulq $0x18, (%rax,%r15,8), %rax
movl 0x4(%rcx,%rax), %edx
movl 0x8(%rcx,%rax), %esi
xorl $0x1, %esi
xorl $0x1, %edx
xorl %ecx, %ecx
callq 0x1abbde
movq (%rsp), %rdi
jmp 0x1ac815
movq %r13, %rdi
callq 0x2220
movq (%rsp), %rdi
testl %ebx, %ebx
je 0x1ac886
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x694eb
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq