// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq 0x58(%rbp), %rax
movb 0x50(%rbp), %al
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
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, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movsd %xmm1, -0x68(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
ja 0x2573ed
jmp 0x257bec
movq 0x40(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq 0x48(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x257532
movq 0x48(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x70(%rbp), %rcx
addq %rcx, %rax
cmpb $0x0, 0x20(%rax)
je 0x25751f
movq 0x48(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x70(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shlq %rax
movq %rax, -0x80(%rbp)
movq 0x48(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x70(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x1f, %rax
andq $0x1, %rax
movl %eax, %eax
orq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0x80(%rbp)
movq -0x70(%rbp), %rax
orq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x40(%rbp), %rax
movq (%rax), %rax
movq 0x40(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x2574fc
movq 0x40(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2574bb
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xd0(%rbp)
jmp 0x2574c9
movl $0x2, %eax
movq %rax, -0xd0(%rbp)
jmp 0x2574c9
movq -0xd0(%rbp), %rcx
movq 0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x80(%rbp), %rdx
movq 0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x40(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x25751f
jmp 0x257521
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x257400
movq 0x40(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x2575b3
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %r13
movsd -0x60(%rbp), %xmm0
movq 0x18(%rbp), %r12
movq 0x20(%rbp), %r15
movq 0x28(%rbp), %r14
movq 0x30(%rbp), %rbx
movq 0x38(%rbp), %r11
movb 0x50(%rbp), %r10b
movsd -0x68(%rbp), %xmm1
movq 0x58(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movzbl %r10b, %r10d
movl %r10d, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x9af40
jmp 0x257bec
jmp 0x2575b5
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x40(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rdx
addq 0x10(%rdx), %rcx
cmpq %rcx, %rax
jae 0x2576a7
movq 0x40(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
addq 0x10(%rax), %rcx
movq 0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq 0x40(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq 0x40(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq 0x40(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq 0x40(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq 0x40(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq 0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x2576a9
movq 0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x88(%rbp)
movq 0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq 0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x40(%rbp), %rcx
movq (%rcx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jae 0x2577d4
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x70(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movb $0x0, 0x2d(%rax)
movq -0xc0(%rbp), %rax
movb $0x0, 0x2e(%rax)
movq -0xc0(%rbp), %rax
movb $0x0, 0x2c(%rax)
movq -0xc0(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq %rdx
movq %rdx, (%rax,%rcx,8)
movq -0xc0(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, %edx
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rdx
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x2576fa
movq -0x88(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0x90(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x24aba0
movq -0x98(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x24aba0
movq $0x0, -0xb8(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jae 0x257b68
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rcx
movq -0x70(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0xa8(%rbp)
movq -0xc0(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0xb0(%rbp)
movq -0x78(%rbp), %rcx
xorl %eax, %eax
cmpq -0x90(%rbp), %rcx
movb %al, -0xd1(%rbp)
jae 0x2578d2
movq -0x88(%rbp), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq -0xa8(%rbp), %rcx
shlq %rcx
orq -0xb0(%rbp), %rcx
cmpq %rcx, %rax
setb %al
movb %al, -0xd1(%rbp)
movb -0xd1(%rbp), %al
testb $0x1, %al
jne 0x2578de
jmp 0x2578ee
jmp 0x2578e0
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x25788d
movq -0x78(%rbp), %rax
cmpq -0x90(%rbp), %rax
jae 0x257b55
movq -0x88(%rbp), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq -0xa8(%rbp), %rcx
shlq %rcx
orq -0xb0(%rbp), %rcx
cmpq %rcx, %rax
jne 0x257b55
movq 0x48(%rbp), %rax
movq (%rax), %rax
movq -0x88(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
movl 0x4(%rax), %eax
movq -0xc8(%rbp), %rcx
movq (%rcx), %rcx
cmpl %ecx, %eax
jne 0x257b53
movq -0xc0(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0xc8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x257b53
movq -0xc0(%rbp), %rax
movl 0x14(%rax), %eax
movq -0xc8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x257b53
movq -0xc0(%rbp), %rax
movl 0x18(%rax), %eax
addl $0x1, %eax
movq -0xc8(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jne 0x257b53
movq -0x50(%rbp), %rdi
movq -0xc8(%rbp), %rax
movl 0x14(%rax), %eax
movq -0xc8(%rbp), %rcx
subl 0x10(%rcx), %eax
movl %eax, %eax
movl %eax, %esi
callq 0xa62c0
movq -0x50(%rbp), %rax
movq (%rax), %rdi
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %esi
movq -0xc8(%rbp), %rax
movl 0x14(%rax), %eax
movq -0xc8(%rbp), %rcx
subl 0x10(%rcx), %eax
movl %eax, %eax
movl %eax, %edx
movq -0xb0(%rbp), %rax
movq -0x40(%rbp), %r8
movq -0xa8(%rbp), %r9
movzbl %al, %ecx
callq 0x9a30
movq -0x48(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x50(%rbp), %rax
movq (%rax), %rdx
movq -0xc8(%rbp), %rax
movl 0x14(%rax), %eax
movq -0xc8(%rbp), %rcx
subl 0x10(%rcx), %eax
movl %eax, %eax
movl %eax, %ecx
movq -0xc8(%rbp), %rax
movq (%rax), %rax
movl %eax, %eax
movl %eax, %r8d
movq -0xc8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %r9d
movq -0xc8(%rbp), %rax
movl 0x14(%rax), %eax
movq -0xc8(%rbp), %r10
subl 0x10(%r10), %eax
movl %eax, %eax
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x26dcc0
cmpq $0x0, %rax
je 0x257b51
movq -0xc0(%rbp), %rax
movb $0x1, 0x2c(%rax)
movq -0xc0(%rbp), %rax
movl 0x30(%rax), %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0xc0(%rbp), %rax
movl $0x0, 0x30(%rax)
movq -0xc0(%rbp), %rax
movb $0x0, 0x2d(%rax)
movq -0xc0(%rbp), %rax
movb $0x0, 0x2e(%rax)
movq -0x58(%rbp), %rdi
movq -0xc0(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %esi
movq -0xc0(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
movq -0xc0(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movl 0x18(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %r8d
callq 0x61ae0
movq -0xc0(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x60af0
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x257b53
jmp 0x257b55
jmp 0x257b57
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x257831
movq -0xa0(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jbe 0x257bea
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %r13
movsd -0x60(%rbp), %xmm0
movq 0x18(%rbp), %r12
movq 0x20(%rbp), %r15
movq 0x28(%rbp), %r14
movq 0x30(%rbp), %rbx
movq 0x38(%rbp), %r11
movb 0x50(%rbp), %r10b
movsd -0x68(%rbp), %xmm1
movq 0x58(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movzbl %r10b, %r10d
movl %r10d, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x9b400
jmp 0x257bec
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop