// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: clean_weak_ma_hit_t(ma_hit_t_alloc*, ma_hit_t_alloc*, long long, unsigned int)
void clean_weak_ma_hit_t(ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, long long num_sources, uint32_t ou_thres)
{
    double startTime = Get_T();
    long long i, j, index;
    uint32_t qn, tn, ou;

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

            if(sources[i].buffer[j].del) continue;
            ou = (sources[i].buffer[j].bl&((uint32_t)0x3fffffff));
            //if this is a weak overlap; ml == 0 -> weak overlap
            if((sources[i].buffer[j].ml == 0) && ((ou_thres==((uint32_t)-1)) || (ou < ou_thres)))
            {   
                if(
                !check_weak_ma_hit(&(sources[qn]), reverse_sources, tn, 
                Get_qs(sources[i].buffer[j]), Get_qe(sources[i].buffer[j]))
                /**
                ||
                !check_weak_ma_hit_reverse(&(reverse_sources[qn]), sources, tn)**/)
                {
                    sources[i].buffer[j].bl |= ((uint32_t)0x40000000);
                    index = get_specific_overlap(&(sources[tn]), tn, qn);
                    // if(index < 0 || index >= sources[tn].length) fprintf(stderr, "sb, tn: %u, qn: %u, index: %ld, length: %u\n", tn, qn, index, sources[tn].length);
                    sources[tn].buffer[index].bl |= ((uint32_t)0x40000000);
                    // ma_hit_t *h = &(sources[i].buffer[j]);
                    // if((Get_qn(*h) == 27087 && Get_tn(*h) == 27128) || (Get_tn(*h) == 27087 && Get_qn(*h) == 27128)) {
                    //     fprintf(stderr, "[M::%s]\t%.*s(qn::%u)\t%u\t%u\t%u\t%c\t%.*s(tn::%u)\t%u\t%u\t%u\t%u\t%u\t255\n", __func__, (int)Get_NAME_LENGTH(R_INF, Get_qn(*h)), Get_NAME((R_INF), Get_qn(*h)), Get_qn(*h), (uint32_t)Get_READ_LENGTH(R_INF, Get_qn(*h)), Get_qs(*h), Get_qe(*h), "+-"[h->rev], 
                    //         (int)Get_NAME_LENGTH(R_INF, Get_tn(*h)), Get_NAME((R_INF), Get_tn(*h)), Get_tn(*h), (uint32_t)Get_READ_LENGTH(R_INF, Get_tn(*h)), Get_ts(*h), Get_te(*h), h->ml, h->bl);
                    // }
                }
            }
        }
    }

    sset_aux s; s.src = sources; s.ul_occ = 0;
    kt_for(asm_opt.thread_num, update_weak_by_contain, &s, num_sources);

    if(ou_thres != ((uint32_t)-1)) {
        s.ul_occ = 1;
        kt_for(asm_opt.thread_num, update_weak_by_contain, &s, num_sources);
    }

    s.ul_occ = 2;
    kt_for(asm_opt.thread_num, update_weak_by_contain, &s, num_sources);

    // for (i = 0; i < num_sources; i++)
    // {
        
    //     for (j = 0; j < sources[i].length; j++)
    //     {
    //         if(sources[i].buffer[j].del) continue;

    //         if(sources[i].buffer[j].bl&((uint32_t)0x40000000))
    //         {
    //             sources[i].buffer[j].del = 1;
    //             sources[i].buffer[j].bl -= ((uint32_t)0x40000000);
    //         }
    //         else
    //         {
    //             sources[i].buffer[j].del = 0;
    //         }
    //     }
    // }

    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 $0x78, %rsp
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, 0x28(%rsp)
movq %rdi, %r14
callq 0x2f3fc
movq %rbx, %rcx
testq %rbx, %rbx
jle 0xb88ba
xorl %eax, %eax
movq %rcx, 0x30(%rsp)
movl %ebp, 0xc(%rsp)
movq %r14, 0x20(%rsp)
movq %rax, 0x38(%rsp)
leaq (%rax,%rax,2), %rax
movl 0xc(%r14,%rax,8), %r8d
testl %r8d, %r8d
je 0xb88a4
leaq (%r14,%rax,8), %r15
xorl %edi, %edi
movq %r15, 0x10(%rsp)
movq (%r15), %r11
leaq (%rdi,%rdi,4), %r12
movq 0x18(%r11,%r12,8), %r13
movabsq $-0x7fffffffc0000000, %rax # imm = 0x8000000040000000
testq %rax, %r13
jne 0xb8895
movq (%r11,%r12,8), %r10
movl 0xc(%r11,%r12,8), %r9d
cmpl $-0x1, %ebp
je 0xb8755
movq %r13, %rax
shrq $0x20, %rax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
cmpl %ebp, %eax
jae 0xb8895
movq %r10, %rbx
shrq $0x20, %rbx
leaq (%rbx,%rbx,2), %rax
movl 0xc(%r14,%rax,8), %ecx
testq %rcx, %rcx
je 0xb8895
leaq (%r11,%r12,8), %rdx
leaq (%r14,%rax,8), %rax
movl 0x8(%rdx), %edx
movl %edx, 0x1c(%rsp)
movq (%rax), %r15
xorl %r14d, %r14d
leaq (%r14,%r14,4), %rax
movq 0x18(%r15,%rax,8), %rdx
movabsq $-0x7fffffffc0000000, %rsi # imm = 0x8000000040000000
andq %rsi, %rdx
cmpq $0x40000000, %rdx       # imm = 0x40000000
jne 0xb87fc
leaq (%r15,%rax,8), %rax
cmpl %r10d, (%rax)
ja 0xb87fc
movl 0x1c(%rsp), %edx
cmpl %edx, 0x8(%rax)
jb 0xb87fc
movl 0xc(%rax), %ebp
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movq 0x28(%rsp), %rax
movl 0xc(%rax,%rdx,8), %eax
testq %rax, %rax
je 0xb87fc
movq 0x28(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
movq (%rdx), %rsi
shlq $0x3, %rax
leaq (%rax,%rax,4), %rax
xorl %edx, %edx
cmpl %r9d, 0xc(%rsi,%rdx)
jne 0xb87f3
cmpl 0x4(%rsi,%rdx), %ebp
je 0xb8818
addq $0x28, %rdx
cmpq %rdx, %rax
jne 0xb87e6
incq %r14
cmpq %rcx, %r14
jne 0xb8783
movl 0xc(%rsp), %ebp
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %r15
jmp 0xb8895
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
orq %rax, %r13
movq %r13, 0x18(%r11,%r12,8)
leaq (%r9,%r9,2), %rax
movq 0x20(%rsp), %r14
movl 0xc(%r14,%rax,8), %r8d
movq (%r14,%rax,8), %rcx
movq $-0x1, %rdx
testq %r8, %r8
je 0xb8870
movq %rcx, %r10
xorl %esi, %esi
movl 0xc(%rsp), %ebp
movq 0x10(%rsp), %r15
cmpl %ebx, 0xc(%r10)
jne 0xb8862
cmpl 0x4(%r10), %r9d
je 0xb887b
incq %rsi
addq $0x28, %r10
cmpq %rsi, %r8
jne 0xb8856
jmp 0xb887e
movl 0xc(%rsp), %ebp
movq 0x10(%rsp), %r15
jmp 0xb887e
movq %rsi, %rdx
leaq (%rdx,%rdx,4), %rax
movabsq $0x4000000000000000, %rdx # imm = 0x4000000000000000
orq %rdx, 0x18(%rcx,%rax,8)
movl 0xc(%r15), %r8d
incq %rdi
movl %r8d, %eax
cmpq %rax, %rdi
jb 0xb8714
movq 0x38(%rsp), %rax
incq %rax
movq 0x30(%rsp), %rcx
cmpq %rcx, %rax
jne 0xb86f2
leaq 0x40(%rsp), %rdx
movq %r14, 0x8(%rdx)
movq $0x0, 0x30(%rdx)
leaq 0x1dce86(%rip), %rbx    # 0x295758
movl 0x7c(%rbx), %edi
leaq 0x5a(%rip), %rsi        # 0xb8936
movq %rcx, %r14
callq 0x108f2c
cmpl $-0x1, %ebp
je 0xb8908
leaq 0x40(%rsp), %rdx
movq $0x1, 0x30(%rdx)
movl 0x7c(%rbx), %edi
leaq 0x36(%rip), %rsi        # 0xb8936
movq %r14, %rcx
callq 0x108f2c
leaq 0x40(%rsp), %rdx
movq $0x2, 0x30(%rdx)
movl 0x7c(%rbx), %edi
leaq 0x17(%rip), %rsi        # 0xb8936
movq %r14, %rcx
callq 0x108f2c
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq