// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: ha_get_ug_candidates(ha_abuf_s*, long, ma_utg_t*, ma_utg_v*, overlap_region_alloc*, Candidates_list*, double, int, int, kvec_t_u8_warp*, kvec_t_u64_warp*, void*, ha_pt_s*, overlap_region*, kvec_t_u64_warp*, double)
void ha_get_ug_candidates(ha_abuf_t *ab, int64_t rid, ma_utg_t *u, ma_utg_v *ua, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres, int max_n_chain, int keep_whole_chain, kvec_t_u8_warp* k_flag,
kvec_t_u64_warp* chain_idx, void *ha_flt_tab, ha_pt_t *ha_idx, overlap_region* f_cigar, kvec_t_u64_warp* dbg_ct, double chain_match_rate)
{
    uint32_t i;
    uint64_t k, l;

    // prepare
    clear_Candidates_list(cl);
    clear_overlap_region_alloc(overlap_list);
    ab->mz.n = 0, ab->n_a = 0;

    // get the list of anchors
    //should use the new version...
    ///ha_sketch_query(u->s, u->len, asm_opt.mz_win, asm_opt.k_mer_length, 0, !(asm_opt.flag & HA_F_NO_HPC), &ab->mz, ha_flt_tab, k_flag, dbg_ct);
    // minimizer of queried read
    if (ab->mz.m > ab->old_mz_m) {
        ab->old_mz_m = ab->mz.m;
        REALLOC(ab->seed, ab->old_mz_m);
    }
    for (i = 0, ab->n_a = 0; i < ab->mz.n; ++i) {
        int n;
        ab->seed[i].a = ha_pt_get(ha_idx, ab->mz.a[i].x, &n);
        ab->seed[i].n = n;
        ab->n_a += n;
    }
    if (ab->n_a > ab->m_a) {
        ab->m_a = ab->n_a;
        kroundup64(ab->m_a);
        REALLOC(ab->a, ab->m_a);
    }
    for (i = 0, k = 0; i < ab->mz.n; ++i) {
        int j;
        ///z is one of the minimizer
        ha_mz1_t *z = &ab->mz.a[i];
        seed1_t *s = &ab->seed[i];
        for (j = 0; j < s->n; ++j) {
            const ha_idxpos_t *y = &s->a[j];
            anchor1_t *an = &ab->a[k++];
            uint8_t rev = z->rev == y->rev? 0 : 1;
            an->other_off = y->pos;
            an->self_off = rev? u->len - 1 - (z->pos + 1 - z->span) : z->pos;
            an->cnt = 1;
            an->srt = (uint64_t)y->rid<<33 | (uint64_t)rev<<32 | an->other_off;
        }
    }

    // sort anchors
    radix_sort_ha_an1(ab->a, ab->a + ab->n_a);
    for (k = 1, l = 0; k <= ab->n_a; ++k) {
        if (k == ab->n_a || ab->a[k].srt != ab->a[l].srt) {
            if (k - l > 1)
                radix_sort_ha_an2(ab->a + l, ab->a + k);
            l = k;
        }
    }


    // copy over to _cl_
    if (ab->m_a >= (uint64_t)cl->size) {
        cl->size = ab->m_a;
        REALLOC(cl->list, cl->size);
    }
    for (k = 0; k < ab->n_a; ++k) {
        k_mer_hit *p = &cl->list[k];
        p->readID = ab->a[k].srt >> 33;
        p->strand = ab->a[k].srt >> 32 & 1;
        p->offset = ab->a[k].other_off;
        p->self_offset = ab->a[k].self_off;
        p->cnt = 1;
    }
    cl->length = ab->n_a;

    calculate_ug_chaining(cl, overlap_list, chain_idx, rid, ua, bw_thres, keep_whole_chain, f_cigar, ab->mz.n, chain_match_rate);

    #if 0
    if (overlap_list->length > 0) {
        fprintf(stderr, "B\t%ld\t%ld\t%d\n", (long)rid, (long)overlap_list->length, rlen);
        for (int i = 0; i < (int)overlap_list->length; ++i) {
            overlap_region *r = &overlap_list->list[i];
            fprintf(stderr, "C\t%d\t%d\t%d\t%c\t%d\t%ld\t%d\t%d\t%c\t%d\t%d\n", (int)r->x_id, (int)r->x_pos_s, (int)r->x_pos_e, "+-"[r->x_pos_strand],
                    (int)r->y_id, (long)Get_READ_LENGTH(R_INF, r->y_id), (int)r->y_pos_s, (int)r->y_pos_e, "+-"[r->y_pos_strand], (int)r->shared_seed, ha_ov_type(r, rlen));
        }
    }
    #endif

    if ((int)overlap_list->length > max_n_chain) {
        int32_t w, n[4], s[4];
        n[0] = n[1] = n[2] = n[3] = 0, s[0] = s[1] = s[2] = s[3] = 0;
        ks_introsort_or_ss(overlap_list->length, overlap_list->list);
        for (i = 0; i < (uint32_t)overlap_list->length; ++i) {
            const overlap_region *r = &overlap_list->list[i];
            w = ha_ov_type(r, u->len);
            ++n[w];
            if ((int)n[w] == max_n_chain) s[w] = r->shared_seed;
        }
        if (s[0] > 0 || s[1] > 0 || s[2] > 0 || s[3] > 0) {
            for (i = 0, k = 0; i < (uint32_t)overlap_list->length; ++i) {
                overlap_region *r = &overlap_list->list[i];
                w = ha_ov_type(r, u->len);
                if (r->shared_seed >= s[w]) {
                    if ((uint32_t)k != i) {
                        overlap_region t;
                        t = overlap_list->list[k];
                        overlap_list->list[k] = overlap_list->list[i];
                        overlap_list->list[i] = t;
                    }
                    ++k;
                }
            }
            overlap_list->length = k;
        }
    }

    ///ks_introsort_or_xs(overlap_list->length, overlap_list->list);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %xmm1, 0x40(%rsp)
movq %xmm0, 0x38(%rsp)
movq %r8, %rbx
movq %rcx, 0x30(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %rbp
movq %r9, 0x28(%rsp)
movq %r9, %rdi
callq 0x13d24
movq %rbx, (%rsp)
movq %rbx, %rdi
callq 0x11935
andl $0x0, 0x18(%rbp)
andq $0x0, (%rbp)
movl 0x1c(%rbp), %esi
cmpl 0x10(%rbp), %esi
jbe 0xe4ecf
movl %esi, 0x10(%rbp)
movq 0x28(%rbp), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%rbp)
movq 0x178(%rsp), %r15
andq $0x0, (%rbp)
xorl %ebx, %ebx
leaq 0x70(%rsp), %r13
xorl %ecx, %ecx
xorl %r12d, %r12d
movl 0x18(%rbp), %eax
cmpq %rax, %r12
jae 0xe4f25
movq 0x20(%rbp), %rax
movq (%rax,%rbx), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0xd4de2
movq 0x28(%rbp), %rdx
movq %rax, 0x8(%rdx,%rbx)
movslq 0x70(%rsp), %rcx
movl %ecx, (%rdx,%rbx)
addq (%rbp), %rcx
movq %rcx, (%rbp)
incq %r12
addq $0x10, %rbx
jmp 0xe4ee8
cmpq 0x8(%rbp), %rcx
jbe 0xe4f84
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x20, %rcx
orq %rax, %rcx
incq %rcx
movq %rcx, 0x8(%rbp)
movq 0x30(%rbp), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x30(%rbp)
movl 0x18(%rbp), %eax
movabsq $0x100000000, %rdx      # imm = 0x100000000
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %r8d, %r8d
xorl %edi, %edi
movq %rax, 0x48(%rsp)
cmpq %rax, %r8
je 0xe50a7
movq 0x20(%rbp), %rcx
movq 0x28(%rbp), %r9
movq %r8, 0x58(%rsp)
shlq $0x4, %r8
movl (%r9,%r8), %r10d
addq %r8, %r9
leaq (%rcx,%r8), %r11
addq $0x8, %r11
testl %r10d, %r10d
movl $0x0, %eax
cmovlel %eax, %r10d
movq %rdi, 0x50(%rsp)
imulq $0x18, %rdi, %r15
xorl %ebx, %ebx
cmpq %rbx, %r10
je 0xe508d
movq 0x8(%r9), %r8
movq 0x30(%rbp), %r13
movq (%r8,%rbx,8), %r12
movq (%r11), %rcx
xorq %r12, %rcx
shrq $0x1c, %r12
movl %r12d, %r14d
andl $0x7ffffff, %r14d       # imm = 0x7FFFFFF
movl %r14d, 0xc(%r13,%r15)
btq $0x37, %rcx
jb 0xe5022
movq (%r11), %r14
shrq $0x1c, %r14
andl $0x7ffffff, %r14d       # imm = 0x7FFFFFF
jmp 0xe504d
movq 0x8(%rsp), %rax
movl (%rax), %r14d
andl %esi, %r14d
movq (%r11), %rax
movq %rax, %rdi
shrq $0x1c, %rdi
notl %edi
orl $0xf8000000, %edi       # imm = 0xF8000000
shrq $0x38, %rax
addl %r14d, %eax
leal (%rdi,%rax), %r14d
decl %r14d
movl %r14d, 0x8(%r13,%r15)
movl $0x1, 0x10(%r13,%r15)
movq (%r8,%rbx,8), %rax
andl $0xfffffff, %eax        # imm = 0xFFFFFFF
shlq $0x21, %rax
shrq $0x17, %rcx
andq %rdx, %rcx
andl $0x7ffffff, %r12d       # imm = 0x7FFFFFF
orq %rcx, %r12
orq %rax, %r12
movq %r12, (%r13,%r15)
incq %rbx
addq $0x18, %r15
jmp 0xe4fdd
movq 0x58(%rsp), %r8
incq %r8
movq 0x50(%rsp), %rdi
addq %rbx, %rdi
movq 0x48(%rsp), %rax
jmp 0xe4f9d
movq 0x30(%rbp), %rdi
imulq $0x18, (%rbp), %rsi
addq %rdi, %rsi
callq 0xe1068
pushq $0x1
popq %rbx
pushq $0x18
popq %r15
xorl %eax, %eax
cmpq (%rbp), %rbx
ja 0xe5111
je 0xe50e0
movq 0x30(%rbp), %rcx
movq (%rcx,%r15), %rdx
imulq $0x18, %rax, %rsi
cmpq (%rcx,%rsi), %rdx
jne 0xe50e0
movq %rax, %rcx
jmp 0xe5105
movq %rbx, %rdx
subq %rax, %rdx
movq %rbx, %rcx
cmpq $0x2, %rdx
jb 0xe5105
movq 0x30(%rbp), %rsi
imulq $0x18, %rax, %rdi
addq %rsi, %rdi
addq %r15, %rsi
callq 0xe1300
movq %rbx, %rcx
incq %rbx
addq $0x18, %r15
movq %rcx, %rax
jmp 0xe50c1
movq 0x8(%rbp), %rsi
movq 0x28(%rsp), %r14
cmpq 0x10(%r14), %rsi
jb 0xe5133
movq %rsi, 0x10(%r14)
movq (%r14), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%r14)
xorl %eax, %eax
movl $0x80000000, %ecx       # imm = 0x80000000
xorl %edx, %edx
xorl %esi, %esi
movq (%rsp), %rbx
movq (%rbp), %rdi
cmpq %rdi, %rsi
jae 0xe51a8
movq (%r14), %rdi
movq 0x30(%rbp), %r8
movq (%r8,%rdx), %r8
shrq $0x21, %r8
movl (%rdi,%rax), %r9d
andl %ecx, %r9d
orl %r8d, %r9d
movl %r9d, (%rdi,%rax)
movq 0x30(%rbp), %r9
movq (%r9,%rdx), %r9
shrq %r9
andl %ecx, %r9d
orl %r8d, %r9d
movl %r9d, (%rdi,%rax)
movq 0x30(%rbp), %r8
movq 0x8(%r8,%rdx), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4(%rdi,%rax)
movl $0x1, 0xc(%rdi,%rax)
incq %rsi
addq $0x18, %rdx
addq $0x10, %rax
jmp 0xe5142
movq %rdi, 0x8(%r14)
movl 0x18(%rbp), %eax
movq %r14, %rdi
movq %rbx, %rsi
movq 0x168(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %r8
movsd 0x38(%rsp), %xmm0
movl 0x158(%rsp), %r9d
movq 0x40(%rsp), %xmm1
pushq %rax
pushq 0x188(%rsp)
callq 0xe472b
popq %rax
popq %rcx
movq 0x10(%rbx), %rdi
cmpl 0x150(%rsp), %edi
jle 0xe532c
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x10(%rsp)
movq (%rbx), %rsi
callq 0xe248f
movl 0x10(%rbx), %r14d
imulq $0xa8, %r14, %rbx
xorl %r15d, %r15d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
cmpq %r15, %rbx
je 0xe5268
movq (%rsp), %rax
movq (%rax), %r12
leaq (%r12,%r15), %rdi
movq 0x8(%rsp), %rax
movl (%rax), %esi
andl %ebp, %esi
callq 0xe3aee
movl %eax, %eax
movl 0x60(%rsp,%rax,4), %ecx
incl %ecx
movl %ecx, 0x60(%rsp,%rax,4)
cmpl 0x150(%rsp), %ecx
jne 0xe525f
movl 0x24(%r12,%r15), %ecx
movl %ecx, 0x10(%rsp,%rax,4)
addq $0xa8, %r15
jmp 0xe5223
movdqa 0x10(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %eax
testl %eax, %eax
je 0xe532c
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %ebx, %ebx
movl %r14d, %eax
cmpq %rax, %rbp
jae 0xe5324
movq (%rsp), %rax
movq (%rax), %r13
leaq (%r15,%r13), %r12
movq 0x8(%rsp), %rax
movl (%rax), %esi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %esi
movq %r12, %rdi
callq 0xe3aee
movl 0x24(%r13,%r15), %ecx
movl %eax, %eax
cmpl 0x10(%rsp,%rax,4), %ecx
jl 0xe5315
movl %ebx, %eax
cmpq %rax, %rbp
je 0xe5312
imulq $0xa8, %rbx, %rax
addq %rax, %r13
movl $0xa8, %edx
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2160
movq (%rsp), %r14
movq (%r14), %rdi
addq %r15, %rdi
movl $0xa8, %edx
leaq 0x70(%rsp), %rsi
callq 0x2160
movl 0x10(%r14), %r14d
incq %rbx
incq %rbp
addq $0xa8, %r15
jmp 0xe5288
movq (%rsp), %rax
movq %rbx, 0x10(%rax)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq