// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movl %r8d, 0x4(%rsp)
movq %rcx, 0x10(%rsp)
movl 0x14(%rdx), %eax
xorl %ebx, %ebx
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x90(%rsp)
addl %eax, %eax
je 0xe99f2
movq %rdx, %r14
movl %eax, %r12d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x20(%r14), %rsi
movl %r13d, %edi
shrl %edi
cmpl $0x0, (%rsi,%rdi,8)
js 0xe99bf
movq 0x28(%r14), %rdx
movq (%rdx,%r13,8), %rax
movq 0x8(%r14), %rcx
cmpl $0x0, 0x4(%rsp)
je 0xe9739
cmpb $0x1, 0x4(%rsi,%rdi,8)
je 0xe99bf
movl %r13d, %r11d
xorq $0x1, %r11
movq (%rdx,%r11,8), %rsi
movq %rsi, %rdi
andq %rbp, %rdi
je 0xe99bf
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r8
movq %r8, 0x20(%rsp)
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rcx,%rsi,8), %rsi
addq $0xc, %rsi
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
movl (%rsi,%r8), %r10d
notl %r10d
shrl $0x1f, %r10d
addl %r10d, %r9d
addq $0x18, %r8
cmpq %r8, %rdi
jne 0xe9782
cmpl $0x2, %r9d
jl 0xe99bf
movq %rax, %rsi
andq %rbp, %rsi
je 0xe99bf
leaq (%rcx,%rdx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xe97c3
cmpl $0x1, %edi
jne 0xe99bf
movl %eax, %ecx
testq %rcx, %rcx
je 0xe99bf
movq %r11, 0x8(%rsp)
movl %eax, %eax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
leaq (%r15,%r15,2), %rax
movq 0x20(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax,8)
js 0xe99a4
movq %rbx, %rbp
movq %r12, %rbx
movq $0x0, 0x68(%rsp)
movq %r14, %rdi
movl %r13d, %esi
movq %rsp, %rdx
leaq 0x28(%rsp), %rcx
leaq 0x30(%rsp), %r8
movq 0x10(%rsp), %r12
movq %r12, %r9
callq 0xe9125
cmpq %r12, 0x28(%rsp)
jg 0xe99b4
cmpl $0x5, %eax
je 0xe9999
testl %eax, %eax
movq %rbx, %r12
je 0xe99b7
cmpl $0x7, %eax
movq %rbp, %rbx
je 0xe99ba
movl (%rsp), %eax
cmpq %rax, %r13
sete %dl
movq 0x68(%rsp), %rcx
cmpq $0x2, %rcx
setae %sil
andb %dl, %sil
cmpb $0x1, %sil
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jne 0xe98a3
movq 0x78(%rsp), %rax
movl -0x8(%rax,%rcx,4), %eax
movl %eax, (%rsp)
decq %rcx
movq %rcx, 0x68(%rsp)
movq 0x28(%r14), %rcx
movq 0x8(%rsp), %rdx
movq (%rcx,%rdx,8), %rdx
movq %rdx, %rsi
andq %rbp, %rsi
je 0xe99bf
movq 0x8(%r14), %rcx
movq %rdx, %rdi
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
leaq (%rcx,%rdi,8), %rcx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movl 0xc(%rcx,%rdi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r8d
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0xe98dc
testl %r8d, %r8d
je 0xe99bf
testl %edx, %edx
je 0xe99a4
movl %edx, %edx
xorl %esi, %esi
leaq (%rsi,%rsi,2), %rdi
movl 0xc(%rcx,%rdi,8), %r8d
testl %r8d, %r8d
js 0xe998b
leaq (%rcx,%rdi,8), %rdi
movl 0x8(%rdi), %r9d
movl %eax, %r10d
xorl %r9d, %r10d
cmpl $0x1, %r10d
jne 0xe998b
orl $0x80000000, %r8d       # imm = 0x80000000
movl %r8d, 0xc(%rdi)
xorq $0x1, %r9
movq 0x28(%r14), %r8
movq (%r8,%r9,8), %r9
movq %r9, %r8
andq %rbp, %r8
je 0xe9989
movl 0x4(%rdi), %edi
movq 0x8(%r14), %r10
shrq $0x20, %r9
shlq $0x3, %r8
leaq (%r8,%r8,2), %r8
leaq (%r9,%r9,2), %r9
leaq (%r10,%r9,8), %r9
addq $0xc, %r9
xorl %r10d, %r10d
movl -0x4(%r9,%r10), %r11d
xorl %edi, %r11d
cmpl $0x1, %r11d
jne 0xe9980
orb $-0x80, 0x3(%r9,%r10)
addq $0x18, %r10
cmpq %r10, %r8
jne 0xe996c
incl %ebx
incq %rsi
cmpq %rdx, %rsi
jne 0xe9909
jmp 0xe99a4
movq %rbx, %r12
movq %rbp, %rbx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
incq %r15
cmpq 0x18(%rsp), %r15
jne 0xe97fd
jmp 0xe99bf
movq %rbx, %r12
movq %rbp, %rbx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
incq %r13
cmpq %r12, %r13
jne 0xe9708
testl %ebx, %ebx
je 0xe99f0
movq %r14, %rdi
callq 0xa0a7d
movq %r14, %rdi
callq 0xa0ab6
movq %r14, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%r14)
movl %ebx, %ebx
jmp 0xe99f2
xorl %ebx, %ebx
movq 0x78(%rsp), %rdi
callq 0x2220
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq