// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: asg_cut_large_indel(asg_t*, asg64_v*, int, float, unsigned int, unsigned int)
uint32_t asg_cut_large_indel(asg_t *g, asg64_v *in, int32_t max_ext, float ou_rat, uint32_t is_ou, uint32_t min_diff)
{
    asg64_v tx = {0,0,0}, *b = NULL;
    uint32_t v, w, n_vtx = g->n_seq<<1, i, k, kv, kw, nv, nw, ou_max, ol_max, to_del, cnt = 0;
    asg_arc_t *av, *aw, *ve, *we;
    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 = 0; i < nv; ++i) {
                if(av[i].del) continue;
                kv++;
            }
            if(kv < 2) continue;

            for (i = 0; i < nv; ++i) {
                if(av[i].del || av[i].no_l_indel) continue;
                ///means there is a large indel at this edge
                kv_push(uint64_t, *b, (((uint64_t)av[i].ol)<<32) | ((uint64_t)(av-g->arc+i)));   
            }
        }
    }

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

        v = g->arc[(uint32_t)b->a[k]].ul>>32; w = g->arc[(uint32_t)b->a[k]].v^1;
        if(g->seq[v>>1].del || g->seq[w>>1].del) continue;
        nv = asg_arc_n(g, v); nw = asg_arc_n(g, w);
        av = asg_arc_a(g, v); aw = asg_arc_a(g, w);
        if(nv<=1 && nw <= 1) continue;

        for (i = kv = ou_max = ol_max = 0, ve = NULL; i < nv; ++i) {
            if(av[i].del) continue;
            if(av[i].v == (w^1)) ve = &(av[i]);
            kv++;
            if(ou_max < av[i].ou) ou_max = av[i].ou;
            if(ol_max < av[i].ol) ol_max = av[i].ol;
        }
        if (kv < 1) continue;
        if (kv >= 2) {
            if (is_ou && ve->ou > ou_max*ou_rat) continue;
            if ((ve->ol + min_diff) > ol_max) continue;
        }
        

        for (i = kw = ou_max = ol_max = 0, we = NULL; i < nw; ++i) {
            if(aw[i].del) continue;
            if(aw[i].v == (v^1)) we = &(aw[i]);
            kw++;
            if(ou_max < aw[i].ou) ou_max = aw[i].ou;
            if(ol_max < aw[i].ol) ol_max = aw[i].ol;
        }
        if (kw < 1) continue;
        if (kw >= 2) {
            if (is_ou && we->ou > ou_max*ou_rat) continue;
            if ((we->ol + min_diff) > ol_max) continue;
        }

        if (kv <= 1 && kw <= 1) continue;

        to_del = 0;
        if (kv > 1 && kw > 1) {
            to_del = 1;
        } else if (kw == 1) {
            if (asg_topocut_aux(g, w^1, max_ext) < max_ext) to_del = 1;
        } else if (kv == 1) {
            if (asg_topocut_aux(g, v^1, max_ext) < max_ext) to_del = 1;
        }

        if (to_del) {
            ve->del = we->del = 1, ++cnt;
        }
    }
    // stats_sysm(g);
    if(!in) free(tx.a);
    if (cnt > 0) asg_cleanup(g);
    return cnt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, 0x28(%rsp)
movl %ecx, 0x2c(%rsp)
movss %xmm0, 0x24(%rsp)
movl %edx, 0x3c(%rsp)
movq %rsi, %r15
movl 0x14(%rdi), %eax
addl %eax, %eax
testq %rsi, %rsi
je 0x1edb65
movq $0x0, (%r15)
testl %eax, %eax
movq %rdi, 0x8(%rsp)
je 0x1edd43
movl %eax, %r11d
xorl %r12d, %r12d
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %r9d, %r9d
xorl %eax, %eax
xorl %r10d, %r10d
movq %r11, 0x40(%rsp)
movq 0x20(%rdi), %rcx
movl %r12d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x1edba5
movq 0x30(%rdi), %rcx
cmpb $0x0, (%rcx,%r12)
je 0x1edbb8
movq %rax, %rbx
incq %r12
movq %rbx, %rax
cmpq %r11, %r12
jne 0x1edb8b
jmp 0x1edd49
movq 0x28(%rdi), %rcx
movq (%rcx,%r12,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0x1edba5
movq 0x8(%rdi), %rdx
movq %rcx, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %r8
movq %r8, 0x30(%rsp)
movl %ecx, %r8d
leaq (%rdx,%rsi,8), %rbp
addq $0xc, %rbp
xorl %ecx, %ecx
movq %r8, 0x10(%rsp)
movq %r8, %rdx
movq %rbp, %rsi
movl (%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %ecx
addq $0x18, %rsi
decq %rdx
jne 0x1edbf8
cmpl $0x2, %ecx
jb 0x1edba5
xorl %r13d, %r13d
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rbp)
js 0x1edc66
cmpw $0x0, 0x4(%rbp)
js 0x1edc66
movq %r10, %rcx
testq %r15, %r15
je 0x1edc33
movq (%r15), %rcx
movq %rax, %rdx
testq %r15, %r15
je 0x1edc3f
movq 0x8(%r15), %rdx
cmpq %rdx, %rcx
jne 0x1edc6e
movq %rax, %rcx
testq %r15, %r15
je 0x1edc50
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x1edc73
movq %rax, %rsi
testq %r15, %r15
je 0x1edc61
movq 0x8(%r15), %rsi
addq %rsi, %rsi
jmp 0x1edc78
movq %rax, %rbx
jmp 0x1edd2b
movq %rax, %rbx
jmp 0x1edccf
movl $0x2, %esi
movq %rsi, %rbx
testq %r15, %r15
je 0x1edc87
movq %rsi, 0x8(%r15)
movq %rax, %rbx
movq %r10, %r14
movq %r9, 0x18(%rsp)
movq %r9, %rdi
testq %r15, %r15
je 0x1edc9b
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
testq %r15, %r15
je 0x1edcb9
movq %rax, 0x10(%r15)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r9
jmp 0x1edcc1
movq %rax, %r9
movq 0x8(%rsp), %rdi
movq %r14, %r10
movq 0x40(%rsp), %r11
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl (%rbp), %eax
andl %r14d, %eax
movq 0x30(%rsp), %rcx
subq 0x8(%rdi), %rcx
movq %r9, %rdx
testq %r15, %r15
je 0x1edcea
movq 0x10(%r15), %rdx
movq %r10, %rsi
testq %r15, %r15
je 0x1edcf5
movq (%r15), %rsi
leaq 0x1(%rsi), %r8
testq %r15, %r15
je 0x1edd03
movq %r8, (%r15)
jmp 0x1edd06
movq %r8, %r10
shlq $0x20, %rax
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %r8 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r8, %rcx
addq %r13, %rcx
orq %rax, %rcx
movq %rcx, (%rdx,%rsi,8)
movq 0x10(%rsp), %rcx
incq %r13
addq $0x18, %rbp
movq %rbx, %rax
cmpq %r13, %rcx
jne 0x1edc1b
jmp 0x1edba8
xorl %r10d, %r10d
xorl %r9d, %r9d
movq %r9, %rdi
testq %r15, %r15
je 0x1edd55
movq 0x10(%r15), %rdi
movq %r9, 0x18(%rsp)
movq %r10, 0x10(%rsp)
movq %r10, %rax
testq %r15, %r15
je 0x1edd6a
movq (%r15), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
xorl %r14d, %r14d
movl $0x0, 0x30(%rsp)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r11
movq %r11, %rax
testq %r15, %r15
je 0x1edd98
movq (%r15), %rax
movl %r14d, %r14d
cmpq %r14, %rax
jbe 0x1ee03d
movq %r10, %rax
testq %r15, %r15
je 0x1eddb0
movq 0x10(%r15), %rax
movq 0x8(%rdi), %rdx
movl (%rax,%r14,8), %eax
leaq (%rax,%rax,2), %rax
cmpl $0x0, 0xc(%rdx,%rax,8)
js 0x1ee035
movq %r10, %rax
testq %r15, %r15
je 0x1eddd3
movq 0x10(%r15), %rax
movq %r10, %rcx
testq %r15, %r15
je 0x1edddf
movq 0x10(%r15), %rcx
movl (%rax,%r14,8), %eax
leaq (%rax,%rax,2), %rax
movq (%rdx,%rax,8), %rax
movq 0x20(%rdi), %r8
movq %rax, %rsi
shrq $0x21, %rsi
cmpl $0x0, (%r8,%rsi,8)
js 0x1ee035
movl (%rcx,%r14,8), %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0x8(%rdx,%rcx,8), %esi
movl %esi, %ecx
shrl %ecx
cmpl $0x0, (%r8,%rcx,8)
js 0x1ee035
shrq $0x20, %rax
movq %rsi, %r9
xorq $0x1, %r9
movq 0x28(%rdi), %r8
movq (%r8,%rax,8), %rcx
movq (%r8,%r9,8), %r13
cmpl $0x1, %ecx
ja 0x1ede42
cmpl $0x2, %r13d
jb 0x1ee035
testl %ecx, %ecx
je 0x1edead
movq %rcx, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
leaq (%rdx,%r8,8), %r9
movl %ecx, %r10d
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %r11d, %r11d
xorl %ecx, %ecx
movl 0xc(%r9), %ebx
testl %ebx, %ebx
js 0x1ede8e
cmpl %esi, 0x8(%r9)
cmoveq %r9, %r12
incl %ecx
movzwl 0x10(%r9), %ebp
andl $0x3fff, %ebp           # imm = 0x3FFF
cmpl %ebp, %r11d
cmovbel %ebp, %r11d
cmpl %ebx, %r8d
cmovbel %ebx, %r8d
addq $0x18, %r9
decq %r10
jne 0x1ede63
cvtsi2ss %r11d, %xmm0
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r11
jmp 0x1edeb8
xorps %xmm0, %xmm0
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r12d, %r12d
testl %ecx, %ecx
je 0x1ee035
cmpl $0x1, %ecx
je 0x1edf0b
cmpl $0x0, 0x2c(%rsp)
je 0x1edef0
movzwl 0x10(%r12), %r9d
andl $0x3fff, %r9d           # imm = 0x3FFF
xorps %xmm1, %xmm1
cvtsi2ss %r9d, %xmm1
mulss 0x24(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x1ee035
movl 0xc(%r12), %r9d
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
andl %ebx, %r9d
addl 0x28(%rsp), %r9d
cmpl %r8d, %r9d
ja 0x1ee035
testl %r13d, %r13d
je 0x1edf7f
movq %r13, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
leaq (%rdx,%r8,8), %r8
movl %r13d, %r9d
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r10d, %r10d
xorl %edx, %edx
movl 0xc(%r8), %r11d
testl %r11d, %r11d
js 0x1edf5d
movl 0x8(%r8), %ebx
xorl %eax, %ebx
cmpl $0x1, %ebx
cmoveq %r8, %r13
incl %edx
movzwl 0x10(%r8), %ebx
andl $0x3fff, %ebx           # imm = 0x3FFF
cmpl %ebx, %r10d
cmovbel %ebx, %r10d
cmpl %r11d, %ebp
cmovbel %r11d, %ebp
addq $0x18, %r8
decq %r9
jne 0x1edf2c
xorps %xmm0, %xmm0
cvtsi2ss %r10d, %xmm0
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r11
jmp 0x1edf89
xorps %xmm0, %xmm0
xorl %edx, %edx
xorl %ebp, %ebp
xorl %r13d, %r13d
testl %edx, %edx
je 0x1ee035
cmpl $0x1, %edx
je 0x1edfd3
cmpl $0x0, 0x2c(%rsp)
je 0x1edfbc
movzwl 0x10(%r13), %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
xorps %xmm1, %xmm1
cvtsi2ss %r8d, %xmm1
mulss 0x24(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x1ee035
movl 0xc(%r13), %r8d
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %r8d
addl 0x28(%rsp), %r8d
cmpl %ebp, %r8d
ja 0x1ee035
movl %ecx, %r9d
xorl $0x1, %r9d
movl %edx, %r8d
xorl $0x1, %r8d
orl %r9d, %r8d
je 0x1ee035
cmpl $0x1, %ecx
setne %r9b
cmpl $0x1, %edx
setne %r8b
testb %r8b, %r9b
jne 0x1ee026
cmpl $0x1, %edx
je 0x1ee008
cmpl $0x1, %ecx
jne 0x1ee035
xorl $0x1, %eax
movl %eax, %esi
movl 0x3c(%rsp), %ebx
movl %ebx, %edx
callq 0xaa77e
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rdi
cmpl %ebx, %eax
jge 0x1ee035
orb $-0x80, 0xf(%r13)
orb $-0x80, 0xf(%r12)
incl 0x30(%rsp)
incl %r14d
jmp 0x1edd8d
testq %r15, %r15
jne 0x1ee04f
movq %r10, %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
movl 0x30(%rsp), %ebx
testl %ebx, %ebx
je 0x1ee05c
callq 0xa02fa
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq