// File: /workspace/github_cpp_makefile_star2_O1/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)
movq %rdx, %rbx
movl 0x14(%rdx), %eax
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 0xe81be
movl %eax, %ebp
xorl %r15d, %r15d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x20(%rbx), %rsi
movl %r15d, %edi
shrl %edi
cmpl $0x0, (%rsi,%rdi,8)
js 0xe7f8b
movq 0x28(%rbx), %rdx
movq (%rdx,%r15,8), %rax
movq 0x8(%rbx), %rcx
cmpl $0x0, 0x4(%rsp)
je 0xe7f29
cmpb $0x1, 0x4(%rsi,%rdi,8)
je 0xe7f8b
movl %r15d, %r11d
xorq $0x1, %r11
movq (%rdx,%r11,8), %rsi
movq %rsi, %rdi
andq %r14, %rdi
je 0xe7f8b
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 0xe7f6e
cmpl $0x2, %r9d
jge 0xe7f9c
incq %r15
cmpq %rbp, %r15
jne 0xe7f00
jmp 0xe81c1
xorl %esi, %esi
movq %rax, %rdi
andq %r14, %rdi
je 0xe7fd6
leaq (%rcx,%rdx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rdx
xorl %edi, %edi
movl (%rcx,%rdi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %esi
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0xe7fb8
cmpl $0x1, %esi
sete %sil
testb %sil, %sil
je 0xe7f8b
movl %eax, %ecx
testq %rcx, %rcx
je 0xe7f8b
movq %r11, 0x8(%rsp)
movl %eax, %eax
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
leaq (%r12,%r12,2), %rax
movq 0x20(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax,8)
js 0xe819d
movq %rbp, %r14
movq $0x0, 0x68(%rsp)
movq %rbx, %rdi
movl %r15d, %esi
movq %rsp, %rdx
leaq 0x28(%rsp), %rcx
leaq 0x30(%rsp), %r8
movq 0x10(%rsp), %rbp
movq %rbp, %r9
callq 0xe7909
cmpq %rbp, 0x28(%rsp)
jg 0xe81b0
cmpl $0x5, %eax
je 0xe8194
testl %eax, %eax
movq %r14, %rbp
je 0xe81b3
cmpl $0x7, %eax
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
je 0xe7f8b
movl (%rsp), %eax
cmpq %rax, %r15
sete %cl
movq 0x68(%rsp), %rax
cmpq $0x2, %rax
setae %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xe808f
movq 0x78(%rsp), %rcx
movl -0x8(%rcx,%rax,4), %ecx
movl %ecx, (%rsp)
decq %rax
movq %rax, 0x68(%rsp)
movq 0x28(%rbx), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq %rdx, %rsi
andq %r14, %rsi
je 0xe7f8b
movq 0x8(%rbx), %rcx
movq %rdx, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movl (%rcx,%rdi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r8d
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0xe80cc
testl %r8d, %r8d
je 0xe7f8b
testl %edx, %edx
je 0xe819d
shlq $0x3, %rax
addq 0x8(%rbx), %rax
movl (%rsp), %ecx
movl %edx, %edx
xorl %esi, %esi
leaq (%rsi,%rsi,2), %rdi
movl 0xc(%rax,%rdi,8), %r8d
testl %r8d, %r8d
js 0xe8186
leaq (%rax,%rdi,8), %rdi
movl 0x8(%rdi), %r9d
movl %ecx, %r10d
xorl %r9d, %r10d
cmpl $0x1, %r10d
jne 0xe8186
orl $0x80000000, %r8d       # imm = 0x80000000
movl %r8d, 0xc(%rdi)
xorq $0x1, %r9
movq 0x28(%rbx), %r8
movq (%r8,%r9,8), %r9
movq %r9, %r8
andq %r14, %r8
je 0xe8183
movl 0x4(%rdi), %edi
movq 0x8(%rbx), %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 0xe817a
orb $-0x80, 0x3(%r9,%r10)
addq $0x18, %r10
cmpq %r10, %r8
jne 0xe8166
incl %r13d
incq %rsi
cmpq %rdx, %rsi
jne 0xe8103
jmp 0xe819d
movq %r14, %rbp
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
incq %r12
cmpq 0x18(%rsp), %r12
jne 0xe7ff1
jmp 0xe7f8b
movq %r14, %rbp
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0xe7f8b
xorl %r13d, %r13d
testl %r13d, %r13d
je 0xe81e2
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movq 0x78(%rsp), %rdi
callq 0x2220
movl %r13d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq