// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: detect_chimeric_reads(ma_hit_t_alloc*, long long, unsigned long*, ma_sub_t*, float, all_ul_t*, unsigned long)
void detect_chimeric_reads(ma_hit_t_alloc* paf, long long n_read, uint64_t* readLen, 
ma_sub_t* coverage_cut, float shift_rate, all_ul_t *x, uint64_t ul_thres)
{
    double startTime = Get_T();
    init_aux_table();
    long long i, rLen, n_simple_remove = 0, n_complex_remove = 0, n_complex_remove_real = 0;
    uint32_t interval_s, interval_e;
    ma_sub_t max_left, max_right;
    kvec_t(char) b_q = {0,0,0};
    kvec_t(char) b_t = {0,0,0};
    for (i = 0; i < n_read; ++i) 
    {
        coverage_cut[i].c = PRIMARY_LABLE;
        rLen = readLen[i];


        max_left.s = max_right.s = rLen;
        max_left.e = max_right.e = 0;
        ///we just need to check UL alignment here as we only need UL which covers the whole HiFi read
        collect_sides(i, paf, x, rLen, &max_left, &max_right, ul_thres);
        ///collect_sides(&(rev_paf[i]), rLen, &max_left, &max_right);
        ///that means this read is an end node
        if(max_left.s == rLen || max_right.s == rLen)
        {
            continue;
        }
        collect_contain(&(paf[i]), NULL, rLen, &max_left, &max_right, 0.1);
        ///collect_contain(&(paf[i]), &(rev_paf[i]), rLen, &max_left, &max_right, 0.1);
        ////shift_rate should be (asm_opt.max_ov_diff_final*2)
        ///this read is a normal read
        if (max_left.e > max_right.s && (max_left.e - max_right.s >= rLen * shift_rate))
        {
            continue;
        }
        ///simple chimeric reads
        if(max_left.e <= max_right.s)
        {
            delete_all_edges(paf, coverage_cut, i);            
            n_simple_remove++;
            continue;
        }

        ///now max_left.e >  max_right.s && max_left.e - max_right.s is small enough
        //[interval_s, interval_e)
        interval_s = max_right.s;
        interval_e = max_left.e;

        /**
        cov = 0;
        cov += intersection_check(&(paf[i]), rLen, interval_s, interval_e);
        cov += intersection_check(&(rev_paf[i]), rLen, interval_s, interval_e);
        if(interval_e - interval_s < WINDOW && cov <= 2)
        {
            coverage_cut[i].del = 1;
            paf[i].length = 0;
            n_complex_remove_real++;
        }
        else**/
        {
            kv_resize(char, b_q, WINDOW*4+20);
            kv_resize(char, b_t, WINDOW*4+20);
            if(intersection_check_by_base(&(paf[i]), rLen, interval_s, interval_e, b_q.a, b_t.a)
              /**||
              intersection_check_by_base(&(rev_paf[i]), rLen, interval_s, interval_e, b_q.a, b_t.a)**/)
            {
                delete_all_edges(paf, coverage_cut, i); 
                n_complex_remove_real++;
            }
        }

        n_complex_remove++;
    }

    free(b_q.a);
    free(b_t.a);

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] takes %0.2f s, n_simple_remove: %lld, n_complex_remove: %lld/%lld\n\n", 
        __func__, Get_T()-startTime, n_simple_remove, n_complex_remove_real, n_complex_remove);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %r12
movq %r8, %r13
movss %xmm0, 0x44(%rsp)
movq %rcx, %rbx
movq %rdx, 0x68(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
callq 0x251c
callq 0x6b0f
xorl %ecx, %ecx
testq %r14, %r14
movl $0x0, %eax
movq %rax, 0x50(%rsp)
cmovleq %rcx, %r14
movq %r14, 0x70(%rsp)
movq %rbx, 0x60(%rsp)
leaq 0x8(%rbx), %r15
movq %rbp, 0x58(%rsp)
movq $0x0, 0x8(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x48(%rsp)
movq $0x0, 0x10(%rsp)
movq %r12, 0x38(%rsp)
movq %r13, 0x30(%rsp)
cmpq %rbx, 0x70(%rsp)
je 0x6bc0f
movb $0x0, (%r15)
movq 0x68(%rsp), %rax
movq (%rax,%rbx,8), %r14
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x18(%rsp), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x18(%rsp)
movl 0x24(%rsp), %ecx
andl %edx, %ecx
orl %eax, %ecx
andl $0x0, 0x1c(%rsp)
movl %ecx, 0x24(%rsp)
andl $0x0, 0x28(%rsp)
movq %r12, (%rsp)
movl %ebx, %edi
movq 0x58(%rsp), %rsi
movq %r13, %rdx
movq %r14, %rcx
leaq 0x24(%rsp), %r8
leaq 0x18(%rsp), %r9
callq 0x6b3c7
movl 0x24(%rsp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r14
je 0x6bbff
movl 0x18(%rsp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r14
je 0x6bbff
movq %rbp, %rdi
xorl %esi, %esi
movq %r14, %rdx
leaq 0x24(%rsp), %rcx
leaq 0x18(%rsp), %r8
movss 0x17c18e(%rip), %xmm0   # 0x1e7cb0
callq 0x6b4f7
movl 0x28(%rsp), %r12d
movl 0x18(%rsp), %r13d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r13d
movl %r12d, %eax
subl %r13d, %eax
jbe 0x6bbd8
cvtsi2ss %rax, %xmm0
cvtsi2ss %r14, %xmm1
mulss 0x44(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jae 0x6bbf5
cmpq $0x5ef, 0x50(%rsp)      # imm = 0x5EF
ja 0x6bb87
movl $0x800, %eax            # imm = 0x800
movq %rax, 0x50(%rsp)
movl $0x800, %esi            # imm = 0x800
movq 0x8(%rsp), %rdi
callq 0x2370
movq %rax, 0x8(%rsp)
cmpq $0x5ef, 0x48(%rsp)      # imm = 0x5EF
ja 0x6bbb0
movl $0x800, %eax            # imm = 0x800
movq %rax, 0x48(%rsp)
movl $0x800, %esi            # imm = 0x800
movq 0x10(%rsp), %rdi
callq 0x2370
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
movl %r13d, %edx
movl %r12d, %ecx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
callq 0x6b6b6
testl %eax, %eax
movq 0x38(%rsp), %r12
movq 0x30(%rsp), %r13
jne 0x6bbe2
jmp 0x6bbff
movq 0x38(%rsp), %r12
movq 0x30(%rsp), %r13
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl %ebx, %edx
callq 0x6a345
jmp 0x6bbff
movq 0x38(%rsp), %r12
movq 0x30(%rsp), %r13
incq %rbx
addq $0xc, %r15
addq $0x18, %rbp
jmp 0x6ba7d
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220