// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: normalize_ma_hit_t_single_side_aggressive(ma_hit_t_alloc*, long long)
void normalize_ma_hit_t_single_side_aggressive(ma_hit_t_alloc* sources, long long num_sources)
{
    double startTime = Get_T();

    ///long long debug_prefect = 0, debug_not_bad = 0, debug_bad = 0;

    UC_Read query, target;
    init_UC_Read(&query);
    init_UC_Read(&target);

    long long i, j, index;
    uint32_t qn, tn, queryLeftLen, queryRightLen, targetLeftLen, targetRightLen;
    long long qLen_0, qLen_1, m;
    for (i = 0; i < num_sources; i++)
    {

        m = 0;
        for (j = 0; j < sources[i].length; j++)
        {
            qn = Get_qn(sources[i].buffer[j]);
            tn = Get_tn(sources[i].buffer[j]);

            // sources[i].buffer[j].bl = Get_qe(sources[i].buffer[j]) - Get_qs(sources[i].buffer[j]);

            index = get_specific_overlap(&(sources[tn]), tn, qn);


            if(index != -1)
            {
                // sources[tn].buffer[index].bl = Get_qe(sources[tn].buffer[index]) 
                //                                             - Get_qs(sources[tn].buffer[index]);

                if(Get_qs(sources[i].buffer[j]) == Get_ts(sources[tn].buffer[index])
                    &&
                   Get_qe(sources[i].buffer[j]) == Get_te(sources[tn].buffer[index])
                    && 
                   Get_ts(sources[i].buffer[j]) == Get_qs(sources[tn].buffer[index])
                    &&
                   Get_te(sources[i].buffer[j]) == Get_qe(sources[tn].buffer[index])
                    &&
                   sources[i].buffer[j].rev == sources[tn].buffer[index].rev)
                {
                    ///actually two edges are same, here is just to unify el, ml, no_l_indel, bl
                    set_reverse_overlap(&(sources[tn].buffer[index]), &(sources[i].buffer[j]));

                    sources[i].buffer[m] = sources[i].buffer[j];
                    m++;

                    ///debug_prefect++;
                    continue;
                }


                get_end_match_length(&(sources[i].buffer[j]), &query, &target,
                &queryLeftLen, &queryRightLen);

                get_end_match_length(&(sources[tn].buffer[index]), &query, &target,
                &targetLeftLen, &targetRightLen);

                ///query is right
                if(queryLeftLen>0 && queryRightLen>0 && (targetLeftLen==0 || targetRightLen==0))
                {
                    set_reverse_overlap(&(sources[tn].buffer[index]), &(sources[i].buffer[j]));

                    sources[i].buffer[m] = sources[i].buffer[j];
                    m++;

                    ///debug_not_bad++;
                    continue;
                }


                ///target is right
                if(targetLeftLen>0 && targetRightLen>0 && (queryLeftLen==0 || queryRightLen==0))
                {
                    set_reverse_overlap(&(sources[i].buffer[j]), &(sources[tn].buffer[index]));

                    sources[i].buffer[m] = sources[i].buffer[j];
                    m++;

                    ///debug_not_bad++;
                    continue;
                }


                ///query is right
                if((queryLeftLen+queryRightLen)>(targetLeftLen+targetRightLen))
                {
                    set_reverse_overlap(&(sources[tn].buffer[index]), &(sources[i].buffer[j]));
                }///target is right
                else if((queryLeftLen+queryRightLen)<(targetLeftLen+targetRightLen))
                {
                    set_reverse_overlap(&(sources[i].buffer[j]), &(sources[tn].buffer[index]));
                }
                else
                {
                    qLen_0 = Get_qe(sources[i].buffer[j]) - Get_qs(sources[i].buffer[j]);
                    qLen_1 = Get_qe(sources[tn].buffer[index]) - Get_qs(sources[tn].buffer[index]);
                    if(qLen_0 >= qLen_1)
                    {
                        set_reverse_overlap(&(sources[tn].buffer[index]), &(sources[i].buffer[j]));
                    }
                    else
                    {
                        set_reverse_overlap(&(sources[i].buffer[j]), &(sources[tn].buffer[index]));
                    }
                }

                sources[i].buffer[m] = sources[i].buffer[j];
                m++;

                ///debug_bad++;
            }
        }

        sources[i].length = m;
    }

    destory_UC_Read(&query);
    destory_UC_Read(&target);

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] takes %0.2fs\n\n", __func__, Get_T()-startTime);
    }

    // fprintf(stderr, "debug_prefect: %ld, debug_not_bad: %ld, debug_bad: %ld\n", debug_prefect, debug_not_bad,
    // debug_bad);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x2510
movsd %xmm0, -0x18(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x9850
leaq -0x58(%rbp), %rdi
callq 0x9850
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0xcf6d0
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x8(%rbp), %rcx
imulq $0x18, -0x60(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jge 0xcf6a7
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movl -0x78(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl -0x78(%rbp), %esi
movl -0x74(%rbp), %edx
callq 0xce1b0
movq %rax, -0x70(%rbp)
cmpq $-0x1, -0x70(%rbp)
je 0xcf694
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movl -0x78(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
imulq $0x28, -0x70(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x10(%rcx), %eax
jne 0xcf2e3
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x8(%rbp), %rcx
movl -0x78(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
imulq $0x28, -0x70(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x14(%rcx), %eax
jne 0xcf2e3
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x8(%rbp), %rcx
movl -0x78(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
imulq $0x28, -0x70(%rbp), %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
cmpl %ecx, %eax
jne 0xcf2e3
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq -0x8(%rbp), %rcx
movl -0x78(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
imulq $0x28, -0x70(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jne 0xcf2e3
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x1f, %rax
andq $0x1, %rax
movq -0x8(%rbp), %rcx
movl -0x78(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
imulq $0x28, -0x70(%rbp), %rdx
addq %rdx, %rcx
movq 0x18(%rcx), %rcx
shrq $0x1f, %rcx
andq $0x1, %rcx
cmpl %ecx, %eax
jne 0xcf2e3
movq -0x8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0x70(%rbp), %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rsi
callq 0xce230
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rsi
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0xa0(%rbp), %rax
addq %rax, %rdi
movl $0x28, %edx
callq 0x2180
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0xcf696
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rdi
leaq -0x38(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x7c(%rbp), %rcx
leaq -0x80(%rbp), %r8
callq 0xcee70
movq -0x8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0x70(%rbp), %rax
addq %rax, %rdi
leaq -0x38(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x84(%rbp), %rcx
leaq -0x88(%rbp), %r8
callq 0xcee70
cmpl $0x0, -0x7c(%rbp)
jbe 0xcf3f4
cmpl $0x0, -0x80(%rbp)
jbe 0xcf3f4
cmpl $0x0, -0x84(%rbp)
je 0xcf36d
cmpl $0x0, -0x88(%rbp)
jne 0xcf3f4
movq -0x8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0x70(%rbp), %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rsi
callq 0xce230
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rsi
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0xa0(%rbp), %rax
addq %rax, %rdi
movl $0x28, %edx
callq 0x2180
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0xcf696
cmpl $0x0, -0x84(%rbp)
jbe 0xcf4a5
cmpl $0x0, -0x88(%rbp)
jbe 0xcf4a5
cmpl $0x0, -0x7c(%rbp)
je 0xcf41e
cmpl $0x0, -0x80(%rbp)
jne 0xcf4a5
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x70(%rbp), %rax
addq %rax, %rsi
callq 0xce230
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rsi
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0xa0(%rbp), %rax
addq %rax, %rdi
movl $0x28, %edx
callq 0x2180
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0xcf696
movl -0x7c(%rbp), %eax
addl -0x80(%rbp), %eax
movl -0x84(%rbp), %ecx
addl -0x88(%rbp), %ecx
cmpl %ecx, %eax
jbe 0xcf4f5
movq -0x8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0x70(%rbp), %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rsi
callq 0xce230
jmp 0xcf647
movl -0x7c(%rbp), %eax
addl -0x80(%rbp), %eax
movl -0x84(%rbp), %ecx
addl -0x88(%rbp), %ecx
cmpl %ecx, %eax
jae 0xcf545
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x70(%rbp), %rax
addq %rax, %rsi
callq 0xce230
jmp 0xcf645
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x8(%rbp), %rcx
imulq $0x18, -0x60(%rbp), %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
imulq $0x28, -0x68(%rbp), %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x90(%rbp)
movq -0x8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x70(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x8(%rbp), %rcx
movl -0x78(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
imulq $0x28, -0x70(%rbp), %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x98(%rbp), %rax
jl 0xcf60e
movq -0x8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0x70(%rbp), %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rsi
callq 0xce230
jmp 0xcf643
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x70(%rbp), %rax
addq %rax, %rsi
callq 0xce230
jmp 0xcf645
jmp 0xcf647
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rsi
imulq $0x28, -0x68(%rbp), %rax
addq %rax, %rsi
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
imulq $0x28, -0xa0(%rbp), %rax
addq %rax, %rdi
movl $0x28, %edx
callq 0x2180
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0xcf696
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0xcf098
movq -0xa0(%rbp), %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
imulq $0x18, -0x60(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xcf077
leaq -0x38(%rbp), %rdi
callq 0x9670
leaq -0x58(%rbp), %rdi
callq 0x9670
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)