// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: push_idel_info(haplotype_evdience_alloc*, unsigned long, unsigned long, unsigned long, ul_ov_t*, unsigned long, ul_ov_t*, unsigned long, overlap_region*, unsigned long*, unsigned long, unsigned long, long, long, long, double, long)
void push_idel_info(haplotype_evdience_alloc *h, uint64_t s, uint64_t e, uint64_t err, ul_ov_t *a, uint64_t an, ul_ov_t *a1, uint64_t an1, overlap_region *oa, uint64_t *idx, uint64_t idx_n, uint64_t rid, int64_t het_cov, int64_t hom_cov, int64_t n_hap, double cut_rate, int64_t cut_bd)
{
    // fprintf(stderr, "\n");
    uint64_t k, q[2], os, oe, /**hn = h->length,**/ bl0 = h->length; int64_t cc, occ0, occ1, occ2; ul_ov_t *p; overlap_region *z; haplotype_evdience ev; int64_t ec; SnpStats *ps = NULL;
    for (k = occ0 = occ1 = occ2 = 0; k < idx_n; k++) {
        p = &(a[idx[k]]); z = &(oa[ovlp_id(*p)]); 
        if(z->is_match != 1) continue;

        q[0] = z->w_list.a[ovlp_cur_wid(*p)].x_start+ovlp_bd(*p);
        q[1] = z->w_list.a[ovlp_cur_wid(*p)].x_end+1-ovlp_bd(*p);
        os = MAX(q[0], s); oe = MIN(q[1], e);
        if((oe > os) && ((oe - os) >= ((e - s) - (oe - os)))) {
            // fprintf(stderr, "[M::%s]\ts::%lu\n", __func__, s);
            ec = extract_sub_err(z, s, e, os, oe, err, 0.2, p);
            if(ec >= 0) {
                occ0++;
                ev.misBase = 0;
                ev.overlapID = ovlp_id(*p);
                ev.site = s;
                ev.overlapSite = h->snp_stat.n;
                ev.type = 0;
                ev.cov = ec;///error
                // fprintf(stderr, "+0+[M::%s]\th->length::%u\tsite::%u\toverlapSite::%u\n", __func__, h->length, ev.site, ev.overlapSite);
                addHaplotypeEvdience(h, &ev, NULL);
            } else {
                occ2++;
            }
        }
    }

    ///add sv
    for (k = 0, occ1 = an1; k < an1; k++) {
        ev.misBase = 0;
        ev.overlapID = a1[k].tn;
        ev.site = s;
        ev.overlapSite = h->snp_stat.n;
        ev.type = 1;
        ev.cov = a1[k].qn;///error
        // fprintf(stderr, "+1+[M::%s]\th->length::%u\tsite::%u\toverlapSite::%u\n", __func__, h->length, ev.site, ev.overlapSite);
        addHaplotypeEvdience(h, &ev, NULL);
    }
    
    // for (k = hn; k < h->length; k++) {
    //     z = &(oa[h->list[k].overlapID]); 
        // fprintf(stderr, "[M::%s]\t%.*s\tq::[%u, %u)\terr::%u\ttype::%u\n", __func__, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), h->list[k].site, h->list[k].overlapSite, h->list[k].cov, h->list[k].type);
    // }
    
    ///for debug indel
    // if(occ0 < 2 || occ1 < 2) {
    //     h->length = bl0;
    //     return;
    // }
    occ0++;
    cc = ((het_cov > 0)?(het_cov):(hom_cov/n_hap)); cc *= cut_rate; if(cc < cut_bd) cc = cut_bd;
    if(occ0 < cc || occ1 < cc) {
        h->length = bl0;
        return;
    }

    kv_pushp(SnpStats, h->snp_stat, &ps);
    ps->id = h->snp_stat.n-1;
    ps->occ_0 = occ0;
    ps->occ_1 = occ1;
    ps->occ_2 = occ2;
    ps->site = s;
    ps->score = -1;
    ps->overlap_num = 0;
    ps->is_homopolymer = 0;
    // fprintf(stderr, "-[M::%s]\trid::%lu\t%.*s\tq::[%lu,%lu)\terr::%lu\tocc0::%lu\tocc1::%lu\tocc2::%lu\n", __func__, rid, (int)Get_NAME_LENGTH(R_INF, rid), Get_NAME(R_INF, rid), s, e, err, 1 + occ0, occ1, occ2);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdi, %r14
movq 0xa0(%rsp), %r10
movl 0x10(%rdi), %eax
movl %eax, 0x2c(%rsp)
testq %r10, %r10
movq %rsi, 0x18(%rsp)
je 0x719c5
movq %r8, %r15
movq %rdx, %r13
movq 0x98(%rsp), %r11
movq 0x90(%rsp), %rbx
xorl %ebp, %ebp
movq $0x0, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movq (%r11,%rbp,8), %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %r12
addq %rax, %r12
movl 0xc(%r15,%r12), %eax
imulq $0xa8, %rax, %rdi
cmpb $0x1, 0x2c(%rbx,%rdi)
jne 0x719b2
addq %r15, %r12
addq %rbx, %rdi
movq 0x58(%rdi), %rax
movl (%r12), %r8d
movl 0x18(%r12), %edx
shlq $0x5, %r8
movslq (%rax,%r8), %rcx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
addq %rdx, %rcx
cmpq %rsi, %rcx
ja 0x71897
movq %rsi, %rcx
movl 0x4(%rax,%r8), %eax
subl %edx, %eax
incl %eax
movslq %eax, %r8
cmpq %r13, %r8
jb 0x718ab
movq %r13, %r8
movq %r8, %rax
subq %rcx, %rax
jbe 0x719b2
movq %r13, %r9
subq %rax, %r9
subq %rsi, %r9
cmpq %r9, %rax
jb 0x719b2
movq %r12, (%rsp)
movq %r13, %rdx
movq 0x40(%rsp), %r9
movsd 0x1bfca3(%rip), %xmm0   # 0x231580
callq 0x6d2ae
testq %rax, %rax
js 0x7194e
movq %rax, %rdi
movl 0xc(%r12), %r12d
movl 0x220(%r14), %ebx
movl 0x10(%r14), %ecx
leal 0x1(%rcx), %eax
cmpl 0x14(%r14), %eax
jbe 0x71969
movl %ecx, %eax
shrl %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x2, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x4, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x8, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x10, %eax
orl %ecx, %eax
incl %eax
movl %eax, 0x14(%r14)
movq %rdi, 0x30(%rsp)
movq (%r14), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x30(%rsp), %rdi
movq %rax, (%r14)
movl 0x10(%r14), %ecx
jmp 0x7196c
incl 0x14(%rsp)
movq 0x18(%rsp), %rsi
movq 0xa0(%rsp), %r10
movq 0x98(%rsp), %r11
jmp 0x719aa
movq (%r14), %rax
movq 0x18(%rsp), %rsi
movq 0xa0(%rsp), %r10
movq 0x98(%rsp), %r11
incq 0x20(%rsp)
movl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
movl %esi, (%rax,%rcx,8)
movl %r12d, 0x4(%rax,%rcx,8)
movl %ebx, 0x8(%rax,%rcx,8)
xorl %edx, %edx
movb %dl, 0xc(%rax,%rcx,8)
movl %edi, 0x10(%rax,%rcx,8)
movb %dl, 0x14(%rax,%rcx,8)
incl 0x10(%r14)
movq 0x90(%rsp), %rbx
incq %rbp
cmpq %rbp, %r10
jne 0x71845
incq 0x20(%rsp)
jmp 0x719d7
movl $0x1, %eax
movq %rax, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
cmpq $0x0, 0x88(%rsp)
je 0x71a93
movq 0x80(%rsp), %r12
movl 0x10(%r14), %ecx
movq 0x88(%rsp), %r13
movl (%r12), %ebp
movl 0xc(%r12), %r15d
movl 0x220(%r14), %ebx
leal 0x1(%rcx), %eax
cmpl 0x14(%r14), %eax
jbe 0x71a59
movl %ecx, %eax
shrl %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x2, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x4, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x8, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x10, %eax
orl %ecx, %eax
incl %eax
movl %eax, 0x14(%r14)
movq (%r14), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x18(%rsp), %rsi
movq %rax, (%r14)
movl 0x10(%r14), %ecx
jmp 0x71a5c
movq (%r14), %rax
movl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
movl %esi, (%rax,%rcx,8)
movl %r15d, 0x4(%rax,%rcx,8)
movl %ebx, 0x8(%rax,%rcx,8)
movb $0x1, 0xc(%rax,%rcx,8)
movl %ebp, 0x10(%rax,%rcx,8)
movb $0x0, 0x14(%rax,%rcx,8)
movl 0x10(%r14), %ecx
incl %ecx
movl %ecx, 0x10(%r14)
addq $0x1c, %r12
decq %r13
jne 0x719fa
movq 0xc8(%rsp), %rcx
movq 0xb0(%rsp), %rax
testq %rax, %rax
jg 0x71aba
movq 0xb8(%rsp), %rax
cqto
idivq 0xc0(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x38(%rsp), %xmm0
cvttsd2si %xmm0, %rax
cmpq %rcx, %rax
cmovgq %rax, %rcx
movq 0x20(%rsp), %r15
cmpq %rcx, %r15
setge %al
movq 0x88(%rsp), %rbx
cmpq %rbx, %rcx
setle %cl
testb %cl, %al
jne 0x71afe
movl 0x2c(%rsp), %eax
movl %eax, 0x10(%r14)
jmp 0x71b8b
movq 0x218(%r14), %rax
movq 0x220(%r14), %rcx
cmpq 0x228(%r14), %rcx
jne 0x71b4f
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmovneq %rdx, %rcx
movq %rcx, 0x228(%r14)
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %rsi
movq %rax, 0x218(%r14)
movq 0x220(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x220(%r14)
leaq (%rcx,%rcx,4), %rdx
movl %ecx, (%rax,%rdx,8)
movl %r15d, 0x8(%rax,%rdx,8)
movl %ebx, 0xc(%rax,%rdx,8)
movl 0x14(%rsp), %ecx
movl %ecx, 0x10(%rax,%rdx,8)
movl %esi, 0x20(%rax,%rdx,8)
movl $0xffffffff, 0x1c(%rax,%rdx,8) # imm = 0xFFFFFFFF
movl $0x0, 0x4(%rax,%rdx,8)
movb $0x0, 0x24(%rax,%rdx,8)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq