// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movss %xmm0, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x4c(%rbp)
movl $0x0, -0x74(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x31d943
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x31d94b
leaq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jae 0x31db7e
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x31d988
jmp 0x31db70
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x44(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x31db6e
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x44(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x60(%rbp)
cmpl $0x2, -0x60(%rbp)
jae 0x31d9e3
jmp 0x31db70
movl $0x0, -0x58(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x31da28
movq -0x80(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x31da14
jmp 0x31da1d
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x31d9f1
cmpl $0x2, -0x58(%rbp)
jae 0x31da33
jmp 0x31db70
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x31db6c
movq -0x80(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x31da7b
movq -0x80(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movw 0x10(%rax), %ax
shrw $0xf, %ax
cmpw $0x0, %ax
je 0x31da80
jmp 0x31db5e
jmp 0x31da82
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x40(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x31daf3
movq -0x40(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x31dab2
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0x31dac0
movl $0x2, %eax
movq %rax, -0xa0(%rbp)
jmp 0x31dac0
movq -0xa0(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x80(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
shlq $0x20, %rax
movq %rax, -0xa8(%rbp)
movq -0x80(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rcx, %rax
movl $0x18, %ecx
cqto
idivq %rcx
movq -0xa8(%rbp), %rdx
movl -0x50(%rbp), %ecx
addq %rcx, %rax
orq %rax, %rdx
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x31db5e
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x31da3a
jmp 0x31db6e
jmp 0x31db70
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x31d95d
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x40(%rbp), %rax
movq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x31e0b7
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x54(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x31dbe8
jmp 0x31e0a9
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x54(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x54(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
xorl $0x1, %eax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x31dc6f
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x48(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x31dc74
jmp 0x31e0a9
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x48(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x44(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x48(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
cmpl $0x1, -0x60(%rbp)
ja 0x31dcf8
cmpl $0x1, -0x64(%rbp)
ja 0x31dcf8
jmp 0x31e0a9
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x68(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x50(%rbp)
movq $0x0, -0x90(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x31de06
movq -0x80(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x31dd49
jmp 0x31ddf8
movq -0x80(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0x48(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0x31dd79
movq -0x80(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movl -0x68(%rbp), %eax
movq -0x80(%rbp), %rcx
movl -0x50(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movw 0x10(%rcx), %cx
andw $0x3fff, %cx            # imm = 0x3FFF
movzwl %cx, %ecx
cmpl %ecx, %eax
jae 0x31ddbf
movq -0x80(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movw 0x10(%rax), %ax
andw $0x3fff, %ax            # imm = 0x3FFF
movzwl %ax, %eax
movl %eax, -0x68(%rbp)
movl -0x6c(%rbp), %eax
movq -0x80(%rbp), %rcx
movl -0x50(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x31ddf6
movq -0x80(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x6c(%rbp)
jmp 0x31ddf8
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x31dd1f
cmpl $0x1, -0x58(%rbp)
jae 0x31de11
jmp 0x31e0a9
cmpl $0x2, -0x58(%rbp)
jb 0x31de66
cmpl $0x0, -0x1c(%rbp)
je 0x31de48
movq -0x90(%rbp), %rax
movzwl 0x10(%rax), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cvtsi2ss %eax, %xmm0
movl -0x68(%rbp), %eax
cvtsi2ss %rax, %xmm1
mulss -0x18(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x31de48
jmp 0x31e0a9
movq -0x90(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl -0x20(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jbe 0x31de64
jmp 0x31e0a9
jmp 0x31de66
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x68(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x50(%rbp)
movq $0x0, -0x98(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x64(%rbp), %eax
jae 0x31df89
movq -0x88(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x31deba
jmp 0x31df7b
movq -0x88(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0x44(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0x31def0
movq -0x88(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x68(%rbp), %eax
movq -0x88(%rbp), %rcx
movl -0x50(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movw 0x10(%rcx), %cx
andw $0x3fff, %cx            # imm = 0x3FFF
movzwl %cx, %ecx
cmpl %ecx, %eax
jae 0x31df3c
movq -0x88(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movw 0x10(%rax), %ax
andw $0x3fff, %ax            # imm = 0x3FFF
movzwl %ax, %eax
movl %eax, -0x68(%rbp)
movl -0x6c(%rbp), %eax
movq -0x88(%rbp), %rcx
movl -0x50(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x31df79
movq -0x88(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x6c(%rbp)
jmp 0x31df7b
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x31de8d
cmpl $0x1, -0x5c(%rbp)
jae 0x31df94
jmp 0x31e0a9
cmpl $0x2, -0x5c(%rbp)
jb 0x31dfe9
cmpl $0x0, -0x1c(%rbp)
je 0x31dfcb
movq -0x98(%rbp), %rax
movzwl 0x10(%rax), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cvtsi2ss %eax, %xmm0
movl -0x68(%rbp), %eax
cvtsi2ss %rax, %xmm1
mulss -0x18(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x31dfcb
jmp 0x31e0a9
movq -0x98(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl -0x20(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jbe 0x31dfe7
jmp 0x31e0a9
jmp 0x31dfe9
cmpl $0x1, -0x58(%rbp)
ja 0x31dffa
cmpl $0x1, -0x5c(%rbp)
ja 0x31dffa
jmp 0x31e0a9
movl $0x0, -0x70(%rbp)
cmpl $0x1, -0x58(%rbp)
jbe 0x31e016
cmpl $0x1, -0x5c(%rbp)
jbe 0x31e016
movl $0x1, -0x70(%rbp)
jmp 0x31e066
cmpl $0x1, -0x5c(%rbp)
jne 0x31e03c
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %esi
xorl $0x1, %esi
movl -0x14(%rbp), %edx
callq 0xdd220
cmpl -0x14(%rbp), %eax
jge 0x31e03a
movl $0x1, -0x70(%rbp)
jmp 0x31e064
cmpl $0x1, -0x58(%rbp)
jne 0x31e062
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %esi
xorl $0x1, %esi
movl -0x14(%rbp), %edx
callq 0xdd220
cmpl -0x14(%rbp), %eax
jge 0x31e060
movl $0x1, -0x70(%rbp)
jmp 0x31e062
jmp 0x31e064
jmp 0x31e066
cmpl $0x0, -0x70(%rbp)
je 0x31e0a7
movq -0x98(%rbp), %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x90(%rbp), %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x31e0a9
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x31dba8
cmpq $0x0, -0x10(%rbp)
jne 0x31e0c7
movq -0x28(%rbp), %rdi
callq 0x2210
cmpl $0x0, -0x74(%rbp)
jbe 0x31e0d6
movq -0x8(%rbp), %rdi
callq 0xcdd10
movl -0x74(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)