// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq 0x48(%rbp), %rax
movb 0x40(%rbp), %al
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movq %r9, -0x48(%rbp)
movsd %xmm0, -0x50(%rbp)
movsd %xmm1, -0x58(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
ja 0x9afb9
jmp 0x9b3ea
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movsd 0x2e7232(%rip), %xmm0   # 0x382200
mulsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movq -0x78(%rbp), %rsi
movq 0x18(%rbp), %rdx
movsd 0x2e7012(%rip), %xmm0   # 0x382000
divsd -0xb8(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
leaq -0xb0(%rbp), %rdi
callq 0x9f470
movq -0xa8(%rbp), %rax
addq $0x3e, %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rdi
movq -0x68(%rbp), %rsi
shlq %rsi
callq 0xa62c0
movq $0x0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x9b3d1
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x60(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movl 0x30(%rax), %ecx
movq -0x180(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x180(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rax
movq (%rax), %rdx
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq -0x48(%rbp), %r8
movsd -0xb8(%rbp), %xmm0
movq -0xa8(%rbp), %r9
movsd 0x2e71b9(%rip), %xmm1   # 0x382268
xorl %eax, %eax
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x50da0
cmpl $0x0, %eax
jne 0x9b0d1
jmp 0x9b3c0
movq -0x180(%rbp), %rdi
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %rax
movq (%rax), %r8
movq -0x40(%rbp), %rax
movq (%rax), %r9
movq -0x48(%rbp), %r11
movq -0xa8(%rbp), %r10
movsd -0xb8(%rbp), %xmm1
movsd 0x2e717b(%rip), %xmm2   # 0x382280
addsd -0xc0(%rbp), %xmm2
xorl %eax, %eax
movl %eax, %edx
movsd 0x2e715f(%rip), %xmm0   # 0x382278
leaq -0xd0(%rbp), %rax
movq %rdx, %rsi
movq %r11, (%rsp)
movq $0x0, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq $0x1f, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x52e60
movsd %xmm0, -0xc8(%rbp)
movq -0x180(%rbp), %rax
movb $0x0, 0x2c(%rax)
movsd -0xc8(%rbp), %xmm0
ucomisd -0xb8(%rbp), %xmm0
jbe 0x9b17b
jmp 0x9b3c0
movq -0xd0(%rbp), %rax
movl %eax, %ecx
movq -0x180(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x180(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movq -0x38(%rbp), %rax
movq (%rax), %r8
movq -0x40(%rbp), %r9
movq -0x48(%rbp), %r15
movq 0x10(%rbp), %r14
movsd -0x50(%rbp), %xmm0
movq -0x78(%rbp), %rbx
movq 0x20(%rbp), %r11
movq 0x28(%rbp), %r10
leaq -0xd0(%rbp), %rax
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x99620
cmpq $0x0, %rax
jne 0x9b1fc
jmp 0x9b3c0
cmpq $0x0, 0x48(%rbp)
jl 0x9b22a
movq -0x180(%rbp), %rdi
movsd -0xb8(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
movq 0x48(%rbp), %rsi
callq 0xc2090
cmpl $0x0, %eax
jne 0x9b22a
jmp 0x9b3c0
cmpb $0x0, 0x40(%rbp)
je 0x9b2bb
movq -0x180(%rbp), %rax
movq %rax, -0x188(%rbp)
movsd 0x2e70ae(%rip), %xmm1   # 0x3822f8
mulsd -0x50(%rbp), %xmm1
movsd 0x2e7099(%rip), %xmm0   # 0x3822f0
ucomisd %xmm1, %xmm0
jbe 0x9b274
movsd 0x2e7093(%rip), %xmm0   # 0x3822f8
mulsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x190(%rbp)
jmp 0x9b286
movsd 0x2e7074(%rip), %xmm0   # 0x3822f0
movsd %xmm0, -0x190(%rbp)
jmp 0x9b286
movq -0x188(%rbp), %rdi
movsd -0x190(%rbp), %xmm1
movl $0x180, %esi            # imm = 0x180
movl $0x7d0, %edx            # imm = 0x7D0
movsd 0x2e3d71(%rip), %xmm0   # 0x37f018
movl $0x80, %ecx
callq 0xc24a0
cmpl $0x0, %eax
je 0x9b2bb
jmp 0x9b3c0
movq -0x180(%rbp), %rdi
movq 0x10(%rbp), %rsi
movq -0x38(%rbp), %rax
movq (%rax), %rdx
movq -0x78(%rbp), %rcx
movq -0x30(%rbp), %r11
movq -0x40(%rbp), %r10
movq 0x38(%rbp), %rax
xorl %r8d, %r8d
movq $-0x1, %r9
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x9acc0
movq -0x70(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x9b378
movq -0x20(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x70(%rbp), %rax
addq %rax, %rsi
leaq -0x178(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x20(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x60(%rbp), %rax
addq %rax, %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x70(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x20(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x60(%rbp), %rax
addq %rax, %rdi
leaq -0x178(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x70(%rbp)
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movb $0x1, 0x2c(%rax)
movq -0x180(%rbp), %rax
movb $0x0, 0x2d(%rax)
movq -0x180(%rbp), %rax
movb $0x0, 0x2e(%rax)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x9b03c
movq -0x70(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
ja 0x9b3ea
jmp 0x9b3ea
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)