// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movl %r8d, 0x20(%rsp)
movl %ecx, 0x24(%rsp)
movss %xmm0, 0x1c(%rsp)
movl %edx, 0x18(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
movl 0x14(%rdi), %eax
addl %eax, %eax
testq %rsi, %rsi
je 0x1acefe
andq $0x0, (%r12)
movl %eax, %edi
xorl %r15d, %r15d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
pushq $0x18
popq %r11
xorl %r8d, %r8d
movq $0x0, (%rsp)
xorl %r9d, %r9d
movq %rbx, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
cmpq %rdi, %r15
je 0x1ad0b3
movq 0x20(%rbx), %rax
movl %r15d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1acf48
movq 0x30(%rbx), %rax
cmpb $0x0, (%rax,%r15)
je 0x1acf4d
incq %r15
jmp 0x1acf25
movq 0x28(%rbx), %rax
movq (%rax,%r15,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1acf48
movq 0x8(%rbx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %r14
movl %eax, %eax
movq %rax, 0x8(%rsp)
imulq $0x18, %rax, %rax
leaq (%rcx,%rdx), %rbp
addq $0xc, %rbp
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rcx, %rax
je 0x1acf9c
movl (%rbp,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x18, %rcx
jmp 0x1acf86
cmpl $0x2, %edx
jb 0x1acf48
xorl %r13d, %r13d
movq %r14, 0x30(%rsp)
movq 0x8(%rsp), %rax
cmpq %r13, %rax
je 0x1ad0a9
cmpl $0x0, (%rbp)
js 0x1ad09d
cmpw $0x0, 0x4(%rbp)
js 0x1ad09d
testq %r12, %r12
je 0x1acffc
movq (%r12), %rax
cmpq 0x8(%r12), %rax
jne 0x1ad057
movq %r9, %r14
movq %r8, %rbx
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r12)
movq 0x10(%r12), %rdi
jmp 0x1ad01d
cmpq (%rsp), %r8
jne 0x1ad057
leaq (%r8,%r8), %rsi
movq %r8, %rbx
testq %r8, %r8
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, (%rsp)
movq %r9, %r14
movq %r9, %rdi
shlq $0x3, %rsi
callq 0x2370
testq %r12, %r12
je 0x1ad03d
movq %rax, 0x10(%r12)
movq %rbx, %r8
movq 0x10(%rsp), %rbx
movq %r14, %r9
jmp 0x1ad048
movq %rax, %r9
movq %rbx, %r8
movq 0x10(%rsp), %rbx
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
pushq $0x18
popq %r11
movq 0x30(%rsp), %r14
movl (%rbp), %ecx
andl %r10d, %ecx
movq %r14, %rax
subq 0x8(%rbx), %rax
cqto
idivq %r11
testq %r12, %r12
je 0x1ad081
movq (%r12), %rdx
movq 0x10(%r12), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, (%r12)
jmp 0x1ad08a
movq %r8, %rdx
incq %r8
movq %r9, %rsi
shlq $0x20, %rcx
addq %r13, %rax
orq %rcx, %rax
movq %rax, (%rsi,%rdx,8)
movq 0x8(%rsp), %rax
incq %r13
addq $0x18, %rbp
jmp 0x1acfae
movq 0x28(%rsp), %rdi
jmp 0x1acf48
movq %r9, 0x8(%rsp)
movq %r9, %rdi
movq %r8, (%rsp)
movq %r8, %rax
testq %r12, %r12
je 0x1ad0d0
movq (%r12), %rax
movq 0x10(%r12), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
xorl %r14d, %r14d
movl $0x0, 0x28(%rsp)
movq (%rsp), %r9
movq 0x8(%rsp), %rdi
movl %r14d, %r14d
testq %r12, %r12
je 0x1ad106
cmpq %r14, (%r12)
jbe 0x1ad3a0
movq 0x10(%r12), %rax
jmp 0x1ad112
movq %rdi, %rax
cmpq %r14, %r9
jbe 0x1ad39b
movq 0x8(%rbx), %rcx
movl (%rax,%r14,8), %eax
imulq $0x18, %rax, %rax
cmpl $0x0, 0xc(%rcx,%rax)
js 0x1ad332
movq %rdi, %rax
testq %r12, %r12
je 0x1ad136
movq 0x10(%r12), %rax
movq %rdi, %rdx
testq %r12, %r12
je 0x1ad143
movq 0x10(%r12), %rdx
movl (%rax,%r14,8), %eax
imulq $0x18, %rax, %rax
movq (%rcx,%rax), %rsi
movq 0x20(%rbx), %r8
movq %rsi, %rax
shrq $0x21, %rax
cmpl $0x0, (%r8,%rax,8)
js 0x1ad332
movl (%rdx,%r14,8), %eax
imulq $0x18, %rax, %rax
movl 0x8(%rcx,%rax), %eax
movl %eax, %edx
shrl %edx
cmpl $0x0, (%r8,%rdx,8)
js 0x1ad332
shrq $0x20, %rsi
movq %rax, %rdx
xorq $0x1, %rdx
movq 0x28(%rbx), %r10
movq (%r10,%rsi,8), %r8
movq (%r10,%rdx,8), %r11
movl %r11d, %edx
orl %r8d, %edx
testl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
je 0x1ad332
movq %r8, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %r10
addq %rcx, %r10
movq %r11, %rdx
movq %r11, 0x30(%rsp)
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq %rcx, %rdx
movl %r8d, %r11d
xorl %ecx, %ecx
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %ebp, %ebp
subq $0x1, %r11
jb 0x1ad210
movl 0xc(%r10), %r13d
testl %r13d, %r13d
js 0x1ad20a
cmpl %eax, 0x8(%r10)
cmoveq %r10, %rbp
incl %ecx
movzwl 0x10(%r10), %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
cmpl %r15d, %r9d
cmovbel %r15d, %r9d
cmpl %r13d, %r8d
cmovbel %r13d, %r8d
addq $0x18, %r10
jmp 0x1ad1d7
testl %ecx, %ecx
je 0x1ad2db
cmpl $0x1, %ecx
movq 0x8(%rsp), %rdi
je 0x1ad26f
cmpl $0x0, 0x24(%rsp)
je 0x1ad24f
movzwl 0x10(%rbp), %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
cvtsi2ss %r10d, %xmm0
movq 0x8(%rsp), %rdi
cvtsi2ss %r9d, %xmm1
mulss 0x1c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1ad266
movl 0xc(%rbp), %r9d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andl %r10d, %r9d
addl 0x20(%rsp), %r9d
cmpl %r8d, %r9d
jbe 0x1ad26f
movq (%rsp), %r9
jmp 0x1ad332
movl 0x30(%rsp), %r10d
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %ebx, %ebx
xorl %r13d, %r13d
subq $0x1, %r10
jb 0x1ad2c0
movl 0xc(%rdx), %r11d
testl %r11d, %r11d
js 0x1ad2ba
movl 0x8(%rdx), %r15d
xorl %esi, %r15d
cmpl $0x1, %r15d
cmoveq %rdx, %r13
incl %r9d
movzwl 0x10(%rdx), %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
cmpl %r15d, %r8d
cmovbel %r15d, %r8d
cmpl %r11d, %ebx
cmovbel %r11d, %ebx
addq $0x18, %rdx
jmp 0x1ad27f
testl %r9d, %r9d
je 0x1ad2e6
cmpl $0x1, %r9d
movq 0x8(%rsp), %rdi
jne 0x1ad2f1
cmpl $0x1, %ecx
jne 0x1ad33a
movq (%rsp), %r9
jmp 0x1ad32d
movq (%rsp), %r9
movq 0x8(%rsp), %rdi
jmp 0x1ad332
movq (%rsp), %r9
movq 0x8(%rsp), %rdi
jmp 0x1ad32d
cmpl $0x0, 0x24(%rsp)
movq (%rsp), %r9
je 0x1ad31a
movzwl 0x10(%r13), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cvtsi2ss %eax, %xmm0
cvtsi2ss %r8d, %xmm1
mulss 0x1c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1ad32d
movl 0xc(%r13), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
addl 0x20(%rsp), %eax
cmpl %ebx, %eax
jbe 0x1ad361
movq 0x10(%rsp), %rbx
incl %r14d
jmp 0x1ad0ed
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl %eax, %esi
movl 0x18(%rsp), %r15d
movl %r15d, %edx
callq 0x71a0e
movq 0x8(%rsp), %rdi
cmpl %r15d, %eax
movq (%rsp), %r9
jge 0x1ad332
jmp 0x1ad38c
cmpl $0x1, %ecx
movq 0x10(%rsp), %rbx
jne 0x1ad38c
xorl $0x1, %esi
movq %rbx, %rdi
movl 0x18(%rsp), %r15d
movl %r15d, %edx
callq 0x71a0e
movq 0x8(%rsp), %rdi
movq (%rsp), %r9
cmpl %r15d, %eax
jge 0x1ad332
orb $-0x80, 0xf(%r13)
orb $-0x80, 0xf(%rbp)
incl 0x28(%rsp)
jmp 0x1ad332
callq 0x2220
movl 0x28(%rsp), %ebp
testl %ebp, %ebp
je 0x1ad3b0
movq %rbx, %rdi
callq 0x694eb
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq