// File: /workspace/github_cpp_makefile_star2_O2/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, 0xc(%rsp)
movq %rdx, %r14
movq %rsi, 0x38(%rsp)
movq %rdi, 0x10(%rsp)
callq 0x251c
xorl %eax, %eax
testq %r14, %r14
movl $0x0, %ecx
cmovgq %r14, %rcx
movq %rcx, 0x20(%rsp)
movabsq $-0x7fffffffc0000000, %rsi # imm = 0x8000000040000000
movq %r14, 0x18(%rsp)
cmpq 0x20(%rsp), %rax
je 0x7d7f2
movq %rax, 0x28(%rsp)
imulq $0x18, %rax, %r15
addq 0x10(%rsp), %r15
pushq $0x18
popq %r14
xorl %ebx, %ebx
movl 0xc(%r15), %eax
cmpq %rax, %rbx
jae 0x7d7e0
movq (%r15), %rax
movq (%rax,%r14), %rdx
testq %rsi, %rdx
jne 0x7d7d4
movq -0x18(%rax,%r14), %rcx
movl -0xc(%rax,%r14), %r12d
cmpl $-0x1, 0xc(%rsp)
je 0x7d756
shrq $0x20, %rdx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
cmpl 0xc(%rsp), %edx
jae 0x7d7d4
movq %rcx, %r13
shrq $0x20, %r13
imulq $0x18, %r13, %rdi
addq 0x10(%rsp), %rdi
movl -0x10(%rax,%r14), %r8d
movq %rsi, %rbp
movq 0x38(%rsp), %rsi
movq %r12, %rdx
callq 0xc3138
movq %rbp, %rsi
testl %eax, %eax
jne 0x7d7d4
movq (%r15), %rax
movabsq $0x4000000000000000, %rcx # imm = 0x4000000000000000
orq %rcx, (%rax,%r14)
imulq $0x18, %r12, %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x696ab
movq %rbp, %rsi
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %rdx
movq (%rcx,%rdx), %rcx
imulq $0x28, %rax, %rax
movabsq $0x4000000000000000, %rdx # imm = 0x4000000000000000
orq %rdx, 0x18(%rcx,%rax)
incq %rbx
addq $0x28, %r14
jmp 0x7d718
movq 0x28(%rsp), %rax
incq %rax
movq 0x18(%rsp), %r14
jmp 0x7d6f9
leaq 0x40(%rsp), %rdx
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rdx)
andq $0x0, 0x30(%rdx)
leaq 0x1d5f4c(%rip), %rbx    # 0x253758
movl 0x7c(%rbx), %edi
leaq 0x5c(%rip), %rsi        # 0x7d872
movq %r14, %rcx
callq 0xc67c0
cmpl $-0x1, 0xc(%rsp)
je 0x7d844
leaq 0x40(%rsp), %rdx
movq $0x1, 0x30(%rdx)
movl 0x7c(%rbx), %edi
leaq 0x36(%rip), %rsi        # 0x7d872
movq %r14, %rcx
callq 0xc67c0
leaq 0x40(%rsp), %rdx
movq $0x2, 0x30(%rdx)
movl 0x7c(%rbx), %edi
leaq 0x17(%rip), %rsi        # 0x7d872
movq %r14, %rcx
callq 0xc67c0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq