// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: if_sup_chimeric(ma_hit_t_alloc*, unsigned long, asg64_v*, int)
int32_t if_sup_chimeric(ma_hit_t_alloc* src, uint64_t rLen, asg64_v *b, int if_exact)
{
    uint32_t k, qs, qe, l[2], r[2], st, bn;
    int32_t dp, op;
    l[0] = r[0] = rLen; l[1] = r[1] = 0;
    for (k = 0; k < src->length; k++){
        if(src->buffer[k].del) continue;
        if(if_exact && !(src->buffer[k].el)) continue;

        qs = Get_qs(src->buffer[k]); qe = Get_qe(src->buffer[k]);

        ///overlaps from left side
        if(qs == 0){
            if(qs < l[0]) l[0] = qs;
            if(qe > l[1]) l[1] = qe;
        }

        ///overlaps from right side
        if(qe == rLen){
            if(qs < r[0]) r[0] = qs;
            if(qe > r[1]) r[1] = qe;
        }

        ///note: if (qs == 0 && qe == rLen)
        ///this overlap would be added to both b_left and b_right
        ///that is what we want
    }
    if (l[1] > r[0]) return 0;
    if (l[1] <= l[0] || r[1] <= r[0]) return 1;

    bn = b->n;
    if(l[1] > l[0]) {
        kv_push(uint64_t, *b, (l[0]<<1)); kv_push(uint64_t, *b, (l[1]<<1)|1);
    }
    if(r[1] > r[0]) {
        kv_push(uint64_t, *b, (r[0]<<1)); kv_push(uint64_t, *b, (r[1]<<1)|1);
    }

    ///check contained overlaps
    for (k = 0; k < src->length; k++) {
        if(src->buffer[k].del) continue;
        if(if_exact && !(src->buffer[k].el)) continue;

        qs = Get_qs(src->buffer[k]); qe = Get_qe(src->buffer[k]);
        if(qs == 0 || qe == rLen) continue;

        kv_push(uint64_t, *b, (qs<<1)); kv_push(uint64_t, *b, (qe<<1)|1);
    }
    radix_sort_srt64(b->a + bn, b->a + b->n);
    l[0] = r[0] = rLen; l[1] = r[1] = 0;

    for (k = bn, dp = st = 0; k < b->n; k++) {
        op = dp;
        ///if a[j] is qe
        if (b->a[k]&1) --dp;
        else ++dp;

        if(op < 1 && dp >= 1) {
            st = b->a[k]>>1;
        } else if(op >= 1 && dp < 1) {
            if(st == 0) l[0] = st, l[1] = b->a[k]>>1;
            if((b->a[k]>>1) == rLen) r[0] = st, r[1] = b->a[k]>>1;
        }
    }
    
    b->n = bn;
    if (l[1] > r[0]) return 0;

    return 1;
}

///remove single node
void asg_arc_cut_chimeric(asg_t *g, ma_hit_t_alloc* src, asg64_v *in, uint32_t ou_thres, telo_end_t *te)
{
	asg64_v tx = {0,0,0}, *b = NULL;
	uint32_t v, w, ei[2] = {0}, k, i, n_vtx = g->n_seq<<1;
    uint32_t nw, el_n, cnt = 0; asg_arc_t *aw;
	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 (te && te->hh[v>>1]) continue;
		if(g->seq_vis[v] == 0) {
            if((get_arcs(g, v, &(ei[0]), 1)!=1) || (get_arcs(g, v^1, &(ei[1]), 1)!=1)) continue;
            assert((g->arc[ei[0]].ul>>32) == v && (g->arc[ei[1]].ul>>32) == (v^1));
            if((get_arcs(g, g->arc[ei[0]].v^1, NULL, 0)<2) || (get_arcs(g, g->arc[ei[1]].v^1, NULL, 0)<2)) continue;
            if(g->arc[ei[0]].el) continue;       
            if(ou_thres!=(uint32_t)-1&&g->arc[ei[0]].ou>=ou_thres&&g->arc[ei[1]].ou>=ou_thres) continue;///UL
            if(!if_sup_chimeric(&(src[v>>1]), g->seq[v>>1].len, b, 1)) continue;///HiFi
            kv_push(uint64_t, *b, (((uint64_t)(g->arc[ei[0]].ol))<<32)|((uint64_t)(ei[0])));
        }
	}

    radix_sort_srt64(b->a, b->a + b->n);
    ///here all edges are inexact matches 
    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;
        aw = asg_arc_a(g, w); nw = asg_arc_n(g, w);
        if((get_arcs(g, v, &(ei[0]), 1)!=1) || (get_arcs(g, v^1, &(ei[1]), 1)!=1)) continue;
        if((get_arcs(g, g->arc[ei[0]].v^1, NULL, 0)<2) || (get_arcs(g, g->arc[ei[1]].v^1, NULL, 0)<2)) continue;

        for (i = el_n = 0; i < nw; i++) {
            if ((aw[i].del) || (aw[i].v==(v^1)) || (!aw[i].el)) continue;
            el_n++; break;
        }

        if(!el_n) continue;
        if(te && te->hh[v>>1]) continue;
        asg_seq_del(g, v>>1);
        cnt++;
    }
    // stats_sysm(g);
	if(!in) free(tx.a);
    if (cnt > 0) asg_cleanup(g);
}

void asg_arc_cut_inexact(asg_t *g, ma_hit_t_alloc* src, asg64_v *in, int32_t max_ext, uint32_t is_ou, uint32_t is_trio, uint32_t min_diff, float ou_rat/**, asg64_v *dbg**/)
{
    asg64_v tx = {0,0,0}, *b = NULL;
    uint32_t v, w, i, k, n_vtx = g->n_seq<<1;
    asg_arc_t *av, *aw, *ve, *vmax, *we; uint32_t nv, nw, kv, kw, ol_max, ou_max, to_del, cnt = 0, mm_ol, mm_ou;
    uint32_t trioF = (uint32_t)-1, ntrioF = (uint32_t)-1;
    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 || av[i].el) continue;
                kv_push(uint64_t, *b, (uint64_t)((((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;
        assert((!g->arc[(uint32_t)b->a[k]].el));

        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(((v>>1) == 50356 && (w>>1) == 1276292)||((v>>1) == 1276292 && (w>>1) == 50356)) {
        //     fprintf(stderr, "[0]v->%u, w->%u, nv->%u, nw->%u\n", v, w, nv, nw);
        // }
        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, vmax = 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, vmax = &(av[i]);
            if(ou_max < av[i].ou) ou_max = av[i].ou;
        }
        // if(((v>>1) == 50356 && (w>>1) == 1276292)||((v>>1) == 1276292 && (w>>1) == 50356)) {
        //     fprintf(stderr, "[1]v->%u, w->%u, kv->%u, ve->ol->%u, ol_max->%u\n", v, w, kv, ve->ol, ol_max);
        // }
        if (kv < 1) continue;
        if (kv >= 2) {
            if (mm_ol >= ol_max) 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; 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(ou_max < aw[i].ou) ou_max = aw[i].ou;
        }
        // if(((v>>1) == 50356 && (w>>1) == 1276292)||((v>>1) == 1276292 && (w>>1) == 50356)) {
        //     fprintf(stderr, "[1]v->%u, w->%u, kw->%u, we->ol->%u, ol_max->%u\n", v, w, kw, we->ol, ol_max);
        // }
        if (kw < 1) continue;
        if (kw >= 2) {
            if (mm_ol >= ol_max) 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 there is an inexact edge between two good reads
        if(src[v>>1].is_fully_corrected == 1 && src[w>>1].is_fully_corrected == 1) {
            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;
            }
        }
        ///TODO: should check if the edge wmax also works
        if(src[v>>1].is_fully_corrected == 1 && src[w>>1].is_fully_corrected == 0) {
            if(vmax && vmax->v != ve->v && vmax->el == 1 && src[vmax->v>>1].is_fully_corrected == 1) {
                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(((v>>1) == 50356 && (w>>1) == 1276292)||((v>>1) == 1276292 && (w>>1) == 50356)) {
        //     fprintf(stderr, "[3]v->%u, w->%u, to_del->%u, el->%u, src[v>>1].is_fully_corrected->%u, src[w>>1].is_fully_corrected->%u\n", 
        //             v, w, to_del, g->arc[(uint32_t)b->a[k]].el, src[v>>1].is_fully_corrected, src[w>>1].is_fully_corrected);
        //     fprintf(stderr, "[4]v->%u, w->%u, to_del->%u, vmax->v->%u, vmax->el->%u, src[vmax->v>>1].is_fully_corrected->%u\n", 
        //             v, w, to_del, vmax->v, vmax->el, src[vmax->v>>1].is_fully_corrected);
        // }

        if (to_del) {
            ve->del = we->del = 1, ++cnt;
            /**
            if(dbg) {
                kv_push(uint64_t, *dbg, ve - g->arc); 
                kv_push(uint64_t, *dbg, we - g->arc); 
                // if(((v>>1) == 50356 && (w>>1) == 1276292)||((v>>1) == 1276292 && (w>>1) == 50356)) {
                //     fprintf(stderr, "[5]v->%u, w->%u, (ve - g->arc)->%u, (we - g->arc)->%u\n", 
                //     v, w, (uint32_t)(ve - g->arc), (uint32_t)(we - g->arc));
                // }
            }
            **/
        }
        
    }
    // 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 $0x18, %rsp
movl %ecx, %ebp
movl 0xc(%rdi), %ecx
testq %rcx, %rcx
je 0x1ef729
movq %rdx, %r9
movq (%rdi), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
movl %esi, %r14d
xorl %r13d, %r13d
movl %esi, %r15d
xorl %ebx, %ebx
cmpq $0x0, 0x18(%rax,%rdx)
js 0x1ef6bd
testl %ebp, %ebp
je 0x1ef68c
cmpb $0x0, 0x20(%rax,%rdx)
je 0x1ef6bd
movl 0x8(%rax,%rdx), %r10d
cmpl %ebx, %r10d
movl %ebx, %r8d
cmoval %r10d, %r8d
movl (%rax,%rdx), %r11d
testl %r11d, %r11d
cmovel %r8d, %ebx
cmovel %r11d, %r15d
cmpq %rsi, %r10
jne 0x1ef6bd
cmpl %r11d, %r14d
cmovael %r11d, %r14d
cmpl %r13d, %r10d
cmoval %r10d, %r13d
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0x1ef679
xorl %eax, %eax
cmpl %r14d, %ebx
ja 0x1ef9c8
cmpl %r15d, %ebx
jbe 0x1ef729
movl $0x1, %eax
cmpl %r14d, %r13d
jbe 0x1ef9c8
movq %rsi, 0x10(%rsp)
movq (%r9), %r12
cmpq 0x8(%r9), %r12
movq %r9, (%rsp)
movq %rdi, 0x8(%rsp)
jne 0x1ef733
leaq (%r12,%r12), %rax
testq %r12, %r12
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%r9)
movq 0x10(%r9), %rdi
shlq $0x3, %rsi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
jmp 0x1ef73a
movl $0x1, %eax
jmp 0x1ef9c8
movq 0x10(%r9), %rax
movq %r12, %rcx
addl %r15d, %r15d
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
movq %r15, (%rax,%rcx,8)
movq (%r9), %rcx
cmpq 0x8(%r9), %rcx
jne 0x1ef77c
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leal 0x1(,%rbx,2), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r9)
movq %rdx, (%rax,%rcx,8)
movq (%r9), %rcx
cmpq 0x8(%r9), %rcx
jne 0x1ef7c2
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
addl %r14d, %r14d
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
movq %r14, (%rax,%rcx,8)
movq (%r9), %rcx
cmpq 0x8(%r9), %rcx
jne 0x1ef804
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
movq 0x8(%rsp), %rdi
leal 0x1(,%r13,2), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r9)
movq %rdx, (%rax,%rcx,8)
movl 0xc(%rdi), %ecx
testl %ecx, %ecx
je 0x1ef90d
movl $0x20, %ebx
xorl %r14d, %r14d
movq (%rdi), %rdx
cmpq $0x0, -0x8(%rdx,%rbx)
js 0x1ef8fb
testl %ebp, %ebp
je 0x1ef84c
cmpb $0x0, (%rdx,%rbx)
je 0x1ef8fb
movq -0x20(%rdx,%rbx), %r13
testl %r13d, %r13d
je 0x1ef8fb
movl -0x18(%rdx,%rbx), %r15d
cmpq 0x10(%rsp), %r15
je 0x1ef8fb
movq (%r9), %rcx
cmpq 0x8(%r9), %rcx
jne 0x1ef89e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
addl %r13d, %r13d
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
movq %r13, (%rax,%rcx,8)
movq (%r9), %rcx
cmpq 0x8(%r9), %rcx
jne 0x1ef8e0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
movq 0x8(%rsp), %rdi
leal 0x1(,%r15,2), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r9)
movq %rdx, (%rax,%rcx,8)
movl 0xc(%rdi), %ecx
incq %r14
movl %ecx, %edx
addq $0x28, %rbx
cmpq %rdx, %r14
jb 0x1ef82f
movl %r12d, %ebx
leaq (%rax,%rbx,8), %rdi
movq (%r9), %rcx
leaq (%rax,%rcx,8), %rsi
callq 0x1ee586
movq (%rsp), %rdx
movq (%rdx), %rax
xorl %ecx, %ecx
cmpq %rbx, %rax
jbe 0x1ef9b3
movq 0x10(%rdx), %rdx
incl %r12d
xorl %edi, %edi
movq %rbx, %r9
xorl %r8d, %r8d
movq 0x10(%rsp), %rcx
movl %ecx, %esi
xorl %ecx, %ecx
movl %edi, %r10d
movq (%rdx,%r9,8), %r9
movl %r9d, %edi
andl $0x1, %edi
negl %edi
orl $0x1, %edi
addl %r10d, %edi
testl %r10d, %r10d
setle %r11b
testl %edi, %edi
setg %bpl
andb %r11b, %bpl
cmpb $0x1, %bpl
jne 0x1ef97d
shrq %r9
movl %r9d, %r8d
jmp 0x1ef9a6
testl %r10d, %r10d
setg %r10b
testl %edi, %edi
setle %r11b
andb %r10b, %r11b
cmpb $0x1, %r11b
jne 0x1ef9a6
shrq %r9
testl %r8d, %r8d
cmovel %r9d, %ecx
cmpq 0x10(%rsp), %r9
cmovel %r8d, %esi
movl %r12d, %r9d
incl %r12d
cmpq %r9, %rax
ja 0x1ef94a
jmp 0x1ef9ba
movq 0x10(%rsp), %rax
movl %eax, %esi
movq (%rsp), %rax
movq %rbx, (%rax)
xorl %eax, %eax
cmpl %esi, %ecx
setbe %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq