// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x58(%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x18862b
movq %rdi, %r12
movq %rsi, 0x50(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %r8, 0x20(%rsp)
movq %r9, 0x30(%rsp)
movq 0xe8(%rsp), %r8
movq 0xe0(%rsp), %r14
movq $0x0, (%r14)
movl 0xc(%r8), %ecx
testl %ecx, %ecx
je 0x1882d3
movq %r12, 0x8(%rsp)
movl $0x20, %ebx
xorl %r15d, %r15d
movq (%r8), %rax
cmpb $0x0, (%rax,%rbx)
je 0x188240
movl -0x14(%rax,%rbx), %r13d
movq -0x8(%rax,%rbx), %rbp
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
jne 0x18821d
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r8, %r12
callq 0x2370
movq %r12, %r8
movq %rax, 0x10(%r14)
movq (%r14), %rdx
movl 0xc(%r12), %ecx
shlq $0x21, %r13
andl $0x80000000, %ebp       # imm = 0x80000000
leaq (,%rbp,2), %rsi
addq %r13, %rsi
orq %r15, %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, (%r14)
movq %rsi, (%rax,%rdx,8)
incq %r15
movl %ecx, %eax
addq $0x28, %rbx
cmpq %rax, %r15
jb 0x1881cb
movq (%r14), %r13
testq %r13, %r13
movq 0x8(%rsp), %r12
je 0x1882d3
movq 0x10(%r12), %rdi
leaq (%rdi,%r13), %rax
cmpq %rax, 0x8(%r14)
jae 0x18833f
movq %r8, %rbp
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(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %rbx
movq %rax, 0x10(%r14)
movq (%r14), %r13
movq 0x10(%r12), %rdi
jmp 0x188346
movzbl 0xf0(%rsp), %eax
movq %r12, %rdi
movq 0x50(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq 0x20(%rsp), %r8
movq 0x30(%rsp), %r9
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
pushq 0xf8(%rsp)
pushq %rax
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
callq 0x840d4
jmp 0x188622
movq %r8, %rbp
movq 0x10(%r14), %rbx
leaq (%rbx,%r13,8), %rsi
testq %rdi, %rdi
je 0x188596
movq (%r12), %rax
addq $0x2e, %rax
xorl %ecx, %ecx
movb $0x0, (%rax)
movw $0x0, -0x2(%rax)
movl -0x1e(%rax), %edx
movl -0x12(%rax), %r8d
shlq $0x21, %rdx
shlq $0x20, %r8
orq %rdx, %r8
orq %rcx, %r8
movq %r8, (%rsi,%rcx,8)
incq %rcx
addq $0xa8, %rax
cmpq %rcx, %rdi
jne 0x18835d
movq %rdi, 0x18(%rsp)
movq %rbx, %rdi
movq %rsi, 0x10(%rsp)
callq 0x17fc92
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movq 0x10(%rsp), %rdi
callq 0x17fc92
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
xorl %r15d, %r15d
xorl %r9d, %r9d
xorl %r14d, %r14d
movq %rbp, %rsi
cmpq %r13, %r14
jae 0x18856a
movq (%r12), %rax
movl (%r8,%r15,8), %ecx
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %r10
movl 0x10(%rax,%rcx), %r12d
movl 0x1c(%rax,%rcx), %ebp
leaq (%r12,%r12), %rax
orq %rbp, %rax
movq (%rbx,%r14,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
cmpq %rax, %rdx
jae 0x188419
incq %r14
cmpq %r14, %r13
jne 0x1883f9
movq %r13, %r14
jmp 0x188565
jne 0x188565
movq (%rsi), %rax
movl %ecx, %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x4(%r10), %edx
cmpl (%rax,%rcx,8), %edx
jne 0x188565
leaq (%rax,%rcx,8), %rcx
movl 0x8(%r10), %eax
incl %eax
cmpl 0x8(%rcx), %eax
jne 0x188565
movl 0x14(%r10), %eax
cmpl 0x10(%rcx), %eax
jne 0x188565
movl 0x18(%r10), %edx
incl %edx
cmpl 0x14(%rcx), %edx
jne 0x188565
movq %r9, 0x28(%rsp)
subl %eax, %edx
movq 0x20(%rsp), %rsi
movq (%rsi), %rdi
cmpq %rdx, 0x10(%rsi)
movq %r10, 0x70(%rsp)
movq %rcx, 0x48(%rsp)
jge 0x1884b1
movq %rdx, %rsi
movq %rdx, 0x68(%rsp)
callq 0x2370
movq %rax, %rdi
movq 0x20(%rsp), %rax
movq %rdi, (%rax)
movq 0x68(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x48(%rsp), %rcx
movl 0x10(%rcx), %eax
movl 0x14(%rcx), %edx
subl %eax, %edx
movl %eax, %esi
movzbl %bpl, %ecx
movq 0x40(%rsp), %r8
movq %r12, %r9
callq 0x32ef8
movq 0x48(%rsp), %rax
movl 0x14(%rax), %edx
subl 0x10(%rax), %edx
movl (%rax), %edi
movl 0x8(%rax), %eax
subq %rdi, %rax
cmpq %rdx, %rax
jne 0x188578
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq 0x38(%rsp), %rax
addq (%rax), %rdi
callq 0x21a0
testl %eax, %eax
movq 0x8(%rsp), %r12
jne 0x18857d
movq 0x70(%rsp), %rbp
movw $0x1, 0x2c(%rbp)
movl 0x30(%rbp), %eax
movl %eax, 0x24(%rbp)
movl $0x0, 0x30(%rbp)
movb $0x0, 0x2e(%rbp)
movl 0x4(%rbp), %esi
movl 0x8(%rbp), %edx
incl %edx
movl 0x14(%rbp), %ecx
movl 0x18(%rbp), %r8d
incl %r8d
movq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0x663fa
movq %rbp, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %r12
callq 0x65a71
movq 0x28(%rsp), %r9
incq %r9
movq 0xe8(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
jmp 0x18856a
movq 0x8(%rsp), %r12
incq %r15
cmpq %rdi, %r15
jne 0x1883cd
jmp 0x1885b6
movq 0x8(%rsp), %r12
movq 0xe8(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movq 0x28(%rsp), %r9
jmp 0x18856a
movq %rdi, %r14
movq %rbx, %rdi
movq %rsi, %rbx
callq 0x17fc92
leaq (%rbx,%r14,8), %rsi
movq %rbx, %rdi
callq 0x17fc92
movq %r14, %rdi
xorl %r9d, %r9d
cmpq %r9, %rdi
movq 0x30(%rsp), %r9
movq 0x20(%rsp), %r8
movq 0x38(%rsp), %rcx
movq 0x40(%rsp), %rdx
movq 0x50(%rsp), %rsi
jbe 0x18862b
movzbl 0xf0(%rsp), %eax
movq %r12, %rdi
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
pushq 0xf8(%rsp)
pushq %rax
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
callq 0x84470
addq $0xb8, %rsp
jmp 0x18862f
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq