// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: asg_arc_cut_chimeric_cmk(asg_t*, asg64_v*, int, float, float, unsigned int, unsigned int, unsigned int, unsigned int, unsigned char*, unsigned int)
void asg_arc_cut_chimeric_cmk(asg_t *g, asg64_v *in, int32_t max_ext, float len_rat, float ou_rat, uint32_t is_ou, uint32_t is_topo, uint32_t min_ou, uint32_t test_bub, uint8_t *cmk, uint32_t cmk_cut)
{
    asg64_v tx = {0,0,0}, *b = NULL; asg_arc_t *av, *aw, *ve, *we; 
    uint32_t i, k, v, w, n_vtx = g->n_seq<<1, nv, nw, kv, kw, ol_max, ou_max, to_del, cnt = 0, mm_ol, mm_ou;
    
    if(in) b = in;
    else b = &tx;
	b->n = 0;

    for (v = 0; v < n_vtx; ++v) {
        // if((v>>1)==17078) fprintf(stderr, "[M::%s::] v:%u, del:%u, seq_vis:%u\n", __func__, v, g->seq[v>>1].del, g->seq_vis[v]);
        if (g->seq[v>>1].del) continue;
        if (cmk[v>>1] > cmk_cut) continue;
        if((test_bub == 0) || (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;
                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;

        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; i < nv; ++i) {
            if(av[i].del) continue;
            kv++;
            
            if(ol_max < av[i].ol) ol_max = av[i].ol;
            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 && mm_ou > min_ou) continue;
        }
        

        for (i = kw = ol_max = ou_max = 0; i < nw; ++i) {
            if(aw[i].del) continue;
            kw++;

            if(ol_max < aw[i].ol) ol_max = aw[i].ol;
            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 && mm_ou > min_ou) 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 + 1) < max_ext + 1) to_del = 1;
            }
        }

        if (to_del) {
            asg_seq_del(g, v>>1); ++cnt;
        }
    }
    // stats_sysm(g);
    if(!in) free(tx.a);
    if (cnt > 0) asg_cleanup(g);

    // fprintf(stderr, "[M::%s::] cnt:%u\n", __func__, cnt);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, 0x30(%rsp)
movl %r8d, 0x64(%rsp)
movl %ecx, 0x38(%rsp)
movss %xmm1, 0x34(%rsp)
movss %xmm0, 0x3c(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, %r13
movl 0xb0(%rsp), %r8d
movq 0xa8(%rsp), %r11
movl 0xa0(%rsp), %ebx
movl 0x14(%rdi), %eax
addl %eax, %eax
testq %rsi, %rsi
je 0x1a9486
andq $0x0, (%r13)
movl %eax, %edx
xorl %r14d, %r14d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
pushq $0x18
popq %r12
xorl %r9d, %r9d
movq $0x0, 0x8(%rsp)
xorl %r10d, %r10d
movq %rdi, 0x10(%rsp)
movl %r8d, 0x58(%rsp)
movq %r11, 0x50(%rsp)
movl %ebx, 0x24(%rsp)
movq %rdx, 0x40(%rsp)
cmpq %rdx, %r14
je 0x1a9663
movq 0x20(%rdi), %rcx
movl %r14d, %eax
shrl %eax
cmpl $0x0, (%rcx,%rax,8)
js 0x1a965b
movzbl (%r11,%rax), %eax
cmpl %r8d, %eax
ja 0x1a965b
testl %ebx, %ebx
je 0x1a94f8
movq 0x30(%rdi), %rax
cmpb $0x0, (%rax,%r14)
jne 0x1a965b
movq 0x28(%rdi), %rax
movq (%rax,%r14,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1a965b
movq 0x8(%rdi), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %rsi
movq %rsi, 0x18(%rsp)
movl %eax, %eax
movq %rax, 0x28(%rsp)
imulq $0x18, %rax, %rax
leaq (%rcx,%rdx), %r15
addq $0xc, %r15
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rcx, %rax
je 0x1a9550
movl (%r15,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x18, %rcx
jmp 0x1a953a
cmpl $0x2, %edx
jb 0x1a9656
xorl %ebx, %ebx
movq 0x28(%rsp), %r11
cmpq %rbx, %r11
je 0x1a9648
cmpl $0x0, (%r15)
js 0x1a963c
testq %r13, %r13
je 0x1a95a0
movq (%r13), %rax
cmpq 0x8(%r13), %rax
jne 0x1a95fb
movq %r10, %r12
movq %r9, %rbp
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
jmp 0x1a95c3
cmpq 0x8(%rsp), %r9
jne 0x1a95fb
leaq (%r9,%r9), %rsi
movq %r9, %rbp
testq %r9, %r9
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rsp)
movq %r10, %r12
movq %r10, %rdi
shlq $0x3, %rsi
callq 0x2370
testq %r13, %r13
je 0x1a95e2
movq %rax, 0x10(%r13)
movq 0x10(%rsp), %rdi
movq %rbp, %r9
movq %r12, %r10
jmp 0x1a95ed
movq %rax, %r10
movq 0x10(%rsp), %rdi
movq %rbp, %r9
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
pushq $0x18
popq %r12
movq 0x28(%rsp), %r11
movl (%r15), %ecx
andl %ebp, %ecx
movq 0x18(%rsp), %rax
subq 0x8(%rdi), %rax
cqto
idivq %r12
testq %r13, %r13
je 0x1a9625
movq (%r13), %rdx
movq 0x10(%r13), %rsi
leaq 0x1(%rdx), %r8
movq %r8, (%r13)
jmp 0x1a962e
movq %r9, %rdx
incq %r9
movq %r10, %rsi
shlq $0x20, %rcx
addq %rbx, %rax
orq %rcx, %rax
movq %rax, (%rsi,%rdx,8)
incq %rbx
addq $0x18, %r15
jmp 0x1a9560
movl 0x58(%rsp), %r8d
movq 0x50(%rsp), %r11
movl 0x24(%rsp), %ebx
movq 0x40(%rsp), %rdx
incq %r14
jmp 0x1a94bb
movq %r10, 0x18(%rsp)
movq %r10, %rdi
movq %r9, 0x8(%rsp)
movq %r9, %rax
testq %r13, %r13
je 0x1a9680
movq (%r13), %rax
movq 0x10(%r13), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq 0x48(%rsp), %rax
incl %eax
movl %eax, 0x60(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x24(%rsp)
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r11
movq 0x18(%rsp), %r10
movl %r12d, %r12d
testq %r13, %r13
je 0x1a96c6
cmpq %r12, (%r13)
jbe 0x1a9a24
movq 0x10(%r13), %rax
jmp 0x1a96d2
movq %r10, %rax
cmpq %r12, %r11
jbe 0x1a9a17
movq 0x8(%rdi), %rcx
movl (%rax,%r12,8), %eax
imulq $0x18, %rax, %rax
cmpl $0x0, 0xc(%rcx,%rax)
js 0x1a975d
movq %r10, %rsi
testq %r13, %r13
je 0x1a96f1
movq 0x10(%r13), %rsi
movq %r10, %rax
testq %r13, %r13
je 0x1a96fd
movq 0x10(%r13), %rax
movl (%rsi,%r12,8), %esi
imulq $0x18, %rsi, %rsi
movq (%rcx,%rsi), %rsi
shrq $0x20, %rsi
movq 0x20(%rdi), %r8
movl %esi, %r14d
shrl %r14d
cmpl $0x0, (%r8,%r14,8)
js 0x1a975d
movl (%rax,%r12,8), %eax
imulq $0x18, %rax, %rax
movl 0x8(%rcx,%rax), %ebx
movl %ebx, %eax
shrl %eax
cmpl $0x0, (%r8,%rax,8)
js 0x1a975d
movq %rbx, %rax
xorq $0x1, %rax
movq 0x28(%rdi), %r8
movq (%r8,%rsi,8), %r10
movq (%r8,%rax,8), %r9
movl %r9d, %eax
orl %r10d, %eax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jne 0x1a9765
movq %rdx, %r15
jmp 0x1a9967
movq %rdx, %r15
jmp 0x1a996c
movq %rbx, 0x50(%rsp)
movq 0x18(%rsp), %rax
testq %r13, %r13
je 0x1a9778
movq 0x10(%r13), %rax
movq %r10, %rbp
shrq $0x20, %rbp
movq %r9, %r8
shrq $0x20, %r8
movl (%rax,%r12,8), %eax
imulq $0x18, %rax, %rax
addq %rcx, %rax
movl %r9d, %edi
movq %rdi, 0x40(%rsp)
incq %rdi
imulq $0x18, %r8, %r8
movq %r8, 0x58(%rsp)
leaq (%rcx,%r8), %r15
addq $-0x18, %r15
decq %rdi
je 0x1a97c5
movl 0x20(%r15), %r8d
xorl %esi, %r8d
addq $0x18, %r15
cmpl $0x1, %r8d
jne 0x1a97ad
jmp 0x1a97c8
movq %rdx, %r15
movl 0xc(%rax), %edx
movl %edx, 0x28(%rsp)
movl 0xc(%r15), %r11d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %r11d
movzwl 0x10(%rax), %eax
movl $0x3fff, %edx           # imm = 0x3FFF
andl %edx, %eax
movzwl 0x10(%r15), %ebx
andl %edx, %ebx
cmpw %bx, %ax
cmovbl %eax, %ebx
movl %r10d, %eax
imulq $0x18, %rax, %r8
imulq $0x18, %rbp, %rax
leaq (%rcx,%rax), %r9
addq $0x10, %r9
xorl %eax, %eax
xorl %edx, %edx
xorl %r10d, %r10d
xorl %ebp, %ebp
cmpq %rax, %r8
je 0x1a983c
movl -0x4(%r9,%rax), %edi
testl %edi, %edi
js 0x1a9836
incl %edx
cmpl %edi, %r10d
cmovbel %edi, %r10d
movzwl (%r9,%rax), %edi
andl $0x3fff, %edi           # imm = 0x3FFF
cmpl %edi, %ebp
cmovbel %edi, %ebp
addq $0x18, %rax
jmp 0x1a980f
movl 0x28(%rsp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %r11d, %eax
cmovael %r11d, %eax
testl %edx, %edx
je 0x1a995d
movzwl %bx, %r11d
cmpl $0x1, %edx
jne 0x1a991b
movl %eax, %ebp
imulq $0x18, 0x40(%rsp), %r10
movq 0x58(%rsp), %rax
leaq (%rcx,%rax), %rbx
addq $0x10, %rbx
xorl %eax, %eax
xorl %ecx, %ecx
xorl %r9d, %r9d
xorl %r8d, %r8d
cmpq %rax, %r10
je 0x1a98ad
movl -0x4(%rbx,%rax), %edi
testl %edi, %edi
js 0x1a98a7
incl %ecx
cmpl %edi, %r9d
cmovbel %edi, %r9d
movzwl (%rbx,%rax), %edi
andl $0x3fff, %edi           # imm = 0x3FFF
cmpl %edi, %r8d
cmovbel %edi, %r8d
addq $0x18, %rax
jmp 0x1a9880
testl %ecx, %ecx
je 0x1a995d
cmpl $0x1, %ecx
movq 0x18(%rsp), %r10
jne 0x1a9977
movl %edx, %eax
xorl $0x1, %eax
movl %ecx, %edi
xorl $0x1, %edi
orl %eax, %edi
sete %al
cmpl $0x0, 0x64(%rsp)
je 0x1a99ba
testb %al, %al
movq 0x10(%rsp), %rdi
jne 0x1a99bf
cmpl $0x1, %edx
setne %al
cmpl $0x1, %ecx
setne %r8b
testb %r8b, %al
je 0x1a99c6
movl %r14d, %esi
callq 0x1a606e
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %rdi
incl 0x24(%rsp)
jmp 0x1a99bf
cvtsi2ss %eax, %xmm0
cvtsi2ss %r10d, %xmm1
mulss 0x3c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1a995d
cmpl $0x0, 0x38(%rsp)
je 0x1a9861
cvtsi2ss %r11d, %xmm0
cvtsi2ss %ebp, %xmm1
mulss 0x34(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x1a9861
cmpl 0x30(%rsp), %r11d
jbe 0x1a9861
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r11
movq 0x18(%rsp), %r10
incl %r12d
movq %r15, %rdx
jmp 0x1a96ae
cvtsi2ss %ebp, %xmm0
cvtsi2ss %r9d, %xmm1
mulss 0x3c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1a99ba
cmpl $0x0, 0x38(%rsp)
je 0x1a98c3
cvtsi2ss %r11d, %xmm0
cvtsi2ss %r8d, %xmm1
mulss 0x34(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x1a98c3
cmpl 0x30(%rsp), %r11d
jbe 0x1a98c3
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r11
jmp 0x1a996c
cmpl $0x1, %ecx
jne 0x1a99f0
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rbx
movl %ebx, %edx
callq 0x71a0e
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %rdi
cmpl %ebx, %eax
jl 0x1a9900
jmp 0x1a99bf
cmpl $0x1, %edx
jne 0x1a99bf
xorl $0x1, %esi
movl 0x60(%rsp), %edx
callq 0x71a0e
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %rdi
cmpl 0x48(%rsp), %eax
jle 0x1a9900
jmp 0x1a99bf
movq %r10, %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
cmpl $0x0, 0x24(%rsp)
je 0x1a9a3e
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x694eb
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq