// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_simple_circle_untig(ma_hit_t_alloc*, ma_sub_t*, asg_t*, long long, int)
long long asg_arc_del_simple_circle_untig(ma_hit_t_alloc* sources, ma_sub_t* coverage_cut, asg_t *g, long long circleLen, int is_drop)
{
    uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0, convex, flag;
    long long ll;
    asg_arc_t *aw;
    uint32_t nw, k;
    buf_t b;
    memset(&b, 0, sizeof(buf_t));

    C_graph cg;
    init_C_graph(&cg, g->n_seq);

    for (v = 0; v < n_vtx; ++v) 
    {
        uint32_t i, n_arc = 0, nv = asg_arc_n(g, v);
        asg_arc_t *av = asg_arc_a(g, v);
        ///some node could be deleted
        if (g->seq[v>>1].del) continue;
        if(is_drop && g->seq[v>>1].c == ALTER_LABLE) continue;
        if(get_real_length(g, v^1, NULL)<=1) continue;

        n_arc = get_real_length(g, v, NULL);
        if (n_arc != 1) continue;

        for (i = 0; i < nv; i++)
        {
            ///actually there is just one un-del edge
            if (!av[i].del)
            {
                b.b.n = 0;    
                flag = detect_single_path_with_dels_by_length(g, v, &convex, &ll, &b, circleLen);
                if(ll > circleLen || flag == LONG_TIPS)
                {
                    break;
                }
                if(flag == LOOP)
                {
                    break;
                }
                if(flag != END_TIPS && flag != LONG_TIPS)
                {
                    if(v == convex && b.b.n > 1)
                    {
                        convex = b.b.a[b.b.n - 2];
                        b.b.n--;
                    }

                    w = v^1;
                    n_arc = get_real_length(g, w, NULL);
                    if(n_arc == 0)
                    {
                        break;
                    }
                    aw = asg_arc_a(g, w);
                    nw = asg_arc_n(g, w);
                    for (k = 0; k < nw; k++)
                    {
                        if ((!aw[k].del) && (aw[k].v == (convex^1)))
                        {
                            // coverage = get_untig_coverage(sources, coverage_cut, b.b.a, b.b.n);
                            // copy_untig(g, (coverage/asm_opt.coverage), b.b.a, b.b.n, &cg);

                            
                            aw[k].del = 1;
                            asg_arc_del(g, aw[k].v^1, aw[k].ul>>32^1, 1);
                            n_reduced++;
                        }
                    }
                }
            }
        }
    }

    if (n_reduced) {
        asg_cleanup(g);
        asg_symm(g);
    }

    free(b.b.a);
    destory_C_graph(&cg);

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %d self-circles\n", 
        __func__, n_reduced);
    }

    return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, %rbx
movl 0x14(%rdx), %eax
leal (%rax,%rax), %r12d
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rcx
movaps %xmm0, 0x50(%rcx)
movaps %xmm0, 0x40(%rcx)
movaps %xmm0, 0x30(%rcx)
movaps %xmm0, 0x20(%rcx)
movaps %xmm0, 0x10(%rcx)
andq $0x0, 0x60(%rcx)
movaps %xmm0, (%rcx)
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl %eax, 0x80(%rsp)
movl %eax, 0x84(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x20(%rsp)
movl %r8d, 0x4(%rsp)
movq %r15, 0x28(%rsp)
movq %r12, 0x10(%rsp)
cmpq %r12, %r13
je 0xab2d0
movq 0x20(%rbx), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xab2bd
movq 0x28(%rbx), %rdx
movq (%rdx,%r13,8), %r14
movq %r14, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq 0x8(%rbx), %rdx
movq %rdx, 0x30(%rsp)
testl %ebp, %ebp
je 0xab11a
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xab2bd
movl %r13d, %esi
xorl $0x1, %esi
movq %rbx, %rdi
movl %esi, 0x1c(%rsp)
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jl 0xab2bd
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0xab2bd
movq %r13, 0x8(%rsp)
movl 0x1c(%rsp), %eax
movq %rax, 0x40(%rsp)
movl %r14d, %r12d
xorl %r13d, %r13d
movq 0x30(%rsp), %r14
movq %r12, 0x38(%rsp)
cmpq %r12, %r13
je 0xab2af
imulq $0x18, %r13, %rax
cmpl $0x0, 0xc(%r14,%rax)
js 0xab2a7
andq $0x0, 0xc8(%rsp)
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
leaq 0x24(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x90(%rsp), %r8
movq %r15, %r9
callq 0xaac6e
cmpq %r15, 0x48(%rsp)
jg 0xab2af
cmpl $0x5, %eax
je 0xab2a7
testl %eax, %eax
je 0xab2af
cmpl $0x7, %eax
je 0xab2af
movl 0x24(%rsp), %r15d
cmpq %r15, 0x8(%rsp)
sete %cl
movq 0xc8(%rsp), %rax
cmpq $0x2, %rax
setae %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xab214
movq 0xd8(%rsp), %rcx
movl -0x8(%rcx,%rax,4), %r15d
movl %r15d, 0x24(%rsp)
decq %rax
movq %rax, 0xc8(%rsp)
movq %rbx, %rdi
movl 0x1c(%rsp), %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
je 0xab2c5
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rcx
movq 0x40(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rdx, %r14
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %r12
addq $0xc, %r12
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0xab298
movl (%r12,%rbp), %eax
testl %eax, %eax
js 0xab292
movl -0x4(%r12,%rbp), %esi
movl %r15d, %ecx
xorl %esi, %ecx
cmpl $0x1, %ecx
jne 0xab292
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%r12,%rbp)
xorl $0x1, %esi
movl -0x8(%r12,%rbp), %edx
xorl $0x1, %edx
movq %rbx, %rdi
pushq $0x1
popq %rcx
callq 0x6db50
incl 0x20(%rsp)
addq $0x18, %rbp
jmp 0xab253
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r12
incq %r13
jmp 0xab16b
movl 0x4(%rsp), %ebp
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
incq %r13
jmp 0xab0d3
movl 0x4(%rsp), %ebp
movq 0x28(%rsp), %r15
jmp 0xab2b3
movl 0x20(%rsp), %ebp
testl %ebp, %ebp
je 0xab2e8
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq 0xd8(%rsp), %rdi
callq 0x2220
leaq 0x50(%rsp), %rdi
callq 0xab038
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq