// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: collect_abnormal_edges(ma_hit_t_alloc*, ma_hit_t_alloc*, long long)
void collect_abnormal_edges(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, long long readNum)
{
    double startTime = Get_T();
    long long T_edges, T_Single_Dir_Edges_0, T_Single_Dir_Edges_1, T_Conflict_Equal_Edges, T_Conflict_Strong_Edges;
    T_edges = T_Single_Dir_Edges_0 = T_Single_Dir_Edges_1 = T_Conflict_Equal_Edges = T_Conflict_Strong_Edges = 0;
    long long T_Single_Dir_Edges_1_1000 = 0;
    long long related_reads = 0;
    long long related_overlaps = 0;
    long long i, j;
    uint32_t qn, tn;
    int is_equal_f, is_strong_f; 
    int is_equal_b, is_strong_b, is_exist_b; 

    kvec_t(uint64_t) edge_vector;
    kv_init(edge_vector);
    
    for (i = 0; i < readNum; i++)
    {
        for (j = 0; j < (long long)paf[i].length; j++)
        {
            qn = Get_qn(paf[i].buffer[j]);
            tn = Get_tn(paf[i].buffer[j]);
            T_edges++;

            is_equal_f = paf[i].buffer[j].el;
            is_strong_f = paf[i].buffer[j].ml;
            
            is_exist_b = get_specific_overlap(&(paf[tn]), tn, qn);
            if(is_exist_b == -1)
            {
                is_exist_b = get_specific_overlap(&(rev_paf[tn]), tn, qn);
                if(is_exist_b != -1)
                {
                    T_Single_Dir_Edges_0++;
                    kv_push(uint64_t, edge_vector, qn);
                    kv_push(uint64_t, edge_vector, tn);
                    ///related_overlaps += paf[qn].length + rev_paf[qn].length + paf[tn].length + rev_paf[tn].length;
                    // fprintf(stderr, "%.*s(%d) ---(+)--> %.*s(%d), Len: %d\n", 
                    // Get_NAME_LENGTH(R_INF, qn), Get_NAME(R_INF, qn), qn,
                    // Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), tn,
                    // Get_qe(paf[i].buffer[j]) - Get_qs(paf[i].buffer[j]));

                    // fprintf(stderr, "%.*s(%d) ---(-)--> %.*s(%d), Len: %d\n\n", 
                    // Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), tn,
                    // Get_NAME_LENGTH(R_INF, qn), Get_NAME(R_INF, qn), qn,
                    // Get_qe(rev_paf[tn].buffer[is_exist_b]) - Get_qs(rev_paf[tn].buffer[is_exist_b]));
                } 
                else
                {
                    T_Single_Dir_Edges_1++;
                    if(Get_qe(paf[i].buffer[j]) - Get_qs(paf[i].buffer[j]) >= 1000)
                    {
                        T_Single_Dir_Edges_1_1000++;
                        // fprintf(stderr, "%.*s(%d) ---(%d)--> %.*s(%d), Len: %d\n\n", 
                        // Get_NAME_LENGTH(R_INF, qn), Get_NAME(R_INF, qn), qn,
                        // is_strong_f,
                        // Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), tn,
                        // Get_qe(paf[i].buffer[j]) - Get_qs(paf[i].buffer[j]));
                    }
                }

                related_reads = related_reads + 2;
            }
            else
            {
                is_equal_b = paf[tn].buffer[is_exist_b].el;
                is_strong_b = paf[tn].buffer[is_exist_b].ml;

                if(is_equal_f != is_equal_b)
                {
                    T_Conflict_Equal_Edges++;

                    // fprintf(stderr, "%.*s(%d) ---(%d)--> %.*s(%d), Len: %d\n", 
                    // Get_NAME_LENGTH(R_INF, qn), Get_NAME(R_INF, qn), qn,
                    // is_equal_f,
                    // Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), tn,
                    // Get_qe(paf[i].buffer[j]) - Get_qs(paf[i].buffer[j]));

                    // fprintf(stderr, "%.*s(%d) ---(%d)--> %.*s(%d), Len: %d\n\n", 
                    // Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), tn,
                    // is_equal_b,
                    // Get_NAME_LENGTH(R_INF, qn), Get_NAME(R_INF, qn), qn,
                    // Get_qe(paf[tn].buffer[is_exist_b]) - Get_qs(paf[tn].buffer[is_exist_b]));
                } 
                
                if(is_strong_f != is_strong_b)
                {
                    T_Conflict_Strong_Edges++;
                    // fprintf(stderr, "%.*s(%d) ---(%d)--> %.*s(%d), Len: %d\n", 
                    // Get_NAME_LENGTH(R_INF, qn), Get_NAME(R_INF, qn), qn,
                    // is_strong_f,
                    // Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), tn,
                    // Get_qe(paf[i].buffer[j]) - Get_qs(paf[i].buffer[j]));

                    // fprintf(stderr, "%.*s(%d) ---(%d)--> %.*s(%d), Len: %d\n\n", 
                    // Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), tn,
                    // is_strong_b,
                    // Get_NAME_LENGTH(R_INF, qn), Get_NAME(R_INF, qn), qn,
                    // Get_qe(paf[tn].buffer[is_exist_b]) - Get_qs(paf[tn].buffer[is_exist_b]));
                } 

                if(is_equal_f != is_equal_b || is_strong_f != is_strong_b)
                {
                    related_reads++;
                }
            }
        }
    }

    radix_sort_arch64(edge_vector.a, edge_vector.a + edge_vector.n);
    uint64_t pre = (uint64_t)-1;
    long long mn = 0;
    for (i = 0; i < (long long)edge_vector.n; i++)
    {
        if(pre != edge_vector.a[i])
        {
            mn++;
            pre = edge_vector.a[i];
            related_overlaps += paf[pre].length + rev_paf[pre].length;
        }

        if(i>0 && edge_vector.a[i] < edge_vector.a[i-1]) fprintf(stderr, "hehe\n");
    }
    
    

    fprintf(stderr, "****************statistic for abnormal overlaps****************\n");
    fprintf(stderr, "overlaps #: %lld\n", T_edges);
	fprintf(stderr, "one direction overlaps (different phasing)#: %lld\n", T_Single_Dir_Edges_0);
    fprintf(stderr, "one direction overlaps (missing)#: %lld\n", T_Single_Dir_Edges_1);
    fprintf(stderr, "one direction overlaps (missing) >= 1000#: %lld\n", T_Single_Dir_Edges_1_1000);
    fprintf(stderr, "conflict strong/weak overlaps #: %lld\n", T_Conflict_Strong_Edges);
    fprintf(stderr, "conflict exact/inexact overlaps #: %lld\n", T_Conflict_Equal_Edges);
    fprintf(stderr, "related_reads #: %lld/%lld\n", related_reads, mn);
    fprintf(stderr, "related_overlaps #: %lld\n", related_overlaps);
    fprintf(stderr, "****************statistic for abnormal overlaps****************\n");

    fprintf(stderr, "[M::%s] took %0.2fs\n\n", __func__, Get_T()-startTime);

    kv_destroy(edge_vector);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, %rbx
movq %rsi, 0x68(%rsp)
movq %rdi, 0x30(%rsp)
callq 0x251c
movsd %xmm0, 0x78(%rsp)
xorl %ecx, %ecx
testq %rbx, %rbx
movl $0x0, %eax
movq %rax, 0x70(%rsp)
cmovleq %rcx, %rbx
movq %rbx, 0x80(%rsp)
xorl %eax, %eax
movq $0x0, 0x50(%rsp)
movq $0x0, 0x58(%rsp)
movq $0x0, 0x60(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
xorl %edi, %edi
movq $0x0, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
cmpq 0x80(%rsp), %rax
je 0xb4652
movq %rdi, 0x10(%rsp)
movq %rax, 0x88(%rsp)
imulq $0x18, %rax, %rcx
addq 0x30(%rsp), %rcx
pushq $0x20
popq %r8
xorl %edx, %edx
movq %rcx, 0x28(%rsp)
movl 0xc(%rcx), %eax
cmpq %rax, %rdx
jae 0xb4638
movq %rdx, 0x18(%rsp)
movq (%rcx), %r13
movq -0x20(%r13,%r8), %rbx
movq %rbx, 0x90(%rsp)
shrq $0x20, %rbx
movl -0x14(%r13,%r8), %ebp
imulq $0x18, %rbp, %r15
movq 0x30(%rsp), %rax
leaq (%rax,%r15), %r12
movq %r12, %rdi
movl %ebp, %esi
movl %ebx, %edx
movq %r8, %r14
callq 0x696ab
cmpl $-0x1, %eax
jne 0xb4534
movq %r14, %r12
addq 0x68(%rsp), %r15
movq %r15, %rdi
movl %ebp, %esi
movl %ebx, %edx
callq 0x696ab
cmpl $-0x1, %eax
jne 0xb4580
incq 0x48(%rsp)
movq %r12, %r8
movl -0x18(%r13,%r12), %eax
subl 0x90(%rsp), %eax
cmpl $0x3e8, %eax            # imm = 0x3E8
sbbq $-0x1, 0x50(%rsp)
pushq $0x2
popq %rax
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rdx
jmp 0xb4627
movb (%r13,%r14), %cl
movq (%r12), %rdx
cltq
imulq $0x28, %rax, %rax
movq 0x18(%rdx,%rax), %rsi
xorl %edi, %edi
cmpb 0x20(%rdx,%rax), %cl
setne %dil
addq %rdi, 0x60(%rsp)
xorq -0x8(%r13,%r14), %rsi
shrl $0x1e, %esi
andl $0x1, %esi
addq %rsi, 0x58(%rsp)
orb %sil, %dil
movzbl %dil, %eax
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rdx
movq %r14, %r8
jmp 0xb4627
movq 0x20(%rsp), %rax
cmpq %rax, 0x8(%rsp)
jne 0xb45b1
leaq (%rax,%rax), %r14
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq 0x10(%rsp), %rdi
callq 0x2370
movq %rax, %rdi
jmp 0xb45b9
movq 0x10(%rsp), %rdi
movq %rax, %r14
movq 0x8(%rsp), %rcx
leaq 0x1(%rcx), %rax
movq %rbx, (%rdi,%rcx,8)
cmpq %r14, %rax
jne 0xb45ee
leaq (%r14,%r14), %rbx
testq %r14, %r14
pushq $0x2
popq %r14
cmoveq %r14, %rbx
leaq (,%rbx,8), %rsi
callq 0x2370
movq %rbx, 0x20(%rsp)
jmp 0xb45fa
movq %r14, 0x20(%rsp)
movq %rdi, %rax
pushq $0x2
popq %r14
movq 0x18(%rsp), %rdx
movq %r12, %r8
incq 0x40(%rsp)
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rcx
movq %rbp, 0x8(%rax,%rcx,8)
addq $0x2, %rcx
movq %rcx, 0x8(%rsp)
movq %r14, %rax
movq 0x28(%rsp), %rcx
addq %rax, 0x70(%rsp)
incq %rdx
addq $0x28, %r8
jmp 0xb449f
movq 0x88(%rsp), %rax
incq %rax
addq %rdx, 0x38(%rsp)
movq 0x10(%rsp), %rdi
jmp 0xb4470
movq 0x8(%rsp), %r13
leaq (%rdi,%r13,8), %rsi
movq %rdi, %r12
callq 0x66c25
xorl %edi, %edi
testq %r13, %r13
cmovleq %rdi, %r13
pushq $-0x1
popq %r15
xorl %r14d, %r14d
xorl %r8d, %r8d
cmpq %r14, %r13
je 0xb46de
movq (%r12,%r14,8), %rax
cmpq %rax, %r15
je 0xb46a3
incq %r8
imulq $0x18, %rax, %rcx
movq 0x68(%rsp), %rdx
movl 0xc(%rdx,%rcx), %edx
movq 0x30(%rsp), %rsi
addl 0xc(%rsi,%rcx), %edx
addq %rdx, %rdi
movq %rax, %r15
testq %r14, %r14
je 0xb46af
cmpq -0x8(%r12,%r14,8), %rax
jb 0xb46b4
incq %r14
jmp 0xb4676
movq 0x19e925(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
movq %rdi, %rbx
leaq 0x135e90(%rip), %rdi    # 0x1ea558
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %r8, %rbp
callq 0x2410
movq %rbp, %r8
movq %rbx, %rdi
jmp 0xb46af
movq 0x19e8fb(%rip), %rbx    # 0x252fe0
movq (%rbx), %rcx
leaq 0x1363e5(%rip), %r13    # 0x1eaad4
pushq $0x40
popq %rbp
pushq $0x1
popq %rdx
movq %rdi, %r14
movq %r13, %rdi
movq %rbp, %rsi
movq %r8, %r15
callq 0x2410
movq (%rbx), %rdi
leaq 0x1364a7(%rip), %rsi    # 0x1eabb7
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %rdi
leaq 0x1363ef(%rip), %rsi    # 0x1eab15
movq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %rdi
leaq 0x13640c(%rip), %rsi    # 0x1eab48
movq 0x48(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %rdi
leaq 0x13641f(%rip), %rsi    # 0x1eab71
movq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %rdi
leaq 0x13643a(%rip), %rsi    # 0x1eaba2
movq 0x58(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %rdi
leaq 0x13644b(%rip), %rsi    # 0x1eabc9
movq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %rdi
leaq 0x13645e(%rip), %rsi    # 0x1eabf2
movq 0x70(%rsp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %rdi
leaq 0x136461(%rip), %rsi    # 0x1eac0e
movq %r14, %rdx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %rcx
movq %r13, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq (%rbx), %rbx
callq 0x251c
subsd 0x78(%rsp), %xmm0
leaq 0x13644b(%rip), %rsi    # 0x1eac28
leaq 0x13645a(%rip), %rdx    # 0x1eac3e
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
movq %r12, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220