// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: push_info(haplotype_evdience_alloc*, haplotype_evdience*, unsigned long, haplotype_evdience*, overlap_region*, asg8_v*, unsigned long)
int push_info(haplotype_evdience_alloc* h, haplotype_evdience* a, uint64_t a_n, haplotype_evdience* u_a, overlap_region *oa, asg8_v *v8, uint64_t scw)
{
    uint64_t i, k, m, occ_0, occ_1[6], occ_2, diff, rev_n; uint8_t ihpc = 0;
    occ_0 = occ_2 = diff = rev_n = 0; memset(occ_1, 0, sizeof(uint64_t)*6);

    radix_sort_haplotype_evdience_id_srt(a, a + a_n);
    for (k = 1, m = i = 0; k <= a_n; k++) {
        if((k == a_n) || (a[k].overlapID != a[m].overlapID)) {
            a[i] = a[m];
            if(hh_tp(a[i]) == 0){
                occ_0 += a[i].cov;
                if((oa) && (oa[a[i].overlapID].y_pos_strand == 0)) {
                    rev_n += a[i].cov;
                }
                if((!ihpc) && (hh_hp(a[i]))) ihpc = 1;
            }else if(hh_tp(a[i]) == 1){
                occ_1[seq_nt6_table[(uint8_t)(a[i].misBase)]] += a[i].cov;
                diff += a[i].cov;
            }
            occ_2 += a[i].cov;

            i++; m = k;
        }
    }
    a_n = i;
    
    // for (i = 0; i < a_n; i++) {
    //     if(hh_tp(a[i]) == 0){
    //         occ_0 += a[i].cov;
    //         if((oa) && (oa[a[i].overlapID].y_pos_strand == 0)) {
    //             rev_n += a[i].cov;
    //         }
    //         if((!ihpc) && (hh_hp(a[i]))) ihpc = 1;
    //     }else if(hh_tp(a[i]) == 1){
    //         occ_1[seq_nt6_table[(uint8_t)(a[i].misBase)]] += a[i].cov;
    //         diff += a[i].cov;
    //     }
    //     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; 
    if(occ_0 == 0 || diff <= 1) return 0;
    if((oa) && (rev_n == occ_0)) return 0;
    if(!oa) {
        rev_n = occ_2;
    } else {
        rev_n++;
    }

    for (i = m = 0; i < 4; i++) {
        if(occ_1[i] >= 2){
            kv_pushp(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->site = a[0].site;
            p->score = -1;
            p->overlap_num = rev_n;
            p->is_homopolymer = ihpc;
            occ_1[i] = p->id;
            m++;
            // fprintf(stderr, "[M::%s]\tsite::%u\tn0::%u\tn1::%u\n", __func__, p->site, p->occ_0, p->occ_1);
        } else {
            occ_1[i] = (uint64_t)-1;
        }
    }
    occ_1[4] = occ_1[5] = (uint64_t)-1;
    if(m == 0) return 0;
    
    if(!v8) {
        for (i = m = 0; i < a_n; i++) {
            // fprintf(stderr, "[M::%s] a[%lu].misBase->%c\n", __func__, i, a[i].misBase);
            if(hh_tp(a[i]) == 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];
        }
    } else {
        uint64_t tpos, tqual, wqual;
        for (i = m = 0; i < a_n; i++) {
            tpos = a[i].overlapSite;
            if(hh_tp(a[i]) == 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;
            }
            get_wqual(oa[a[i].overlapID].y_id, tpos, oa[a[i].overlapID].y_pos_strand, v8, NULL, scw, &tqual, &wqual);
            a[i].type |= (tqual<<2); a[i].type |= (wqual<<(sc_bn+2));
            // assert(hh_bq(a[i]) == tqual); assert(hh_wq(a[i]) == wqual); 
            // fprintf(stderr, "[M::%s] site::%u, type::%u, occ0::%lu, occ2::%lu, tqual::%lu, wqual::%lu\n", __func__, a[i].site, hh_tp(a[i]), occ_0, occ_2, tqual, wqual);
            u_a[m++] = a[i];
        }
    }
    

    return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x28(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, 0x18(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x30(%rsp)
leaq (%rdx,%rdx,2), %rax
leaq (%rsi,%rax,8), %rsi
movq %r14, %rdi
callq 0x45897
testq %rbp, %rbp
je 0x57552
movq %rbx, 0x20(%rsp)
leaq 0x1c(%r14), %rax
movl $0x1, %ecx
movq $0x0, 0x8(%rsp)
leaq 0x23e450(%rip), %r10    # 0x295630
xorl %r12d, %r12d
xorl %edx, %edx
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %esi, %esi
xorl %ebx, %ebx
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r11
cmpq %rcx, %rbp
je 0x5720f
movl (%rax), %edi
leaq (%rsi,%rsi,2), %r8
cmpl 0x4(%r14,%r8,8), %edi
je 0x57296
leaq (%rsi,%rsi,2), %rsi
leaq (%rbx,%rbx,2), %rdi
leaq (%r14,%rdi,8), %r8
movdqu (%r14,%rsi,8), %xmm0
movdqu %xmm0, (%r8)
movq 0x10(%r14,%rsi,8), %rsi
movq %rsi, 0x10(%r8)
movb 0xc(%r8), %dil
movl %esi, %esi
testb $0x1, %dil
jne 0x5726d
testq %r11, %r11
je 0x5725e
movl 0x4(%r8), %r8d
imulq $0xa8, %r8, %r8
cmpl $0x0, 0x1c(%r11,%r8)
movl $0x0, %r8d
cmoveq %rsi, %r8
addq %r8, %r12
addq %rsi, %r13
cmpb $0x0, 0x8(%rsp)
je 0x57281
movb $0x1, %dil
jmp 0x57288
movzbl 0x14(%r8), %edi
movzbl (%r10,%rdi), %edi
addq %rsi, 0x30(%rsp,%rdi,8)
addq %rsi, %rdx
jmp 0x5728d
shrb %dil
andb $0x1, %dil
movq %rdi, 0x8(%rsp)
addq %rsi, %r15
incq %rbx
movq %rcx, %rsi
incq %rcx
addq $0x18, %rax
cmpq %rbp, %rcx
jbe 0x571f9
cmpq $0x2, %rdx
setb %al
testq %r13, %r13
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x57554
testq %r11, %r11
je 0x572cb
cmpq %r13, %r12
je 0x57554
incl %r12d
incl %r13d
testq %r11, %r11
cmovel %r15d, %r12d
movq %r12, 0x70(%rsp)
xorl %edi, %edi
xorl %ebp, %ebp
movq %r13, 0x68(%rsp)
movq 0x30(%rsp,%rbp,8), %r12
cmpq $0x2, %r12
jb 0x573b4
movq 0x218(%r9), %rax
movq 0x220(%r9), %rcx
cmpq 0x228(%r9), %rcx
jne 0x5735d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x228(%r9)
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rsi
movq %rdi, %r13
movq %rax, %rdi
callq 0x2370
movq %r13, %rdi
movq 0x68(%rsp), %r13
movq 0x10(%rsp), %r11
leaq 0x23e2e6(%rip), %r10    # 0x295630
movq 0x18(%rsp), %r9
movq %rax, 0x218(%r9)
movq 0x220(%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x220(%r9)
leaq (%rcx,%rcx,4), %rdx
movl %ecx, (%rax,%rdx,8)
movl %r13d, 0x8(%rax,%rdx,8)
movl %r12d, 0xc(%rax,%rdx,8)
addl %r13d, %r12d
movl %r15d, %esi
subl %r12d, %esi
movl %esi, 0x10(%rax,%rdx,8)
movl (%r14), %esi
movl %esi, 0x20(%rax,%rdx,8)
movl $0xffffffff, 0x1c(%rax,%rdx,8) # imm = 0xFFFFFFFF
movq 0x70(%rsp), %rsi
movl %esi, 0x4(%rax,%rdx,8)
movq 0x8(%rsp), %rsi
movb %sil, 0x24(%rax,%rdx,8)
movl %ecx, %eax
movq %rax, 0x30(%rsp,%rbp,8)
incq %rdi
jmp 0x573bd
movq $-0x1, 0x30(%rsp,%rbp,8)
incq %rbp
cmpq $0x4, %rbp
jne 0x572e6
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x50(%rsp)
testq %rdi, %rdi
je 0x57552
cmpq $0x0, 0x28(%rsp)
je 0x574db
testq %rbx, %rbx
je 0x57552
addq $0x14, %r14
xorl %ebp, %ebp
leaq 0x78(%rsp), %r15
leaq 0x80(%rsp), %r12
movl -0xc(%r14), %esi
testb $0x1, -0x8(%r14)
jne 0x5741c
movq 0x220(%r9), %rax
decq %rax
jmp 0x5743f
movzbl (%r14), %eax
movzbl (%r10,%rax), %ecx
cmpq $-0x1, 0x30(%rsp,%rcx,8)
je 0x574cc
movl %esi, -0x4(%r14)
movzbl (%r10,%rax), %eax
movq 0x30(%rsp,%rax,8), %rax
leaq -0x14(%r14), %r13
movl %eax, -0xc(%r14)
movl -0x10(%r14), %eax
imulq $0xa8, %rax, %rax
movl 0x10(%r11,%rax), %edi
movl 0x1c(%r11,%rax), %edx
movq 0x28(%rsp), %rcx
xorl %r8d, %r8d
movq 0xc0(%rsp), %r9
pushq %r15
pushq %r12
callq 0x54288
movq 0x20(%rsp), %r11
addq $0x10, %rsp
movb 0x80(%rsp), %al
shlb $0x2, %al
orb -0x8(%r14), %al
movb 0x78(%rsp), %cl
shlb $0x4, %cl
orb %al, %cl
movb %cl, -0x8(%r14)
leaq (,%rbp,2), %rax
addq %rbp, %rax
incq %rbp
movq 0x10(%r13), %rcx
movq 0x20(%rsp), %rdx
movq %rcx, 0x10(%rdx,%rax,8)
movdqu (%r13), %xmm0
movdqu %xmm0, (%rdx,%rax,8)
movq 0x18(%rsp), %r9
leaq 0x23e164(%rip), %r10    # 0x295630
addq $0x18, %r14
decq %rbx
jne 0x57405
jmp 0x57554
testq %rbx, %rbx
je 0x57552
addq $0x14, %r14
xorl %ebp, %ebp
movq 0x20(%rsp), %rsi
testb $0x1, -0x8(%r14)
jne 0x574fd
movl 0x220(%r9), %eax
decl %eax
jmp 0x5751f
movzbl (%r14), %eax
movzbl (%r10,%rax), %ecx
cmpq $-0x1, 0x30(%rsp,%rcx,8)
je 0x57547
movl -0xc(%r14), %ecx
movl %ecx, -0x4(%r14)
movzbl (%r10,%rax), %eax
movl 0x30(%rsp,%rax,8), %eax
movl %eax, -0xc(%r14)
leaq -0x14(%r14), %rax
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
incq %rbp
movq 0x10(%rax), %rdx
movq %rdx, 0x10(%rsi,%rcx,8)
movdqu (%rax), %xmm0
movdqu %xmm0, (%rsi,%rcx,8)
addq $0x18, %r14
decq %rbx
jne 0x574eb
jmp 0x57554
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq