// File: /workspace/github_cpp_makefile_star2_O2/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, 0x50(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x30(%rsp)
andq $0x0, 0x18(%rdi)
movq %rsi, 0x58(%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x510df
movq %r8, %r12
movq %rcx, %r14
movq %rdi, %r13
movq %rdx, 0x38(%rsp)
movq %r9, 0x68(%rsp)
movq 0x158(%rsp), %rax
movsd 0x1926f3(%rip), %xmm1   # 0x1e34c0
mulsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
leaq 0x7e(,%rax,2), %rsi
movq %r8, %rdi
movsd %xmm0, (%rsp)
callq 0x562fa
movsd (%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
addsd 0x192751(%rip), %xmm1   # 0x1e3550
movsd %xmm1, 0x28(%rsp)
movsd 0x1927bb(%rip), %xmm1   # 0x1e35c8
mulsd %xmm0, %xmm1
minsd 0x1927b7(%rip), %xmm1   # 0x1e35d0
movsd %xmm1, 0x48(%rsp)
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %edx, %edx
movq %r13, 0x8(%rsp)
movq %r14, 0x60(%rsp)
movq %r12, 0x20(%rsp)
cmpq 0x10(%r13), %rdx
jae 0x510db
movq (%r13), %rbx
cmpb $0x1, 0x2c(%rbx,%rbp)
jne 0x50ed7
cmpq %rcx, %rdx
je 0x50ebc
imulq $0xa8, %rcx, %r13
addq %rbx, %r13
movq %rdx, %r14
movl $0xa8, %edx
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
movq %r12, %r15
movq %rcx, %r12
callq 0x2160
addq %rbp, %rbx
movl $0xa8, %edx
movq %r13, %rdi
movq 0x8(%rsp), %r13
movq %rbx, %rsi
callq 0x2160
movq (%r13), %rdi
addq %rbp, %rdi
movl $0xa8, %edx
leaq 0x70(%rsp), %rsi
callq 0x2160
movq %r14, %rdx
movq 0x60(%rsp), %r14
movq %r12, %rcx
movq %r15, %r12
movsd (%rsp), %xmm0
movq (%r13), %rbx
imulq $0xa8, %rcx, %rax
incq %rcx
movw $0x1, 0x2c(%rbx,%rax)
movb $0x0, 0x2e(%rbx,%rax)
jmp 0x51073
movq %rdx, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
leaq (%rbx,%rbp), %r13
movl 0x30(%rbx,%rbp), %eax
movl %eax, 0x24(%rbx,%rbp)
movq (%r14), %rdx
movq (%r12), %rcx
subq $0x8, %rsp
movq %r13, %rdi
movq 0x40(%rsp), %r15
movq %r15, %rsi
movq 0x70(%rsp), %r12
movq %r12, %r8
movq 0x160(%rsp), %r9
movsd 0x192625(%rip), %xmm1   # 0x1e3540
pushq $0x0
callq 0x2ba50
popq %rcx
popq %rdx
testl %eax, %eax
je 0x5105a
movq (%r14), %r8
movq 0x20(%rsp), %rax
movq (%rax), %r9
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r15, %rcx
movsd (%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
leaq 0x40(%rsp), %rax
pushq %rax
pushq $0x0
pushq $0x1f
pushq 0x170(%rsp)
pushq $0x0
pushq %r12
callq 0x2cfcc
addq $0x30, %rsp
movb $0x0, 0x2c(%rbx,%rbp)
movsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
movapd %xmm1, %xmm0
ja 0x5105f
movl 0x40(%rsp), %eax
movl %eax, 0x30(%rbx,%rbp)
movq %r14, %rbx
movq (%r14), %r8
subq $0x30, %rsp
leaq 0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x180(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, (%rsp)
movq %r13, %rdi
movq 0x88(%rsp), %rsi
movq %r15, %rdx
movq 0x188(%rsp), %rcx
movq 0x50(%rsp), %r12
movq %r12, %r9
callq 0x4fcd1
addq $0x30, %rsp
movq 0x188(%rsp), %rsi
testq %rsi, %rsi
movq %r13, %r14
js 0x51018
movq %r14, %rdi
movsd (%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
callq 0x62fc5
testl %eax, %eax
je 0x510d1
cmpb $0x0, 0x180(%rsp)
movq 0x8(%rsp), %r13
je 0x51082
movl $0x180, %esi            # imm = 0x180
movl $0x7d0, %edx            # imm = 0x7D0
movl $0x80, %ecx
movq %r14, %rdi
movsd 0x191baf(%rip), %xmm0   # 0x1e2bf0
movsd 0x48(%rsp), %xmm1
callq 0x6314e
testl %eax, %eax
je 0x51082
movsd (%rsp), %xmm0
movq %rbx, %r14
jmp 0x51069
movsd (%rsp), %xmm0
movq 0x20(%rsp), %r12
movq 0x8(%rsp), %r13
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
incq %rdx
addq $0xa8, %rbp
jmp 0x50e34
movq %r14, %rdi
movq %rbx, %r14
movq (%rbx), %rdx
subq $0x8, %rsp
movq 0x158(%rsp), %rsi
movq 0x38(%rsp), %rcx
xorl %r8d, %r8d
pushq $-0x1
popq %r9
pushq 0x180(%rsp)
pushq %r12
pushq 0x50(%rsp)
callq 0x508c8
addq $0x20, %rsp
movq (%r13), %rbx
movsd (%rsp), %xmm0
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
jmp 0x50e4d
movsd (%rsp), %xmm0
movq %rbx, %r14
jmp 0x51064
movq %rcx, 0x10(%r13)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq