// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: gen_hc_r_alin_ea(overlap_region_alloc*, Candidates_list*, All_reads*, UC_Read*, UC_Read*, bit_extz_t*, overlap_region*, double, long, long, long, long, asg16_v*, asg64_v*, ma_hit_t_alloc*, unsigned char, double, long)
void gen_hc_r_alin_ea(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, asg64_v *srt, ma_hit_t_alloc *in, uint8_t chem_drop, double align_gap_rate, int64_t align_gap_max)
{
    if(ol->length <= 0) return;


    // uint64_t k, l, i, s, m, mm_k, *ei, en, *oi, on, tid, trev, nec; int64_t sc, mm_sc, plus, minus; overlap_region *z, t; ma_hit_t *p;
    uint64_t k, i, m, *ei, en, *oi, on, tid, trev, nec; overlap_region *z; ma_hit_t *p;
    srt->n = 0;
    for (k = 0; k < in->length; k++) {
        if(in->buffer[k].el) {
            m = in->buffer[k].tn; m <<= 1; m |= in->buffer[k].rev; 
            m <<= 32; m |= k; kv_push(uint64_t, (*srt), m);
        }
    }

    if(!(srt->n)) {
        gen_hc_r_alin(ol, cl, rref, qu, tu, exz, aux_o, e_rate, wl, rid, khit, move_gap, buf, chem_drop, align_gap_rate, align_gap_max);
    } else {
        ///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);

        kv_resize(uint64_t, *srt, (srt->n + ol->length));
        ei = srt->a; en = srt->n; oi = srt->a + srt->n; on = ol->length;
        for (k = 0; k < on; k++) {
            z = &(ol->list[k]); z->is_match = z->strong = z->without_large_indel = 0;
            oi[k] = z->y_id; oi[k] <<= 1; oi[k] |= z->y_pos_strand;
            oi[k] <<= 32; oi[k] |= k;
        }

        radix_sort_ec64(ei, ei + en); radix_sort_ec64(oi, oi + on);
        for (k = i = nec = 0; k < on; k++) {
            z = &(ol->list[(uint32_t)oi[k]]); tid = z->y_id; trev = z->y_pos_strand;
            for (; (i < en) && ((ei[i]>>32) < ((tid<<1)|trev)); i++);
            if((i < en) && ((ei[i]>>32) == ((tid<<1)|trev))) {
                p = &(in->buffer[(uint32_t)ei[i]]);
                if((z->x_pos_s == ((uint32_t)p->qns)) && (z->x_pos_e + 1 == p->qe) && 
                            (z->y_pos_s == p->ts) && (z->y_pos_e + 1 == p->te)) {
                    resize_UC_Read(tu, p->te - p->ts); recover_UC_Read_sub_region(tu->seq, p->ts, p->te - p->ts, trev, rref, tid);
                    if(exact_ec_check(qu->seq, qu->length, tu->seq, p->te - p->ts, ((uint32_t)p->qns), p->qe, 0, p->te - p->ts)) {
                        z->is_match = 1; z->shared_seed = z->non_homopolymer_errors;///for index
                        z->non_homopolymer_errors = 0; z->strong = z->without_large_indel = 0;
                        set_exact_exz(exz, z->x_pos_s, z->x_pos_e + 1, z->y_pos_s, z->y_pos_e + 1); push_alnw(z, exz);
                        nec++;
                    }
                }
            }
        }
        ///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(on > nec) {
            gen_hc_r_alin_nec(ol, cl, rref, qu, tu, exz, aux_o, e_rate, wl, rid, khit, move_gap, buf, chem_drop, align_gap_rate, align_gap_max);
        }

        // fprintf(stderr, "[M::%s] srt->n::%u, nec::%lu, on::%lu\n", __func__, (uint32_t)srt->n, nec, on);
        ///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(ol->length > 1) {///for duplicated chains
        overlap_region_sort_y_id(ol->list, ol->length);
        for (k = 1, l = m = 0; k <= ol->length; k++) {
            if(k == ol->length || ol->list[k].y_id != ol->list[l].y_id) {
                // fprintf(stderr, "\n[M::%s::tid->%u] n->%lu\n", __func__, ol->list[l].y_id, k - l);
                mm_k = l;
                if(k - l > 1) {
                    for (s = l, mm_sc = INT32_MIN, mm_k = ((uint64_t)-1); s < k; s++) {
                        z = &(ol->list[s]);
                        plus = z->x_pos_e + 1 - z->x_pos_s; minus = (z->non_homopolymer_errors) * 12;
                        sc = plus - minus;
                        if((sc > mm_sc) || ((sc == mm_sc) && ((ol->list[mm_k].x_pos_e+1-ol->list[mm_k].x_pos_s) < (z->x_pos_e+1-z->x_pos_s)))) {
                            mm_sc = sc; mm_k = s;
                        }
                        // fprintf(stderr, "[M::%s::%c] q::[%u, %u), t::[%u, %u), sc::%ld, err::%u, s::%lu\n", __func__, "+-"[z->y_pos_strand], z->x_pos_s, z->x_pos_e + 1, z->y_pos_s, z->y_pos_e + 1, sc, z->non_homopolymer_errors, s);
                    }
                }
                // fprintf(stderr, "[M::%s::tid->%u] mm_k::%lu\n", __func__, ol->list[l].y_id, mm_k);
                if(mm_k != ((uint64_t)-1)) {
                    if(mm_k != m) {
                        t = ol->list[mm_k];
                        ol->list[mm_k] = ol->list[m];
                        ol->list[m] = t;
                    }
                    m++;
                }
                l = k;
            }
        }
        ol->length = m;
    }
    **/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm1, 0x58(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %rdx, 0x38(%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x143ec7
movq %rsi, 0x48(%rsp)
movq %rcx, 0x30(%rsp)
movq %r8, 0x18(%rsp)
movq %r9, 0x20(%rsp)
movq 0xd8(%rsp), %r12
movq 0xd0(%rsp), %rbx
andq $0x0, (%rbx)
pushq $0x20
popq %r14
xorl %r15d, %r15d
movq %rdi, 0x8(%rsp)
movl 0xc(%r12), %eax
cmpq %rax, %r15
jae 0x143b25
movq (%r12), %rax
cmpb $0x0, (%rax,%r14)
je 0x143b19
movl -0x14(%rax,%r14), %r13d
movq -0x8(%rax,%r14), %rbp
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x143af6
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
shlq $0x21, %r13
andl $0x80000000, %ebp       # imm = 0x80000000
leaq (,%rbp,2), %rdx
addq %r13, %rdx
orq %r15, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
incq %r15
addq $0x28, %r14
jmp 0x143aa0
movq (%rbx), %r14
testq %r14, %r14
je 0x143ba7
movq 0x10(%rdi), %r9
leaq (%r9,%r14), %rax
movq 0x10(%rbx), %r13
cmpq %rax, 0x8(%rbx)
jae 0x143c10
movq %r12, %r15
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rbx)
leaq 0x8(,%rcx,8), %rsi
movq %r13, %rdi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, %r13
movq %rax, 0x10(%rbx)
movq (%rbx), %r14
movq 0x10(%rdi), %r9
jmp 0x143c13
movzbl 0xe0(%rsp), %eax
movq 0x48(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
pushq 0xe8(%rsp)
pushq %rax
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
callq 0x509f6
jmp 0x143ebe
movq %r12, %r15
leaq (,%r14,8), %r12
addq %r13, %r12
pushq $0x2e
popq %rax
xorl %ecx, %ecx
cmpq %rcx, %r9
je 0x143c5a
movq (%rdi), %rdx
movb $0x0, (%rdx,%rax)
andw $0x0, -0x2(%rdx,%rax)
movl -0x1e(%rdx,%rax), %esi
movl -0x12(%rdx,%rax), %edx
shlq $0x21, %rsi
shlq $0x20, %rdx
orq %rsi, %rdx
orq %rcx, %rdx
movq %rdx, (%r12,%rcx,8)
incq %rcx
addq $0xa8, %rax
jmp 0x143c23
movq %r13, %rdi
movq %r12, %rsi
movq %r9, 0x10(%rsp)
callq 0x13d044
movq 0x10(%rsp), %rax
leaq (%r12,%rax,8), %rsi
movq %r12, %rdi
callq 0x13d044
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %rdi
xorl %ebp, %ebp
xorl %r10d, %r10d
xorl %ebx, %ebx
movq %r15, %r8
movq %r12, (%rsp)
cmpq %r9, %rbx
je 0x143e55
movq (%rdi), %rax
movl (%r12,%rbx,8), %ecx
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %r11
movl 0x10(%rax,%rcx), %r12d
movl 0x1c(%rax,%rcx), %r15d
leaq (%r12,%r12), %rax
orq %r15, %rax
cmpq %r14, %rbp
movq %r14, %rcx
cmovaq %rbp, %rcx
cmpq %r14, %rbp
jae 0x143ce4
movq (%r13,%rbp,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
cmpq %rax, %rsi
jae 0x143cec
incq %rbp
jmp 0x143cc9
movq %rcx, %rbp
jmp 0x143dc3
jne 0x143dc3
movq (%r8), %rsi
movl %edx, %eax
imulq $0x28, %rax, %rax
movl 0x4(%r11), %ecx
cmpl (%rsi,%rax), %ecx
jne 0x143dc3
addq %rax, %rsi
movl 0x8(%r11), %eax
incl %eax
cmpl 0x8(%rsi), %eax
jne 0x143dc3
movl 0x14(%r11), %eax
cmpl 0x10(%rsi), %eax
jne 0x143dc3
movq %rsi, %rcx
movl 0x18(%r11), %esi
incl %esi
movq %rcx, 0x40(%rsp)
cmpl 0x14(%rcx), %esi
jne 0x143dc3
movq %r11, 0x60(%rsp)
movq %r10, 0x28(%rsp)
subl %eax, %esi
movq 0x18(%rsp), %rdi
callq 0x562fa
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x40(%rsp), %rax
movl 0x10(%rax), %esi
movl 0x14(%rax), %edx
subl %esi, %edx
movzbl %r15b, %ecx
movq 0x38(%rsp), %r8
movq %r12, %r9
callq 0x6c9e
movq 0x40(%rsp), %rax
movl 0x14(%rax), %edx
subl 0x10(%rax), %edx
movl (%rax), %edi
movl 0x8(%rax), %eax
subq %rdi, %rax
cmpq %rdx, %rax
jne 0x143dcf
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x30(%rsp), %rax
addq (%rax), %rdi
callq 0x21a0
testl %eax, %eax
movq (%rsp), %r12
je 0x143dec
movq 0x8(%rsp), %rdi
movq 0xd8(%rsp), %r8
movq 0x10(%rsp), %r9
jmp 0x143de5
movq (%rsp), %r12
incq %rbx
jmp 0x143c93
movq 0x8(%rsp), %rdi
movq 0xd8(%rsp), %r8
movq 0x10(%rsp), %r9
movq (%rsp), %r12
movq 0x28(%rsp), %r10
jmp 0x143dc7
movq 0x60(%rsp), %r15
movw $0x1, 0x2c(%r15)
movl 0x30(%r15), %eax
movl %eax, 0x24(%r15)
andl $0x0, 0x30(%r15)
movb $0x0, 0x2e(%r15)
movl 0x4(%r15), %esi
movl 0x8(%r15), %edx
incl %edx
movl 0x14(%r15), %ecx
movl 0x18(%r15), %r8d
incl %r8d
movq 0x20(%rsp), %rdi
callq 0x352b9
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x34955
movq 0x28(%rsp), %r10
incq %r10
movq 0x8(%rsp), %rdi
movq 0xd8(%rsp), %r8
movq 0x10(%rsp), %r9
jmp 0x143dc7
cmpq %r10, %r9
movq 0x20(%rsp), %r9
movq 0x18(%rsp), %r8
movq 0x30(%rsp), %rcx
movq 0x48(%rsp), %rsi
jbe 0x143ec7
movzbl 0xe0(%rsp), %eax
movq 0x38(%rsp), %rdx
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
pushq 0xe8(%rsp)
pushq %rax
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
callq 0x50d75
addq $0xa8, %rsp
jmp 0x143ecb
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq