// File: /workspace/github_cpp_makefile_star2_O1/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, 0x58(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %rdi, 0x10(%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x1810e0
movq %rsi, 0x48(%rsp)
movq %rdx, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq %r8, 0x8(%rsp)
movq %r9, 0x20(%rsp)
movq 0xe8(%rsp), %r12
movq 0xe0(%rsp), %rbx
movq $0x0, (%rbx)
cmpl $0x0, 0xc(%r12)
je 0x180d29
movl $0x20, %r14d
xorl %r15d, %r15d
movq (%r12), %rax
cmpb $0x0, (%rax,%r14)
je 0x180d18
movl -0x14(%rax,%r14), %r13d
movq -0x8(%rax,%r14), %rbp
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x180cee
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
shlq $0x21, %r13
andl $0x80000000, %ebp       # imm = 0x80000000
leaq (,%rbp,2), %rax
addq %r13, %rax
orq %r15, %rax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rcx,8)
incq %r15
movl 0xc(%r12), %eax
addq $0x28, %r14
cmpq %rax, %r15
jb 0x180cab
movq (%rbx), %rax
testq %rax, %rax
je 0x180ffb
movq 0x10(%rsp), %rcx
addq 0x10(%rcx), %rax
cmpq %rax, 0x8(%rbx)
jae 0x180d95
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)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rbp
movq 0x10(%rbx), %rbx
leaq (%rbx,%rbp,8), %r13
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
testq %rsi, %rsi
je 0x180ded
movq 0x10(%rsp), %rax
movq (%rax), %rax
addq $0x2e, %rax
xorl %ecx, %ecx
movb $0x0, (%rax)
movw $0x0, -0x2(%rax)
movl -0x1e(%rax), %edx
addq %rdx, %rdx
movq %rdx, (%r13,%rcx,8)
orl -0x12(%rax), %edx
shlq $0x20, %rdx
orq %rcx, %rdx
movq %rdx, (%r13,%rcx,8)
incq %rcx
addq $0xa8, %rax
cmpq %rcx, %rsi
jne 0x180dbc
movq %rsi, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x178fc2
movq 0x18(%rsp), %rax
leaq (,%rax,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x178fc2
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x181066
xorl %r15d, %r15d
xorl %r8d, %r8d
xorl %r14d, %r14d
movq 0xe8(%rsp), %r11
cmpq %rbp, %r14
jae 0x180f7d
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl (%r13,%r15,8), %ecx
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %r10
movl 0x10(%rax,%rcx), %r9d
movl 0x1c(%rax,%rcx), %r12d
leaq (%r9,%r9), %rax
orq %r12, %rax
movq (%rbx,%r14,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
cmpq %rax, %rdx
jae 0x180e86
incq %r14
cmpq %r14, %rbp
jne 0x180e66
movq %rbp, %r14
jmp 0x180f7d
jne 0x180f7d
movq (%r11), %rax
movl %ecx, %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x4(%r10), %edx
cmpl (%rax,%rcx,8), %edx
jne 0x180f7d
leaq (%rax,%rcx,8), %rdx
movl 0x8(%r10), %eax
incl %eax
cmpl 0x8(%rdx), %eax
jne 0x180f7d
movl 0x10(%rdx), %eax
cmpl %eax, 0x14(%r10)
jne 0x180f7d
movl 0x18(%r10), %ecx
incl %ecx
movl 0x14(%rdx), %esi
cmpl %esi, %ecx
jne 0x180f7d
movq %r10, 0x70(%rsp)
movq %r8, 0x28(%rsp)
subl %eax, %esi
movq 0x8(%rsp), %rax
cmpq %rsi, 0x10(%rax)
movq %rdx, 0x40(%rsp)
jge 0x180f21
movq (%rax), %rdi
movq %r9, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
callq 0x2370
movq 0x40(%rsp), %rdx
movq 0x68(%rsp), %r9
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x8(%rsp), %rax
movq 0x60(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq (%rax), %rdi
movl 0x10(%rdx), %esi
movl 0x14(%rdx), %edx
subl %esi, %edx
movzbl %r12b, %ecx
movq 0x38(%rsp), %r8
callq 0x337f7
movq 0x40(%rsp), %rcx
movl 0x14(%rcx), %eax
subl 0x10(%rcx), %eax
movl (%rcx), %edi
movl 0x8(%rcx), %edx
subq %rdi, %rdx
cmpq %rax, %rdx
jne 0x180f6b
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq 0x30(%rsp), %rax
addq (%rax), %rdi
callq 0x21a0
testl %eax, %eax
je 0x180f8e
movq 0xe8(%rsp), %r11
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %r8
incq %r15
cmpq %rdi, %r15
jne 0x180e34
jmp 0x181069
movq 0x70(%rsp), %r12
movw $0x1, 0x2c(%r12)
movl 0x30(%r12), %eax
movl %eax, 0x24(%r12)
movl $0x0, 0x30(%r12)
movb $0x0, 0x2e(%r12)
movl 0x4(%r12), %esi
movl 0x8(%r12), %edx
incl %edx
movl 0x14(%r12), %ecx
movl 0x18(%r12), %r8d
incl %r8d
movq 0x20(%rsp), %rdi
callq 0x666da
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x65d35
movq 0x28(%rsp), %r8
incq %r8
movq 0xe8(%rsp), %r11
movq 0x18(%rsp), %rdi
jmp 0x180f7d
movzbl 0xf0(%rsp), %eax
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %r9
movsd 0x50(%rsp), %xmm0
movsd 0x58(%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 0x849e0
jmp 0x1810d7
xorl %r8d, %r8d
cmpq %r8, %rdi
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r8
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rdx
movq 0x48(%rsp), %rsi
jbe 0x1810e0
movzbl 0xf0(%rsp), %eax
movq 0x10(%rsp), %rdi
movsd 0x50(%rsp), %xmm0
movsd 0x58(%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 0x84d7c
addq $0xb8, %rsp
jmp 0x1810e4
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq