// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rdx, %r15
movq %rsi, 0x48(%rsp)
movq %rdi, %rbx
callq 0x300fc
movq %rbx, %r11
movsd %xmm0, 0x50(%rsp)
xorl %r14d, %r14d
movl $0x0, %eax
movq %rax, 0x30(%rsp)
movl $0x0, %eax
movq %rax, 0x38(%rsp)
movl $0x0, %eax
movq %rax, 0x40(%rsp)
movl $0x0, %eax
movq %rax, 0x28(%rsp)
movl $0x0, %eax
movq %rax, 0x20(%rsp)
movl $0x0, %ebx
movl $0x0, %edi
movl $0x0, %eax
movq %rax, 0x8(%rsp)
movq %r15, 0x58(%rsp)
testq %r15, %r15
movq %r11, 0x10(%rsp)
jle 0xe2a3
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
xorl %edi, %edi
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
xorl %ecx, %ecx
xorl %r14d, %r14d
movq %rcx, 0x60(%rsp)
imulq $0x18, %rcx, %rdx
cmpl $0x0, 0xc(%r11,%rdx)
je 0xe290
addq %r11, %rdx
xorl %r13d, %r13d
movq %rdx, 0x18(%rsp)
movq (%rdx), %rcx
imulq $0x28, %r13, %rsi
movq (%rcx,%rsi), %rdx
movq %rdx, %rbp
shrq $0x20, %rbp
movl 0xc(%rcx,%rsi), %r15d
imulq $0x18, %r15, %r12
leaq (%r11,%r12), %r8
movl 0xc(%r11,%r12), %r10d
pushq $-0x1
popq %r9
testq %r10, %r10
je 0xe143
movq (%r8), %r11
xorl %r9d, %r9d
cmpl %ebp, 0xc(%r11)
jne 0xe12e
cmpl 0x4(%r11), %r15d
je 0xe13e
incq %r9
addq $0x28, %r11
cmpq %r9, %r10
jne 0xe122
pushq $-0x1
popq %r9
movq 0x10(%rsp), %r11
cmpl $-0x1, %r9d
jne 0xe1a4
movq 0x48(%rsp), %r8
movl 0xc(%r8,%r12), %r8d
testq %r8, %r8
je 0xe186
addq 0x48(%rsp), %r12
movq (%r12), %r9
xorl %r10d, %r10d
cmpl %ebp, 0xc(%r9)
jne 0xe170
cmpl 0x4(%r9), %r15d
je 0xe180
incq %r10
addq $0x28, %r9
cmpq %r10, %r8
jne 0xe164
pushq $-0x1
popq %r10
cmpl $-0x1, %r10d
jne 0xe201
addq %rsi, %rcx
incq 0x28(%rsp)
movl 0x8(%rcx), %ecx
subl %edx, %ecx
cmpl $0x3e8, %ecx            # imm = 0x3E8
sbbq $-0x1, 0x30(%rsp)
pushq $0x2
popq %rcx
jmp 0xe1e2
movb 0x20(%rcx,%rsi), %dl
movq 0x18(%rcx,%rsi), %rcx
movq (%r8), %rsi
movslq %r9d, %r8
imulq $0x28, %r8, %r10
movq 0x18(%rsi,%r10), %r8
xorl %r9d, %r9d
cmpb 0x20(%rsi,%r10), %dl
setne %r9b
addq %r9, 0x40(%rsp)
xorl %r8d, %ecx
shrl $0x1e, %ecx
andl $0x1, %ecx
addq %rcx, 0x38(%rsp)
orb %cl, %r9b
movzbl %r9b, %ecx
movq 0x18(%rsp), %rdx
addq %rcx, %r14
incq %rbx
incq %r13
movl 0xc(%rdx), %ecx
cmpq %rcx, %r13
jb 0xe0ef
jmp 0xe290
cmpq %rax, 0x8(%rsp)
jne 0xe229
leaq (%rax,%rax), %r12
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %r12, %rax
movq 0x8(%rsp), %rdx
leaq 0x1(%rdx), %rcx
movq %rbp, (%rdi,%rdx,8)
cmpq %rax, %rcx
jne 0xe263
leaq (%rax,%rax), %r12
testq %rax, %rax
pushq $0x2
popq %rbp
cmoveq %rbp, %r12
leaq (,%r12,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %r12, %rax
movq 0x10(%rsp), %r11
jmp 0xe26b
movq 0x10(%rsp), %r11
pushq $0x2
popq %rbp
movq 0x18(%rsp), %rdx
incq 0x20(%rsp)
movq 0x8(%rsp), %rcx
movq %r15, 0x8(%rdi,%rcx,8)
addq $0x2, %rcx
movq %rcx, 0x8(%rsp)
movq %rbp, %rcx
jmp 0xe1e7
movq 0x60(%rsp), %rcx
incq %rcx
cmpq 0x58(%rsp), %rcx
jne 0xe0cf
movq 0x8(%rsp), %r15
leaq (%rdi,%r15,8), %rsi
movq %rdi, %rbp
callq 0x9d53f
testq %r15, %r15
jle 0xe33e
pushq $-0x1
popq %r13
xorl %r8d, %r8d
xorl %r12d, %r12d
xorl %r9d, %r9d
movq 0x10(%rsp), %rsi
movq (%rbp,%r12,8), %rax
cmpq %rax, %r13
je 0xe2f3
incq %r8
imulq $0x18, %rax, %rcx
movq 0x48(%rsp), %rdx
movl 0xc(%rdx,%rcx), %edx
addl 0xc(%rsi,%rcx), %edx
addq %rdx, %r9
movq %rax, %r13
testq %r12, %r12
je 0xe2ff
cmpq -0x8(%rbp,%r12,8), %rax
jb 0xe30b
incq %r12
cmpq %r12, 0x8(%rsp)
jne 0xe2cf
jmp 0xe344
movq 0x27bcce(%rip), %rax    # 0x289fe0
movq (%rax), %rcx
movq %r9, 0x18(%rsp)
leaq 0x21e147(%rip), %rdi    # 0x22c468
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %r8, %r15
callq 0x2410
movq %r15, %r8
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %rsi
jmp 0xe2ff
xorl %r9d, %r9d
xorl %r8d, %r8d
movq 0x27bc95(%rip), %r15    # 0x289fe0
movq (%r15), %rcx
leaq 0x21e68f(%rip), %rdi    # 0x22c9e4
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
movq %r9, %r12
movq %r8, %r13
callq 0x2410
movq (%r15), %rdi
leaq 0x21e757(%rip), %rsi    # 0x22cac7
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r15), %rdi
leaq 0x21e6a1(%rip), %rsi    # 0x22ca25
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r15), %rdi
leaq 0x21e6be(%rip), %rsi    # 0x22ca58
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r15), %rdi
leaq 0x21e6d1(%rip), %rsi    # 0x22ca81
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r15), %rdi
leaq 0x21e6ec(%rip), %rsi    # 0x22cab2
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r15), %rdi
leaq 0x21e6fd(%rip), %rsi    # 0x22cad9
movq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r15), %rdi
leaq 0x21e710(%rip), %rsi    # 0x22cb02
movq %r14, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x22b0
movq (%r15), %rdi
leaq 0x21e715(%rip), %rsi    # 0x22cb1e
movq %r12, %rdx
xorl %eax, %eax
callq 0x22b0
movq (%r15), %rcx
leaq 0x21e5c7(%rip), %rdi    # 0x22c9e4
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq (%r15), %rbx
callq 0x300fc
subsd 0x50(%rsp), %xmm0
leaq 0x21e6fb(%rip), %rsi    # 0x22cb38
leaq 0x21e70a(%rip), %rdx    # 0x22cb4e
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
movq %rbp, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220