// File: /workspace/github_cpp_makefile_star2_O3/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, 0x40(%rsp)
movss %xmm0, 0x44(%rsp)
movl %ecx, 0x24(%rsp)
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq 0x100(%rsp), %rax
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
leaq 0xb0(%rsp), %r13
movaps %xmm0, (%r13)
xorl %ebx, %ebx
movq %rbx, 0x10(%r13)
leaq 0x90(%rsp), %rax
movq %rbx, 0x10(%rax)
movaps %xmm0, (%rax)
movq 0x8(%rdi), %rdi
leal (%rdi,%rdi), %ebp
movl $0x1, %esi
callq 0x2120
testq %r12, %r12
movq %r12, %r8
cmoveq %r13, %r8
testq %r15, %r15
movq %r15, %r9
leaq 0x90(%rsp), %rdx
cmoveq %rdx, %r9
movq %rax, 0x58(%rsp)
movq %rbx, (%r9)
movq %rbx, (%r8)
testl %ebp, %ebp
movq %r8, (%rsp)
movq %r9, 0x50(%rsp)
movq %r15, 0x68(%rsp)
movq %r12, 0x60(%rsp)
je 0x214d16
testq %r12, %r12
movq %r12, %r11
cmoveq %r13, %r11
testq %r15, %r15
leaq 0x90(%rsp), %rax
cmoveq %rax, %r15
movl %ebp, %esi
xorl %edi, %edi
movabsq $0x100000000, %rbx      # imm = 0x100000000
xorl %r12d, %r12d
movq 0x30(%rsp), %r10
movq %r11, 0x8(%rsp)
movq %r14, 0x48(%rsp)
movq %r15, 0x10(%rsp)
movq %rsi, 0x28(%rsp)
movq (%r14), %rax
movl %r12d, %ecx
shrl %ecx
imulq $0x78, %rcx, %rcx
cmpb $0x0, 0x70(%rax,%rcx)
jne 0x214ced
addq %rcx, %rax
movl %r12d, %ecx
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rdx
testl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
je 0x214ced
movq %rdi, 0x18(%rsp)
movq 0x20(%rax,%rcx,8), %rbp
movl %edx, %ebx
addq $0x10, %rbp
xorl %eax, %eax
movq %rbx, %rdx
movq %rbp, %rsi
xorl %r13d, %r13d
xorl %ecx, %ecx
cmpl $0x0, -0x4(%rsi)
js 0x214bbc
incl %eax
movl (%rsi), %edi
cmpq $0x2, %rdi
jae 0x214bb3
xorl $0x1, %edi
addq %rdi, %rcx
jmp 0x214bbc
shrl %edi
cmpq %rdi, %r13
cmovbeq %rdi, %r13
addq $0x20, %rsi
decq %rdx
jne 0x214b9b
cmpl $0x2, %eax
jb 0x214cd9
testq %rcx, %rcx
je 0x214cd9
testq %r13, %r13
je 0x214cd9
movq 0x18(%rsp), %r14
movslq -0x4(%rbp), %rax
testq %rax, %rax
js 0x214cc4
cmpl $0x0, (%rbp)
jne 0x214cc4
testq %r10, %r10
je 0x214c0a
cmpl (%r10), %eax
jae 0x214cc4
addq %rax, %rax
leaq (%rax,%rax,4), %rax
xorl %edx, %edx
divq %r13
movq %rax, %r15
movq (%r8), %rcx
movq 0x10(%r11), %rax
cmpq 0x8(%r11), %rcx
jne 0x214c5b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r11)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r11
movq 0x50(%rsp), %r9
movq (%rsp), %r8
movq %rax, 0x10(%r11)
movq (%r8), %rcx
shlq $0x20, %r15
orq (%r9), %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %r15, (%rax,%rcx,8)
movq (%r9), %rcx
movq 0x10(%rsp), %r15
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x214cb4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r11
movq 0x50(%rsp), %r9
movq (%rsp), %r8
movq %rax, 0x10(%r15)
movq (%r9), %rcx
movq 0x30(%rsp), %r10
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
movq %r14, (%rax,%rcx,8)
addq $0x20, %rbp
incq %r14
decq %rbx
jne 0x214be5
movq 0x48(%rsp), %r14
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
movabsq $0x100000000, %rbx      # imm = 0x100000000
incq %r12
addq %rbx, %rdi
cmpq %rsi, %r12
jne 0x214b49
movq %r13, 0x78(%rsp)
movq (%r8), %rbx
movq 0x68(%rsp), %r15
movq 0x60(%rsp), %r12
leaq 0xb0(%rsp), %r13
testq %r12, %r12
cmovneq %r12, %r13
movq %r13, 0x30(%rsp)
movq 0x10(%r13), %rdi
leaq (%rdi,%rbx,8), %rsi
callq 0x1ee586
movq (%rsp), %r8
cmpq $0x0, (%r8)
je 0x215092
testq %r15, %r15
leaq 0x90(%rsp), %r11
cmovneq %r15, %r11
xorl %r13d, %r13d
xorps %xmm2, %xmm2
xorl %ebx, %ebx
movq 0x30(%rsp), %r12
movq 0x10(%r11), %rax
movq 0x10(%r12), %rcx
movl (%rcx,%r13,8), %ecx
movq (%rax,%rcx,8), %r15
movq %r15, %rcx
movl %r15d, %ebp
shrq $0x20, %r15
movq (%r14), %rax
shrq $0x21, %rcx
imulq $0x78, %rcx, %rcx
addq %rax, %rcx
movl %r15d, %edx
andl $0x1, %edx
leaq (%rdx,%rdx,2), %rdi
movq 0x20(%rcx,%rdi,8), %rdx
shlq $0x5, %rbp
movl 0xc(%rdx,%rbp), %r10d
testl %r10d, %r10d
js 0x214f94
cmpb $0x0, 0x70(%rcx)
jne 0x214f94
movl 0x8(%rdx,%rbp), %esi
movl %esi, %r9d
shrl %r9d
imulq $0x78, %r9, %r9
cmpb $0x0, 0x70(%rax,%r9)
jne 0x214f94
addq %rdx, %rbp
cmpl $0x0, 0x10(%rbp)
jne 0x214f94
leaq (%rcx,%rdi,8), %rcx
addq $0x10, %rcx
movq %rsi, %rdi
xorq $0x1, %rdi
addq %r9, %rax
movq (%rcx), %r8
movl %edi, %ecx
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %r9
movq 0x20(%rax,%rcx,8), %rax
movq %r9, %rcx
cmpl $0x1, %r8d
ja 0x214e13
cmpl $0x2, %ecx
jae 0x214e13
movq (%rsp), %r8
jmp 0x214f94
movq %rdi, 0x88(%rsp)
movl %ebx, 0x10(%rsp)
movq %r11, 0x8(%rsp)
movq %rcx, %r9
movl %r9d, %ecx
movq %rcx, %r11
movq %rax, %rdi
movq %r9, 0x28(%rsp)
testl %r9d, %r9d
je 0x214e4e
movl 0x8(%rdi), %ebx
xorl %r15d, %ebx
cmpl $0x1, %ebx
je 0x214e53
addq $0x20, %rdi
decq %r11
jne 0x214e3a
movq 0x18(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movl 0xc(%rdi), %r9d
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %r9d
cmpl %r9d, %r10d
cmovbl %r10d, %r9d
movaps %xmm2, 0x70(%rsp)
testl %r8d, %r8d
je 0x214f87
movl %r9d, 0x3c(%rsp)
movq %r14, 0x48(%rsp)
movl %r8d, %r11d
shlq $0x5, %r11
xorl %ebx, %ebx
xorl %r10d, %r10d
xorl %edi, %edi
movl 0xc(%rdx,%rbx), %r14d
testl %r14d, %r14d
js 0x214ec3
movl 0x10(%rdx,%rbx), %r12d
cmpl $0x1, %r12d
je 0x214eb5
xorl %r9d, %r9d
testl %r12d, %r12d
setne %r9b
incq 0x70(%rsp,%r9,8)
incl %edi
cmpl %r14d, %r10d
cmovbel %r14d, %r10d
movq 0x30(%rsp), %r12
addq $0x20, %rbx
cmpq %rbx, %r11
jne 0x214e91
testl %edi, %edi
je 0x214f82
cmpq $0x0, 0x70(%rsp)
movq 0x48(%rsp), %r14
movq 0x8(%rsp), %r11
movl 0x10(%rsp), %ebx
je 0x214e0a
cmpq $0x0, 0x78(%rsp)
je 0x214e0a
cmpl $0x2, %edi
jb 0x214f19
cvtsi2ss %r10d, %xmm0
cvtsi2ssl 0x3c(%rsp), %xmm1
mulss 0x44(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x214e0a
cmpl $0x0, 0x28(%rsp)
je 0x214e0a
addq $0xc, %rax
xorl %edx, %edx
xorl %r9d, %r9d
movl %edx, %r10d
movl (%rax), %r11d
cmpl %r11d, %edx
movl %r11d, %edx
cmoval %r10d, %edx
xorl %ebx, %ebx
testl %r11d, %r11d
cmovsl %r10d, %edx
setns %bl
addl %ebx, %r9d
addq $0x20, %rax
decq %rcx
jne 0x214f2d
testl %r9d, %r9d
movq (%rsp), %r8
je 0x214fa5
cmpl $0x1, %r9d
movq 0x8(%rsp), %r11
movl 0x10(%rsp), %ebx
jne 0x214fac
cmpl $0x2, %edi
jb 0x214f94
cmpl $0x0, 0x40(%rsp)
je 0x215010
movq %r14, %rdi
jmp 0x214fe7
movq 0x48(%rsp), %r14
movq 0x8(%rsp), %r11
movq (%rsp), %r8
movl 0x10(%rsp), %ebx
incl %r13d
cmpq %r13, (%r8)
ja 0x214d59
jmp 0x215081
movq 0x8(%rsp), %r11
jmp 0x214f90
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ssl 0x3c(%rsp), %xmm1
mulss 0x44(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x214f94
cmpl $0x2, %edi
setae %al
cmpl $0x0, 0x40(%rsp)
sete %cl
orb %al, %cl
jne 0x215010
cmpl $0x1, %edi
jne 0x214f94
movl %r15d, %esi
xorl $0x1, %esi
movq %r14, %rdi
movl 0x24(%rsp), %ebx
movl %ebx, %edx
movq 0x58(%rsp), %rcx
movq 0x50(%rsp), %r9
callq 0x21478a
xorps %xmm2, %xmm2
movq (%rsp), %r8
cmpl %ebx, %eax
movl 0x10(%rsp), %ebx
movq 0x8(%rsp), %r11
jge 0x214f94
movq 0x18(%rsp), %rax
orb $-0x80, 0xf(%rax)
orb $-0x80, 0xf(%rbp)
movq %r14, %rdi
movl %r15d, %esi
movl 0x24(%rsp), %ebx
movl %ebx, %edx
callq 0x21488f
cmpl %ebx, %eax
jge 0x215063
movq %r14, %rdi
movq 0x88(%rsp), %rsi
movl 0x24(%rsp), %ebx
movl %ebx, %edx
callq 0x21488f
cmpl %ebx, %eax
jge 0x215063
movl 0x10(%rsp), %ebx
incl %ebx
movq 0x8(%rsp), %r11
movq (%rsp), %r8
xorps %xmm2, %xmm2
jmp 0x214f94
movq 0x18(%rsp), %rax
andb $0x7f, 0xf(%rax)
andb $0x7f, 0xf(%rbp)
movq 0x8(%rsp), %r11
movq (%rsp), %r8
xorps %xmm2, %xmm2
jmp 0x214f90
testl %ebx, %ebx
sete %bl
movq 0x68(%rsp), %r15
movq 0x60(%rsp), %r12
jmp 0x215094
movb $0x1, %bl
testq %r12, %r12
je 0x2150a6
movq 0xc0(%rsp), %rdi
callq 0x2220
testq %r15, %r15
je 0x2150b8
movq 0xa0(%rsp), %rdi
callq 0x2220
testb %bl, %bl
jne 0x2150d9
leaq 0x80695(%rip), %rax     # 0x295758
movl 0x7c(%rax), %edi
movq 0x8(%r14), %rcx
leaq -0x18ee(%rip), %rsi     # 0x2137e3
movq %r14, %rdx
callq 0x108f2c
movq 0x58(%rsp), %rdi
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220