// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_hc_r_alin(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(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, err::%f\n", __func__, w.window_length, err);

    for (i = k = 0; i < ol->length; i++) {
        z = &(ol->list[i]); 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;

        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);

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

        // 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, 0x48(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x8445e
movq %r9, %r14
movq %r8, %r15
movq %rcx, %r12
movq %rdi, %rbx
movq %rdx, 0x28(%rsp)
movq 0x158(%rsp), %rax
leaq 0x7e(,%rax,2), %rbp
cmpq %rbp, 0x10(%r8)
movsd %xmm0, (%rsp)
jge 0x8415a
movq (%r15), %rdi
movq %rbp, %rsi
callq 0x2370
movsd (%rsp), %xmm0
movq %rax, (%r15)
movq %rbp, 0x10(%r15)
cmpq $0x0, 0x10(%rbx)
je 0x8444c
movq %r12, 0x38(%rsp)
movq %r14, 0x60(%rsp)
movsd 0x1ad404(%rip), %xmm1   # 0x231570
mulsd %xmm0, %xmm1
addsd 0x1ad488(%rip), %xmm1   # 0x231600
movsd %xmm1, 0x58(%rsp)
movsd 0x1ad4f2(%rip), %xmm1   # 0x231678
mulsd %xmm0, %xmm1
minsd 0x1ad4ee(%rip), %xmm1   # 0x231680
movsd %xmm1, 0x40(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r15, 0x20(%rsp)
movq (%rbx), %r14
leaq (%r14,%r13), %rbx
movl 0x30(%r14,%r13), %eax
movl %eax, 0x24(%r14,%r13)
movq 0x38(%rsp), %rbp
movq (%rbp), %rdx
movq (%r15), %rcx
subq $0x8, %rsp
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
movq 0x68(%rsp), %r15
movq %r15, %r8
movq 0x160(%rsp), %r9
movsd 0x1ad406(%rip), %xmm1   # 0x2315f8
pushq $0x0
callq 0x5b2b4
addq $0x10, %rsp
testl %eax, %eax
je 0x84271
movq %r12, 0x18(%rsp)
movq (%rbp), %r8
movq 0x20(%rsp), %rax
movq (%rax), %r9
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
movsd (%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
leaq 0x68(%rsp), %r12
pushq %r12
xorl %eax, %eax
pushq %rax
pushq $0x1f
pushq 0x170(%rsp)
pushq %rax
pushq %r15
callq 0x5ccc3
addq $0x30, %rsp
movb $0x0, 0x2c(%r14,%r13)
movsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x84285
movapd %xmm1, %xmm0
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r12
jmp 0x8442c
movsd (%rsp), %xmm0
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
jmp 0x8442c
movl 0x68(%rsp), %eax
movl %eax, 0x30(%rbx)
movq (%rbp), %r14
movq %rbx, %rdi
movq 0x50(%rsp), %rbp
movq %rbp, %rsi
callq 0x7d1f5
subq $0x30, %rsp
movq %r12, 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 %r15, (%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x188(%rsp), %rdx
movq 0x58(%rsp), %r12
movq %r12, %rcx
movq %r14, %r8
movq 0x50(%rsp), %r15
movq %r15, %r9
movsd 0x30(%rsp), %xmm0
callq 0x6aee9
addq $0x30, %rsp
movq 0x188(%rsp), %rsi
testq %rsi, %rsi
movq %r12, %r14
movq %rbx, %rbp
js 0x8432f
movq %rbp, %rdi
movsd (%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
callq 0x98f82
testl %eax, %eax
je 0x84442
cmpb $0x0, 0x180(%rsp)
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r12
je 0x84370
movl $0x180, %esi            # imm = 0x180
movl $0x7d0, %edx            # imm = 0x7D0
movl $0x80, %ecx
movq %rbp, %rdi
movsd 0x1ac8b3(%rip), %xmm0   # 0x230c10
movsd 0x40(%rsp), %xmm1
callq 0x99117
testl %eax, %eax
jne 0x84427
movq 0x38(%rsp), %rax
movq (%rax), %rdx
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x158(%rsp), %rsi
movq 0x38(%rsp), %rcx
xorl %r8d, %r8d
movq $-0x1, %r9
pushq 0x180(%rsp)
pushq %r15
pushq %r14
callq 0x83f97
addq $0x20, %rsp
movq 0x8(%rsp), %rcx
cmpq %rcx, %r12
je 0x84406
movq (%rbx), %r14
imulq $0xa8, %rcx, %rbp
addq %r14, %rbp
movl $0xa8, %edx
leaq 0x70(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2160
addq %r13, %r14
movl $0xa8, %edx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2160
movq (%rbx), %rdi
addq %r13, %rdi
movl $0xa8, %edx
movq %r12, %rsi
movq 0x18(%rsp), %r12
callq 0x2160
movq 0x8(%rsp), %rcx
movq (%rbx), %rax
movq %rcx, %rdx
imulq $0xa8, %rcx, %rcx
incq %rdx
movq %rdx, 0x8(%rsp)
movw $0x1, 0x2c(%rax,%rcx)
movb $0x0, 0x2e(%rax,%rcx)
movsd (%rsp), %xmm0
incq %r12
addq $0xa8, %r13
cmpq 0x10(%rbx), %r12
jb 0x841b1
jmp 0x84455
movsd (%rsp), %xmm0
jmp 0x84262
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rbx)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq