// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r8, %r15
movq %rcx, 0x30(%rsp)
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
callq 0x2f3fc
movq %rbx, 0x38(%rsp)
testq %rbx, %rbx
je 0xa3ab8
xorl %ecx, %ecx
movq %r15, 0x28(%rsp)
leaq (%rcx,%rcx,2), %rdx
movq 0x10(%rsp), %rax
cmpl $0x0, 0xc(%rax,%rdx,8)
je 0xa3aa0
movq %rcx, 0x48(%rsp)
movq 0x10(%rsp), %rax
movq %rdx, 0x40(%rsp)
leaq (%rax,%rdx,8), %r11
movl $0x18, %r12d
movq $0x0, 0x20(%rsp)
xorl %r14d, %r14d
movq %r11, 0x18(%rsp)
movq (%r11), %rax
movq (%rax,%r12), %rsi
testq %rsi, %rsi
js 0xa3a7a
movq (%r15), %r8
movq -0x18(%rax,%r12), %rdi
movq %rdi, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
movl (%r8,%r9,4), %ebx
testl %ebx, %ebx
js 0xa3a7a
movl -0xc(%rax,%r12), %r13d
leaq (,%r13,2), %rdx
addq %r13, %rdx
leaq (%r8,%rdx,4), %r10
movl (%r10), %edx
movl %edx, 0x4(%rsp)
testl %edx, %edx
js 0xa3a7a
movq %r13, 0x50(%rsp)
leaq (%r8,%r9,4), %r13
movl -0x10(%rax,%r12), %r8d
testl %esi, %esi
js 0xa396e
movl -0x8(%rax,%r12), %edx
movl -0x4(%rax,%r12), %ebp
movl 0x4(%rsp), %r9d
subl %edx, %r9d
movl $0x0, %r11d
cmovbl %r11d, %r9d
addl %edi, %r9d
movl 0x4(%r10), %r11d
movl %ebp, %r15d
subl %r11d, %r15d
movl $0x0, %r10d
cmovbl %r10d, %r15d
movl %ebx, %r10d
subl %edi, %r10d
movq %r13, 0x8(%rsp)
movl $0x0, %r13d
cmovbl %r13d, %r10d
addl %edx, %r10d
movq 0x8(%rsp), %rdx
movl 0x4(%rdx), %r13d
cmpl %r13d, %r8d
jb 0xa39bf
subl %r8d, %ebp
addl %r13d, %ebp
jmp 0xa39bf
movl -0x8(%rax,%r12), %edx
movl %edx, 0x8(%rsp)
movl -0x4(%rax,%r12), %ebp
movl 0x4(%r10), %r11d
movl %ebp, %r9d
subl %r11d, %r9d
movl $0x0, %r10d
cmovbl %r10d, %r9d
addl %edi, %r9d
movl 0x4(%rsp), %r15d
subl %edx, %r15d
cmovbl %r10d, %r15d
movl 0x4(%r13), %r13d
movl %r8d, %r10d
subl %r13d, %r10d
movl $0x0, %edx
cmovbl %edx, %r10d
addl 0x8(%rsp), %r10d
movl %edi, %edx
subl %ebx, %edx
ja 0xa39bf
addl %edx, %ebp
subl %r15d, %r8d
subl %ebx, %r9d
movl $0x0, %edx
cmovbl %edx, %r9d
cmpl %r13d, %r8d
cmovael %r13d, %r8d
subl %ebx, %r8d
movl 0x4(%rsp), %ebx
subl %ebx, %r10d
cmovbl %edx, %r10d
cmpl %r11d, %ebp
cmovbl %ebp, %r11d
movl %r8d, %edx
subl %r9d, %edx
movslq %edx, %rdx
cmpq 0x30(%rsp), %rdx
jl 0xa3a0d
subl %ebx, %r11d
movl %r11d, %edx
subl %r10d, %edx
movslq %edx, %rdx
cmpq 0x30(%rsp), %rdx
jge 0xa3a40
movabsq $-0x8000000000000000, %rbx # imm = 0x8000000000000000
orq %rbx, %rsi
movq %rsi, (%rax,%r12)
movq 0x28(%rsp), %r15
movq (%r15), %rsi
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rdx
callq 0xa1aa3
testq %rax, %rax
je 0xa3a75
orq %rbx, 0x18(%rax)
jmp 0xa3a75
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rdi
movslq %r9d, %rcx
orq %rdi, %rcx
movq %rcx, -0x18(%rax,%r12)
movl %r8d, -0x10(%rax,%r12)
movl %r10d, -0x8(%rax,%r12)
movl %r11d, -0x4(%rax,%r12)
movq %rsi, (%rax,%r12)
incq 0x20(%rsp)
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %r11
incq %r14
movl 0xc(%r11), %eax
addq $0x28, %r12
cmpq %rax, %r14
jb 0xa38a9
cmpq $0x0, 0x20(%rsp)
movq 0x48(%rsp), %rcx
movq 0x40(%rsp), %rdx
jne 0xa3aaa
movq (%r15), %rax
orl $0x80000000, (%rax,%rdx,4) # imm = 0x80000000
incq %rcx
cmpq 0x38(%rsp), %rcx
jne 0xa386b
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq