// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %r14
movq %rcx, 0x28(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, 0x10(%rsp)
callq 0x251c
xorl %edx, %edx
movq %r14, 0x18(%rsp)
cmpq 0x30(%rsp), %rdx
je 0x6be70
movq %rdx, 0x38(%rsp)
imulq $0x18, %rdx, %rdx
addq 0x10(%rsp), %rdx
pushq $0x18
popq %rbp
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq %rdx, 0x20(%rsp)
movl 0xc(%rdx), %eax
cmpq %rax, %r12
jae 0x6be4d
movq (%rdx), %rax
movq (%rax,%rbp), %rsi
testq %rsi, %rsi
js 0x6be41
movq (%r14), %r15
movq -0x18(%rax,%rbp), %rdi
movq %rdi, %rcx
shrq $0x20, %rcx
imulq $0xc, %rcx, %r8
movl (%r15,%r8), %ebx
testl %ebx, %ebx
js 0x6be41
movl -0xc(%rax,%rbp), %r9d
imulq $0xc, %r9, %r10
addq %r15, %r10
movl (%r10), %r11d
testl %r11d, %r11d
js 0x6be41
movq %r9, 0x40(%rsp)
addq %r8, %r15
movl -0x10(%rax,%rbp), %r8d
testl %esi, %esi
movl %r11d, 0x4(%rsp)
js 0x6bd4a
movl -0x8(%rax,%rbp), %edx
movl %edx, (%rsp)
movl -0x4(%rax,%rbp), %r13d
movl %r11d, %r9d
subl %edx, %r9d
movl $0x0, %r11d
cmovbl %r11d, %r9d
addl %edi, %r9d
movl 0x4(%r10), %r11d
movl %r13d, %r14d
subl %r11d, %r14d
movl $0x0, %r10d
cmovbl %r10d, %r14d
movl %ebx, %r10d
subl %edi, %r10d
movl $0x0, %edx
cmovbl %edx, %r10d
addl (%rsp), %r10d
movl 0x4(%r15), %r15d
cmpl %r15d, %r8d
jb 0x6bd97
subl %r8d, %r13d
addl %r15d, %r13d
jmp 0x6bd97
movl -0x8(%rax,%rbp), %edx
movl %edx, (%rsp)
movl -0x4(%rax,%rbp), %r13d
movl %r11d, %r14d
movl 0x4(%r10), %r11d
movl %r13d, %r9d
subl %r11d, %r9d
movl $0x0, %r10d
cmovbl %r10d, %r9d
addl %edi, %r9d
subl %edx, %r14d
cmovbl %r10d, %r14d
movl 0x4(%r15), %r15d
movl %r8d, %r10d
subl %r15d, %r10d
movl $0x0, %edx
cmovbl %edx, %r10d
addl (%rsp), %r10d
movl %edi, %edx
subl %ebx, %edx
ja 0x6bd97
addl %edx, %r13d
subl %r14d, %r8d
subl %ebx, %r9d
movl $0x0, %edx
cmovbl %edx, %r9d
cmpl %r15d, %r8d
cmovael %r15d, %r8d
subl %ebx, %r8d
movl 0x4(%rsp), %ebx
subl %ebx, %r10d
cmovbl %edx, %r10d
cmpl %r11d, %r13d
cmovbl %r13d, %r11d
movl %r8d, %edx
subl %r9d, %edx
movslq %edx, %rdx
cmpq 0x28(%rsp), %rdx
jl 0x6bde5
subl %ebx, %r11d
movl %r11d, %edx
subl %r10d, %edx
movslq %edx, %rdx
cmpq 0x28(%rsp), %rdx
jge 0x6be07
btsq $0x3f, %rsi
movq %rsi, (%rax,%rbp)
movq 0x18(%rsp), %r14
movq (%r14), %rsi
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rdx
callq 0x6a334
jmp 0x6be3c
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rdi
movslq %r9d, %rcx
orq %rdi, %rcx
movq %rcx, -0x18(%rax,%rbp)
movl %r8d, -0x10(%rax,%rbp)
movl %r10d, -0x8(%rax,%rbp)
movl %r11d, -0x4(%rax,%rbp)
movq %rsi, (%rax,%rbp)
incq 0x8(%rsp)
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %rdx
incq %r12
addq $0x28, %rbp
jmp 0x6bc8a
cmpq $0x0, 0x8(%rsp)
movq 0x38(%rsp), %rdx
jne 0x6be68
movq (%r14), %rax
imulq $0xc, %rdx, %rcx
orl $0x80000000, (%rax,%rcx) # imm = 0x80000000
incq %rdx
jmp 0x6bc5d
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq