// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movl %ecx, 0x4(%rsp)
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movq %rdi, 0x8(%rsp)
callq 0x300fc
testq %r14, %r14
jle 0xb7933
movabsq $-0x7fffffffc0000000, %rax # imm = 0x8000000040000000
xorl %ecx, %ecx
movq %r14, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
leaq (%rcx,%rcx,2), %rcx
movq 0x8(%rsp), %rdx
cmpl $0x0, 0xc(%rdx,%rcx,8)
je 0xb791d
movq 0x8(%rsp), %rdx
leaq (%rdx,%rcx,8), %rsi
xorl %edi, %edi
movq (%rsi), %r10
leaq (%rdi,%rdi,4), %r11
movq 0x18(%r10,%r11,8), %rcx
testq %rax, %rcx
jne 0xb790e
movq (%r10,%r11,8), %r12
movl 0xc(%r10,%r11,8), %r8d
cmpl $-0x1, 0x4(%rsp)
je 0xb77f7
shrq $0x20, %rcx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
cmpl 0x4(%rsp), %ecx
jae 0xb790e
movq %r12, %r9
shrq $0x20, %r9
leaq (%r9,%r9,2), %rcx
movq 0x8(%rsp), %rdx
movl 0xc(%rdx,%rcx,8), %r13d
testq %r13, %r13
je 0xb790e
leaq (%r10,%r11,8), %rdx
movq 0x8(%rsp), %rbx
leaq (%rbx,%rcx,8), %rcx
movl 0x8(%rdx), %edx
movl %edx, 0x14(%rsp)
movq (%rcx), %rcx
xorl %edx, %edx
leaq (%rdx,%rdx,4), %rbx
movq 0x18(%rcx,%rbx,8), %r14
andq %rax, %r14
cmpq $0x40000000, %r14       # imm = 0x40000000
jne 0xb78a0
leaq (%rcx,%rbx,8), %rbx
cmpl %r12d, (%rbx)
ja 0xb78a0
movl 0x14(%rsp), %ebp
cmpl %ebp, 0x8(%rbx)
jb 0xb78a0
movl 0xc(%rbx), %r15d
leaq (%r15,%r15,2), %r14
movq 0x18(%rsp), %rbx
movl 0xc(%rbx,%r14,8), %ebx
testq %rbx, %rbx
je 0xb78a0
movq 0x18(%rsp), %rbp
leaq (,%r14,8), %r14
addq %rbp, %r14
movq (%r14), %r14
shlq $0x3, %rbx
leaq (%rbx,%rbx,4), %rbp
xorl %ebx, %ebx
cmpl %r8d, 0xc(%r14,%rbx)
jne 0xb7897
cmpl 0x4(%r14,%rbx), %r15d
je 0xb78aa
addq $0x28, %rbx
cmpq %rbx, %rbp
jne 0xb7889
incq %rdx
cmpq %r13, %rdx
jne 0xb782e
jmp 0xb790e
movabsq $0x4000000000000000, %rcx # imm = 0x4000000000000000
orq %rcx, 0x18(%r10,%r11,8)
leaq (%r8,%r8,2), %rcx
movq 0x8(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movl 0xc(%rcx), %r10d
movq $-0x1, %rdx
testq %r10, %r10
je 0xb78f8
movq (%rcx), %r11
xorl %ebx, %ebx
cmpl %r9d, 0xc(%r11)
jne 0xb78e7
cmpl 0x4(%r11), %r8d
je 0xb78f5
incq %rbx
addq $0x28, %r11
cmpq %rbx, %r10
jne 0xb78db
jmp 0xb78f8
movq %rbx, %rdx
movq (%rcx), %rcx
leaq (%rdx,%rdx,4), %rdx
movabsq $0x4000000000000000, %r8 # imm = 0x4000000000000000
orq %r8, 0x18(%rcx,%rdx,8)
incq %rdi
movl 0xc(%rsi), %ecx
cmpq %rcx, %rdi
jb 0xb77be
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %r14
cmpq %r14, %rcx
jne 0xb779a
leaq 0x30(%rsp), %rdx
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rdx)
movq $0x0, 0x30(%rdx)
leaq 0x1d2e08(%rip), %rbx    # 0x28a758
movl 0x7c(%rbx), %edi
leaq 0x5c(%rip), %rsi        # 0xb79b6
movq %r14, %rcx
callq 0x1047b4
cmpl $-0x1, 0x4(%rsp)
je 0xb7988
leaq 0x30(%rsp), %rdx
movq $0x1, 0x30(%rdx)
movl 0x7c(%rbx), %edi
leaq 0x36(%rip), %rsi        # 0xb79b6
movq %r14, %rcx
callq 0x1047b4
leaq 0x30(%rsp), %rdx
movq $0x2, 0x30(%rdx)
movl 0x7c(%rbx), %edi
leaq 0x17(%rip), %rsi        # 0xb79b6
movq %r14, %rcx
callq 0x1047b4
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq