// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movq %rdx, %rbx
movq %rsi, 0x68(%rsp)
movq %rdi, 0x10(%rsp)
callq 0x2f3fc
movsd %xmm0, 0x70(%rsp)
movq %rbx, 0x78(%rsp)
testq %rbx, %rbx
jle 0xe34a
movq $0x0, 0x20(%rsp)
movabsq $0x100000000, %r13      # imm = 0x100000000
movq $0x0, 0x60(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
movq $0x0, 0x48(%rsp)
xorl %edi, %edi
xorl %esi, %esi
xorl %r8d, %r8d
xorl %edx, %edx
xorl %ecx, %ecx
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
movq %rcx, 0x80(%rsp)
imulq $0x18, %rcx, %r9
movl 0xc(%rax,%r9), %ecx
movl %ecx, 0x54(%rsp)
testl %ecx, %ecx
je 0xe271
addq %rax, %r9
xorl %r12d, %r12d
movq %r9, 0x58(%rsp)
movq %rdi, 0x40(%rsp)
movq %rdx, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
movq %rbp, 0x18(%rsp)
movq (%r9), %rdx
imulq $0x28, %r12, %rsi
movq (%rdx,%rsi), %rcx
movq %rcx, %rbp
shrq $0x20, %rbp
movl 0xc(%rdx,%rsi), %ebx
imulq $0x18, %rbx, %r8
movl 0xc(%rax,%r8), %eax
testq %rax, %rax
je 0xe0a1
movb 0x20(%rdx,%rsi), %r9b
movq 0x18(%rdx,%rsi), %rdi
movq 0x10(%rsp), %r10
addq %r8, %r10
movq (%r10), %r10
imulq $0x28, %rax, %rax
xorl %r11d, %r11d
xorl %r14d, %r14d
cmpl %ebp, 0xc(%r10,%r14)
jne 0xe080
cmpl 0x4(%r10,%r14), %ebx
je 0xe08e
addq $0x28, %r14
addq %r13, %r11
cmpq %r14, %rax
jne 0xe072
jmp 0xe0a1
movabsq $0x27ffffffd8, %rax     # imm = 0x27FFFFFFD8
cmpq %r14, %rax
jne 0xe140
movq 0x68(%rsp), %rax
movl 0xc(%rax,%r8), %edi
testq %rdi, %rdi
je 0xe0eb
addq 0x68(%rsp), %r8
movq (%r8), %rax
imulq $0x28, %rdi, %rdi
xorl %r8d, %r8d
cmpl %ebp, 0xc(%rax,%r8)
jne 0xe0cd
cmpl 0x4(%rax,%r8), %ebx
je 0xe0d8
addq $0x28, %r8
cmpq %r8, %rdi
jne 0xe0bf
jmp 0xe0eb
movabsq $0x27ffffffd8, %rax     # imm = 0x27FFFFFFD8
cmpq %r8, %rax
jne 0xe186
addq %rsi, %rdx
movq 0x40(%rsp), %rdi
incq %rdi
movl 0x8(%rdx), %eax
subl %ecx, %eax
cmpl $0x3e8, %eax            # imm = 0x3E8
movq 0x28(%rsp), %rdx
sbbq $-0x1, %rdx
pushq $0x2
popq %rcx
movq 0x18(%rsp), %rbp
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %r8
movq 0x58(%rsp), %r9
addq %rcx, %rbp
incq %r15
incq %r12
movl 0x54(%rsp), %eax
cmpq %rax, %r12
movq 0x10(%rsp), %rax
jb 0xe016
jmp 0xe271
sarq $0x20, %r11
imulq $0x28, %r11, %rax
movq 0x18(%r10,%rax), %rcx
xorl %edx, %edx
cmpb 0x20(%r10,%rax), %r9b
setne %dl
movq 0x38(%rsp), %rsi
addq %rdx, %rsi
xorl %ecx, %edi
shrl $0x1e, %edi
andl $0x1, %edi
movq 0x30(%rsp), %r8
addq %rdi, %r8
orb %dil, %dl
movzbl %dl, %ecx
movq 0x18(%rsp), %rbp
movq 0x28(%rsp), %rdx
movq 0x40(%rsp), %rdi
jmp 0xe11b
movq 0x20(%rsp), %rcx
movq 0x60(%rsp), %rdx
cmpq %rdx, %rcx
jne 0xe1c2
leaq (%rcx,%rcx), %r14
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq %r14, %rdx
movq 0x20(%rsp), %rcx
movq %rax, %rdi
jmp 0xe1c7
movq 0x8(%rsp), %rdi
leaq 0x1(%rcx), %rax
movq %rbp, (%rdi,%rcx,8)
cmpq %rdx, %rax
jne 0xe21a
leaq (%rdx,%rdx), %r14
testq %rdx, %rdx
pushq $0x2
popq %rbp
cmoveq %rbp, %r14
leaq (,%r14,8), %rsi
callq 0x2370
movq %rbp, %r10
movq 0x20(%rsp), %rcx
movq %rax, %rdi
movq %r14, 0x60(%rsp)
movq 0x18(%rsp), %rbp
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %rdx
movq 0x48(%rsp), %rax
jmp 0xe23c
movq %rdx, 0x60(%rsp)
movq 0x18(%rsp), %rbp
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %rdx
movq 0x48(%rsp), %rax
pushq $0x2
popq %r10
movq 0x58(%rsp), %r9
incq %rax
movq %rax, 0x48(%rsp)
movq %rdi, 0x8(%rsp)
movq %rbx, 0x8(%rdi,%rcx,8)
addq $0x2, %rcx
movq %rcx, 0x20(%rsp)
movl 0xc(%r9), %eax
movl %eax, 0x54(%rsp)
movq %r10, %rcx
movq 0x40(%rsp), %rdi
jmp 0xe120
movq 0x80(%rsp), %rcx
incq %rcx
cmpq 0x78(%rsp), %rcx
jne 0xdfee
movq %rdi, 0x40(%rsp)
movq %rdx, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rbx
leaq (%rdi,%rbx,8), %rsi
callq 0x9dc69
testq %rbx, %rbx
jle 0xe38f
movq %rbx, %rcx
pushq $-0x1
popq %r13
xorl %r8d, %r8d
xorl %r12d, %r12d
xorl %edi, %edi
movq 0x8(%rsp), %r14
movq (%r14,%r12,8), %rax
cmpq %rax, %r13
je 0xe2fd
incq %r8
imulq $0x18, %rax, %rcx
movq 0x68(%rsp), %rdx
movl 0xc(%rdx,%rcx), %edx
movq 0x10(%rsp), %rsi
addl 0xc(%rsi,%rcx), %edx
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r14
addq %rdx, %rdi
movq %rax, %r13
testq %r12, %r12
je 0xe309
cmpq -0x8(%r14,%r12,8), %rax
jb 0xe316
incq %r12
cmpq %r12, %rcx
jne 0xe2cb
jmp 0xe3b2
movq 0x286cc3(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
movq %rdi, %r14
leaq 0x22a32e(%rip), %rdi    # 0x238658
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %r8, %rbx
callq 0x2410
movq %rbx, %r8
movq %r14, %rdi
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r14
jmp 0xe309
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x9dc69
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %r14d, %r14d
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
jmp 0xe3d0
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x28(%rsp), %r12
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %rbx
jmp 0xe3d0
movq %r14, 0x8(%rsp)
movq 0x28(%rsp), %r12
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %rbx
movq %rdi, 0x18(%rsp)
movq %r8, 0x10(%rsp)
movq 0x286c09(%rip), %r13    # 0x294fe0
movq (%r13), %rcx
leaq 0x22a7f2(%rip), %rdi    # 0x238bd4
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq (%r13), %rdi
leaq 0x22a8bf(%rip), %rsi    # 0x238cb7
movq %r15, %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r13), %rdi
leaq 0x22a808(%rip), %rsi    # 0x238c15
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r13), %rdi
leaq 0x22a826(%rip), %rsi    # 0x238c48
movq %r14, %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r13), %rdi
leaq 0x22a83a(%rip), %rsi    # 0x238c71
movq %r12, %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r13), %rdi
leaq 0x22a856(%rip), %rsi    # 0x238ca2
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r13), %rdi
leaq 0x22a866(%rip), %rsi    # 0x238cc9
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r13), %rdi
leaq 0x22a878(%rip), %rsi    # 0x238cf2
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x22b0
movq (%r13), %rdi
leaq 0x22a87a(%rip), %rsi    # 0x238d0e
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r13), %rcx
leaq 0x22a729(%rip), %rdi    # 0x238bd4
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq (%r13), %rbx
callq 0x2f3fc
subsd 0x70(%rsp), %xmm0
leaq 0x22a85c(%rip), %rsi    # 0x238d28
leaq 0x22a86b(%rip), %rdx    # 0x238d3e
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
movq 0x8(%rsp), %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220