// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movss %xmm0, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
callq 0x2510
movsd %xmm0, -0x40(%rbp)
callq 0x9690
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0xd3069
movq -0x20(%rbp), %rax
imulq $0xc, -0x48(%rbp), %rcx
addq %rcx, %rax
movb $0x0, 0x8(%rax)
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl %eax, %ecx
movl -0x88(%rbp), %eax
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, -0x88(%rbp)
movl -0x7c(%rbp), %eax
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, -0x7c(%rbp)
movl $0x0, -0x84(%rbp)
movl $0x0, -0x78(%rbp)
movq -0x48(%rbp), %rax
movl %eax, %edi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
leaq -0x7c(%rbp), %r8
leaq -0x88(%rbp), %r9
movq %rax, (%rsp)
callq 0xd1f20
movl -0x7c(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
cmpq -0x50(%rbp), %rax
je 0xd2d8b
movl -0x88(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
cmpq -0x50(%rbp), %rax
jne 0xd2d90
jmp 0xd3058
movq -0x8(%rbp), %rdi
imulq $0x18, -0x48(%rbp), %rax
addq %rax, %rdi
movq -0x50(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
leaq -0x7c(%rbp), %rcx
leaq -0x88(%rbp), %r8
movss 0x2b3ae1(%rip), %xmm0   # 0x386898
callq 0xd21b0
movl -0x78(%rbp), %eax
movl -0x88(%rbp), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jbe 0xd2dfa
movl -0x78(%rbp), %eax
movl -0x88(%rbp), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
cvtsi2ss %rax, %xmm0
cvtsi2ssq -0x50(%rbp), %xmm1
mulss -0x24(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jb 0xd2dfa
jmp 0xd3058
movl -0x78(%rbp), %eax
movl -0x88(%rbp), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
ja 0xd2e31
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x48(%rbp), %rax
movl %eax, %edx
callq 0xd0080
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xd3058
movl -0x88(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x6c(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x70(%rbp)
cmpq $0x5f0, -0x98(%rbp) # imm = 0x5F0
jae 0xd2f1f
movq $0x5f0, -0x98(%rbp) # imm = 0x5F0
movq -0x98(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
shrq %rax
orq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
shrq $0x2, %rax
orq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
shrq $0x4, %rax
orq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
shrq $0x8, %rax
orq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
shrq $0x10, %rax
orq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
shlq $0x0, %rsi
callq 0x2360
movq %rax, -0x90(%rbp)
jmp 0xd2f21
jmp 0xd2f23
cmpq $0x5f0, -0xb0(%rbp) # imm = 0x5F0
jae 0xd2ffd
movq $0x5f0, -0xb0(%rbp) # imm = 0x5F0
movq -0xb0(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shrq %rax
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shrq $0x2, %rax
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shrq $0x4, %rax
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shrq $0x8, %rax
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
shrq $0x10, %rax
orq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
shlq $0x0, %rsi
callq 0x2360
movq %rax, -0xa8(%rbp)
jmp 0xd2fff
movq -0x8(%rbp), %rdi
imulq $0x18, -0x48(%rbp), %rax
addq %rax, %rdi
movq -0x50(%rbp), %rsi
movl -0x6c(%rbp), %edx
movl -0x70(%rbp), %ecx
movq -0x90(%rbp), %r8
movq -0xa8(%rbp), %r9
callq 0xd25b0
cmpl $0x0, %eax
je 0xd304c
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x48(%rbp), %rax
movl %eax, %edx
callq 0xd0080
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xd2ccd
movq -0x90(%rbp), %rdi
callq 0x2210
movq -0xa8(%rbp), %rdi
callq 0x2210
addq $0xc0, %rsp
popq %rbp
retq
nopw (%rax,%rax)