// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %r9, 0x18(%rsp)
movq %r8, 0x30(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
testq %rdx, %rdx
je 0x569b7
movq %rsi, %r15
leaq 0x14(%rsi), %r8
xorl %eax, %eax
leaq 0x23ef38(%rip), %r10    # 0x295630
movq %rdx, %rbx
xorl %ebp, %ebp
xorl %r11d, %r11d
movzbl -0x8(%r8), %r9d
movl -0x4(%r8), %esi
cmpl $0x1, %r9d
je 0x56719
testl %r9d, %r9d
jne 0x5672a
addq %rsi, %r11
jmp 0x5672a
movzbl (%r8), %r9d
movzbl (%r10,%r9), %r9d
addq %rsi, 0x40(%rsp,%r9,8)
addq %rsi, %rax
addq %rsi, %rbp
addq $0x18, %r8
decq %rbx
jne 0x56700
cmpq $0x2, %rax
setb %al
testq %r11, %r11
sete %sil
orb %al, %sil
jne 0x569b7
movq %rdx, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
incl %r11d
movq %r11, 0x38(%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %rdi, 0x8(%rsp)
movq 0x40(%rsp,%r13,8), %r12
cmpq $0x2, %r12
jb 0x567ca
movq 0x218(%rdi), %rax
movq 0x220(%rdi), %rdx
movq 0x228(%rdi), %rcx
cmpq $0x0, 0x18(%rsp)
je 0x567d8
cmpq %rcx, %rdx
jne 0x56817
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x228(%rdi)
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rdx
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x1ee21d
jmp 0x56804
movq $-0x1, 0x40(%rsp,%r13,8)
jmp 0x56986
cmpq %rcx, %rdx
jne 0x56817
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x228(%rdi)
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x218(%rdi)
movq 0x220(%rdi), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x220(%rdi)
leaq (%rdx,%rdx,4), %rdx
leaq (%rax,%rdx,8), %rax
decl %ecx
movl %ecx, (%rax)
movq 0x38(%rsp), %rdx
movl %edx, 0x8(%rax)
movl %r12d, 0xc(%rax)
addl %edx, %r12d
movl %ebp, %edx
subl %r12d, %edx
movl %edx, 0x10(%rax)
movl (%r15), %esi
movl %esi, 0x20(%rax)
movl $0xffffffff, 0x1c(%rax) # imm = 0xFFFFFFFF
movl %ebp, 0x4(%rax)
cmpl $-0x1, %ebx
jne 0x56979
movq %r14, 0x10(%rsp)
movq 0x30(%rsp), %rdx
movq (%rdx), %rdi
movq 0x8(%rdx), %rdx
cmpq $0x4, %rsi
movl $0x3, %r9d
cmovaeq %rsi, %r9
leaq 0x3(%rsi), %r8
leaq -0x1(%rdx), %r10
cmpq %rdx, %r8
cmovlq %r8, %r10
cmpq %rsi, %r10
jle 0x568c0
xorl %edx, %edx
movq %rsi, %r11
xorl %ebx, %ebx
movb 0x1(%rdi,%r11), %r8b
testb %bl, %bl
je 0x568ae
cmpb %bl, %r8b
jne 0x568c7
incq %rdx
movl %ebx, %r8d
jmp 0x568b6
movl $0x1, %edx
movl %r8d, %ebx
incq %r11
cmpq %r11, %r10
jne 0x56898
jmp 0x568ca
xorl %r8d, %r8d
xorl %edx, %edx
jmp 0x568ca
movl %ebx, %r8d
addq $-0x3, %r9
cmpq %rsi, %r9
jge 0x56906
xorl %r10d, %r10d
movq %rsi, %rbx
xorl %r12d, %r12d
movb -0x1(%rdi,%rbx), %r11b
testb %r12b, %r12b
je 0x568f3
cmpb %r12b, %r11b
jne 0x5690e
incq %r10
movl %r12d, %r11d
jmp 0x568fc
movl $0x1, %r10d
movl %r11d, %r12d
decq %rbx
cmpq %r9, %rbx
jg 0x568dc
jmp 0x56911
xorl %r11d, %r11d
xorl %r10d, %r10d
jmp 0x56911
movl %r12d, %r11d
movb (%rdi,%rsi), %sil
xorl %ebx, %ebx
cmpb %sil, %r8b
setne %dil
sete %bl
cmpb %sil, %r11b
sete %r9b
addq %rbx, %rdx
movl $0x1, %ebx
cmpq $0x2, %rdx
jg 0x5696f
andb %r9b, %dil
movzbl %dil, %edi
addq %rdi, %r10
cmpq $0x2, %r10
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r14
jg 0x56979
xorb %r8b, %sil
xorb %r8b, %r11b
orb %sil, %r11b
sete %sil
addq %rdx, %r10
cmpq $0x3, %r10
setge %dl
andb %sil, %dl
movzbl %dl, %ebx
jmp 0x56979
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r14
movb %bl, 0x24(%rax)
movl %ecx, %eax
movq %rax, 0x40(%rsp,%r13,8)
incq %r14
incq %r13
cmpq $0x4, %r13
jne 0x5676f
movq 0x20(%rsp), %r10
testq %r10, %r10
sete %al
testq %r14, %r14
sete %cl
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x60(%rsp)
orb %al, %cl
movq 0x28(%rsp), %r8
je 0x569c8
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x14, %r15
xorl %eax, %eax
leaq 0x23ec5b(%rip), %r9     # 0x295630
cmpb $0x0, -0x8(%r15)
je 0x56a00
movzbl (%r15), %ecx
movzbl (%r9,%rcx), %edx
cmpq $-0x1, 0x40(%rsp,%rdx,8)
je 0x56a2a
movl -0xc(%r15), %edx
movl %edx, -0x4(%r15)
movzbl (%r9,%rcx), %ecx
movl 0x40(%rsp,%rcx,8), %ecx
jmp 0x56a08
movl 0x220(%rdi), %ecx
decl %ecx
movl %ecx, -0xc(%r15)
leaq -0x14(%r15), %rcx
leaq (%rax,%rax,2), %rdx
incq %rax
movq 0x10(%rcx), %rsi
movq %rsi, 0x10(%r8,%rdx,8)
movdqu (%rcx), %xmm0
movdqu %xmm0, (%r8,%rdx,8)
addq $0x18, %r15
decq %r10
jne 0x569d5
jmp 0x569b9