// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: asg_arc_cut_length_adv(asg_t*, asg64_v*, int, float, float, unsigned int, unsigned int, unsigned int, unsigned int, ma_hit_t_alloc*, R_to_U*, unsigned int*)
void asg_arc_cut_length_adv(asg_t *g, asg64_v *in, int32_t max_ext, float len_rat, float ou_rat, uint32_t is_ou, uint32_t is_trio, 
uint32_t is_topo, uint32_t min_diff, ma_hit_t_alloc *rev, R_to_U* rI, uint32_t *max_drop_len)
{
    asg64_v tx = {0,0,0}, *b = NULL;
    uint32_t i, k, v, w, n_vtx = g->n_seq<<1, nv, nw, kv, kw, trioF = (uint32_t)-1, ntrioF = (uint32_t)-1, ol_max, ou_max, to_del, cnt = 0, mm_ol, mm_ou;
    asg_arc_t *av, *aw, *ve, *we, *vl_max, *wl_max;

    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) continue;
                if(max_drop_len && av[i].ol >= (*max_drop_len)) continue;
                kv_push(uint64_t, *b, (((uint64_t)av[i].ol)<<32) | ((uint64_t)(av-g->arc+i)));   
            }
        }
	}

    if(rev && rI) memset(g->seq_vis, 0, g->n_seq*2*sizeof(uint8_t));
    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;

        if(is_trio) {
            if(get_arcs(g, v, NULL, 0)<=1 && get_arcs(g, w, NULL, 0)<=1) continue;///speedup
            trioF = get_tip_trio_infor(g, v^1);
            ntrioF = (trioF==FATHER? MOTHER : (trioF==MOTHER? FATHER : (uint32_t)-1));
        }

        ve = &(g->arc[(uint32_t)b->a[k]]);
        for (i = 0; i < nw; ++i) {
            if (aw[i].v == (v^1)) {
                we = &(aw[i]);
                break;
            }
        }
        ///mm_ol and mm_ou are used to make edge with long indel more easy to be cutted
        mm_ol = MIN(ve->ol, we->ol); mm_ou = MIN(ve->ou, we->ou);

        for (i = kv = ol_max = ou_max = 0, /**ve =**/ vl_max = NULL; i < nv; ++i) {
            if(av[i].del) continue;
            kv++;
            if(is_trio && get_tip_trio_infor(g, av[i].v) == ntrioF) continue;
            if(ol_max < av[i].ol) ol_max = av[i].ol, vl_max = &(av[i]);
            if(ou_max < av[i].ou) ou_max = av[i].ou;
        }
        if (kv < 1) continue;
        if (kv >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
            if (is_ou && mm_ou > ou_max*ou_rat) continue;
            if ((mm_ol + min_diff) > ol_max) continue;
        }
        

        for (i = kw = ol_max = ou_max = 0, wl_max = NULL; i < nw; ++i) {
            if(aw[i].del) continue;
            kw++;
            if(is_trio && get_tip_trio_infor(g, aw[i].v) == ntrioF) continue;
            if(ol_max < aw[i].ol) ol_max = aw[i].ol, wl_max = &(aw[i]);
            if(ou_max < aw[i].ou) ou_max = aw[i].ou;
        }
        if (kw < 1) continue;
        if (kw >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
            if (is_ou && mm_ou > ou_max*ou_rat) continue;
            if ((mm_ol + min_diff) > ol_max) continue;
        }

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

        to_del = 0;
        if(is_topo) {
            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(rev && rI) {
            if((to_del == 0) && vl_max && (ve->v!=vl_max->v) && (trans_path_check(ve->v, vl_max->v, g, rev, rI, max_ext, b)==0)) {
                to_del = 1;
            }
            if((to_del == 0) && wl_max && (we->v!=wl_max->v) && (trans_path_check(we->v, wl_max->v, g, rev, rI, max_ext, b)==0)) {
                to_del = 1;
            }
            if(vl_max && wl_max) assert(ve->v!=vl_max->v||we->v!=wl_max->v);
        }
        

        if (to_del) {
            ve->del = we->del = 1, ++cnt;
        }
    }
    // 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 $0xc8, %rsp
movl %r9d, 0x80(%rsp)
movl %r8d, 0x54(%rsp)
movl %ecx, 0x70(%rsp)
movss %xmm1, 0x6c(%rsp)
movss %xmm0, 0x74(%rsp)
movl %edx, 0x4c(%rsp)
movq 0x118(%rsp), %r11
xorps %xmm0, %xmm0
leaq 0xb0(%rsp), %r10
movaps %xmm0, (%r10)
andq $0x0, 0x10(%r10)
movl 0x14(%rdi), %r14d
addl %r14d, %r14d
testq %rsi, %rsi
movq %rsi, 0x90(%rsp)
cmovneq %rsi, %r10
andq $0x0, (%r10)
xorl %r15d, %r15d
pushq $0x18
popq %r12
movq %rdi, 0x28(%rsp)
movq %r10, 0x20(%rsp)
cmpq %r14, %r15
je 0x1a9c06
movq 0x20(%rdi), %rax
movl %r15d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1a9ae9
movq 0x30(%rdi), %rax
cmpb $0x0, (%rax,%r15)
je 0x1a9aee
incq %r15
jmp 0x1a9ac6
movq 0x28(%rdi), %rax
movq (%rax,%r15,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1a9ae9
movq 0x8(%rdi), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %r13
movl %eax, %r9d
imulq $0x18, %r9, %rax
leaq (%rcx,%rdx), %rbp
addq $0xc, %rbp
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rax
je 0x1a9b3d
movl (%rbp,%rdx), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %esi
addq $0x18, %rdx
jmp 0x1a9b23
cmpl $0x2, %esi
jb 0x1a9ae9
xorl %ebx, %ebx
movq %r15, 0x60(%rsp)
movq %r9, 0x30(%rsp)
cmpq %rbx, %r9
je 0x1a9ae9
movl (%rbp), %esi
testl %esi, %esi
js 0x1a9bfa
testq %r11, %r11
je 0x1a9b6c
cmpl (%r11), %esi
jae 0x1a9bfa
movq (%r10), %r8
movq 0x10(%r10), %r9
cmpq 0x8(%r10), %r8
jne 0x1a9bcf
leaq (%r8,%r8), %rsi
testq %r8, %r8
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r10)
shlq $0x3, %rsi
movq %r9, %rdi
movq %r13, %r15
movq %r12, %r13
movq %r14, %r12
movq %r11, %r14
callq 0x2370
movq %r14, %r11
movq %r12, %r14
movq %r13, %r12
movq %r15, %r13
movq 0x60(%rsp), %r15
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %rdi
movq %rax, %r9
movq %rax, 0x10(%r10)
movl (%rbp), %esi
movq 0x8(%rdi), %rcx
movq (%r10), %r8
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
shlq $0x20, %rsi
movq %r13, %rax
subq %rcx, %rax
cqto
idivq %r12
addq %rbx, %rax
orq %rsi, %rax
leaq 0x1(%r8), %rdx
movq %rdx, (%r10)
movq %rax, (%r9,%r8,8)
movq 0x30(%rsp), %r9
incq %rbx
addq $0x18, %rbp
jmp 0x1a9b4e
cmpq $0x0, 0x108(%rsp)
setne %al
cmpq $0x0, 0x110(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0x1f(%rsp)
cmpb $0x1, %cl
jne 0x1a9c43
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rdi
movl 0x14(%rax), %edx
addl %edx, %edx
xorl %esi, %esi
callq 0x20e0
movq 0x20(%rsp), %r10
movq 0x10(%r10), %rdi
movq (%r10), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq 0x20(%rsp), %r9
movq $0x0, 0x78(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0x60(%rsp)
xorl %r13d, %r13d
movq 0x28(%rsp), %r8
movl %r13d, %r13d
cmpq %r13, (%r9)
jbe 0x1aa1f3
movq 0x8(%r8), %r14
movq 0x10(%r9), %rax
movl (%rax,%r13,8), %eax
imulq $0x18, %rax, %rdx
cmpl $0x0, 0xc(%r14,%rdx)
js 0x1a9cdc
movq (%r14,%rdx), %rsi
movq 0x20(%r8), %rcx
movq %rsi, %rdi
shrq $0x21, %rdi
cmpl $0x0, (%rcx,%rdi,8)
js 0x1a9cdc
movl 0x8(%r14,%rdx), %edi
movl %edi, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x1a9cdc
shrq $0x20, %rsi
movq %rdi, %rbp
xorq $0x1, %rbp
movq 0x28(%r8), %rcx
movq (%rcx,%rsi,8), %r11
movq (%rcx,%rbp,8), %rbx
movl %ebx, %ecx
orl %r11d, %ecx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
jne 0x1a9ce7
movq %r10, %rcx
incl %r13d
movq %rcx, %r10
jmp 0x1a9c70
movq %rdi, 0x88(%rsp)
cmpl $0x0, 0x54(%rsp)
je 0x1a9d8a
movq %r11, 0x58(%rsp)
movq %r10, 0x30(%rsp)
movq %r8, %rdi
movq %rsi, %r12
xorl %edx, %edx
xorl %ecx, %ecx
movq %r8, %r15
callq 0x1a5422
cmpl $0x1, %eax
ja 0x1a9d3d
movq %r15, %rdi
movl %ebp, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5422
cmpl $0x2, %eax
jae 0x1a9d3d
movq 0x30(%rsp), %rcx
movq %r15, %r8
movq 0x20(%rsp), %r9
jmp 0x1a9cdf
movl %r12d, %esi
xorl $0x1, %esi
movq %r15, %rdi
callq 0x728c0
xorl %ecx, %ecx
cmpb $0x2, %al
sete %cl
cmpb $0x1, %al
leal -0x1(%rcx,%rcx), %ecx
pushq $0x2
popq %rax
cmovel %eax, %ecx
movl %ecx, 0x60(%rsp)
movq 0x8(%r15), %rcx
movq 0x20(%rsp), %r9
movq 0x10(%r9), %rax
movq %r13, 0x38(%rsp)
movl (%rax,%r13,8), %eax
movq %r15, %r8
movq %r12, %rsi
movq 0x30(%rsp), %r10
movq 0x58(%rsp), %r11
jmp 0x1a9d92
movq %r13, 0x38(%rsp)
movq %r14, %rcx
movq %r11, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %r12
addq %r14, %r12
movq %rbx, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%r14,%rdx), %rdi
movq %rdi, 0xa8(%rsp)
imulq $0x18, %rax, %rdi
addq %rcx, %rdi
movl %ebx, %r13d
leaq 0x1(%r13), %rax
addq %r14, %rdx
addq $-0x18, %rdx
decq %rax
je 0x1a9de1
movl 0x20(%rdx), %ecx
xorl %esi, %ecx
addq $0x18, %rdx
cmpl $0x1, %ecx
jne 0x1a9dcc
jmp 0x1a9de4
movq %r10, %rdx
movq %rsi, 0x98(%rsp)
movl 0xc(%rdi), %eax
movq %rax, 0x40(%rsp)
movl 0xc(%rdx), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
movl %ecx, 0x84(%rsp)
movq %rdi, 0xa0(%rsp)
movzwl 0x10(%rdi), %eax
movl %eax, 0x58(%rsp)
movq %rdx, 0x10(%rsp)
movzwl 0x10(%rdx), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movl %eax, 0x50(%rsp)
movl %r11d, %ebx
xorl %esi, %esi
xorl %r14d, %r14d
xorl %r15d, %r15d
movq $0x0, 0x30(%rsp)
subq $0x1, %rbx
jb 0x1a9ec6
movl 0xc(%r12), %eax
testl %eax, %eax
js 0x1a9ebd
incl %esi
cmpl $0x0, 0x54(%rsp)
je 0x1a9e91
movq %rsi, %rbp
movl 0x8(%r12), %esi
movq %r8, %rdi
callq 0x728c0
movzbl %al, %eax
cmpl %eax, 0x60(%rsp)
jne 0x1a9e7f
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq %rbp, %rsi
jmp 0x1a9ebd
movl 0xc(%r12), %eax
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq %rbp, %rsi
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %eax, %r14d
cmovbel %eax, %r14d
movq 0x30(%rsp), %rax
cmovbq %r12, %rax
movq %rax, 0x30(%rsp)
movzwl 0x10(%r12), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cmpl %eax, %r15d
cmovbel %eax, %r15d
addq $0x18, %r12
jmp 0x1a9e3b
movq 0x40(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x84(%rsp), %ecx
cmpl %ecx, %eax
cmovael %ecx, %eax
movq %rax, 0x40(%rsp)
movl 0x58(%rsp), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movl 0x50(%rsp), %ecx
cmpw %cx, %ax
cmovael %ecx, %eax
testl %esi, %esi
je 0x1aa021
movzwl %ax, %edi
cmpl $0x1, %esi
movq 0xa8(%rsp), %rcx
jne 0x1a9fd9
movl %edi, 0x50(%rsp)
xorl %edi, %edi
xorl %r15d, %r15d
xorl %r10d, %r10d
xorl %r12d, %r12d
subq $0x1, %r13
jb 0x1a9fbd
movl 0xc(%rcx), %eax
testl %eax, %eax
js 0x1a9fb4
incl %edi
cmpl $0x0, 0x54(%rsp)
je 0x1a9f94
movl %r10d, 0x58(%rsp)
movq %rdi, %r14
movq %rcx, %rbp
movq %rsi, %rbx
movl 0x8(%rcx), %esi
movq %r8, %rdi
callq 0x728c0
movzbl %al, %eax
cmpl %eax, 0x60(%rsp)
jne 0x1a9f79
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq %rbx, %rsi
movq %rbp, %rcx
movq %r14, %rdi
movl 0x58(%rsp), %r10d
jmp 0x1a9fb4
movl 0xc(%rbp), %eax
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq %rbx, %rsi
movq %rbp, %rcx
movq %r14, %rdi
movl 0x58(%rsp), %r10d
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %eax, %r15d
cmovbel %eax, %r15d
cmovbq %rcx, %r12
movzwl 0x10(%rcx), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cmpl %eax, %r10d
cmovbel %eax, %r10d
addq $0x18, %rcx
jmp 0x1a9f1f
testl %edi, %edi
je 0x1aa021
cmpl $0x1, %edi
movq 0x38(%rsp), %r13
jne 0x1aa030
cmpl $0x1, %esi
je 0x1aa026
movb $0x1, %dl
xorl %ecx, %ecx
jmp 0x1aa07e
cvtsi2ssl 0x40(%rsp), %xmm0
cvtsi2ss %r14d, %xmm1
mulss 0x74(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1aa021
cmpl $0x0, 0x70(%rsp)
je 0x1aa00a
cvtsi2ss %edi, %xmm0
cvtsi2ss %r15d, %xmm1
mulss 0x6c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1aa021
movl 0x100(%rsp), %eax
movq 0x40(%rsp), %rdx
addl %edx, %eax
cmpl %r14d, %eax
jbe 0x1a9f10
movq 0x38(%rsp), %r13
movq 0x10(%rsp), %rcx
jmp 0x1a9cdf
cvtsi2ssl 0x40(%rsp), %xmm0
cvtsi2ss %r15d, %xmm1
mulss 0x74(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1aa026
cmpl $0x0, 0x70(%rsp)
je 0x1aa063
cvtsi2ssl 0x50(%rsp), %xmm0
cvtsi2ss %r10d, %xmm1
mulss 0x6c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1aa026
movl 0x100(%rsp), %eax
movq 0x40(%rsp), %rcx
addl %eax, %ecx
cmpl %r15d, %ecx
ja 0x1aa026
cmpl $0x1, %esi
sete %cl
xorl %edx, %edx
movb $0x1, %al
cmpl $0x0, 0x80(%rsp)
movq 0xa0(%rsp), %r14
movq 0x98(%rsp), %r10
je 0x1aa0b4
cmpl $0x1, %esi
setne %sil
cmpl $0x1, %edi
setne %dil
testb %dil, %sil
je 0x1aa0bd
xorl %eax, %eax
pushq $0x1
popq %rcx
jmp 0x1aa0b6
xorl %ecx, %ecx
movq 0x30(%rsp), %r15
jmp 0x1aa104
testb %dl, %dl
movq 0x30(%rsp), %r15
jne 0x1aa0d6
testb %cl, %cl
je 0x1aa102
xorl $0x1, %r10d
movq %r10, 0x88(%rsp)
movq %r8, %rdi
movq 0x88(%rsp), %rsi
movl 0x4c(%rsp), %ebx
movl %ebx, %edx
callq 0x71a0e
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %r8
xorl %ecx, %ecx
cmpl %ebx, %eax
setge %al
setl %cl
jmp 0x1aa104
xorl %ecx, %ecx
cmpb $0x0, 0x1f(%rsp)
je 0x1aa1cc
testq %r15, %r15
setne %dl
andb %dl, %al
cmpb $0x1, %al
jne 0x1aa15b
movl 0x8(%r14), %edi
movl 0x8(%r15), %esi
xorl %ecx, %ecx
cmpl %esi, %edi
je 0x1aa15b
movq %r9, (%rsp)
movq %r8, %rdx
movq 0x108(%rsp), %rcx
movq 0x110(%rsp), %r8
movl 0x4c(%rsp), %r9d
callq 0x1a7e3d
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %r8
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
testl %ecx, %ecx
jne 0x1aa1a9
testq %r12, %r12
je 0x1aa1a9
movq 0x10(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x8(%r12), %esi
xorl %ecx, %ecx
cmpl %esi, %edi
je 0x1aa1a9
movq %r9, (%rsp)
movq %r8, %rdx
movq 0x108(%rsp), %rcx
movq 0x110(%rsp), %r8
movl 0x4c(%rsp), %r9d
callq 0x1a7e3d
movq 0x20(%rsp), %r9
movq 0x28(%rsp), %r8
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
testq %r15, %r15
je 0x1aa1cc
testq %r12, %r12
je 0x1aa1cc
movl 0x8(%r14), %eax
cmpl 0x8(%r15), %eax
jne 0x1aa1cc
movq 0x10(%rsp), %rax
movl 0x8(%rax), %eax
cmpl 0x8(%r12), %eax
je 0x1aa231
testl %ecx, %ecx
je 0x1aa026
movq 0x10(%rsp), %rcx
orb $-0x80, 0xf(%rcx)
orb $-0x80, 0xf(%r14)
movq 0x78(%rsp), %rax
incl %eax
movq %rax, 0x78(%rsp)
jmp 0x1a9cdf
cmpq $0x0, 0x90(%rsp)
jne 0x1aa210
movq 0xc0(%rsp), %rdi
callq 0x2220
movq 0x28(%rsp), %r8
cmpl $0x0, 0x78(%rsp)
je 0x1aa21f
movq %r8, %rdi
callq 0x694eb
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4d5a4(%rip), %rdi     # 0x1f77dc
leaq 0x4d29a(%rip), %rsi     # 0x1f74d9
leaq 0x4d6fb(%rip), %rcx     # 0x1f7941
movl $0x6fa, %edx            # imm = 0x6FA
callq 0x2110