// File: /workspace/github_cpp_makefile_star2_O2/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, 0x58(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x38(%rsp)
andq $0x0, 0x18(%rdi)
movq %rsi, 0x60(%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x50d63
movq %r8, %r15
movq %rcx, %r12
movq %rdi, %rbp
movq %rdx, 0x40(%rsp)
movq %r9, 0x68(%rsp)
movq 0x158(%rsp), %rax
movsd 0x192a72(%rip), %xmm1   # 0x1e34c0
mulsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
leaq 0x7e(,%rax,2), %rsi
movq %r8, %rdi
movsd %xmm0, (%rsp)
callq 0x562fa
movsd (%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
addsd 0x192ad0(%rip), %xmm1   # 0x1e3550
movsd %xmm1, 0x30(%rsp)
movsd 0x192b3a(%rip), %xmm1   # 0x1e35c8
mulsd %xmm0, %xmm1
minsd 0x192b36(%rip), %xmm1   # 0x1e35d0
movsd %xmm1, 0x50(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
xorl %edx, %edx
movq %rbp, 0x20(%rsp)
movq %r15, 0x28(%rsp)
cmpq 0x10(%rbp), %rdx
jae 0x50d5a
movq %rdx, 0x8(%rsp)
movq (%rbp), %rbx
leaq (%rbx,%r13), %rbp
movl 0x30(%rbx,%r13), %eax
movl %eax, 0x24(%rbx,%r13)
movq (%r12), %rdx
movq (%r15), %rcx
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x48(%rsp), %r14
movq %r14, %rsi
movq 0x70(%rsp), %r15
movq %r15, %r8
movq 0x160(%rsp), %r9
movsd 0x192a39(%rip), %xmm1   # 0x1e3540
pushq $0x0
callq 0x2ba50
popq %rcx
popq %rdx
testl %eax, %eax
je 0x50c52
movq (%r12), %r8
movq 0x28(%rsp), %rax
movq (%rax), %r9
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r14, %rcx
movsd (%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x48(%rsp), %rax
pushq %rax
pushq $0x0
pushq $0x1f
pushq 0x170(%rsp)
pushq $0x0
pushq %r15
callq 0x2cfcc
addq $0x30, %rsp
movb $0x0, 0x2c(%rbx,%r13)
movsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
movapd %xmm1, %xmm0
ja 0x50c57
movl 0x48(%rsp), %eax
movq %r13, 0x18(%rsp)
movl %eax, 0x30(%rbx,%r13)
movq %r12, %rbx
movq (%r12), %r8
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 %r15, (%rsp)
movq %rbp, %rdi
movq 0x90(%rsp), %rsi
movq %r14, %rdx
movq 0x188(%rsp), %rcx
movq 0x58(%rsp), %r15
movq %r15, %r9
callq 0x4fcd1
addq $0x30, %rsp
movq 0x188(%rsp), %rsi
testq %rsi, %rsi
movq %rbp, %r14
js 0x50c0d
movq %r14, %rdi
movsd (%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
callq 0x62fc5
testl %eax, %eax
je 0x50d43
cmpb $0x0, 0x180(%rsp)
movq 0x20(%rsp), %rbp
je 0x50c75
movl $0x180, %esi            # imm = 0x180
movl $0x7d0, %edx            # imm = 0x7D0
movl $0x80, %ecx
movq %r14, %rdi
movsd 0x191fba(%rip), %xmm0   # 0x1e2bf0
movsd 0x50(%rsp), %xmm1
callq 0x6314e
testl %eax, %eax
je 0x50c75
movsd (%rsp), %xmm0
movq %rbx, %r12
jmp 0x50d50
movsd (%rsp), %xmm0
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %rbp
movq 0x8(%rsp), %rdx
incq %rdx
addq $0xa8, %r13
jmp 0x50ab8
movq %rbx, %r12
movq (%rbx), %rdx
subq $0x8, %rsp
movq %r14, %rdi
movq 0x158(%rsp), %rsi
movq 0x40(%rsp), %rcx
xorl %r8d, %r8d
pushq $-0x1
popq %r9
pushq 0x180(%rsp)
pushq %r15
pushq 0x58(%rsp)
callq 0x508c8
addq $0x20, %rsp
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %rdx
cmpq %rbx, %rdx
je 0x50d15
movq (%rbp), %r13
imulq $0xa8, 0x10(%rsp), %rbx
addq %r13, %rbx
movl $0xa8, %edx
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x2160
movq 0x18(%rsp), %r14
addq %r14, %r13
movl $0xa8, %edx
movq %rbx, %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rbx
callq 0x2160
movq (%rbp), %rdi
addq %r14, %rdi
movl $0xa8, %edx
leaq 0x70(%rsp), %rsi
callq 0x2160
movq 0x8(%rsp), %rdx
movq (%rbp), %rax
imulq $0xa8, %rbx, %rcx
incq %rbx
movq %rbx, 0x10(%rsp)
movw $0x1, 0x2c(%rax,%rcx)
movb $0x0, 0x2e(%rax,%rcx)
movsd (%rsp), %xmm0
movq 0x18(%rsp), %r13
jmp 0x50c66
movsd (%rsp), %xmm0
movq %rbx, %r12
movq 0x20(%rsp), %rbp
movq 0x18(%rsp), %r13
jmp 0x50c61
movq 0x10(%rsp), %rax
movq %rax, 0x10(%rbp)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq