// File: /workspace/github_cpp_makefile_star2_O2/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)
movabsq $-0x8000000000000000, %r15 # imm = 0x8000000000000000
callq 0x251c
xorl %eax, %eax
testq %r14, %r14
cmovleq %rax, %r14
movq %r14, 0x38(%rsp)
cmpq %r14, %rax
je 0x6aac5
movq %rax, 0x28(%rsp)
imulq $0xc, %rax, %rax
cmpl $0x0, (%rbx,%rax)
js 0x6aaa9
imulq $0x18, 0x28(%rsp), %rsi
addq 0x8(%rsp), %rsi
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %rsi, 0x18(%rsp)
movl 0xc(%rsi), %eax
cmpq %rax, %r15
jae 0x6aaa9
movq (%rsi), %r13
movq (%r13,%rbp), %r14
shrq $0x20, %r14
imulq $0xc, %r14, %rcx
movq %rbx, %r12
movl (%rbx,%rcx), %eax
testl %eax, %eax
js 0x6aa9a
movl 0xc(%r13,%rbp), %edi
imulq $0xc, %rdi, %rdx
addq %r12, %rdx
movl (%rdx), %r8d
testl %r8d, %r8d
js 0x6aa9a
movq 0x18(%r13,%rbp), %rbx
testq %rbx, %rbx
js 0x6aa9a
movq %rdi, 0x30(%rsp)
movq %r13, %rdi
addq %rbp, %rdi
addq %r12, %rcx
movl 0x4(%rcx), %esi
subl %eax, %esi
movl 0x4(%rdx), %edx
subl %r8d, %edx
leaq 0x1e8d88(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x20(%rsp), %ecx
movl 0x24(%rsp), %r8d
leaq 0x40(%rsp), %r9
callq 0x6a4a7
cmpl $-0x3, %eax
je 0x6aa43
cmpl $-0x2, %eax
movq 0x18(%rsp), %rsi
movq 0x30(%rsp), %rdx
jne 0x6aa9a
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %rbx
movq %rbx, 0x18(%r13,%rbp)
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movl %r14d, %ecx
callq 0x6a334
movl 0x4(%r13,%rbp), %edx
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x6a345
movl 0x4(%r13,%rbp), %esi
movl 0xc(%r13,%rbp), %edx
jmp 0x6aa86
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %rbx
movq %rbx, 0x18(%r13,%rbp)
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq 0x30(%rsp), %rdx
movl %r14d, %ecx
callq 0x6a334
movl 0xc(%r13,%rbp), %edx
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x6a345
movl 0x4(%r13,%rbp), %edx
movl 0xc(%r13,%rbp), %esi
movq 0x10(%rsp), %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6ab56
movq 0x18(%rsp), %rsi
incq %r15
addq $0x28, %rbp
movq %r12, %rbx
jmp 0x6a960
movq 0x28(%rsp), %rax
incq %rax
movq 0x38(%rsp), %r14
movabsq $-0x8000000000000000, %r15 # imm = 0x8000000000000000
jmp 0x6a92f
movq 0x10(%rsp), %rdi
callq 0x6abd8
xorl %eax, %eax
pushq $0x18
popq %rcx
cmpq %r14, %rax
je 0x6ab47
imulq $0x18, %rax, %rdx
addq 0x8(%rsp), %rdx
movq %rcx, %rsi
xorl %r8d, %r8d
xorl %edi, %edi
movl 0xc(%rdx), %r9d
cmpq %r9, %r8
jae 0x6ab34
movq (%rdx), %r9
movq (%r9,%rsi), %r10
testq %r10, %r10
js 0x6ab2b
movl -0x14(%r9,%rsi), %r11d
imulq $0xc, %r11, %r11
cmpl $0x0, (%rbx,%r11)
js 0x6ab24
movl -0xc(%r9,%rsi), %r11d
imulq $0xc, %r11, %r11
cmpl $0x0, (%rbx,%r11)
js 0x6ab24
incq %rdi
jmp 0x6ab2b
orq %r15, %r10
movq %r10, (%r9,%rsi)
incq %r8
addq $0x28, %rsi
jmp 0x6aaea
testq %rdi, %rdi
jne 0x6ab42
imulq $0xc, %rax, %rdx
orb $-0x80, 0x3(%rbx,%rdx)
incq %rax
jmp 0x6aad4
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq