// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: normalize_ma_hit_t_single_side_advance(ma_hit_t_alloc*, long long, unsigned int, unsigned char*)
void normalize_ma_hit_t_single_side_advance(ma_hit_t_alloc* sources, long long num_sources, uint32_t recuse_el, uint8_t *cmk)
{
    double startTime = Get_T();

    long long i, j, index;
    uint32_t qn, tn, is_del = 0;
    long long qLen_0, qLen_1;
    ma_hit_t ele;
    for (i = 0; i < num_sources; i++)
    {

        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]);

            ///if(sources[i].buffer[j].del) continue;

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


            ///if(index != -1 && sources[tn].buffer[index].del == 0)
            if(index != -1) {
                is_del = 0;
                if(sources[i].buffer[j].del || sources[tn].buffer[index].del) is_del = 1;

                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) {
                    ///qn must be not equal to tn
                    ///make sources[qn] = sources[tn] if qn > tn
                    if(qn < tn) {
                        set_reverse_overlap(&(sources[tn].buffer[index]), &(sources[i].buffer[j]));
                    }
                } else if(qLen_0 > qLen_1) {
                    set_reverse_overlap(&(sources[tn].buffer[index]), &(sources[i].buffer[j]));
                }

                if(recuse_el && sources[i].buffer[j].el && sources[tn].buffer[index].el) is_del = 0;
                if(cmk && (cmk[qn] <= asm_opt.chemical_cov/**FORCE_CUT**/ || cmk[tn] <= asm_opt.chemical_cov/**FORCE_CUT**/)) is_del = 1;
                
                sources[i].buffer[j].del = is_del;
                sources[tn].buffer[index].del = is_del;
            } else {///means this edge just occurs in one direction
                is_del = 1; 
                set_reverse_overlap(&ele, &(sources[i].buffer[j]));
                if(recuse_el && sources[i].buffer[j].el && ele.el) is_del = 0;
                if(cmk && (cmk[qn] <= asm_opt.chemical_cov/**FORCE_CUT**/ || cmk[tn] <= asm_opt.chemical_cov/**FORCE_CUT**/)) is_del = 1;
                sources[i].buffer[j].del = ele.del = is_del;
                add_ma_hit_t_alloc(&(sources[tn]), &ele);
            }
        }
    }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r12
movl %edx, 0x4(%rsp)
movq %rsi, %rbx
movq %rdi, 0x20(%rsp)
callq 0x251c
xorl %eax, %eax
testq %rbx, %rbx
cmovleq %rax, %rbx
movq %rbx, 0x30(%rsp)
xorl %eax, %eax
movq %r12, 0x8(%rsp)
cmpq 0x30(%rsp), %rax
je 0x699bc
movq %rax, 0x38(%rsp)
imulq $0x18, %rax, %rsi
addq 0x20(%rsp), %rsi
xorl %ebx, %ebx
xorl %ecx, %ecx
movq %rsi, 0x10(%rsp)
movl 0xc(%rsi), %eax
cmpq %rax, %rcx
jae 0x699af
movq %rcx, 0x28(%rsp)
movq (%rsi), %r15
leaq (%r15,%rbx), %r13
movq (%r15,%rbx), %r12
movq %r12, %r14
shrq $0x20, %r14
movl 0xc(%r15,%rbx), %esi
imulq $0x18, %rsi, %rbp
addq 0x20(%rsp), %rbp
movq %rbp, %rdi
movq %rsi, 0x18(%rsp)
movl %r14d, %edx
callq 0x696ab
cmpq $-0x1, %rax
je 0x69890
movq %r13, %rsi
cmpq $0x0, 0x18(%r15,%rbx)
movq (%rbp), %rdi
movq %rbp, %r13
js 0x69829
imulq $0x28, %rax, %rcx
xorl %ebp, %ebp
cmpq $0x0, 0x18(%rdi,%rcx)
jns 0x6982e
pushq $0x1
popq %rcx
movl %ecx, %ebp
movl 0x8(%r15,%rbx), %ecx
subl %r12d, %ecx
imulq $0x28, %rax, %r15
movl 0x8(%rdi,%r15), %eax
subl (%rdi,%r15), %eax
cmpl %eax, %ecx
jne 0x6984c
cmpl %r14d, 0x18(%rsp)
movq 0x8(%rsp), %r12
jbe 0x6985b
addq %r15, %rdi
callq 0x696da
cmpl $0x0, 0x4(%rsp)
je 0x69933
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
cmpb $0x0, 0x20(%rax,%rbx)
je 0x69938
movq (%r13), %rax
cmpb $0x0, 0x20(%rax,%r15)
movl $0x0, %eax
cmovnel %eax, %ebp
jmp 0x69938
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x696da
pushq $0x1
popq %rax
cmpl $0x0, 0x4(%rsp)
je 0x698bd
cmpb $0x0, 0x20(%r15,%rbx)
sete %al
cmpb $0x0, 0x60(%rsp)
sete %cl
orb %al, %cl
movzbl %cl, %eax
movq 0x8(%rsp), %r12
testq %r12, %r12
je 0x698f1
movzbl (%r12,%r14), %edx
leaq 0x1e9e85(%rip), %rcx    # 0x253758
movq 0x2b8(%rcx), %rcx
cmpq %rdx, %rcx
jae 0x698ee
movq 0x18(%rsp), %rdx
movzbl (%r12,%rdx), %edx
cmpq %rdx, %rcx
jb 0x698f1
pushq $0x1
popq %rax
shlq $0x3f, %rax
movq 0x58(%rsp), %rcx
movabsq $0x7fffffffffffffff, %rdx # imm = 0x7FFFFFFFFFFFFFFF
andq %rdx, %rcx
orq %rax, %rcx
movq %rcx, 0x58(%rsp)
movq 0x18(%r15,%rbx), %rcx
andq %rdx, %rcx
orq %rax, %rcx
movq %rcx, 0x18(%r15,%rbx)
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x69052
movq 0x10(%rsp), %rsi
jmp 0x6999e
movq 0x10(%rsp), %rsi
testq %r12, %r12
je 0x69969
movzbl (%r12,%r14), %ecx
leaq 0x1e9e0f(%rip), %rax    # 0x253758
movq 0x2b8(%rax), %rax
cmpq %rcx, %rax
jae 0x69964
movq 0x18(%rsp), %rcx
movzbl (%r12,%rcx), %ecx
cmpq %rcx, %rax
jb 0x69969
pushq $0x1
popq %rax
movl %eax, %ebp
movq (%rsi), %rax
shlq $0x3f, %rbp
movq 0x18(%rax,%rbx), %rcx
movabsq $0x7fffffffffffffff, %rdx # imm = 0x7FFFFFFFFFFFFFFF
andq %rdx, %rcx
orq %rbp, %rcx
movq %rcx, 0x18(%rax,%rbx)
movq (%r13), %rax
movq 0x18(%rax,%r15), %rcx
andq %rdx, %rcx
orq %rbp, %rcx
movq %rcx, 0x18(%rax,%r15)
movq 0x28(%rsp), %rcx
incq %rcx
addq $0x28, %rbx
jmp 0x697be
movq 0x38(%rsp), %rax
incq %rax
jmp 0x6979c
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq