// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
callq 0x2510
movsd %xmm0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0xf0fc9
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
imulq $0x18, -0x30(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jge 0xf0fb6
movq -0x8(%rbp), %rax
imulq $0x18, -0x30(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
imulq $0x18, -0x30(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
imulq $0x18, -0x30(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
je 0xf0e15
jmp 0xf0fa5
movq -0x8(%rbp), %rax
imulq $0x18, -0x30(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
imulq $0x18, -0x30(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x1e, %rax
andq $0x1, %rax
cmpl $0x0, %eax
jne 0xf0fa3
cmpl $-0x1, -0x1c(%rbp)
je 0xf0e80
movl -0x4c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0xf0fa3
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movl -0x48(%rbp), %eax
movl %eax, %edx
movq -0x8(%rbp), %rax
imulq $0x18, -0x30(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
imulq $0x18, -0x30(%rbp), %r8
addq %r8, %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %r8
addq %r8, %rax
movl 0x8(%rax), %r8d
callq 0x167170
cmpl $0x0, %eax
jne 0xf0fa1
movq -0x8(%rbp), %rax
imulq $0x18, -0x30(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rcx
shrq $0x20, %rcx
andq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
orl $0x40000000, %ecx       # imm = 0x40000000
movl %ecx, %ecx
movl %ecx, %edx
movq 0x18(%rax), %rcx
andq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
shlq $0x20, %rdx
movabsq $-0x7fffffff00000001, %rsi # imm = 0x80000000FFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl -0x48(%rbp), %esi
movl -0x44(%rbp), %edx
callq 0xce1b0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rcx
shrq $0x20, %rcx
andq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
orl $0x40000000, %ecx       # imm = 0x40000000
movl %ecx, %ecx
movl %ecx, %edx
movq 0x18(%rax), %rcx
andq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
shlq $0x20, %rdx
movabsq $-0x7fffffff00000001, %rsi # imm = 0x80000000FFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x18(%rax)
jmp 0xf0fa3
jmp 0xf0fa5
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xf0d92
jmp 0xf0fb8
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xf0d7c
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq $0x0, -0x58(%rbp)
leaq 0x2d3818(%rip), %rax    # 0x3c47f8
movl 0x7c(%rax), %edi
movq -0x18(%rbp), %rcx
leaq 0x72(%rip), %rsi        # 0xf1060
leaq -0x88(%rbp), %rdx
callq 0x16d710
cmpl $-0x1, -0x1c(%rbp)
je 0xf1029
movq $0x1, -0x58(%rbp)
leaq 0x2d37e9(%rip), %rax    # 0x3c47f8
movl 0x7c(%rax), %edi
movq -0x18(%rbp), %rcx
leaq 0x43(%rip), %rsi        # 0xf1060
leaq -0x88(%rbp), %rdx
callq 0x16d710
movq $0x2, -0x58(%rbp)
leaq 0x2d37c0(%rip), %rax    # 0x3c47f8
movl 0x7c(%rax), %edi
movq -0x18(%rbp), %rcx
leaq 0x1a(%rip), %rsi        # 0xf1060
leaq -0x88(%rbp), %rdx
callq 0x16d710
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)