// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: insert_snp_ee(haplotype_evdience_alloc*, haplotype_evdience*, unsigned long, haplotype_evdience*, UC_Read*, void*)
int insert_snp_ee(haplotype_evdience_alloc* h, haplotype_evdience* a, uint64_t a_n, haplotype_evdience* u_a, UC_Read* g_read, void *km)
{
    uint64_t i, m, occ_0, occ_1[6], occ_2, diff;
    occ_0 = occ_2 = diff = 0; memset(occ_1, 0, sizeof(uint64_t)*6);

    for (i = 0; i < a_n; i++) {
        if(a[i].type == 0){
            occ_0 += a[i].cov;
        }else if(a[i].type == 1){
            occ_1[seq_nt6_table[(uint8_t)(a[i].misBase)]] += a[i].cov;
            diff += a[i].cov;
        }
        // else if(a[i].type == 2){
        //     occ_2++;
        //     diff++;
        // }
        occ_2 += a[i].cov;
    }

    /**
     1. if occ_0 = 0, that means all overlaps are different with this read at this site
     2. it is not possible that occ_1 = 0,
     3. if occ_1 = 1, there are only one difference. It must be a sequencing error.
        (for repeat, it maybe a snp at repeat. but ...)
    **/
    SnpStats *p = NULL; 
    uint32_t is_homopolymer = (uint32_t)-1; 
    if(occ_0 == 0 || diff <= 1) return 0;
    for (i = m = 0; i < 4; i++) {
        if(occ_1[i] >= 2){
            if(!km) kv_pushp(SnpStats, h->snp_stat, &p);
            else kv_pushp_km(km, SnpStats, h->snp_stat, &p);
            p->id = h->snp_stat.n-1;
            p->occ_0 = 1 + occ_0;
            p->occ_1 = occ_1[i];
            p->occ_2 = occ_2 - p->occ_0 - p->occ_1;
            p->overlap_num = 0;
            p->site = a[0].site;
            p->score = -1;
            p->overlap_num = occ_2;
            if(is_homopolymer == (uint32_t)-1) {
                is_homopolymer = if_is_homopolymer_strict(p->site, g_read->seq, g_read->length);
            }
            p->is_homopolymer = is_homopolymer;
            occ_1[i] = p->id;
            m++;
        } else {
            occ_1[i] = (uint64_t)-1;
        }
    }
    occ_1[4] = occ_1[5] = (uint64_t)-1;
    if(m == 0) return 0;
    
    for (i = m = 0; i < a_n; i++) {
        // fprintf(stderr, "[M::%s] a[%lu].misBase->%c\n", __func__, i, a[i].misBase);
        if(a[i].type == 0) {
            a[i].overlapSite = h->snp_stat.n-1;
        } else if(occ_1[seq_nt6_table[(uint8_t)(a[i].misBase)]]!=(uint64_t)-1){
            a[i].cov = a[i].overlapSite;///note: only renew cov here!!!
            a[i].overlapSite = occ_1[seq_nt6_table[(uint8_t)(a[i].misBase)]];
        } else {
            continue;
        }
        u_a[m++] = a[i];
    }

    /**
    // if(c_snp && ovlp) {
    //     ;
    // }
    for (i = m = 0; i < 4; i++) {
        if(occ_1[i] >= 2) {
            insert_snp_vv(h, a, a_n, s_H[i], g_read, km);
            m++;
        }
    }
    if(m == 0) return 0;

    for (i = m = 0; i < a_n; i++) {
        if(a[i].type == 0){
            u_a[m++] = a[i];
        }else if(a[i].type == 1){
            if(occ_1[seq_nt6_table[(uint8_t)(a[i].misBase)]] >= 2) u_a[m++] = a[i];
        }
    }
    **/

    return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x18(%rsp)
movq %r8, 0x28(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r9
pxor %xmm0, %xmm0
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x30(%rsp)
leaq 0x14(%rsi), %rax
xorl %r11d, %r11d
leaq 0x22bb84(%rip), %r10    # 0x253630
xorl %ebx, %ebx
xorl %r8d, %r8d
subq $0x1, %rdx
jb 0x27ae5
movzbl -0x8(%rax), %edi
movl -0x4(%rax), %esi
cmpl $0x1, %edi
je 0x27acc
testl %edi, %edi
jne 0x27adc
addq %rsi, %r11
jmp 0x27adc
movzbl (%rax), %edi
movzbl (%r10,%rdi), %edi
addq %rsi, 0x30(%rsp,%rdi,8)
addq %rsi, %r8
addq %rsi, %rbx
addq $0x18, %rax
jmp 0x27ab1
testq %r11, %r11
sete %al
cmpq $0x2, %r8
setb %dl
orb %al, %dl
jne 0x27cd7
movq %rcx, 0x20(%rsp)
incl %r11d
pushq $-0x1
popq %r8
xorl %ebp, %ebp
xorl %esi, %esi
movq %r9, 0x8(%rsp)
movq %r11, (%rsp)
cmpq $0x4, %rbp
je 0x27c55
movq 0x30(%rsp,%rbp,8), %r12
cmpq $0x2, %r12
jb 0x27b7e
movq 0x218(%r9), %rax
movq 0x220(%r9), %rdx
movq 0x228(%r9), %rcx
cmpq $0x0, 0x18(%rsp)
je 0x27b89
cmpq %rcx, %rdx
jne 0x27bd6
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x228(%r9)
imulq $0x28, %rcx, %rdx
movq 0x18(%rsp), %rdi
movq %rsi, 0x10(%rsp)
movq %rax, %rsi
movl %r8d, %r13d
callq 0x1a3acc
jmp 0x27bb7
orq $-0x1, 0x30(%rsp,%rbp,8)
jmp 0x27c4d
cmpq %rcx, %rdx
jne 0x27bd6
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x228(%r9)
movq %rsi, 0x10(%rsp)
imulq $0x28, %rcx, %rsi
movq %rax, %rdi
movl %r8d, %r13d
callq 0x2370
movl %r13d, %r8d
movq 0x10(%rsp), %rsi
movq (%rsp), %r11
movq 0x8(%rsp), %r9
movq %rax, 0x218(%r9)
movq 0x220(%r9), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x220(%r9)
imulq $0x28, %rdx, %r13
addq %rax, %r13
decl %ecx
movl %ecx, (%r13)
movl %r11d, 0x8(%r13)
movl %r12d, 0xc(%r13)
addl %r11d, %r12d
movl %ebx, %eax
subl %r12d, %eax
movl %eax, 0x10(%r13)
movl (%r15), %edi
movl %edi, 0x20(%r13)
orl $-0x1, 0x1c(%r13)
movl %ebx, 0x4(%r13)
cmpl $-0x1, %r8d
jne 0x27c3f
movq 0x28(%rsp), %rax
movq %rsi, %r12
movq (%rax), %rsi
movq 0x8(%rax), %rdx
callq 0x52f6d
movq %r12, %rsi
movq (%rsp), %r11
movq 0x8(%rsp), %r9
movl %eax, %r8d
movl (%r13), %ecx
movb %r8b, 0x24(%r13)
movl %ecx, %eax
movq %rax, 0x30(%rsp,%rbp,8)
incq %rsi
incq %rbp
jmp 0x27b13
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x50(%rsp)
testq %rsi, %rsi
je 0x27cd7
addq $0x14, %r15
xorl %eax, %eax
movq 0x20(%rsp), %rdi
leaq 0x22b9ba(%rip), %r8     # 0x253630
subq $0x1, %r14
jb 0x27cd9
cmpb $0x0, -0x8(%r15)
je 0x27ca7
movzbl (%r15), %ecx
movzbl (%r8,%rcx), %edx
cmpq $-0x1, 0x30(%rsp,%rdx,8)
je 0x27cd1
movl -0xc(%r15), %edx
movl %edx, -0x4(%r15)
movzbl (%r8,%rcx), %ecx
movl 0x30(%rsp,%rcx,8), %ecx
jmp 0x27cb0
movl 0x220(%r9), %ecx
decl %ecx
movl %ecx, -0xc(%r15)
leaq -0x14(%r15), %rcx
imulq $0x18, %rax, %rdx
incq %rax
movq 0x10(%rcx), %rsi
movq %rsi, 0x10(%rdi,%rdx)
movdqu (%rcx), %xmm0
movdqu %xmm0, (%rdi,%rdx)
addq $0x18, %r15
jmp 0x27c76
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq