// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_hc_r_alin_nec(overlap_region_alloc*, Candidates_list*, All_reads*, UC_Read*, UC_Read*, bit_extz_t*, overlap_region*, double, long, long, long, long, asg16_v*, unsigned char, double, long)
void gen_hc_r_alin_nec(overlap_region_alloc* ol, Candidates_list *cl, All_reads *rref, UC_Read* qu, UC_Read* tu, bit_extz_t *exz, overlap_region *aux_o, double e_rate, int64_t wl, int64_t rid, int64_t khit, int64_t move_gap, asg16_v* buf, uint8_t chem_drop, double align_gap_rate, int64_t align_gap_max)
{
    uint64_t i, bs, k, ql = qu->length; Window_Pool w; double err, e_max, rr; int64_t re;
    overlap_region t; overlap_region *z; //asg64_v iidx, buf, buf1;
    ol->mapped_overlaps_length = 0;
    if(ol->length <= 0) return;
    // if(ol->length && ol->list[0].x_id == 19350) e_rate = 0.1;

    ///base alignment
    err = e_rate; e_max = err * 1.5;
    init_Window_Pool(&w, ql, wl, (int)(1.0/err));
    bs = (w.window_length)+(THRESHOLD_MAX_SIZE<<1)+1;
    resize_UC_Read(tu, bs<<1); 
    // fprintf(stderr, "[M::%s] window_length::%lld\n", __func__, w.window_length);

    ///debug for memory
    // snprintf(NULL, 0, "dwn::%u\tdcn::%u", (uint32_t)aux_o->w_list.n, (uint32_t)aux_o->w_list.c.n);

    for (i = k = 0; i < ol->length; i++) {
        z = &(ol->list[i]); 
        if(z->is_match != 1) {
            z->shared_seed = z->non_homopolymer_errors;///for index

            // if(z->x_id == 19350 && z->y_id == 19324) fprintf(stderr, "-z-[M::%s] tid::%u\t%.*s\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id));
            
            if(!align_hc_ed_post_extz(z, rref, qu->seq, tu->seq, exz, err, w.window_length, OVERLAP_THRESHOLD_HIFI_FILTER, 0, NULL)) continue;

            // if(z->x_id == 19350 && z->y_id == 19324) fprintf(stderr, "-m-[M::%s] tid::%u\t%.*s\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id));

            rr = gen_extend_err_exz(z, NULL, NULL, rref, qu->seq, tu->seq, exz, NULL, w.window_length, -1, err, (e_max+0.000001), THRESHOLD_MAX_SIZE, 0, &re);
            z->is_match = 0; 

            // if(z->x_id == 19350 && z->y_id == 19324) fprintf(stderr, "-0-[M::%s] tid::%u\t%.*s\trr::%f\tre::%ld\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), rr, re);

            if (rr > err) continue;
            z->non_homopolymer_errors = re;

            ///debug for memory
            // snprintf(NULL, 0, "dwn::%u\tdcn::%u", (uint32_t)aux_o->w_list.n, (uint32_t)aux_o->w_list.c.n);

            if(!gen_hc_fast_cigar(z, cl, rref, w.window_length, qu->seq, tu, exz, aux_o, e_rate, ql, rid, khit, &re)) continue;

            if((align_gap_max >= 0) && (!ff_lunalign(z, err, align_gap_rate, align_gap_max))) continue;

            if(chem_drop && ff_tend(z, 384, 2000, 0.1, (((e_rate*10)<0.36)?(e_rate*10):(0.36)), 128)) continue;

            // if(z->x_id == 3196 && z->y_id == 3199) fprintf(stderr, "-1-[M::%s] tid::%u\t%.*s\trr::%f\tre::%ld\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), rr, re);
            ///debug for memory
            // snprintf(NULL, 0, "dwn::%u\tdcn::%u", (uint32_t)aux_o->w_list.n, (uint32_t)aux_o->w_list.c.n);

            reassign_gaps(z, aux_o, qu->seq, ql, NULL, -1, rref, tu, buf);

            ///debug for memory
            // snprintf(NULL, 0, "dwn::%u\tdcn::%u", (uint32_t)aux_o->w_list.n, (uint32_t)aux_o->w_list.c.n);
        }

        // if(z->x_id == 3196 && z->y_id == 3199) fprintf(stderr, "-2-[M::%s] tid::%u\t%.*s\trr::%f\tre::%u\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), rr, z->non_homopolymer_errors);

        // if(z->x_id == 19350) fprintf(stderr, "-1-[M::%s] tid::%u\t%.*s\trr::%f\terr::%u\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), rr, z->non_homopolymer_errors);

        if(k != i) {
            t = ol->list[k];
            ol->list[k] = ol->list[i];
            ol->list[i] = t;
        }
        z = &(ol->list[k++]); z->is_match = 1; ///z->non_homopolymer_errors = re;
        z->strong = z->without_large_indel = 0;
    }
    ol->length = k;
    if(ol->length <= 0) return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movsd %xmm1, 0x58(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x18(%rdi)
movq %rdi, 0x10(%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x84820
movq %r9, %rbx
movq %rcx, %r13
movq %rdx, %r12
movq 0x158(%rsp), %rbp
movq %r8, %r15
leaq 0x7e(,%rbp,2), %r14
cmpq %r14, 0x10(%r8)
movsd %xmm0, 0x8(%rsp)
jge 0x84505
movq (%r15), %rdi
movq %r14, %rsi
callq 0x2370
movq 0x158(%rsp), %rbp
movsd 0x8(%rsp), %xmm0
movq %rax, (%r15)
movq %r14, 0x10(%r15)
movq 0x10(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x84815
movq %r12, 0x28(%rsp)
movq %r15, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
movsd 0x1ad054(%rip), %xmm1   # 0x231570
mulsd %xmm0, %xmm1
addsd 0x1ad0d8(%rip), %xmm1   # 0x231600
movsd %xmm1, 0x68(%rsp)
movsd 0x1ad142(%rip), %xmm1   # 0x231678
mulsd %xmm0, %xmm1
minsd 0x1ad13e(%rip), %xmm1   # 0x231680
movsd %xmm1, 0x50(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ecx, %ecx
movq 0x10(%rsp), %rbx
movq %r13, 0x20(%rsp)
movq (%rbx), %r14
cmpb $0x1, 0x2c(%r14,%r12)
jne 0x845eb
cmpq %rcx, %r15
je 0x845ce
imulq $0xa8, %rcx, %rbp
addq %r14, %rbp
movl $0xa8, %edx
leaq 0x70(%rsp), %rdi
movq %rbp, %rsi
movq %rcx, %r13
callq 0x2160
addq %r12, %r14
movl $0xa8, %edx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2160
movq (%rbx), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x70(%rsp), %rsi
callq 0x2160
movq %r13, %rcx
movq 0x20(%rsp), %r13
movq 0x158(%rsp), %rbp
movsd 0x8(%rsp), %xmm0
movq (%rbx), %r14
imulq $0xa8, %rcx, %rax
incq %rcx
movw $0x1, 0x2c(%r14,%rax)
movb $0x0, 0x2e(%r14,%rax)
jmp 0x84699
movq %rcx, 0x18(%rsp)
leaq (%r14,%r12), %rbx
movl 0x30(%r14,%r12), %eax
movl %eax, 0x24(%r14,%r12)
movq (%r13), %rdx
movq 0x30(%rsp), %rax
movq (%rax), %rcx
subq $0x8, %rsp
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %r8
movq %rbp, %r9
movsd 0x1acfd2(%rip), %xmm1   # 0x2315f8
pushq $0x0
callq 0x5b2b4
addq $0x10, %rsp
testl %eax, %eax
je 0x84689
movq (%r13), %r8
movq 0x30(%rsp), %rax
movq (%rax), %r9
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
movsd 0x8(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
leaq 0x48(%rsp), %rax
pushq %rax
xorl %eax, %eax
pushq %rax
pushq $0x1f
pushq %rbp
pushq %rax
pushq 0x60(%rsp)
callq 0x5ccc3
addq $0x30, %rsp
movb $0x0, 0x2c(%rbx)
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x846b2
movapd %xmm1, %xmm0
jmp 0x8468f
movsd 0x8(%rsp), %xmm0
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rcx
incq %r15
addq $0xa8, %r12
cmpq 0x10(%rbx), %r15
jb 0x8455a
jmp 0x8481c
movl 0x48(%rsp), %eax
movl %eax, 0x30(%r14,%r12)
movq (%r13), %r14
movq %rbx, %rdi
movq 0x60(%rsp), %r13
movq %r13, %rsi
callq 0x7d1f5
subq $0x30, %rsp
leaq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x180(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x68(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq 0x58(%rsp), %rcx
movq %r14, %r8
movq 0x60(%rsp), %r13
movq %r13, %r9
movsd 0x38(%rsp), %xmm0
callq 0x6aee9
addq $0x30, %rsp
movq 0x188(%rsp), %rsi
testq %rsi, %rsi
movq %rbx, %r14
js 0x8475e
movq %r14, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
callq 0x98f82
testl %eax, %eax
je 0x847f8
cmpb $0x0, 0x180(%rsp)
movq 0x10(%rsp), %rbx
je 0x8479e
movl $0x180, %esi            # imm = 0x180
movl $0x7d0, %edx            # imm = 0x7D0
movl $0x80, %ecx
movq %r14, %rdi
movsd 0x1ac489(%rip), %xmm0   # 0x230c10
movsd 0x50(%rsp), %xmm1
callq 0x99117
testl %eax, %eax
je 0x8479e
movsd 0x8(%rsp), %xmm0
jmp 0x84803
movq 0x20(%rsp), %rax
movq (%rax), %rdx
subq $0x8, %rsp
movq %r14, %rdi
movq 0x158(%rsp), %rsi
movq 0x48(%rsp), %rcx
xorl %r8d, %r8d
movq $-0x1, %r9
pushq 0x180(%rsp)
pushq %r13
movq %rax, %r13
pushq 0x40(%rsp)
callq 0x83f97
addq $0x20, %rsp
movq (%rbx), %r14
movsd 0x8(%rsp), %xmm0
movq 0x158(%rsp), %rbp
movq 0x18(%rsp), %rcx
jmp 0x84569
movsd 0x8(%rsp), %xmm0
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %r13
movq 0x158(%rsp), %rbp
jmp 0x84694
xorl %ecx, %ecx
movq 0x10(%rsp), %rbx
movq %rcx, 0x10(%rbx)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq