// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %r9, 0x10(%rsp)
movq %r8, (%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x20(%rsp)
imulq $0x18, %rdx, %rsi
addq %r14, %rsi
movq %r14, %rdi
callq 0x19538
leaq 0x1c(%r14), %rax
pushq $0x1
popq %rcx
xorl %r12d, %r12d
leaq 0x22b201(%rip), %r9     # 0x253630
xorl %esi, %esi
xorl %r10d, %r10d
xorl %r15d, %r15d
xorl %edx, %edx
xorl %r11d, %r11d
xorl %r13d, %r13d
movq (%rsp), %rbx
cmpq %rbp, %rcx
ja 0x284f3
je 0x2845f
movl (%rax), %edi
imulq $0x18, %rsi, %r8
cmpl 0x4(%r14,%r8), %edi
je 0x284e7
imulq $0x18, %rsi, %rsi
imulq $0x18, %r12, %rdi
leaq (%r14,%rdi), %r8
movdqu (%r14,%rsi), %xmm0
movdqu %xmm0, (%r14,%rdi)
movq 0x10(%r14,%rsi), %rsi
movq %rsi, 0x10(%r14,%rdi)
movb 0xc(%r14,%rdi), %dil
movl %esi, %esi
testb $0x1, %dil
jne 0x284c0
cmpq $0x0, (%rsp)
je 0x284b3
movl 0x4(%r8), %r8d
imulq $0xa8, %r8, %r8
cmpl $0x0, 0x1c(%rbx,%r8)
movl $0x0, %r8d
cmoveq %rsi, %r8
addq %r8, %r11
addq %rsi, %r10
testb %r13b, %r13b
je 0x284d4
movb $0x1, %r13b
jmp 0x284de
movzbl 0x14(%r8), %edi
movzbl (%r9,%rdi), %edi
addq %rsi, 0x20(%rsp,%rdi,8)
addq %rsi, %rdx
jmp 0x284de
shrb %dil
andb $0x1, %dil
movl %edi, %r13d
addq %rsi, %r15
incq %r12
movq %rcx, %rsi
incq %rcx
addq $0x18, %rax
jmp 0x28443
testq %r10, %r10
sete %al
cmpq $0x2, %rdx
setb %cl
xorl %ebp, %ebp
orb %al, %cl
movq 0x8(%rsp), %rdi
jne 0x28719
cmpq $0x0, (%rsp)
je 0x2851f
cmpq %r10, %r11
je 0x28719
incl %r11d
incl %r10d
cmpq $0x0, (%rsp)
cmovel %r15d, %r11d
xorl %ebp, %ebp
xorl %r8d, %r8d
movq %r10, 0x58(%rsp)
movq %r11, 0x50(%rsp)
cmpq $0x4, %rbp
je 0x2861e
movq %r13, %rbx
movq 0x20(%rsp,%rbp,8), %r13
cmpq $0x2, %r13
jb 0x2860d
movq 0x218(%rdi), %rax
movq 0x220(%rdi), %rcx
cmpq 0x228(%rdi), %rcx
jne 0x285bf
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x228(%rdi)
imulq $0x28, %rdx, %rsi
movq %rax, %rdi
movq %r8, 0x60(%rsp)
callq 0x2370
movq 0x60(%rsp), %r8
movq 0x50(%rsp), %r11
movq 0x58(%rsp), %r10
leaq 0x22b084(%rip), %r9     # 0x253630
movq 0x8(%rsp), %rdi
movq %rax, 0x218(%rdi)
movq 0x220(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x220(%rdi)
imulq $0x28, %rcx, %rdx
movl %ecx, (%rax,%rdx)
movl %r10d, 0x8(%rax,%rdx)
movl %r13d, 0xc(%rax,%rdx)
addl %r10d, %r13d
movl %r15d, %esi
subl %r13d, %esi
movl %esi, 0x10(%rax,%rdx)
movl (%r14), %esi
movl %esi, 0x20(%rax,%rdx)
orl $-0x1, 0x1c(%rax,%rdx)
movl %r11d, 0x4(%rax,%rdx)
movq %rbx, %r13
movb %r13b, 0x24(%rax,%rdx)
movl %ecx, %eax
movq %rax, 0x20(%rsp,%rbp,8)
incq %r8
jmp 0x28616
orq $-0x1, 0x20(%rsp,%rbp,8)
movq %rbx, %r13
incq %rbp
jmp 0x2853d
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
testq %r8, %r8
je 0x28717
cmpq $0x0, 0x10(%rsp)
je 0x2872a
addq $0x14, %r14
xorl %ebp, %ebp
leaq 0x68(%rsp), %rbx
leaq 0x70(%rsp), %r15
subq $0x1, %r12
jb 0x28719
movl -0xc(%r14), %esi
testb $0x1, -0x8(%r14)
jne 0x2866e
movq 0x220(%rdi), %rax
decq %rax
jmp 0x28691
movzbl (%r14), %eax
movzbl (%r9,%rax), %ecx
cmpq $-0x1, 0x20(%rsp,%rcx,8)
je 0x2870e
movl %esi, -0x4(%r14)
movzbl (%r9,%rax), %eax
movq 0x20(%rsp,%rax,8), %rax
leaq -0x14(%r14), %r13
movl %eax, -0xc(%r14)
movl -0x10(%r14), %eax
imulq $0xa8, %rax, %rax
movq (%rsp), %rcx
movl 0x10(%rcx,%rax), %edi
movl 0x1c(%rcx,%rax), %edx
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
movq 0xb0(%rsp), %r9
pushq %rbx
pushq %r15
callq 0x25d2c
popq %rax
popq %rcx
movb 0x70(%rsp), %al
shlb $0x2, %al
orb -0x8(%r14), %al
movb 0x68(%rsp), %cl
shlb $0x4, %cl
orb %al, %cl
movb %cl, -0x8(%r14)
imulq $0x18, %rbp, %rax
incq %rbp
movq 0x10(%r13), %rcx
movq 0x18(%rsp), %rdx
movq %rcx, 0x10(%rdx,%rax)
movdqu (%r13), %xmm0
movdqu %xmm0, (%rdx,%rax)
movq 0x8(%rsp), %rdi
leaq 0x22af22(%rip), %r9     # 0x253630
addq $0x18, %r14
jmp 0x2864d
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x14, %r14
xorl %ebp, %ebp
movq 0x18(%rsp), %rsi
subq $0x1, %r12
jb 0x28719
testb $0x1, -0x8(%r14)
jne 0x2874c
movl 0x220(%rdi), %eax
decl %eax
jmp 0x2876e
movzbl (%r14), %eax
movzbl (%r9,%rax), %ecx
cmpq $-0x1, 0x20(%rsp,%rcx,8)
je 0x2878f
movl -0xc(%r14), %ecx
movl %ecx, -0x4(%r14)
movzbl (%r9,%rax), %eax
movl 0x20(%rsp,%rax,8), %eax
movl %eax, -0xc(%r14)
leaq -0x14(%r14), %rax
imulq $0x18, %rbp, %rcx
incq %rbp
movq 0x10(%rax), %rdx
movq %rdx, 0x10(%rsi,%rcx)
movdqu (%rax), %xmm0
movdqu %xmm0, (%rsi,%rcx)
addq $0x18, %r14
jmp 0x28735