// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: delete_all_edges_carefully(ma_hit_t_alloc*, ma_sub_t*, int, int, unsigned int)
uint32_t delete_all_edges_carefully(ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, 
int max_hang, int min_ovlp, uint32_t qn)
{
    if(coverage_cut[qn].del) return 0;

    int32_t r;
    asg_arc_t t;
    ma_hit_t *h = NULL;
    ma_sub_t *sq = NULL;
    ma_sub_t *st = NULL;
    ma_hit_t_alloc* x = &(sources[qn]);
    uint32_t i, keep_edge = 0;
    uint32_t flag[2];
    flag[0] = flag[1] = 0;
    for (i = 0; i < x->length; i++)
    {
        h = &(x->buffer[i]);
        if(h->del) continue;
        ///now the edge has not been removed
        sq = &(coverage_cut[Get_qn(*h)]);
        st = &(coverage_cut[Get_tn(*h)]);
        if(sq->del || st->del)
        {
            h->del = 1;
            delete_single_edge(sources, coverage_cut, Get_tn(*h), Get_qn(*h));
            continue;
        }
        
        r = ma_hit2arc(h, sq->e - sq->s, st->e - st->s, max_hang, 
                                asm_opt.max_hang_rate, min_ovlp, &t);
        ///if it is a contained read
        if(r < 0)
        {
            h->del = 1;
            delete_single_edge(sources, coverage_cut, Get_tn(*h), Get_qn(*h));
            continue;
        }
        
        if(get_real_sources_length(sources, coverage_cut, max_hang, min_ovlp, (t.v^1))==1)
        {
            flag[((t.ul>>32)^1)&1] = 1;
            keep_edge++;
            continue;
        }

        // h->del = 1;
        // delete_single_edge(sources, coverage_cut, Get_tn(*h), Get_qn(*h));
    }


    if(keep_edge != 0)
    {
        for (i = 0; i < x->length; i++)
        {
            h = &(x->buffer[i]);
            if(h->del) continue;
            ///now the edge has not been removed
            sq = &(coverage_cut[Get_qn(*h)]);
            st = &(coverage_cut[Get_tn(*h)]);
            if(sq->del || st->del) continue;
            
            r = ma_hit2arc(h, sq->e - sq->s, st->e - st->s, max_hang, 
                                    asm_opt.max_hang_rate, min_ovlp, &t);
            ///if it is a contained read
            if(r < 0) continue;

            if(flag[(t.ul>>32)&1] == 1) continue;
            
            if(get_real_sources_length(sources, coverage_cut, max_hang, min_ovlp, (t.v^1))==1) continue;
            
            h->del = 1;
            delete_single_edge(sources, coverage_cut, Get_tn(*h), Get_qn(*h));
        }
    }

    if(keep_edge == 0) coverage_cut[qn].del = 1;
    return keep_edge;
}

