// File: /workspace/github_cpp_makefile_star2_O1/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, 0x70(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
callq 0x300fc
callq 0x3366f
movq %r14, 0x68(%rsp)
testq %r14, %r14
jle 0xa2c1d
movq %rbx, 0x60(%rsp)
leaq 0x8(%rbx), %r15
movq %rbp, 0x58(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x50(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x8(%rsp)
movq $0x0, 0x48(%rsp)
movq %r12, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movb $0x0, (%r15)
movq 0x70(%rsp), %rax
movq (%rax,%rbx,8), %r14
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x2c(%rsp), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x2c(%rsp)
movl 0x38(%rsp), %ecx
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x38(%rsp)
xorl %eax, %eax
movl %eax, 0x30(%rsp)
movl %eax, 0x3c(%rsp)
movq %r12, (%rsp)
movl %ebx, %edi
movq 0x58(%rsp), %rsi
movq %r13, %rdx
movq %r14, %rcx
leaq 0x38(%rsp), %r8
leaq 0x2c(%rsp), %r9
callq 0xa262b
movl 0x38(%rsp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r14
je 0xa2c05
movl 0x2c(%rsp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r14
je 0xa2c05
movq %rbp, %rdi
xorl %esi, %esi
movq %r14, %rdx
leaq 0x38(%rsp), %rcx
leaq 0x2c(%rsp), %r8
movss 0x18709b(%rip), %xmm0   # 0x229bb8
callq 0xa277a
movl 0x3c(%rsp), %r12d
movl 0x2c(%rsp), %r13d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r13d
movl %r12d, %eax
subl %r13d, %eax
jbe 0xa2bde
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r14, %xmm1
mulss 0x44(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jae 0xa2bfb
cmpl %r13d, %r12d
jbe 0xa2bde
cmpq $0x5ef, 0x48(%rsp)      # imm = 0x5EF
ja 0xa2b8d
movl $0x800, %eax            # imm = 0x800
movq %rax, 0x48(%rsp)
movl $0x800, %esi            # imm = 0x800
movq 0x8(%rsp), %rdi
callq 0x2370
movq %rax, 0x8(%rsp)
cmpq $0x5ef, 0x50(%rsp)      # imm = 0x5EF
ja 0xa2bb6
movl $0x800, %eax            # imm = 0x800
movq %rax, 0x50(%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 0xa296d
testl %eax, %eax
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
jne 0xa2be8
jmp 0xa2c05
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl %ebx, %edx
callq 0xa142c
jmp 0xa2c05
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
incq %rbx
addq $0x18, %rbp
addq $0xc, %r15
cmpq %rbx, 0x68(%rsp)
jne 0xa2a83
jmp 0xa2c2f
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
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