// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x50(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x2100
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x4957f
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
cmpl $0x0, %eax
jne 0x494e5
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x49551
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
cmpl $0x1, %eax
jne 0x4954f
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rax
movzbl 0x14(%rax), %eax
movl %eax, %edx
leaq 0x37b12c(%rip), %rax    # 0x3c4650
movzbl (%rax,%rdx), %eax
addq -0x80(%rbp,%rax,8), %rcx
movq %rcx, -0x80(%rbp,%rax,8)
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x49551
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x494a9
movq $0x0, -0x98(%rbp)
movl $0xffffffff, -0x9c(%rbp) # imm = 0xFFFFFFFF
cmpq $0x0, -0x50(%rbp)
je 0x495a5
cmpq $0x1, -0x90(%rbp)
ja 0x495b1
movl $0x0, -0x4(%rbp)
jmp 0x499e2
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
jae 0x4989a
movq -0x40(%rbp), %rax
cmpq $0x2, -0x80(%rbp,%rax,8)
jb 0x4987a
cmpq $0x0, -0x38(%rbp)
jne 0x496a7
jmp 0x495e9
movq -0x10(%rbp), %rax
movq 0x220(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x228(%rcx), %rax
jne 0x49670
movq -0x10(%rbp), %rax
cmpq $0x0, 0x228(%rax)
je 0x49626
movq -0x10(%rbp), %rax
movq 0x228(%rax), %rax
shlq %rax
movq %rax, -0xa8(%rbp)
jmp 0x49634
movl $0x2, %eax
movq %rax, -0xa8(%rbp)
jmp 0x49634
movq -0xa8(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x228(%rax)
movq -0x10(%rbp), %rax
movq 0x218(%rax), %rdi
movq -0x10(%rbp), %rax
imulq $0x28, 0x228(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x218(%rax)
movq -0x10(%rbp), %rax
movq 0x218(%rax), %rax
movq -0x10(%rbp), %rdx
movq 0x220(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x220(%rdx)
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
jmp 0x49768
jmp 0x496a9
movq -0x10(%rbp), %rax
movq 0x220(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x228(%rcx), %rax
jne 0x49734
movq -0x10(%rbp), %rax
cmpq $0x0, 0x228(%rax)
je 0x496e6
movq -0x10(%rbp), %rax
movq 0x228(%rax), %rax
shlq %rax
movq %rax, -0xb0(%rbp)
jmp 0x496f4
movl $0x2, %eax
movq %rax, -0xb0(%rbp)
jmp 0x496f4
movq -0xb0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x228(%rax)
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x218(%rax), %rsi
movq -0x10(%rbp), %rax
imulq $0x28, 0x228(%rax), %rdx
callq 0x30e740
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x218(%rax)
movq -0x10(%rbp), %rax
movq 0x218(%rax), %rax
movq -0x10(%rbp), %rdx
movq 0x220(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x220(%rdx)
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
jmp 0x49768
movq -0x10(%rbp), %rax
movq 0x220(%rax), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x98(%rbp), %rax
movl %ecx, (%rax)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movl %eax, %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rax
movl %eax, %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x88(%rbp), %rax
movq -0x98(%rbp), %rcx
movl 0x8(%rcx), %ecx
subq %rcx, %rax
movq -0x98(%rbp), %rcx
movl 0xc(%rcx), %ecx
subq %rcx, %rax
movl %eax, %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x98(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x98(%rbp), %rax
movl $0xffffffff, 0x1c(%rax) # imm = 0xFFFFFFFF
movq -0x88(%rbp), %rax
movl %eax, %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpl $-0x1, -0x9c(%rbp)
jne 0x49846
movq -0x98(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, %edi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0xa0650
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movb %al, %cl
movq -0x98(%rbp), %rax
movb %cl, 0x24(%rax)
movq -0x98(%rbp), %rax
movl (%rax), %eax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movq %rcx, -0x80(%rbp,%rax,8)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x49887
movq -0x40(%rbp), %rax
movq $-0x1, -0x80(%rbp,%rax,8)
jmp 0x49889
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x495c1
movq $-0x1, -0x58(%rbp)
movq $-0x1, -0x60(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x498bd
movl $0x0, -0x4(%rbp)
jmp 0x499e2
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x499db
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
cmpl $0x0, %eax
jne 0x49912
movq -0x10(%rbp), %rax
movq 0x220(%rax), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
jmp 0x4998e
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movzbl 0x14(%rax), %eax
movl %eax, %ecx
leaq 0x37ad25(%rip), %rax    # 0x3c4650
movzbl (%rax,%rcx), %eax
cmpq $-0x1, -0x80(%rbp,%rax,8)
je 0x4998a
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movzbl 0x14(%rax), %eax
movl %eax, %ecx
leaq 0x37ace2(%rip), %rax    # 0x3c4650
movzbl (%rax,%rcx), %eax
movq -0x80(%rbp,%rax,8), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
jmp 0x4998c
jmp 0x499ca
jmp 0x4998e
movq -0x18(%rbp), %rcx
imulq $0x18, -0x40(%rbp), %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0x48(%rbp)
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x498cd
movq -0x48(%rbp), %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nop