// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rcx, 0x18(%rsp)
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, (%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x20(%rsp)
leaq (%rdx,%rdx,2), %rax
leaq (%rsi,%rax,8), %rsi
movq %r14, %rdi
callq 0x46275
testq %r12, %r12
je 0x57e4e
leaq 0x1c(%r14), %rax
movl $0x1, %ecx
xorl %edx, %edx
xorl %r10d, %r10d
xorl %esi, %esi
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %edi, %edi
xorl %ebx, %ebx
movq %r15, 0x8(%rsp)
cmpq %rcx, %r12
je 0x57d99
movl (%rax), %r8d
leaq (%rdi,%rdi,2), %r9
cmpl 0x4(%r14,%r9,8), %r8d
je 0x57e35
leaq (%rdi,%rdi,2), %r8
leaq (%rbx,%rbx,2), %rdi
leaq (%r14,%rdi,8), %rdi
movdqu (%r14,%r8,8), %xmm0
movdqu %xmm0, (%rdi)
movq 0x10(%r14,%r8,8), %r9
movq %r9, 0x10(%rdi)
movb 0xc(%rdi), %r8b
movl %r9d, %r9d
testb $0x1, %r8b
jne 0x57dff
testq %r15, %r15
je 0x57df4
movq %rdx, %r11
movq %r10, %rdx
movl 0x4(%rdi), %r10d
imulq $0xa8, %r10, %r10
cmpl $0x0, 0x1c(%r15,%r10)
movl $0x0, %r10d
cmoveq %r9, %r10
addq %r10, %rdx
movq %rdx, %r10
movq %r11, %rdx
addq %r9, %rbp
testb %dl, %dl
je 0x57e1a
movb $0x1, %dl
jmp 0x57e24
movzbl 0x14(%rdi), %r8d
leaq 0x232825(%rip), %r11    # 0x28a630
movzbl (%r11,%r8), %r8d
addq %r9, 0x20(%rsp,%r8,8)
addq %r9, %rsi
jmp 0x57e29
shrb %r8b
andb $0x1, %r8b
movl %r8d, %edx
movq 0x8(%rsp), %r15
movl 0x10(%rdi), %edi
addq %rdi, %r13
incq %rbx
movq %rcx, %rdi
incq %rcx
addq $0x18, %rax
cmpq %r12, %rcx
jbe 0x57d82
cmpq $0x2, %rsi
setb %cl
jmp 0x57e5c
movb $0x1, %cl
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r10d, %r10d
xorl %edx, %edx
movq (%rsp), %rdi
testq %rbp, %rbp
movl $0x0, %eax
je 0x58120
testb %cl, %cl
jne 0x58120
testq %r15, %r15
je 0x57e84
cmpq %rbp, %r10
je 0x58120
incl %r10d
incl %ebp
movq %r15, 0x8(%rsp)
testq %r15, %r15
cmovel %r13d, %r10d
xorl %r8d, %r8d
xorl %r15d, %r15d
movq %rbx, 0x58(%rsp)
movq %rdx, 0x60(%rsp)
movq %r13, 0x50(%rsp)
movq 0x20(%rsp,%r15,8), %r12
cmpq $0x2, %r12
jb 0x57f7b
movq 0x228(%rdi), %rax
cmpq %rax, 0x220(%rdi)
jne 0x57f1b
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 %rbp, %r13
movq %r10, %rbp
movq %r8, %rbx
callq 0x2370
movq %rbx, %r8
movq %rbp, %r10
movq %r13, %rbp
movq 0x50(%rsp), %r13
movq 0x58(%rsp), %rbx
movq (%rsp), %rdi
movq %rax, 0x218(%rdi)
movq 0x218(%rdi), %rax
movq 0x220(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x220(%rdi)
leaq (%rcx,%rcx,4), %rdx
movl %ecx, (%rax,%rdx,8)
movl %ebp, 0x8(%rax,%rdx,8)
movl %r12d, 0xc(%rax,%rdx,8)
addl %ebp, %r12d
movl %r13d, %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
movl %r10d, 0x4(%rax,%rdx,8)
movq 0x60(%rsp), %rsi
movb %sil, 0x24(%rax,%rdx,8)
movl %ecx, %eax
movq %rax, 0x20(%rsp,%r15,8)
incq %r8
jmp 0x57f84
movq $-0x1, 0x20(%rsp,%r15,8)
incq %r15
cmpq $0x4, %r15
jne 0x57eaa
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
testq %r8, %r8
je 0x58116
cmpq $0x0, 0x10(%rsp)
je 0x5809a
testq %rbx, %rbx
je 0x5811a
addq $0x14, %r14
xorl %r15d, %r15d
leaq 0x70(%rsp), %r12
leaq 0x232664(%rip), %r13    # 0x28a630
movl -0xc(%r14), %esi
testb $0x1, -0x8(%r14)
jne 0x57fe3
movq 0x220(%rdi), %rax
decq %rax
jmp 0x58008
movzbl (%r14), %eax
movzbl (%r13,%rax), %ecx
cmpq $-0x1, 0x20(%rsp,%rcx,8)
je 0x58088
movl %esi, -0x4(%r14)
movzbl (%r13,%rax), %eax
movq 0x20(%rsp,%rax,8), %rax
leaq -0x14(%r14), %rbp
movl %eax, -0xc(%r14)
movl -0x10(%r14), %eax
imulq $0xa8, %rax, %rax
movq 0x8(%rsp), %rcx
movl 0x10(%rcx,%rax), %edi
movl 0x1c(%rcx,%rax), %edx
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
movq 0xb0(%rsp), %r9
leaq 0x68(%rsp), %rax
pushq %rax
pushq %r12
callq 0x54e80
addq $0x10, %rsp
movb 0x70(%rsp), %al
shlb $0x2, %al
orb -0x8(%r14), %al
movb %al, -0x8(%r14)
movb 0x68(%rsp), %cl
shlb $0x4, %cl
orb %al, %cl
movb %cl, -0x8(%r14)
leaq (%r15,%r15,2), %rax
incq %r15
movq 0x10(%rbp), %rcx
movq 0x18(%rsp), %rdx
movq %rcx, 0x10(%rdx,%rax,8)
movdqu (%rbp), %xmm0
movdqu %xmm0, (%rdx,%rax,8)
movq (%rsp), %rdi
addq $0x18, %r14
decq %rbx
jne 0x57fcc
jmp 0x5811d
testq %rbx, %rbx
je 0x58116
addq $0x14, %r14
xorl %eax, %eax
leaq 0x232584(%rip), %r8     # 0x28a630
movq %rdi, %r9
testb $0x1, -0x8(%r14)
jne 0x580c0
movl 0x220(%rdi), %ecx
decl %ecx
jmp 0x580e2
movzbl (%r14), %ecx
movzbl (%r8,%rcx), %edx
cmpq $-0x1, 0x20(%rsp,%rdx,8)
je 0x5810b
movl -0xc(%r14), %edx
movl %edx, -0x4(%r14)
movzbl (%r8,%rcx), %ecx
movl 0x20(%rsp,%rcx,8), %ecx
movl %ecx, -0xc(%r14)
leaq -0x14(%r14), %rcx
leaq (%rax,%rax,2), %rdx
incq %rax
movq 0x10(%rcx), %rsi
movq 0x18(%rsp), %rdi
movq %rsi, 0x10(%rdi,%rdx,8)
movdqu (%rcx), %xmm0
movdqu %xmm0, (%rdi,%rdx,8)
movq %r9, %rdi
addq $0x18, %r14
decq %rbx
jne 0x580af
jmp 0x58120
xorl %eax, %eax
jmp 0x58120
xorl %r15d, %r15d
movq %r15, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq