// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_arc_cut_length(usg_t*, asg64_v*, asg64_v*, int, float, unsigned int, unsigned int, unsigned int*)
void usg_arc_cut_length(usg_t *g, asg64_v *in_0, asg64_v *in_1, int32_t max_ext, float len_rat, uint32_t is_trio, 
uint32_t is_topo, uint32_t *max_drop_len)
{
    // if(len_rat > 0.7) {
    //     fprintf(stderr, "+[M::%s::] max_ext::%d, len_rat::%f\n", __func__, max_ext, len_rat);
    // }
    asg64_v tx = {0,0,0}, tz = {0,0,0}, *b = NULL, *ub = NULL;
    uint32_t i, k, v, w, n_vtx = g->n<<1, nv, nw, kv, kw, /**trioF = (uint32_t)-1, ntrioF = (uint32_t)-1,**/ ol_max, ou_max, to_del, cnt = 0, mm_ol;
    usg_arc_t *av, *aw, *ve, *we; uint64_t x, kocc[2], ou; uint8_t *f; CALLOC(f, g->n);
    b = ((in_0)?(in_0):(&tx)); ub = ((in_1)?(in_1):(&tz));
    
    for (v = 0, b->n = ub->n = 0; v < n_vtx; ++v) {
        if (g->a[v>>1].del) continue;
        av = usg_arc_a(g, v); nv = usg_arc_n(g, v);
        if (nv < 2) continue;
        for (i = kv = kocc[0] = kocc[1] = 0; i < nv; ++i) {
            if(av[i].del) continue;
            kv++; 
            if((av[i].ou>>1) > 0) { ///if av[i].ou == 1, ignore it
                if(kocc[1] < (av[i].ou>>1)) kocc[1] = (av[i].ou>>1);
            } else if(av[i].ou == 0) { 
                kocc[0]++;
            }            
        }
        if(kv < 2 || kocc[0] == 0 || kocc[1] == 0) continue;
        ou = kocc[1];
        for (i = 0; i < nv; ++i) {
            if(av[i].del || av[i].ou) continue;
            if(max_drop_len && av[i].ol >= (*max_drop_len)) continue;
            x = (((uint64_t)av[i].ol)*10)/ou; x <<= 32;
            kv_push(uint64_t, *b, ((x)|((uint64_t)(ub->n))));
            kv_push(uint64_t, *ub, ((((uint64_t)(v))<<32)|((uint64_t)(i))));   
        }
    }
    // if(len_rat > 0.7) {
    //     fprintf(stderr, "[M::%s::] max_ext::%d, len_rat::%f, b->n::%u, ub->n::%u\n", 
    //     __func__, max_ext, len_rat, (uint32_t)b->n, (uint32_t)ub->n);
    // }

    radix_sort_srt64(b->a, b->a + b->n);
    for (k = 0; k < b->n; k++) {
        v = ub->a[(uint32_t)b->a[k]]>>32; 
        ve = &(usg_arc_a(g, v)[(uint32_t)(ub->a[(uint32_t)b->a[k]])]);
        w = ve->v^1;
        if(ve->del || g->a[v>>1].del || g->a[w>>1].del || ve->ou) continue;
        nv = usg_arc_n(g, v); nw = usg_arc_n(g, w);
        av = usg_arc_a(g, v); aw = usg_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));
        // }    
        for (i = 0; i < nw; ++i) {
            if (aw[i].v == (v^1)) {
                we = &(aw[i]);
                break;
            }
        }
        mm_ol = MIN(ve->ol, we->ol); kocc[0] = kocc[1] = 0;

        for (i = kv = ol_max = ou_max = 0; i < nv; ++i) {
            if(av[i].del) continue;
            kv++; 
            if(av[i].ou != 1) kocc[!!(av[i].ou)]++; ///if av[i].ou == 1, ignore it
            // if(is_trio && get_tip_trio_infor(g, av[i].v) == ntrioF) continue;
            if(ol_max < av[i].ol) ol_max = av[i].ol;
        }
        if (kv < 1 || kocc[0] < 1 || kocc[1] < 1) continue;
        if (kv >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
        }

        
        for (i = kw = ol_max = ou_max = 0; 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;
        }
        if (kw < 1) continue;
        if (kw >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
        }

        if (kv <= 1 && kw <= 1) continue;
        // if(len_rat > 0.7) {
        //     fprintf(stderr, "0[M::%s::] v>>1::%u(%c), w>>1::%u(%c), kv::%u, kw::%u\n", 
        //                             __func__, v>>1, "+-"[v&1], w>>1, "+-"[w&1], kv, kw);
        // }

        to_del = 1;
        if(is_topo) {
            to_del = 0;
            if (kv > 1 && kw > 1) {
                to_del = 1;
            } else if (kw == 1) {
                if (usg_naive_topocut_aux(g, w^1, max_ext, f, b, ub) < max_ext) to_del = 1;
            } else if (kv == 1) {
                if (usg_naive_topocut_aux(g, v^1, max_ext, f, b, ub) < max_ext) to_del = 1;
            }
        } 
        // if(len_rat > 0.7) {
        //     fprintf(stderr, "1[M::%s::] v>>1::%u(%c), w>>1::%u(%c), kv::%u, kw::%u, to_del::%u\n", 
        //                             __func__, v>>1, "+-"[v&1], w>>1, "+-"[w&1], kv, kw, to_del);
        // }

        if (to_del) {
            ve->del = we->del = 1; 
            if((usg_naive_topocut_aux_sec(g, v, max_ext) < max_ext) && 
                        (usg_naive_topocut_aux_sec(g, w, max_ext) < max_ext)) {
                // if((((v>>1) == 308) && ((w>>1) == 311)) || (((w>>1) == 308) && ((v>>1) == 311))) {
                //     fprintf(stderr, "[M::%s::] v>>1::%u, v&1::%u, kv::%u, w>>1::%u, w&1::%u, kw::%u, max_ext::%d\n", 
                //     __func__, v>>1, v&1, kv, w>>1, w&1, kw, max_ext);
                // }
                // if((((v>>1) == 306) && ((w>>1) == 310)) || (((w>>1) == 306) && ((v>>1) == 310))) {
                //     fprintf(stderr, "[M::%s::] v>>1::%u, v&1::%u, kv::%u, w>>1::%u, w&1::%u, kw::%u, max_ext::%d\n", 
                //     __func__, v>>1, v&1, kv, w>>1, w&1, kw, max_ext);
                // }
                ++cnt;
            } else {
                ve->del = we->del = 0; 
            }
            
        }
        // if(len_rat > 0.7) {
        //     fprintf(stderr, "2[M::%s::] v>>1::%u(%c), w>>1::%u(%c), kv::%u, kw::%u, to_del::%u\n", 
        //                             __func__, v>>1, "+-"[v&1], w>>1, "+-"[w&1], kv, kw, to_del);
        // }
    }

    if(in_0) free(tx.a); if(in_1) free(tz.a);
    if (cnt > 0) usg_cleanup(g);
    free(f);
    // fprintf(stderr, "-[M::%s::] max_ext::%d, len_rat::%f\n", __func__, max_ext, len_rat);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %r9d, 0x28(%rsp)
movss %xmm0, 0x2c(%rsp)
movl %ecx, 0x14(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0xe0(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %r12
andq $0x0, 0x10(%r12)
leaq 0x70(%rsp), %r13
andq $0x0, 0x10(%r13)
movaps %xmm0, (%r12)
movaps %xmm0, (%r13)
movq 0x8(%rdi), %rdi
leal (%rdi,%rdi), %ebp
pushq $0x1
popq %rsi
callq 0x2120
movq 0x20(%rsp), %r9
movq %r13, %rdi
movq %r12, %r8
movq %rbp, %r10
testq %r14, %r14
movq %r14, 0x60(%rsp)
cmovneq %r14, %r8
testq %rbx, %rbx
movq %rbx, 0x68(%rsp)
cmovneq %rbx, %rdi
andq $0x0, (%rdi)
movq %rax, 0x30(%rsp)
andq $0x0, (%r8)
xorl %r13d, %r13d
movabsq $0x100000000, %r11      # imm = 0x100000000
xorl %r14d, %r14d
movq %r8, 0x8(%rsp)
movq %rdi, (%rsp)
movq %r15, 0x18(%rsp)
movq %rbp, 0x38(%rsp)
cmpq %r10, %r14
je 0x1c7063
movq (%r15), %rax
movl %r14d, %ecx
shrl %ecx
imulq $0x78, %rcx, %rcx
cmpb $0x0, 0x70(%rax,%rcx)
je 0x1c6ecf
incq %r14
addq %r11, %r13
jmp 0x1c6eab
addq %rcx, %rax
movl %r14d, %ecx
andl $0x1, %ecx
imulq $0x18, %rcx, %rcx
movq 0x10(%rax,%rcx), %rdx
testl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
je 0x1c6ec7
movq 0x20(%rax,%rcx), %r15
movl %edx, %ebp
shlq $0x5, %rbp
xorl %ecx, %ecx
xorl %eax, %eax
xorl %ebx, %ebx
xorl %edx, %edx
cmpq %rcx, %rbp
je 0x1c6f2d
cmpl $0x0, 0xc(%r15,%rcx)
js 0x1c6f27
incl %edx
movl 0x10(%r15,%rcx), %esi
cmpq $0x2, %rsi
jae 0x1c6f1e
xorl $0x1, %esi
addq %rsi, %rax
jmp 0x1c6f27
shrl %esi
cmpq %rsi, %rbx
cmovbeq %rsi, %rbx
addq $0x20, %rcx
jmp 0x1c6efc
cmpl $0x2, %edx
jb 0x1c7036
testq %rax, %rax
je 0x1c7036
testq %rbx, %rbx
je 0x1c7036
movq %r14, 0x40(%rsp)
movq %r13, 0x48(%rsp)
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x1c7040
movslq 0xc(%r15,%r12), %rax
testq %rax, %rax
js 0x1c702a
cmpl $0x0, 0x10(%r15,%r12)
jne 0x1c702a
testq %r9, %r9
je 0x1c6f86
cmpl (%r9), %eax
jae 0x1c702a
imulq $0xa, %rax, %rax
xorl %edx, %edx
divq %rbx
movq %rax, %r14
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0x1c6fcd
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %rdi
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
shlq $0x20, %r14
orq (%rdi), %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %r14, (%rax,%rcx,8)
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x1c701a
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %rdi
movq 0x8(%rsp), %r8
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq 0x20(%rsp), %r9
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %r13, (%rax,%rcx,8)
addq $0x20, %r12
incq %r13
jmp 0x1c6f55
movq 0x18(%rsp), %r15
jmp 0x1c6ec7
movq 0x18(%rsp), %r15
movq 0x38(%rsp), %r10
movq 0x48(%rsp), %r13
movabsq $0x100000000, %r11      # imm = 0x100000000
movq 0x40(%rsp), %r14
jmp 0x1c6ec7
movq %rbx, 0x58(%rsp)
movq 0x10(%r8), %rdi
movq (%r8), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq (%rsp), %r9
movq 0x8(%rsp), %r8
movl $0x0, 0x20(%rsp)
xorps %xmm2, %xmm2
xorl %r12d, %r12d
movl %r12d, %r12d
cmpq %r12, (%r8)
jbe 0x1c7349
movq 0x10(%r9), %rax
movq 0x10(%r8), %rcx
movl (%rcx,%r12,8), %ecx
movq (%rax,%rcx,8), %r13
movq %r13, %rcx
movl %r13d, %ebx
shrq $0x20, %r13
movq (%r15), %rax
shrq $0x21, %rcx
imulq $0x78, %rcx, %rcx
addq %rax, %rcx
movl %r13d, %edx
andl $0x1, %edx
imulq $0x18, %rdx, %rsi
movq 0x20(%rsi,%rcx), %rdx
shlq $0x5, %rbx
movl 0xc(%rdx,%rbx), %edi
testl %edi, %edi
js 0x1c7104
cmpb $0x0, 0x70(%rcx)
jne 0x1c7104
movl 0x8(%rdx,%rbx), %r14d
movl %r14d, %r10d
shrl %r10d
imulq $0x78, %r10, %r10
cmpb $0x0, 0x70(%rax,%r10)
jne 0x1c7104
addq %rdx, %rbx
cmpl $0x0, 0x10(%rbx)
je 0x1c710f
movq %rbp, %r14
incl %r12d
movq %r14, %rbp
jmp 0x1c708f
addq %rsi, %rcx
addq $0x10, %rcx
movq %r14, %rsi
xorq $0x1, %rsi
addq %r10, %rax
movq (%rcx), %rcx
movl %esi, %r9d
andl $0x1, %r9d
imulq $0x18, %r9, %r10
movq 0x10(%rax,%r10), %r11
movl %ecx, %r9d
orl %r11d, %r9d
testl $0xfffffffe, %r9d       # imm = 0xFFFFFFFE
jne 0x1c7150
movq %rbp, %r14
movq 0x8(%rsp), %r8
movq (%rsp), %r9
jmp 0x1c7107
movq %r14, 0x38(%rsp)
movl %ecx, %r9d
movq 0x20(%rax,%r10), %rax
movl %r11d, %ecx
leaq 0x1(%rcx), %r10
movq %rax, %r8
leaq -0x20(%rax), %r14
decq %r10
je 0x1c7183
movl 0x28(%r14), %r11d
xorl %r13d, %r11d
addq $0x20, %r14
cmpl $0x1, %r11d
jne 0x1c716b
jmp 0x1c7186
movq %rbp, %r14
movq %rsi, 0x40(%rsp)
movl 0xc(%r14), %esi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %esi
movl %esi, 0x48(%rsp)
movaps %xmm2, 0x50(%rsp)
shlq $0x5, %r9
xorl %r15d, %r15d
xorl %esi, %esi
xorl %r11d, %r11d
cmpq %r15, %r9
je 0x1c71e0
movl 0xc(%rdx,%r15), %ebp
testl %ebp, %ebp
js 0x1c71da
movl 0x10(%rdx,%r15), %r10d
cmpl $0x1, %r10d
je 0x1c71d1
xorl %eax, %eax
testl %r10d, %r10d
setne %al
incq 0x50(%rsp,%rax,8)
incl %esi
cmpl %ebp, %r11d
cmovbel %ebp, %r11d
addq $0x20, %r15
jmp 0x1c71ab
movl 0x48(%rsp), %eax
cmpl %eax, %edi
cmovbl %edi, %eax
testl %esi, %esi
je 0x1c728f
cmpq $0x0, 0x50(%rsp)
movq 0x18(%rsp), %r15
je 0x1c7145
cmpq $0x0, 0x58(%rsp)
je 0x1c7145
cvtsi2ss %eax, %xmm0
cmpl $0x2, %esi
jb 0x1c722b
cvtsi2ss %r11d, %xmm1
mulss 0x2c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1c7145
shlq $0x5, %rcx
xorl %r9d, %r9d
xorl %edi, %edi
xorl %edx, %edx
cmpq %r9, %rcx
je 0x1c725f
movl 0xc(%r8,%r9), %eax
cmpl %eax, %edx
movl %eax, %r10d
cmoval %edx, %r10d
xorl %r11d, %r11d
testl %eax, %eax
setns %r11b
cmovnsl %r10d, %edx
addl %r11d, %edi
addq $0x20, %r9
jmp 0x1c7236
testl %edi, %edi
je 0x1c7145
cmpl $0x1, %edi
movq 0x8(%rsp), %r8
movq (%rsp), %r9
jne 0x1c7299
cmpl $0x2, %esi
jb 0x1c7107
cmpl $0x0, 0x28(%rsp)
je 0x1c72f4
movq %r15, %rdi
movq 0x38(%rsp), %rsi
jmp 0x1c72d0
movq 0x18(%rsp), %r15
jmp 0x1c7145
cvtsi2ss %edx, %xmm1
mulss 0x2c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1c7107
cmpl $0x2, %esi
setae %al
cmpl $0x0, 0x28(%rsp)
sete %cl
orb %al, %cl
jne 0x1c72f4
cmpl $0x1, %esi
jne 0x1c7107
movl %r13d, %esi
xorl $0x1, %esi
movq %r15, %rdi
movl 0x14(%rsp), %ebp
movl %ebp, %edx
movq 0x30(%rsp), %rcx
callq 0x1c6b51
xorps %xmm2, %xmm2
movq (%rsp), %r9
movq 0x8(%rsp), %r8
cmpl %ebp, %eax
jge 0x1c7107
orb $-0x80, 0xf(%r14)
orb $-0x80, 0xf(%rbx)
movq %r15, %rdi
movl %r13d, %esi
movl 0x14(%rsp), %ebp
movl %ebp, %edx
callq 0x1c6c44
cmpl %ebp, %eax
jge 0x1c732f
movq %r15, %rdi
movq 0x40(%rsp), %rsi
movl 0x14(%rsp), %ebp
movl %ebp, %edx
callq 0x1c6c44
cmpl %ebp, %eax
jge 0x1c732f
incl 0x20(%rsp)
jmp 0x1c7338
andb $0x7f, 0xf(%r14)
andb $0x7f, 0xf(%rbx)
movq 0x8(%rsp), %r8
movq (%rsp), %r9
xorps %xmm2, %xmm2
jmp 0x1c7107
cmpq $0x0, 0x60(%rsp)
je 0x1c735e
movq 0xa0(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x68(%rsp)
je 0x1c7373
movq 0x80(%rsp), %rdi
callq 0x2220
cmpl $0x0, 0x20(%rsp)
je 0x1c7382
movq %r15, %rdi
callq 0x1c5ce9
movq 0x30(%rsp), %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220