// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_hit_contained_advance(ma_hit_t_alloc*, long long, ma_sub_t*, R_to_U*, int, int)
void ma_hit_contained_advance(ma_hit_t_alloc* sources, long long n_read, ma_sub_t *coverage_cut, 
R_to_U* ruIndex, int max_hang, int min_ovlp)
{
    ///uint32_t qn_num = 0, no_fully_qn_num = 0, tn_num = 0, no_fully_tn_num = 0;
    double startTime = Get_T();
	int32_t r;
	long long i, j, m;
	asg_arc_t t;
    ma_hit_t *h = NULL;
    ma_sub_t *sq = NULL;
    ma_sub_t *st = NULL;


    for (i = 0; i < n_read; ++i) 
    {
        if(coverage_cut[i].del) continue;

        for (j = 0; j < (long long)sources[i].length; j++)
        {
            h = &(sources[i].buffer[j]);
            //check the corresponding two reads 
		    sq = &(coverage_cut[Get_qn(*h)]);
            st = &(coverage_cut[Get_tn(*h)]);
            /****************************may have trio bugs********************************/
            if(sq->del || st->del) continue;
            if(h->del) continue;
            /****************************may have trio bugs********************************/
            r = ma_hit2arc(h, sq->e - sq->s, st->e - st->s, max_hang, asm_opt.max_hang_rate, min_ovlp, &t);
            ///r could not be MA_HT_SHORT_OVLP or MA_HT_INT
            if (r == MA_HT_QCONT) 
            {
                h->del = 1;
                delete_single_edge(sources, coverage_cut, Get_tn(*h), Get_qn(*h));
        
                delete_all_edges(sources, coverage_cut, Get_qn(*h));
                set_R_to_U(ruIndex, Get_qn(*h), Get_tn(*h), 0, NULL);
            }
		    else if (r == MA_HT_TCONT) 
            {
                h->del = 1;
                delete_single_edge(sources, coverage_cut, Get_tn(*h), Get_qn(*h));

                delete_all_edges(sources, coverage_cut, Get_tn(*h));
                set_R_to_U(ruIndex, Get_tn(*h), Get_qn(*h), 0, NULL);
            }
        }
    }

    transfor_R_to_U(ruIndex);


    for (i = 0; i < n_read; ++i) 
    {
        m = 0;
        for (j = 0; j < (long long)sources[i].length; j++)
        {
            ma_hit_t *h = &(sources[i].buffer[j]);
            if(h->del) continue;
            ///both the qn and tn have not been deleted
            if(coverage_cut[Get_qn(*h)].del != 1 && coverage_cut[Get_tn(*h)].del != 1)
            {
                h->del = 0;
                m++;
            }
            else
            {
                h->del = 1;
            }
        }

        ///if sources[i].length == 0, that means all overlapped reads with read i are the contained reads
        if(m == 0)
        {
            coverage_cut[i].del = 1;
        }
    }

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, 0x24(%rsp)
movl %r8d, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
callq 0x300fc
testq %r14, %r14
jle 0xa1c2d
xorl %eax, %eax
movq %r14, 0x30(%rsp)
movq %rax, 0x38(%rsp)
leaq (%rax,%rax,2), %rax
cmpl $0x0, (%rbx,%rax,4)
js 0xa1c17
movq 0x8(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xa1c17
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %rsi, 0x18(%rsp)
movq (%rsi), %rbp
movq (%rbp,%r15), %r14
shrq $0x20, %r14
leaq (%r14,%r14,2), %rcx
movq %rbx, %r12
movl (%rbx,%rcx,4), %eax
testl %eax, %eax
js 0xa1c01
movl 0xc(%rbp,%r15), %edi
leaq (%rdi,%rdi,2), %rdx
leaq (%r12,%rdx,4), %rdx
movl (%rdx), %r8d
testl %r8d, %r8d
js 0xa1c01
movq 0x18(%rbp,%r15), %rbx
testq %rbx, %rbx
js 0xa1c01
movq %rdi, 0x28(%rsp)
leaq (%r15,%rbp), %rdi
leaq (%r12,%rcx,4), %rcx
movl 0x4(%rcx), %esi
subl %eax, %esi
movl 0x4(%rdx), %edx
subl %r8d, %edx
leaq 0x1e8c4d(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl 0x20(%rsp), %ecx
movl 0x24(%rsp), %r8d
leaq 0x40(%rsp), %r9
callq 0xa159b
cmpl $-0x3, %eax
je 0xa1b94
cmpl $-0x2, %eax
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %rdx
jne 0xa1c01
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %rbx
movq %rbx, 0x18(%rbp,%r15)
movq 0x8(%rsp), %rdi
movq %r12, %rbx
movq %r12, %rsi
movl %r14d, %ecx
callq 0xa13a9
testq %rax, %rax
je 0xa1b76
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
orq %rcx, 0x18(%rax)
movl 0x4(%rbp,%r15), %edx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0xa142c
movl 0x4(%rbp,%r15), %esi
movl 0xc(%rbp,%r15), %edx
jmp 0xa1bed
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %rbx
movq %rbx, 0x18(%rbp,%r15)
movq 0x8(%rsp), %rdi
movq %r12, %rbx
movq %r12, %rsi
movq 0x28(%rsp), %rdx
movl %r14d, %ecx
callq 0xa13a9
testq %rax, %rax
je 0xa1bd1
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
orq %rcx, 0x18(%rax)
movl 0xc(%rbp,%r15), %edx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0xa142c
movl 0x4(%rbp,%r15), %edx
movl 0xc(%rbp,%r15), %esi
movq 0x10(%rsp), %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xa1cd4
movq 0x18(%rsp), %rsi
incq %r13
movl 0xc(%rsi), %eax
addq $0x28, %r15
cmpq %rax, %r13
movq %r12, %rbx
jb 0xa1aa7
movq 0x38(%rsp), %rax
incq %rax
movq 0x30(%rsp), %r14
cmpq %r14, %rax
jne 0xa1a70
movq 0x10(%rsp), %rdi
callq 0xa1d4b
testq %r14, %r14
movabsq $-0x8000000000000000, %r15 # imm = 0x8000000000000000
jle 0xa1cc5
xorl %eax, %eax
leaq (%rax,%rax,2), %rcx
movq 0x8(%rsp), %rdx
cmpl $0x0, 0xc(%rdx,%rcx,8)
je 0xa1cb8
movq 0x8(%rsp), %rdx
leaq (%rdx,%rcx,8), %rdx
movl $0x18, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movq (%rdx), %r9
movq (%r9,%rsi), %r10
testq %r10, %r10
js 0xa1ca3
movl -0x14(%r9,%rsi), %r11d
leaq (%r11,%r11,2), %r11
cmpl $0x0, (%rbx,%r11,4)
js 0xa1c9c
movl -0xc(%r9,%rsi), %r11d
leaq (%r11,%r11,2), %r11
cmpl $0x0, (%rbx,%r11,4)
js 0xa1c9c
incq %rdi
jmp 0xa1ca3
orq %r15, %r10
movq %r10, (%r9,%rsi)
incq %r8
movl 0xc(%rdx), %r9d
addq $0x28, %rsi
cmpq %r9, %r8
jb 0xa1c6b
testq %rdi, %rdi
jne 0xa1cbd
orb $-0x80, 0x3(%rbx,%rcx,4)
incq %rax
cmpq %r14, %rax
jne 0xa1c48
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq