// File: /workspace/github_cpp_makefile_star2_O1/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 %r8, 0x18(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x85121
movq %r9, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, %rbx
movq 0x158(%rsp), %r9
leaq 0x7e(,%r9,2), %rbp
movq 0x18(%rsp), %rax
cmpq %rbp, 0x10(%rax)
movsd %xmm0, 0x8(%rsp)
jge 0x84e0d
movq 0x18(%rsp), %r13
movq (%r13), %rdi
movq %rbp, %rsi
callq 0x2370
movq 0x158(%rsp), %r9
movsd 0x8(%rsp), %xmm0
movq %rax, (%r13)
movq %rbp, 0x10(%r13)
cmpq $0x0, 0x10(%rbx)
je 0x8511b
movq %r12, 0x28(%rsp)
movq %r15, 0x40(%rsp)
movq %r14, 0x30(%rsp)
movsd 0x1a0641(%rip), %xmm1   # 0x225470
mulsd %xmm0, %xmm1
addsd 0x1a06c5(%rip), %xmm1   # 0x225500
movsd %xmm1, 0x68(%rsp)
movsd 0x1a072f(%rip), %xmm1   # 0x225578
mulsd %xmm0, %xmm1
minsd 0x1a072b(%rip), %xmm1   # 0x225580
movsd %xmm1, 0x50(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %edx, %edx
movq %rbx, 0x20(%rsp)
movq (%rbx), %r14
cmpb $0x1, 0x2c(%r14,%r12)
jne 0x84efa
cmpq %rdx, %r15
je 0x84edc
movq (%rbx), %r14
imulq $0xa8, %rdx, %rbp
addq %r14, %rbp
movq %rdx, 0x10(%rsp)
movl $0xa8, %edx
leaq 0x70(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
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
movq %r13, %rsi
callq 0x2160
movq 0x10(%rsp), %rdx
movq 0x158(%rsp), %r9
movsd 0x8(%rsp), %xmm0
movq (%rbx), %rax
imulq $0xa8, %rdx, %rcx
incq %rdx
movw $0x1, 0x2c(%rax,%rcx)
movb $0x0, 0x2e(%rax,%rcx)
jmp 0x84faf
movq %rdx, 0x10(%rsp)
leaq (%r14,%r12), %r13
movl 0x30(%r14,%r12), %eax
movl %eax, 0x24(%r14,%r12)
movq 0x40(%rsp), %rbx
movq (%rbx), %rdx
movq 0x18(%rsp), %rax
movq (%rax), %rcx
subq $0x8, %rsp
movq %r13, %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %r8
movq %r9, %rbp
movsd 0x1a05bf(%rip), %xmm1   # 0x2254f8
pushq $0x0
callq 0x5bc89
addq $0x10, %rsp
testl %eax, %eax
je 0x84f9c
movq (%rbx), %r8
movq 0x18(%rsp), %rax
movq (%rax), %r9
movq %r13, %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 0x58(%rsp)
callq 0x5d4ae
addq $0x30, %rsp
movb $0x0, 0x2c(%r13)
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x84fc8
movapd %xmm1, %xmm0
jmp 0x84fa2
movsd 0x8(%rsp), %xmm0
movq 0x20(%rsp), %rbx
movq %rbp, %r9
movq 0x10(%rsp), %rdx
incq %r15
addq $0xa8, %r12
cmpq 0x10(%rbx), %r15
jb 0x84e68
jmp 0x8511d
movl 0x48(%rsp), %eax
movl %eax, 0x30(%r14,%r12)
movq (%rbx), %r14
movq %r13, %rdi
movq 0x60(%rsp), %rbx
movq %rbx, %rsi
callq 0x7d780
subq $0x30, %rsp
leaq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x180(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq 0x58(%rsp), %rcx
movq %r14, %r8
movq 0x48(%rsp), %r9
movsd 0x38(%rsp), %xmm0
callq 0x6b301
addq $0x30, %rsp
movq 0x188(%rsp), %rsi
testq %rsi, %rsi
js 0x8506d
movq %r13, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
callq 0x98408
testl %eax, %eax
je 0x85103
cmpb $0x0, 0x180(%rsp)
movq 0x20(%rsp), %rbx
je 0x850ad
movl $0x180, %esi            # imm = 0x180
movl $0x7d0, %edx            # imm = 0x7D0
movl $0x80, %ecx
movq %r13, %rdi
movsd 0x19fb3a(%rip), %xmm0   # 0x224bd0
movsd 0x50(%rsp), %xmm1
callq 0x98591
testl %eax, %eax
je 0x850ad
movsd 0x8(%rsp), %xmm0
jmp 0x8510e
movq 0x40(%rsp), %rax
movq (%rax), %rdx
subq $0x8, %rsp
movq %r13, %rdi
movq 0x158(%rsp), %rsi
movq 0x40(%rsp), %rcx
xorl %r8d, %r8d
movq $-0x1, %r9
pushq 0x180(%rsp)
pushq 0x28(%rsp)
pushq 0x40(%rsp)
callq 0x8489b
addq $0x20, %rsp
movsd 0x8(%rsp), %xmm0
movq 0x158(%rsp), %r9
movq 0x10(%rsp), %rdx
jmp 0x84e77
movsd 0x8(%rsp), %xmm0
movq 0x20(%rsp), %rbx
movq 0x158(%rsp), %r9
jmp 0x84faa
xorl %edx, %edx
movq %rdx, 0x10(%rbx)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq