// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rcx, %rbx
movl %edx, 0xc(%rsp)
movq %rsi, %r14
movq %rdi, %r13
callq 0x300fc
movq %r14, 0x58(%rsp)
testq %r14, %r14
jle 0xa08dc
movq %rbx, %r9
leaq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
movq %rbx, 0x18(%rsp)
movq %r13, 0x70(%rsp)
movq %rax, 0x60(%rsp)
leaq (%rax,%rax,2), %rax
cmpl $0x0, 0xc(%r13,%rax,8)
je 0xa08c9
leaq (,%rax,8), %r10
addq %r13, %r10
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
movq %r10, 0x20(%rsp)
movq (%r10), %rax
leaq (,%rbp,4), %r15
addq %rbp, %r15
movq (%rax,%r15,8), %rcx
movq %rcx, %rbx
shrq $0x20, %rbx
movl 0xc(%rax,%r15,8), %r12d
leaq (%r12,%r12,2), %rdx
leaq (,%rdx,8), %r11
addq %r13, %r11
movl 0xc(%r11), %esi
movq $-0x1, %rdx
testq %rsi, %rsi
je 0xa0693
movq (%r11), %rdi
xorl %r8d, %r8d
cmpl %ebx, 0xc(%rdi)
jne 0xa0682
cmpl 0x4(%rdi), %r12d
je 0xa0690
incq %r8
addq $0x28, %rdi
cmpq %r8, %rsi
jne 0xa0677
jmp 0xa0693
movq %r8, %rdx
leaq (%rax,%r15,8), %rsi
cmpq $-0x1, %rdx
je 0xa0787
cmpq $0x0, 0x18(%rax,%r15,8)
leaq (%rdx,%rdx,4), %r14
js 0xa06bb
movq (%r11), %rax
xorl %r13d, %r13d
cmpq $0x0, 0x18(%rax,%r14,8)
jns 0xa06c1
movl $0x1, %r13d
movl 0x8(%rsi), %edx
subl %ecx, %edx
movq (%r11), %rax
movl 0x8(%rax,%r14,8), %ecx
subl (%rax,%r14,8), %ecx
cmpl %ecx, %edx
jne 0xa06d9
cmpl %ebx, %r12d
jbe 0xa06f8
leaq (%rax,%r14,8), %rdi
movq %r11, 0x68(%rsp)
callq 0xa052a
movq 0x68(%rsp), %r11
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %r9
cmpl $0x0, 0xc(%rsp)
je 0xa071c
movq (%r10), %rax
cmpb $0x0, 0x20(%rax,%r15,8)
je 0xa071c
movq (%r11), %rax
cmpb $0x0, 0x20(%rax,%r14,8)
movl $0x0, %eax
cmovnel %eax, %r13d
testq %r9, %r9
je 0xa0749
movzbl (%r9,%rbx), %ecx
leaq 0x1ea02b(%rip), %rax    # 0x28a758
movq 0x2b8(%rax), %rax
cmpq %rcx, %rax
jae 0xa0743
movzbl (%r9,%r12), %ecx
cmpq %rcx, %rax
jb 0xa0749
movl $0x1, %r13d
movq (%r10), %rax
shlq $0x3f, %r13
movq 0x18(%rax,%r15,8), %rcx
movabsq $0x7fffffffffffffff, %rdx # imm = 0x7FFFFFFFFFFFFFFF
andq %rdx, %rcx
orq %r13, %rcx
movq %rcx, 0x18(%rax,%r15,8)
movq (%r11), %rax
movq 0x18(%rax,%r14,8), %rcx
andq %rdx, %rcx
orq %r13, %rcx
movq %rcx, 0x18(%rax,%r14,8)
movq 0x70(%rsp), %r13
jmp 0xa08b9
movq %r12, %rdx
shlq $0x20, %rdx
movq %rdx, 0x30(%rsp)
movl 0x10(%rsi), %edi
orq %rdx, %rdi
movq %rdi, 0x30(%rsp)
movl 0x14(%rsi), %edx
movl %edx, 0x38(%rsp)
movl %ebx, 0x3c(%rsp)
movl %ecx, 0x40(%rsp)
movl 0x8(%rsi), %ecx
movl %ecx, 0x44(%rsp)
movq 0x18(%rsi), %rdx
movl %edx, %ecx
andl $0x80000000, %ecx       # imm = 0x80000000
movq 0x28(%rsp), %r8
btrq $0x1f, %r8
orq %rcx, %r8
movl %edx, %ecx
andl $0x40000000, %ecx       # imm = 0x40000000
movq %r8, %rdi
andq $-0x40000001, %rdi      # imm = 0xBFFFFFFF
orq %rcx, %rdi
movabsq $0x7fffffff00000000, %rcx # imm = 0x7FFFFFFF00000000
andq %rcx, %rdx
movq %r8, 0x48(%rsp)
movb 0x20(%rsi), %cl
movb %cl, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
movq %rdi, %r8
movabsq $-0x7fffffff00000001, %rdi # imm = 0x80000000FFFFFFFF
andq %rdi, %r8
orq %rdx, %r8
movb 0x21(%rsi), %dl
movb %dl, 0x51(%rsp)
movq %r8, 0x48(%rsp)
cmpl $0x0, 0xc(%rsp)
je 0xa083b
cmpb $0x0, 0x20(%rax,%r15,8)
sete %dl
testb %cl, %cl
sete %cl
orb %dl, %cl
movzbl %cl, %ecx
jmp 0xa0840
movl $0x1, %ecx
testq %r9, %r9
je 0xa086c
movzbl (%r9,%rbx), %esi
leaq 0x1e9f07(%rip), %rdx    # 0x28a758
movq 0x2b8(%rdx), %rdx
cmpq %rsi, %rdx
jae 0xa0867
movzbl (%r9,%r12), %esi
cmpq %rsi, %rdx
jb 0xa086c
movl $0x1, %ecx
shlq $0x3f, %rcx
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
andq %rsi, %r8
orq %rcx, %r8
movq %r8, 0x48(%rsp)
movq 0x18(%rax,%r15,8), %rdx
andq %rsi, %rdx
orq %rcx, %rdx
movq %rdx, 0x18(%rax,%r15,8)
movq %r11, %rdi
leaq 0x30(%rsp), %rsi
movq %r8, %rbx
callq 0x9fdd9
movq 0x18(%rsp), %r9
movq %rbx, 0x10(%rsp)
movq %rbx, 0x28(%rsp)
movq 0x20(%rsp), %r10
incq %rbp
movl 0xc(%r10), %eax
cmpq %rax, %rbp
jb 0xa0634
movq 0x60(%rsp), %rax
incq %rax
cmpq 0x58(%rsp), %rax
jne 0xa0603
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq