// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
movl %r9d, 0x6c(%rsp)
movss %xmm0, 0x4c(%rsp)
movl %ecx, 0x34(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq 0x100(%rsp), %rax
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rax
movaps %xmm0, (%rax)
xorl %ebx, %ebx
movq %rbx, 0x10(%rax)
leaq 0xb0(%rsp), %r13
movq %rbx, 0x10(%r13)
movaps %xmm0, (%r13)
movq 0x8(%rdi), %rdi
leal (%rdi,%rdi), %ebp
movl $0x1, %esi
callq 0x2120
leaq 0x90(%rsp), %rcx
movq %rax, 0x70(%rsp)
testq %r15, %r15
movq %r15, %r8
cmoveq %rcx, %r8
testq %r14, %r14
movq %r14, %rax
cmoveq %r13, %rax
movq %rax, 0x60(%rsp)
movq %rbx, (%rax)
movq %rbx, (%r8)
movq 0x50(%rsp), %r9
testl %ebp, %ebp
movq %r12, 0x88(%rsp)
movq %r8, 0x18(%rsp)
movq %r14, 0x80(%rsp)
movq %r15, 0x78(%rsp)
je 0x2099c3
testq %r15, %r15
movq %r15, %r10
cmoveq %rcx, %r10
testq %r14, %r14
movq %r14, %r11
cmoveq %r13, %r11
movq 0x58(%rsp), %rbx
movl %ebp, %esi
xorl %r14d, %r14d
movabsq $0x100000000, %r15      # imm = 0x100000000
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
movq %r10, 0x28(%rsp)
movq %r11, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq (%r12), %rax
movl %r13d, %ecx
shrl %ecx
imulq $0x78, %rcx, %rcx
cmpb $0x0, 0x70(%rax,%rcx)
jne 0x2099a2
addq %rcx, %rax
movl %r13d, %ecx
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rdx
testl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
je 0x2099a2
movq %r14, 0x20(%rsp)
movq 0x20(%rax,%rcx,8), %r15
movl %edx, %ebp
addq $0x10, %r15
xorl %eax, %eax
movq %rbp, %rcx
movq %r15, %rdx
xorl %ebx, %ebx
xorl %r9d, %r9d
cmpl $0x0, -0x4(%rdx)
js 0x209852
incl %eax
movl (%rdx), %esi
cmpq $0x2, %rsi
jae 0x209849
xorl $0x1, %esi
addq %rsi, %r9
jmp 0x209852
shrl %esi
cmpq %rsi, %rbx
cmovbeq %rsi, %rbx
addq $0x20, %rdx
decq %rcx
jne 0x209831
cmpl $0x2, %eax
jb 0x20998e
testq %r9, %r9
je 0x20998e
testq %rbx, %rbx
je 0x20998e
movq 0x20(%rsp), %r12
movq %r9, 0x10(%rsp)
movslq -0x4(%r15), %rax
testq %rax, %rax
js 0x209976
cmpl $0x0, (%r15)
jne 0x209976
testq %rdi, %rdi
je 0x2098a4
cmpl (%rdi), %eax
jae 0x209976
addq %rax, %rax
leaq (%rax,%rax,4), %rax
xorl %edx, %edx
divq %rbx
movq %rax, %r14
movq 0x8(%r10), %rax
cmpq %rax, (%r8)
jne 0x2098fa
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r10)
movq 0x10(%r10), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x40(%rsp), %r11
movq 0x28(%rsp), %r10
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r8
movq %rax, 0x10(%r10)
shlq $0x20, %r14
movq 0x60(%rsp), %rsi
orq (%rsi), %r14
movq 0x10(%r10), %rax
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %r14, (%rax,%rcx,8)
movq 0x8(%r11), %rax
cmpq %rax, (%rsi)
jne 0x20995f
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r11)
movq 0x10(%r11), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x40(%rsp), %r11
movq 0x28(%rsp), %r10
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r8
movq %rax, 0x10(%r11)
movq 0x10(%r11), %rax
movq 0x60(%rsp), %rsi
movq (%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rsi)
movq %r12, (%rax,%rcx,8)
addq $0x20, %r15
incq %r12
decq %rbp
jne 0x209880
movq 0x88(%rsp), %r12
movq 0x38(%rsp), %rsi
movq 0x20(%rsp), %r14
movabsq $0x100000000, %r15      # imm = 0x100000000
incq %r13
addq %r15, %r14
cmpq %rsi, %r13
jne 0x2097de
movq %rbx, 0x58(%rsp)
movq 0x80(%rsp), %r14
movq 0x78(%rsp), %r15
testq %r15, %r15
leaq 0x90(%rsp), %rbx
cmovneq %r15, %rbx
movq %r9, 0x50(%rsp)
movq 0x10(%rbx), %rdi
movq (%r8), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
movq 0x18(%rsp), %r8
cmpq $0x0, (%r8)
je 0x209d56
testq %r14, %r14
leaq 0xb0(%rsp), %r15
cmovneq %r14, %r15
xorl %ebp, %ebp
xorps %xmm2, %xmm2
xorl %r11d, %r11d
movq %r15, 0x10(%rsp)
movq 0x10(%r15), %rax
movq 0x10(%rbx), %rcx
movl (%rcx,%rbp,8), %ecx
movq (%rax,%rcx,8), %r14
movq %r14, %rcx
movl %r14d, %r13d
shrq $0x20, %r14
movq (%r12), %rax
shrq $0x21, %rcx
imulq $0x78, %rcx, %rdx
addq %rax, %rdx
movl %r14d, %ecx
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %r10
movq 0x20(%rdx,%r10,8), %rdi
shlq $0x5, %r13
movl 0xc(%rdi,%r13), %ecx
testl %ecx, %ecx
js 0x209abc
cmpb $0x0, 0x70(%rdx)
jne 0x209abc
movl 0x8(%rdi,%r13), %esi
movl %esi, %r9d
shrl %r9d
imulq $0x78, %r9, %r9
cmpb $0x0, 0x70(%rax,%r9)
jne 0x209abc
addq %rdi, %r13
cmpl $0x0, 0x10(%r13)
jne 0x209abc
movl %r11d, 0x8(%rsp)
leaq (%rdx,%r10,8), %rdx
addq $0x10, %rdx
movq %rsi, %r10
xorq $0x1, %r10
addq %r9, %rax
movq (%rdx), %r9
movl %r10d, %edx
andl $0x1, %edx
leaq (%rdx,%rdx,2), %rdx
movq 0x10(%rax,%rdx,8), %r11
movq 0x20(%rax,%rdx,8), %rax
cmpl $0x1, %r9d
ja 0x209acc
cmpl $0x2, %r11d
jae 0x209acc
movl 0x8(%rsp), %r11d
incl %ebp
cmpq %rbp, (%r8)
ja 0x209a12
jmp 0x209d41
movq %r10, 0x38(%rsp)
movq %rax, %rdx
movl %r11d, %r10d
testl %r11d, %r11d
movq %r11, 0x20(%rsp)
je 0x209af7
movl 0x8(%rdx), %r11d
xorl %r14d, %r11d
cmpl $0x1, %r11d
je 0x209afc
addq $0x20, %rdx
decl %r10d
jne 0x209ae1
movq 0x28(%rsp), %rdx
movq %rdx, 0x28(%rsp)
movl 0xc(%rdx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
cmpl %edx, %ecx
cmovbl %ecx, %edx
movaps %xmm2, 0x50(%rsp)
testl %r9d, %r9d
je 0x209b7e
movq %rbx, 0x40(%rsp)
movl %r9d, %r9d
shlq $0x5, %r9
xorl %r10d, %r10d
xorl %r11d, %r11d
xorl %ecx, %ecx
movl 0xc(%rdi,%r10), %ebx
testl %ebx, %ebx
js 0x209b69
movl 0x10(%rdi,%r10), %r15d
cmpl $0x1, %r15d
je 0x209b5b
xorl %r12d, %r12d
testl %r15d, %r15d
setne %r12b
incq 0x50(%rsp,%r12,8)
movq 0x88(%rsp), %r12
incl %ecx
cmpl %ebx, %r11d
cmovbel %ebx, %r11d
movq 0x10(%rsp), %r15
addq $0x20, %r10
cmpq %r10, %r9
jne 0x209b30
cvtsi2ss %r11d, %xmm0
movq 0x40(%rsp), %rbx
jmp 0x209b83
xorl %ecx, %ecx
xorps %xmm0, %xmm0
testl %ecx, %ecx
movq 0x18(%rsp), %r8
movl 0x8(%rsp), %r11d
je 0x209abc
cmpq $0x0, 0x50(%rsp)
je 0x209abc
cmpq $0x0, 0x58(%rsp)
je 0x209abc
cmpl $0x2, %ecx
jb 0x209bc5
cvtsi2ss %edx, %xmm1
mulss 0x4c(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x209abc
movq 0x20(%rsp), %rdi
testl %edi, %edi
je 0x209c2b
movq %rbx, 0x40(%rsp)
movl %edi, %r8d
shlq $0x5, %r8
xorl %r9d, %r9d
xorl %r10d, %r10d
xorl %edi, %edi
movl %r10d, %r11d
movl 0xc(%rax,%r9), %ebx
cmpl %ebx, %r10d
movl %ebx, %r10d
cmoval %r11d, %r10d
xorl %r15d, %r15d
testl %ebx, %ebx
cmovsl %r11d, %r10d
setns %r15b
addl %r15d, %edi
addq $0x20, %r9
cmpq %r9, %r8
jne 0x209be2
xorps %xmm0, %xmm0
cvtsi2ss %r10d, %xmm0
movq 0x40(%rsp), %rbx
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r8
movl 0x8(%rsp), %r11d
jmp 0x209c30
xorps %xmm0, %xmm0
xorl %edi, %edi
testl %edi, %edi
je 0x209abc
cmpl $0x1, %edi
je 0x209c53
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
mulss 0x4c(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x209abc
cmpl $0x1, %ecx
ja 0x209c61
cmpl $0x1, %edi
je 0x209abc
cmpl $0x2, %ecx
setae %al
cmpl $0x1, %edi
setne %dl
cmpl $0x0, 0x6c(%rsp)
je 0x209cc4
andb %dl, %al
jne 0x209cc4
cmpl $0x1, %edi
je 0x209c8c
cmpl $0x1, %ecx
jne 0x209abc
movl %r14d, %esi
xorl $0x1, %esi
movq %r12, %rdi
movq %rbx, %r15
movl 0x34(%rsp), %ebx
movl %ebx, %edx
movq 0x70(%rsp), %rcx
movq 0x60(%rsp), %r9
callq 0x209405
movl 0x8(%rsp), %r11d
xorps %xmm2, %xmm2
movq 0x18(%rsp), %r8
cmpl %ebx, %eax
movq %r15, %rbx
movq 0x10(%rsp), %r15
jge 0x209abc
movq %rbx, %r15
movq 0x28(%rsp), %rax
orb $-0x80, 0xf(%rax)
orb $-0x80, 0xf(%r13)
movq %r12, %rdi
movl %r14d, %esi
movl 0x34(%rsp), %ebx
movl %ebx, %edx
callq 0x20951f
cmpl %ebx, %eax
jge 0x209d1e
movq %r12, %rdi
movq 0x38(%rsp), %rsi
movl 0x34(%rsp), %ebx
movl %ebx, %edx
callq 0x20951f
cmpl %ebx, %eax
jge 0x209d1e
movl 0x8(%rsp), %r11d
incl %r11d
movq %r15, %rbx
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r8
xorps %xmm2, %xmm2
jmp 0x209abc
movq 0x28(%rsp), %rax
andb $0x7f, 0xf(%rax)
andb $0x7f, 0xf(%r13)
movq %r15, %rbx
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r8
xorps %xmm2, %xmm2
jmp 0x209ab7
testl %r11d, %r11d
sete %bl
movq 0x80(%rsp), %r14
movq 0x78(%rsp), %r15
jmp 0x209d58
movb $0x1, %bl
testq %r15, %r15
je 0x209d6a
movq 0xa0(%rsp), %rdi
callq 0x2220
testq %r14, %r14
je 0x209d7c
movq 0xc0(%rsp), %rdi
callq 0x2220
testb %bl, %bl
jne 0x209d9e
leaq 0x809d1(%rip), %rax     # 0x28a758
movl 0x7c(%rax), %edi
movq 0x8(%r12), %rcx
leaq -0x1aae(%rip), %rsi     # 0x2082e8
movq %r12, %rdx
callq 0x1047b4
movq 0x70(%rsp), %rdi
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220