// File: /workspace/github_cpp_makefile_star2_O2/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 %r8, %r13
movq %rcx, 0x40(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movq 0xa0(%rsp), %r10
movq 0x98(%rsp), %r11
movq 0x90(%rsp), %r9
movq 0x80(%rsp), %rbp
movl 0x10(%rdi), %eax
movl %eax, 0x24(%rsp)
movl $0x0, 0x8(%rsp)
movq $0x0, 0x28(%rsp)
xorl %r15d, %r15d
movq %rdx, 0x30(%rsp)
cmpq %r15, %r10
je 0x3ff47
imulq $0x1c, (%r11,%r15,8), %r12
movl 0xc(%r13,%r12), %eax
imulq $0xa8, %rax, %rdi
cmpb $0x1, 0x2c(%r9,%rdi)
jne 0x3ff3f
addq %r13, %r12
addq %r9, %rdi
movq 0x58(%rdi), %rax
movl (%r12), %esi
movl 0x18(%r12), %r8d
shlq $0x5, %rsi
movslq (%rax,%rsi), %rcx
andl $0x3fffffff, %r8d       # imm = 0x3FFFFFFF
addq %r8, %rcx
cmpq %rbx, %rcx
ja 0x3fe96
movq %rbx, %rcx
movl 0x4(%rax,%rsi), %eax
subl %r8d, %eax
incl %eax
movslq %eax, %r8
cmpq %rdx, %r8
jb 0x3feaa
movq %rdx, %r8
movq %r8, %rax
subq %rcx, %rax
jbe 0x3ff3f
movq %rdx, %rsi
subq %rax, %rsi
subq %rbx, %rsi
cmpq %rsi, %rax
jb 0x3ff37
movq %r12, (%rsp)
movq %rbx, %rsi
movq 0x40(%rsp), %r9
movsd 0x1a35f8(%rip), %xmm0   # 0x1e34d0
callq 0x3bc1b
testq %rax, %rax
js 0x3ff1e
incq 0x28(%rsp)
xorl %edx, %edx
movb %dl, 0x20(%rsp)
movl 0xc(%r12), %ecx
movl %ecx, 0x10(%rsp)
movl %ebx, 0xc(%rsp)
movl 0x220(%r14), %ecx
movl %ecx, 0x14(%rsp)
movb %dl, 0x18(%rsp)
movl %eax, 0x1c(%rsp)
movq %r14, %rdi
leaq 0xc(%rsp), %rsi
xorl %edx, %edx
callq 0x532ee
jmp 0x3ff22
incl 0x8(%rsp)
movq 0x30(%rsp), %rdx
movq 0xa0(%rsp), %r10
movq 0x98(%rsp), %r11
movq 0x90(%rsp), %r9
incq %r15
jmp 0x3fe43
leaq 0xc(%rsp), %r12
movq 0x88(%rsp), %r13
movq %r13, %r15
subq $0x1, %r15
jb 0x3ff97
movb $0x0, 0x20(%rsp)
movl 0xc(%rbp), %eax
movl %eax, 0x10(%rsp)
movl %ebx, 0xc(%rsp)
movl 0x220(%r14), %eax
movl %eax, 0x14(%rsp)
movb $0x1, 0x18(%rsp)
movl (%rbp), %eax
movl %eax, 0x1c(%rsp)
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x532ee
addq $0x1c, %rbp
jmp 0x3ff57
movq 0x28(%rsp), %r15
incq %r15
movq 0xb0(%rsp), %rax
testq %rax, %rax
jg 0x3ffbe
movq 0xb8(%rsp), %rax
cqto
idivq 0xc0(%rsp)
cvtsi2sd %rax, %xmm0
mulsd 0x38(%rsp), %xmm0
cvttsd2si %xmm0, %rax
movq 0xc8(%rsp), %rcx
cmpq %rcx, %rax
cmovgq %rax, %rcx
cmpq %rcx, %r15
setge %al
cmpq %r13, %rcx
setle %cl
testb %cl, %al
jne 0x3fff7
movl 0x24(%rsp), %eax
movl %eax, 0x10(%r14)
jmp 0x40074
movq 0x218(%r14), %rax
movq 0x220(%r14), %rcx
cmpq 0x228(%r14), %rcx
jne 0x4003d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmovneq %rdx, %rcx
movq %rcx, 0x228(%r14)
imulq $0x28, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x218(%r14)
movq 0x220(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x220(%r14)
imulq $0x28, %rcx, %rdx
movl %ecx, (%rax,%rdx)
movl %r15d, 0x8(%rax,%rdx)
movl %r13d, 0xc(%rax,%rdx)
movl 0x8(%rsp), %ecx
movl %ecx, 0x10(%rax,%rdx)
movl %ebx, 0x20(%rax,%rdx)
orl $-0x1, 0x1c(%rax,%rdx)
andl $0x0, 0x4(%rax,%rdx)
movb $0x0, 0x24(%rax,%rdx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq