// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_hit_cut(ma_hit_t_alloc*, long long, unsigned long*, long long, ma_sub_t**)
void ma_hit_cut(ma_hit_t_alloc* sources, long long n_read, uint64_t* readLen, 
long long mini_overlap_length, ma_sub_t** coverage_cut)
{
    double startTime = Get_T();
	size_t i, j;
    ma_hit_t* p;
    ma_sub_t* rq;
    ma_sub_t* rt;
    long long rLen = 0;
    for (i = 0; i < (uint64_t)n_read; ++i) 
    {
        rLen = 0;
        for (j = 0; j < sources[i].length; j++)
        {
            ///this is a overlap
            p = &(sources[i].buffer[j]);
            if(p->del) continue;

            rq = &((*coverage_cut)[Get_qn(*p)]);
            rt = &((*coverage_cut)[Get_tn(*p)]);
            ///if any of target read and the query read has no enough coverage
		    if (rq->del || rt->del) continue;
            int qs, qe, ts, te;




            ///target and query in different strand
            if (p->rev) 
            {
                /**
                here is an example in different strand:
                                      
                                       (te) (rt->e)           (rt->s)    (ts)
                                        |      |                 |        |
                target ----------------------------------------------------
                                        ------------------------------------------- query
                                        qs                                qe
                **/
                qs = p->te < rt->e? Get_qs(*p): Get_qs(*p) + (p->te - rt->e);
                qe = p->ts > rt->s? p->qe : p->qe - (rt->s - p->ts);
                ts = p->qe < rq->e? p->ts : p->ts + (p->qe - rq->e);
                te = Get_qs(*p) > rq->s? p->te : p->te - (rq->s - Get_qs(*p));
            }
            else ///target and query in same strand
            { 
                /**
                note: ts is the targe start in this overlap, 
                while rt->s is the high coverage start in the whole target (not only in this overlap)
                so this line is to normalize the qs in quey to high coverage region
                **/
                //(rt->s - p->ts) is the offset        
                qs = p->ts > rt->s? Get_qs(*p): Get_qs(*p) + (rt->s - p->ts); 
                //(p->te - rt->e) is the offset
                qe = p->te < rt->e? p->qe : p->qe - (p->te - rt->e);
                //(rq->s - Get_qs(*p) is the offset
                ts = Get_qs(*p) > rq->s? p->ts : p->ts + (rq->s - Get_qs(*p));
                //(p->qe - rq->e) is the offset
                te = p->qe < rq->e? p->te : p->te - (p->qe - rq->e);
		    }

            

            //cut by self coverage
            //and normalize the qs, qe, ts, te by rq->s and rt->e
            qs = ((uint32_t)qs > rq->s? qs : rq->s) - rq->s;
            qe = ((uint32_t)qe < rq->e? qe : rq->e) - rq->s;
            ts = ((uint32_t)ts > rt->s? ts : rt->s) - rt->s;
            te = ((uint32_t)te < rt->e? te : rt->e) - rt->s;

            if (qe - qs >= mini_overlap_length && te - ts >= mini_overlap_length) 
            {
                ///p->qns = p->qns>>32<<32 | qs;
                p->qns = p->qns>>32;
                p->qns = p->qns << 32;
                p->qns = p->qns | qs;

                p->qe = qe;
                p->ts = ts;
                p->te = te;
                p->del = 0;
                rLen++;
		    }
            else
            {
                p->del = 1;
                delete_single_edge(sources, (*coverage_cut), Get_tn(*p), Get_qn(*p));
                ///delete_all_edges(paf, coverage_cut, i); 
            }
        }

        if(rLen == 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
callq 0x2510
movsd %xmm0, -0x30(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0xd3617
movq $0x0, -0x60(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
imulq $0x18, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0xd35de
movq -0x8(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
je 0xd3134
jmp 0xd35cd
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xd3188
movq -0x58(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xd318d
jmp 0xd35cd
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x1f, %rax
andq $0x1, %rax
cmpl $0x0, %eax
je 0xd32b5
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x58(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0xd31c2
movq -0x48(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x74(%rbp)
jmp 0xd31dc
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
movl 0x14(%rcx), %ecx
movq -0x58(%rbp), %rdx
subl 0x4(%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jbe 0xd3205
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x78(%rbp)
jmp 0xd3224
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x48(%rbp), %rdx
subl 0x10(%rdx), %ecx
subl %ecx, %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x50(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0xd3246
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x7c(%rbp)
jmp 0xd3260
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x48(%rbp), %rcx
movl 0x8(%rcx), %ecx
movq -0x50(%rbp), %rdx
subl 0x4(%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jbe 0xd3289
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x80(%rbp)
jmp 0xd32aa
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x50(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x48(%rbp), %rdx
movq (%rdx), %rdx
subl %edx, %ecx
subl %ecx, %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0xd33e3
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jbe 0xd32db
movq -0x48(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x84(%rbp)
jmp 0xd32fd
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x48(%rbp), %rdx
subl 0x10(%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x58(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0xd3325
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x88(%rbp)
jmp 0xd3342
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x48(%rbp), %rcx
movl 0x14(%rcx), %ecx
movq -0x58(%rbp), %rdx
subl 0x4(%rdx), %ecx
subl %ecx, %eax
movl %eax, -0x88(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jbe 0xd3371
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x8c(%rbp)
jmp 0xd3395
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x50(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x48(%rbp), %rdx
movq (%rdx), %rdx
subl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x50(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0xd33bd
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x90(%rbp)
jmp 0xd33da
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x48(%rbp), %rcx
movl 0x8(%rcx), %ecx
movq -0x50(%rbp), %rdx
subl 0x4(%rdx), %ecx
subl %ecx, %eax
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x64(%rbp), %eax
movq -0x50(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jbe 0xd3401
movl -0x64(%rbp), %eax
movl %eax, -0x94(%rbp)
jmp 0xd3412
movq -0x50(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq -0x50(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x64(%rbp)
movl -0x68(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0xd3440
movl -0x68(%rbp), %eax
movl %eax, -0x98(%rbp)
jmp 0xd344d
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
movq -0x50(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x68(%rbp)
movl -0x6c(%rbp), %eax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jbe 0xd3482
movl -0x6c(%rbp), %eax
movl %eax, -0x9c(%rbp)
jmp 0xd3493
movq -0x58(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x6c(%rbp)
movl -0x70(%rbp), %eax
movq -0x58(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0xd34c1
movl -0x70(%rbp), %eax
movl %eax, -0xa0(%rbp)
jmp 0xd34ce
movq -0x58(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x70(%rbp)
movl -0x68(%rbp), %eax
subl -0x64(%rbp), %eax
cltq
cmpq -0x20(%rbp), %rax
jl 0xd3584
movl -0x70(%rbp), %eax
subl -0x6c(%rbp), %eax
cltq
cmpq -0x20(%rbp), %rax
jl 0xd3584
movq -0x48(%rbp), %rax
movq (%rax), %rcx
shrq $0x20, %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq (%rax), %rcx
shlq $0x20, %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movslq -0x64(%rbp), %rax
orq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movl -0x68(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x6c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x70(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x48(%rbp), %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
andq 0x18(%rax), %rcx
orq $0x0, %rcx
movq %rcx, 0x18(%rax)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xd35cb
movq -0x48(%rbp), %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
andq 0x18(%rax), %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
orq %rdx, %rcx
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rsi
movq -0x48(%rbp), %rax
movl 0xc(%rax), %edx
movq -0x48(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, %ecx
callq 0xd0020
jmp 0xd35cd
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xd30e7
cmpq $0x0, -0x60(%rbp)
jne 0xd3604
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0xc, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, (%rax)
jmp 0xd3606
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xd30c9
addq $0xa0, %rsp
popq %rbp
retq