// Assembly:
movl %r8d, %eax
leaq (%rax,%rax,2), %rax
cmpl $0x0, (%rsi,%rax,4)
js 0xa18d1
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, 0x20(%rsp)
movl %ecx, 0x24(%rsp)
movabsq $-0x8000000000000000, %r9 # imm = 0x8000000000000000
movq %rdi, 0x28(%rsp)
movq %rax, 0x38(%rsp)
leaq (%rdi,%rax,8), %rdi
movq $0x0, 0x30(%rsp)
cmpl $0x0, 0xc(%rdi)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
je 0xa18d4
xorl %ebp, %ebp
xorl %r15d, %r15d
movl $0x0, 0x1c(%rsp)
movq (%rdi), %r14
movq 0x18(%r14,%rbp), %rbx
testq %rbx, %rbx
js 0xa18bc
movq (%r14,%rbp), %r13
shrq $0x20, %r13
leaq (,%r13,2), %rax
addq %r13, %rax
movl 0xc(%r14,%rbp), %r12d
movl (%rsi,%rax,4), %ecx
testl %ecx, %ecx
js 0xa1839
leaq (%r12,%r12,2), %rdx
leaq (%rsi,%rdx,4), %rdx
movl (%rdx), %r8d
testl %r8d, %r8d
js 0xa1839
leaq (%r14,%rbp), %rdi
leaq (%rsi,%rax,4), %rax
movl 0x4(%rax), %esi
subl %ecx, %esi
movl 0x4(%rdx), %edx
subl %r8d, %edx
leaq 0x1e8f7e(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl 0x20(%rsp), %ecx
movl 0x24(%rsp), %r8d
leaq 0x40(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xa1867
movl 0x48(%rsp), %r8d
xorl $0x1, %r8d
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl 0x20(%rsp), %edx
movl 0x24(%rsp), %ecx
callq 0xa14b7
cmpl $0x1, %eax
jne 0xa18a8
movl 0x44(%rsp), %eax
notl %eax
andl $0x1, %eax
movl $0x1, 0x30(%rsp,%rax,4)
incl 0x1c(%rsp)
jmp 0xa18a8
orq %r9, %rbx
movq %rbx, 0x18(%r14,%rbp)
movq 0x28(%rsp), %rdi
movl %r12d, %edx
movl %r13d, %ecx
movq %r9, %rbx
callq 0xa13a9
testq %rax, %rax
je 0xa185d
orq %rbx, 0x18(%rax)
movq 0x8(%rsp), %rsi
movq %rbx, %r9
jmp 0xa18b7
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %rbx
movq %rbx, 0x18(%r14,%rbp)
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl %r12d, %edx
movl %r13d, %ecx
callq 0xa13a9
testq %rax, %rax
je 0xa18a8
movabsq $-0x8000000000000000, %r9 # imm = 0x8000000000000000
orq %r9, 0x18(%rax)
movq 0x8(%rsp), %rsi
jmp 0xa18b7
movq 0x8(%rsp), %rsi
movabsq $-0x8000000000000000, %r9 # imm = 0x8000000000000000
movq 0x10(%rsp), %rdi
incq %r15
movl 0xc(%rdi), %eax
addq $0x28, %rbp
cmpq %rax, %r15
jb 0xa177c
jmp 0xa18dc
xorl %eax, %eax
retq
movl $0x0, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
je 0xa1a13
cmpl $0x0, 0xc(%rdi)
je 0xa1a20
xorl %r15d, %r15d
leaq 0x1e8e5d(%rip), %rbx    # 0x28a758
leaq 0x40(%rsp), %r13
xorl %ebp, %ebp
movq (%rdi), %r12
cmpq $0x0, 0x18(%r12,%r15)
js 0xa19fe
movl 0x4(%r12,%r15), %eax
leaq (%rax,%rax,2), %rcx
movl (%rsi,%rcx,4), %eax
testl %eax, %eax
js 0xa19fe
movl 0xc(%r12,%r15), %edx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rdx,4), %rdx
movl (%rdx), %r8d
testl %r8d, %r8d
js 0xa19fe
leaq (%r12,%r15), %rdi
leaq (%rsi,%rcx,4), %rcx
movl 0x4(%rcx), %esi
subl %eax, %esi
movl 0x4(%rdx), %edx
subl %r8d, %edx
movss 0x158(%rbx), %xmm0
movl 0x20(%rsp), %ecx
movl 0x24(%rsp), %r8d
movq %r13, %r9
callq 0xa159b
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
testl %eax, %eax
js 0xa19fe
movl 0x44(%rsp), %eax
andl $0x1, %eax
cmpl $0x1, 0x30(%rsp,%rax,4)
je 0xa19fe
movl 0x48(%rsp), %r8d
xorl $0x1, %r8d
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl 0x20(%rsp), %edx
movl 0x24(%rsp), %ecx
callq 0xa14b7
movq 0x10(%rsp), %rdi
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
movq 0x8(%rsp), %rsi
cmpl $0x1, %eax
je 0xa19fe
orq %rcx, 0x18(%r12,%r15)
movl 0x4(%r12,%r15), %ecx
movl 0xc(%r12,%r15), %edx
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0xa13a9
movq 0x10(%rsp), %rdi
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
movq 0x8(%rsp), %rsi
testq %rax, %rax
je 0xa19fe
orq %rcx, 0x18(%rax)
incq %rbp
movl 0xc(%rdi), %eax
addq $0x28, %r15
cmpq %rax, %rbp
jb 0xa1902
jmp 0xa1a20
movq 0x38(%rsp), %rax
leaq (%rsi,%rax,4), %rax
orb $-0x80, 0x3(%rax)
movl 0x1c(%rsp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq