// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
callq 0x300fc
movq %rbx, 0x30(%rsp)
testq %rbx, %rbx
je 0xa2ec9
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdx
movq 0x8(%rsp), %rax
cmpl $0x0, 0xc(%rax,%rdx,8)
je 0xa2eac
movq %rcx, 0x40(%rsp)
movq 0x8(%rsp), %rax
movq %rdx, 0x38(%rsp)
leaq (%rax,%rdx,8), %r10
movl $0x18, %r12d
movq $0x0, 0x20(%rsp)
xorl %r14d, %r14d
movq %r10, 0x10(%rsp)
movq (%r10), %rax
movq (%rax,%r12), %rsi
testq %rsi, %rsi
js 0xa2e86
movq 0x18(%rsp), %rcx
movq (%rcx), %rdx
movq -0x18(%rax,%r12), %rdi
movq %rdi, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r8
movl (%rdx,%r8,4), %r15d
testl %r15d, %r15d
js 0xa2e86
movl -0xc(%rax,%r12), %ebx
leaq (%rbx,%rbx,2), %r9
leaq (%rdx,%r9,4), %r11
movl (%r11), %ebp
testl %ebp, %ebp
js 0xa2e86
movq %rbx, 0x48(%rsp)
leaq (%rdx,%r8,4), %r13
testl %esi, %esi
movq %r13, 0x50(%rsp)
js 0xa2d70
movl -0x8(%rax,%r12), %edx
movl %edx, (%rsp)
movl %ebp, %r9d
subl %edx, %r9d
movl $0x0, %edx
cmovbl %edx, %r9d
addl %edi, %r9d
movl -0x4(%rax,%r12), %ebx
movl %ebx, %r8d
subl 0x4(%r11), %r8d
cmovbl %edx, %r8d
movl %r15d, %r10d
subl %edi, %r10d
cmovbl %edx, %r10d
movl -0x10(%rax,%r12), %edx
addl (%rsp), %r10d
movl 0x4(%r13), %r13d
cmpl %r13d, %edx
jb 0xa2dc1
subl %edx, %ebx
addl %r13d, %ebx
jmp 0xa2dc1
movl -0x4(%rax,%r12), %ebx
movl %ebx, %r9d
movq %r11, (%rsp)
subl 0x4(%r11), %r9d
movl $0x0, %edx
cmovbl %edx, %r9d
addl %edi, %r9d
movl -0x10(%rax,%r12), %r10d
movl -0x8(%rax,%r12), %edx
movl %ebp, %r8d
subl %edx, %r8d
movl $0x0, %r11d
cmovbl %r11d, %r8d
xorl %r11d, %r11d
subl 0x4(%r13), %r10d
cmovbl %r11d, %r10d
addl %edx, %r10d
movl %edi, %edx
subl %r15d, %edx
ja 0xa2dbd
addl %edx, %ebx
movq (%rsp), %r11
movl -0x10(%rax,%r12), %edx
subl %r8d, %edx
subl %r15d, %r9d
movl $0x0, %r13d
cmovbl %r13d, %r9d
movq 0x50(%rsp), %r8
movl 0x4(%r8), %r8d
cmpl %r8d, %edx
cmovael %r8d, %edx
subl %r15d, %edx
subl %ebp, %r10d
cmovbl %r13d, %r10d
movl 0x4(%r11), %r8d
cmpl %r8d, %ebx
cmovbl %ebx, %r8d
movl %edx, %r11d
subl %r9d, %r11d
movslq %r11d, %r11
cmpq 0x28(%rsp), %r11
jl 0xa2e1e
subl %ebp, %r8d
movl %r8d, %r11d
subl %r10d, %r11d
movslq %r11d, %r11
cmpq 0x28(%rsp), %r11
jge 0xa2e51
movabsq $-0x8000000000000000, %rbx # imm = 0x8000000000000000
orq %rbx, %rsi
movq %rsi, (%rax,%r12)
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rsp), %rdi
movq 0x48(%rsp), %rdx
callq 0xa13a9
testq %rax, %rax
je 0xa2e81
orq %rbx, 0x18(%rax)
jmp 0xa2e81
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rdi
movslq %r9d, %rcx
orq %rdi, %rcx
movq %rcx, -0x18(%rax,%r12)
movl %edx, -0x10(%rax,%r12)
movl %r10d, -0x8(%rax,%r12)
movl %r8d, -0x4(%rax,%r12)
movq %rsi, (%rax,%r12)
incq 0x20(%rsp)
movq 0x10(%rsp), %r10
incq %r14
movl 0xc(%r10), %eax
addq $0x28, %r12
cmpq %rax, %r14
jb 0xa2cc4
cmpq $0x0, 0x20(%rsp)
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %rdx
jne 0xa2ebb
movq 0x18(%rsp), %rax
movq (%rax), %rax
orl $0x80000000, (%rax,%rdx,4) # imm = 0x80000000
incq %rcx
cmpq 0x30(%rsp), %rcx
jne 0xa2c86
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq