// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdx, %rbx
movq %rdi, 0x10(%rsp)
movl 0xc(%rdi), %edx
xorl %eax, %eax
movl $0x0, %ebp
movl %esi, %r15d
movl $0x0, %r13d
movl %esi, %r14d
testq %rdx, %rdx
je 0x1e4cda
movq 0x10(%rsp), %rdi
movq (%rdi), %r11
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rdx
xorl %r10d, %r10d
movl %esi, %r14d
xorl %r13d, %r13d
movl %esi, %r15d
xorl %edi, %edi
cmpq $0x0, 0x18(%r11,%r10)
js 0x1e4ccd
testl %ecx, %ecx
je 0x1e4c9c
cmpb $0x0, 0x20(%r11,%r10)
je 0x1e4ccd
movl 0x8(%r11,%r10), %r8d
cmpl %edi, %r8d
movl %edi, %ebp
cmoval %r8d, %ebp
movl (%r11,%r10), %r9d
testl %r9d, %r9d
cmovnel %edi, %ebp
cmovel %r9d, %r15d
cmpq %rsi, %r8
jne 0x1e4ccf
cmpl %r9d, %r14d
cmovael %r9d, %r14d
cmpl %r13d, %r8d
cmoval %r8d, %r13d
jmp 0x1e4ccf
movl %edi, %ebp
addq $0x28, %r10
movl %ebp, %edi
cmpq %r10, %rdx
jne 0x1e4c88
cmpl %r14d, %ebp
ja 0x1e4fd0
movl $0x1, %eax
cmpl %r15d, %ebp
jbe 0x1e4fd0
cmpl %r14d, %r13d
jbe 0x1e4fd0
movq %rsi, 0x8(%rsp)
movq (%rbx), %r12
movq 0x8(%rbx), %rax
cmpq %rax, %r12
movl %ecx, 0x4(%rsp)
jne 0x1e4d38
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movl 0x4(%rsp), %ecx
movq %rax, 0x10(%rbx)
addl %r15d, %r15d
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r15, (%rdi,%rax,8)
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1e4d7b
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
callq 0x2370
movl 0x4(%rsp), %ecx
movq %rax, 0x10(%rbx)
leal 0x1(,%rbp,2), %eax
movq (%rbx), %rsi
movq 0x10(%rbx), %rdi
leaq 0x1(%rsi), %rdx
movq %rdx, (%rbx)
movq %rax, (%rdi,%rsi,8)
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1e4dc2
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
callq 0x2370
movl 0x4(%rsp), %ecx
movq %rax, 0x10(%rbx)
addl %r14d, %r14d
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r14, (%rdi,%rax,8)
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1e4e05
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
callq 0x2370
movl 0x4(%rsp), %ecx
movq %rax, 0x10(%rbx)
leal 0x1(,%r13,2), %eax
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
leaq 0x1(%rdi), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rdi,8)
movq 0x10(%rsp), %rdx
cmpl $0x0, 0xc(%rdx)
je 0x1e4f19
movl $0x20, %r14d
xorl %r15d, %r15d
movq (%rdx), %rax
cmpq $0x0, -0x8(%rax,%r14)
js 0x1e4f06
testl %ecx, %ecx
je 0x1e4e55
cmpb $0x0, (%rax,%r14)
je 0x1e4f06
movq -0x20(%rax,%r14), %r13
testl %r13d, %r13d
je 0x1e4f06
movl -0x18(%rax,%r14), %ebp
cmpq 0x8(%rsp), %rbp
je 0x1e4f06
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1e4ea5
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movl 0x4(%rsp), %ecx
movq %rax, 0x10(%rbx)
addl %r13d, %r13d
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r13, (%rdi,%rax,8)
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1e4ee8
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
callq 0x2370
movl 0x4(%rsp), %ecx
movq %rax, 0x10(%rbx)
leal 0x1(,%rbp,2), %eax
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
leaq 0x1(%rdi), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rdi,8)
movq 0x10(%rsp), %rdx
incq %r15
movl 0xc(%rdx), %eax
addq $0x28, %r14
cmpq %rax, %r15
jb 0x1e4e37
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movl %r12d, %r14d
leaq (%rcx,%r14,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x1e3b6a
movq (%rbx), %rax
xorl %ecx, %ecx
cmpq %r14, %rax
jbe 0x1e4fbf
movq 0x10(%rbx), %rdx
incl %r12d
xorl %edi, %edi
movq %r14, %r9
xorl %r8d, %r8d
movq 0x8(%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 0x1e4f89
shrq %r9
movl %r9d, %r8d
jmp 0x1e4fb2
testl %r10d, %r10d
setg %r10b
testl %edi, %edi
setle %r11b
andb %r10b, %r11b
cmpb $0x1, %r11b
jne 0x1e4fb2
shrq %r9
testl %r8d, %r8d
cmovel %r9d, %ecx
cmpq 0x8(%rsp), %r9
cmovel %r8d, %esi
movl %r12d, %r9d
incl %r12d
cmpq %r9, %rax
ja 0x1e4f56
jmp 0x1e4fc6
movq 0x8(%rsp), %rax
movl %eax, %esi
movq %r14, (%rbx)
xorl %eax, %eax
cmpl %esi, %ecx
setbe %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq