// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %r8, 0x28(%rsp)
movq %rsi, %r15
pxor %xmm0, %xmm0
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
xorl %eax, %eax
testq %rdx, %rdx
movq %rdi, 0x8(%rsp)
je 0x57287
leaq 0x14(%r15), %r11
xorl %r10d, %r10d
leaq 0x2333fe(%rip), %rsi    # 0x28a630
movq %rdx, %rdi
xorl %ebx, %ebx
xorl %r12d, %r12d
movzbl -0x8(%r11), %r8d
cmpl $0x1, %r8d
je 0x57253
testl %r8d, %r8d
jne 0x57268
movl -0x4(%r11), %r8d
addq %r8, %r12
jmp 0x57268
movl -0x4(%r11), %r8d
movzbl (%r11), %r9d
movzbl (%rsi,%r9), %r9d
addq %r8, 0x40(%rsp,%r9,8)
addq %r8, %r10
movl -0x4(%r11), %r8d
addq %r8, %rbx
addq $0x18, %r11
decq %rdi
jne 0x5723a
cmpq $0x2, %r10
setb %sil
movq 0x8(%rsp), %rdi
jmp 0x5728f
movb $0x1, %sil
xorl %r12d, %r12d
xorl %ebx, %ebx
testq %r12, %r12
je 0x575b7
movq %rcx, 0x20(%rsp)
testb %sil, %sil
jne 0x575b7
movq %rdx, 0x18(%rsp)
incl %r12d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
xorl %r13d, %r13d
movq %r15, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq 0x40(%rsp,%r13,8), %r14
cmpq $0x2, %r14
jb 0x57328
cmpq $0x0, 0x10(%rsp)
je 0x57336
movq 0x228(%rdi), %rax
cmpq %rax, 0x220(%rdi)
jne 0x57385
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x228(%rdi)
movq 0x218(%rdi), %rsi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rdx
movq 0x10(%rsp), %rdi
movl %ebp, (%rsp)
movq %r10, %rbp
callq 0x1e3800
movq %rbp, %r10
movl (%rsp), %ebp
jmp 0x57379
movq $-0x1, 0x40(%rsp,%r13,8)
jmp 0x5751b
movq 0x228(%rdi), %rax
cmpq %rax, 0x220(%rdi)
jne 0x57385
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x228(%rdi)
movq 0x218(%rdi), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
movq %r10, (%rsp)
callq 0x2370
movq (%rsp), %r10
movq 0x8(%rsp), %rdi
movq %rax, 0x218(%rdi)
movq 0x218(%rdi), %rcx
movq 0x220(%rdi), %rdx
leaq 0x1(%rdx), %rax
movq %rax, 0x220(%rdi)
leaq (%rdx,%rdx,4), %rdx
decl %eax
movl %eax, (%rcx,%rdx,8)
movl %r12d, 0x8(%rcx,%rdx,8)
movl %r14d, 0xc(%rcx,%rdx,8)
addl %r12d, %r14d
movl %ebx, %esi
subl %r14d, %esi
movl %esi, 0x10(%rcx,%rdx,8)
movl $0x0, 0x4(%rcx,%rdx,8)
movl (%r15), %r15d
movl %r15d, 0x20(%rcx,%rdx,8)
movl $0xffffffff, 0x1c(%rcx,%rdx,8) # imm = 0xFFFFFFFF
movl %ebx, 0x4(%rcx,%rdx,8)
cmpl $-0x1, %ebp
jne 0x574ff
movq %r10, (%rsp)
movq 0x28(%rsp), %rsi
movq (%rsi), %r8
movq 0x8(%rsi), %rsi
cmpq $0x4, %r15
movl $0x3, %r10d
cmovgeq %r15, %r10
leaq 0x3(%r15), %r9
leaq -0x1(%rsi), %r11
cmpq %rsi, %r9
cmovlq %r9, %r11
cmpq %r15, %r11
jle 0x57444
xorl %esi, %esi
movq %r15, %r14
xorl %ebp, %ebp
movb 0x1(%r8,%r14), %r9b
testb %bpl, %bpl
je 0x57432
cmpb %bpl, %r9b
jne 0x5744b
incq %rsi
movl %ebp, %r9d
jmp 0x5743a
movl $0x1, %esi
movl %r9d, %ebp
incq %r14
cmpq %r14, %r11
jne 0x5741b
jmp 0x5744e
xorl %r9d, %r9d
xorl %esi, %esi
jmp 0x5744e
movl %ebp, %r9d
addq $-0x3, %r10
cmpq %r15, %r10
jge 0x5748a
xorl %r11d, %r11d
movq %r15, %rbp
xorl %r14d, %r14d
movb -0x1(%r8,%rbp), %r12b
testb %r14b, %r14b
je 0x57477
cmpb %r14b, %r12b
jne 0x57492
incq %r11
movl %r14d, %r12d
jmp 0x57480
movl $0x1, %r11d
movl %r12d, %r14d
decq %rbp
cmpq %r10, %rbp
jg 0x57460
jmp 0x57495
xorl %r12d, %r12d
xorl %r11d, %r11d
jmp 0x57495
movl %r14d, %r12d
movb (%r8,%r15), %r15b
xorl %r14d, %r14d
cmpb %r15b, %r9b
setne %r8b
sete %r14b
cmpb %r15b, %r12b
sete %r10b
addq %r14, %rsi
movl $0x1, %ebp
cmpq $0x2, %rsi
jg 0x574f6
andb %r10b, %r8b
movzbl %r8b, %r8d
addq %r8, %r11
cmpq $0x2, %r11
movq 0x8(%rsp), %rdi
movq (%rsp), %r10
jg 0x574ff
xorb %r9b, %r15b
xorb %r9b, %r12b
orb %r15b, %r12b
sete %r8b
addq %rsi, %r11
cmpq $0x3, %r11
setge %sil
andb %r8b, %sil
movzbl %sil, %ebp
jmp 0x574ff
movq 0x8(%rsp), %rdi
movq (%rsp), %r10
leaq (%rcx,%rdx,8), %rcx
movb %bpl, 0x24(%rcx)
movl %eax, %eax
movq %rax, 0x40(%rsp,%r13,8)
incq %r10
movq 0x38(%rsp), %r15
movq 0x30(%rsp), %r12
incq %r13
cmpq $0x4, %r13
jne 0x572c3
testq %r10, %r10
sete %cl
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x60(%rsp)
movq 0x18(%rsp), %r9
testq %r9, %r9
sete %dl
xorl %eax, %eax
orb %cl, %dl
jne 0x575b7
addq $0x14, %r15
xorl %eax, %eax
leaq 0x2330da(%rip), %rcx    # 0x28a630
movq 0x20(%rsp), %r8
cmpb $0x0, -0x8(%r15)
je 0x57584
movzbl (%r15), %edx
movzbl (%rcx,%rdx), %esi
cmpq $-0x1, 0x40(%rsp,%rsi,8)
je 0x575ae
movl -0xc(%r15), %esi
movl %esi, -0x4(%r15)
movzbl (%rcx,%rdx), %edx
movl 0x40(%rsp,%rdx,8), %edx
jmp 0x5758c
movl 0x220(%rdi), %edx
decl %edx
movl %edx, -0xc(%r15)
leaq -0x14(%r15), %rdx
leaq (%rax,%rax,2), %rsi
incq %rax
movq 0x10(%rdx), %r10
movq %r10, 0x10(%r8,%rsi,8)
movdqu (%rdx), %xmm0
movdqu %xmm0, (%r8,%rsi,8)
addq $0x18, %r15
decq %r9
jne 0x5755b
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq