// File: /workspace/github_cpp_makefile_star2_O3/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 $0x128, %rsp            # imm = 0x128
movsd %xmm1, 0x48(%rsp)
movq %xmm0, 0x40(%rsp)
movq %r9, %r12
movq %r8, %rbx
movq %rcx, 0x38(%rsp)
movq %rdx, %r14
movq %rsi, 0x30(%rsp)
movq %rdi, %rbp
movq %r9, %rdi
callq 0x3f1bd
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
callq 0x3d018
movl $0x0, 0x18(%rbp)
movq $0x0, (%rbp)
movl 0x1c(%rbp), %esi
cmpl 0x10(%rbp), %esi
jbe 0x12a191
movl %esi, 0x10(%rbp)
movq 0x28(%rbp), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%rbp)
cmpl $0x0, 0x18(%rbp)
movq $0x0, (%rbp)
je 0x12a191
movq %r12, 0x18(%rsp)
movq 0x188(%rsp), %r15
xorl %ebx, %ebx
leaq 0x80(%rsp), %r13
xorl %r12d, %r12d
movq 0x20(%rbp), %rax
movq (%rax,%rbx), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x1194bb
movq 0x28(%rbp), %rcx
movq %rax, 0x8(%rcx,%rbx)
movslq 0x80(%rsp), %rax
movl %eax, (%rcx,%rbx)
addq (%rbp), %rax
movq %rax, (%rbp)
incq %r12
movl 0x18(%rbp), %edx
addq $0x10, %rbx
cmpq %rdx, %r12
jb 0x129fab
cmpq 0x8(%rbp), %rax
jbe 0x12a051
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
movq %rcx, %rax
shrq $0x20, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rbp)
movq 0x30(%rbp), %rdi
leaq 0x8(,%rax,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x30(%rbp)
movl 0x18(%rbp), %edx
leaq 0x8(%rbp), %rax
movq %rax, 0x10(%rsp)
testl %edx, %edx
movq 0x18(%rsp), %r12
je 0x12a19a
movq 0x20(%rbp), %rax
movq %rax, 0x58(%rsp)
movq 0x28(%rbp), %rcx
movl %edx, %eax
movq %rax, 0x60(%rsp)
xorl %esi, %esi
movabsq $0x100000000, %rdi      # imm = 0x100000000
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %r9d, %r9d
movq %rcx, 0x50(%rsp)
movq %rsi, %rax
shlq $0x4, %rax
movslq (%rcx,%rax), %r10
testq %r10, %r10
jle 0x12a181
leaq (%rcx,%rax), %rdx
movq 0x8(%rdx), %r11
movq 0x30(%rbp), %rdx
movq 0x58(%rsp), %rcx
leaq (%rcx,%rax), %rbx
addq $0x8, %rbx
movq %r9, 0x68(%rsp)
leaq (%r9,%r9,2), %rax
leaq (%rdx,%rax,8), %r13
addq $0x10, %r13
xorl %r15d, %r15d
movq (%r11,%r15,8), %rax
movq (%rbx), %rdx
xorq %rax, %rdx
shrq $0x1c, %rax
movl %eax, %r12d
andl $0x7ffffff, %r12d       # imm = 0x7FFFFFF
movl %r12d, -0x4(%r13)
btq $0x37, %rdx
jb 0x12a109
movq (%rbx), %r12
shrq $0x1c, %r12
andl $0x7ffffff, %r12d       # imm = 0x7FFFFFF
jmp 0x12a12f
movl (%r14), %r12d
andl %r8d, %r12d
movq (%rbx), %r9
movq %r9, %rcx
shrq $0x1c, %rcx
notl %ecx
orl $0xf8000000, %ecx       # imm = 0xF8000000
shrq $0x38, %r9
addl %r12d, %r9d
leal (%rcx,%r9), %r12d
decl %r12d
movl %r12d, -0x8(%r13)
movl $0x1, (%r13)
movq (%r11,%r15,8), %rcx
andl $0xfffffff, %ecx        # imm = 0xFFFFFFF
shlq $0x21, %rcx
shrq $0x17, %rdx
andq %rdi, %rdx
andl $0x7ffffff, %eax        # imm = 0x7FFFFFF
orq %rdx, %rax
orq %rcx, %rax
movq %rax, -0x10(%r13)
incq %r15
addq $0x18, %r13
cmpq %r15, %r10
jne 0x12a0d6
movq 0x68(%rsp), %r9
addq %r15, %r9
movq 0x18(%rsp), %r12
movq 0x50(%rsp), %rcx
incq %rsi
cmpq 0x60(%rsp), %rsi
jne 0x12a095
jmp 0x12a19a
leaq 0x8(%rbp), %rax
movq %rax, 0x10(%rsp)
movq (%rbp), %rax
movq 0x30(%rbp), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x125ce1
movq (%rbp), %rcx
testq %rcx, %rcx
je 0x12a21c
movl $0x1, %ebx
movl $0x18, %r15d
xorl %eax, %eax
cmpq %rcx, %rbx
je 0x12a1e1
movq 0x30(%rbp), %rdx
movq (%rdx,%r15), %rsi
leaq (%rax,%rax,2), %rdi
cmpq (%rdx,%rdi,8), %rsi
jne 0x12a1e1
movq %rax, %rdx
jmp 0x12a20b
movq %rbx, %rsi
subq %rax, %rsi
movq %rbx, %rdx
cmpq $0x2, %rsi
jb 0x12a20b
movq 0x30(%rbp), %rsi
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rdi
addq %r15, %rsi
callq 0x125f7f
movq (%rbp), %rcx
movq %rbx, %rdx
incq %rbx
addq $0x18, %r15
movq %rdx, %rax
cmpq %rcx, %rbx
jbe 0x12a1c5
jmp 0x12a21e
xorl %ecx, %ecx
movq 0x10(%rsp), %rax
movq (%rax), %rsi
cmpq 0x10(%r12), %rsi
jb 0x12a247
movq %rsi, 0x10(%r12)
movq (%r12), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%r12)
movq (%rbp), %rcx
movq 0x190(%rsp), %rax
movq 0x178(%rsp), %rdx
movl 0x168(%rsp), %r9d
movl 0x160(%rsp), %r15d
testq %rcx, %rcx
je 0x12a2df
movq 0x30(%rbp), %r10
xorl %ecx, %ecx
movl $0x80000000, %esi       # imm = 0x80000000
xorl %edi, %edi
xorl %r8d, %r8d
movq (%r12), %r11
movq (%r10,%rdi), %r10
shrq $0x21, %r10
movl (%r11,%rcx), %ebx
andl %esi, %ebx
orl %r10d, %ebx
movl %ebx, (%r11,%rcx)
movq 0x30(%rbp), %rbx
movq (%rbx,%rdi), %rbx
shrq %rbx
andl %esi, %ebx
orl %r10d, %ebx
movl %ebx, (%r11,%rcx)
movq 0x30(%rbp), %r10
movq 0x8(%r10,%rdi), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4(%r11,%rcx)
movl $0x1, 0xc(%r11,%rcx)
incq %r8
movq (%rbp), %r11
addq $0x18, %rdi
addq $0x10, %rcx
cmpq %r11, %r8
jb 0x12a27c
jmp 0x12a2e2
xorl %r11d, %r11d
movq %r11, 0x8(%r12)
movl 0x18(%rbp), %r10d
movq %r12, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r8
movsd 0x40(%rsp), %xmm0
movq 0x48(%rsp), %xmm1
pushq %r10
pushq %rax
callq 0x129768
addq $0x10, %rsp
movq 0x10(%rbx), %rdi
cmpl %r15d, %edi
jle 0x12a4bf
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x20(%rsp)
movq (%rbx), %rsi
callq 0x127252
movq 0x8(%rsp), %r11
movl 0x10(%r11), %eax
testq %rax, %rax
je 0x12a4bf
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl (%r14), %ecx
movq (%r11), %rdx
decl %ecx
imulq $0xa8, %rax, %rsi
xorl %edi, %edi
movl 0x4(%rdx,%rdi), %r9d
movl 0x8(%rdx,%rdi), %r10d
testl %r9d, %r9d
je 0x12a3b0
movl $0x3, %r8d
cmpl %ecx, %r10d
jb 0x12a386
xorl %r8d, %r8d
testl %r9d, %r9d
setne %r8b
movl 0x70(%rsp,%r8,4), %r9d
incl %r9d
movl %r9d, 0x70(%rsp,%r8,4)
cmpl %r15d, %r9d
jne 0x12a3a2
movl 0x24(%rdx,%rdi), %r9d
movl %r9d, 0x20(%rsp,%r8,4)
addq $0xa8, %rdi
cmpq %rdi, %rsi
jne 0x12a362
jmp 0x12a3bd
movl $0x2, %r8d
cmpl %ecx, %r10d
jne 0x12a37c
jmp 0x12a386
movdqa 0x20(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %ecx
testl %ecx, %ecx
je 0x12a4bf
testq %rax, %rax
je 0x12a4b8
xorl %ebp, %ebp
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %ebx, %ebx
xorl %r15d, %r15d
movq (%r11), %rcx
movl (%r14), %edi
andl %r9d, %edi
movl 0x4(%rcx,%rbp), %edx
movl 0x8(%rcx,%rbp), %r8d
decl %edi
testl %edx, %edx
je 0x12a4a5
movl $0x3, %esi
cmpl %edi, %r8d
jb 0x12a41a
xorl %esi, %esi
testl %edx, %edx
setne %sil
movl 0x24(%rcx,%rbp), %edx
cmpl 0x20(%rsp,%rsi,4), %edx
jl 0x12a48e
movl %r15d, %edx
cmpq %rdx, %rbx
je 0x12a48b
leaq (%rcx,%rbp), %r12
imulq $0xa8, %r15, %r13
addq %rcx, %r13
movl $0xa8, %edx
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
movq (%rax), %rdi
addq %rbp, %rdi
movl $0xa8, %edx
leaq 0x80(%rsp), %rsi
callq 0x2160
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r11
movl 0x10(%r11), %eax
incq %r15
incq %rbx
movl %eax, %ecx
addq $0xa8, %rbp
cmpq %rcx, %rbx
jb 0x12a3ec
jmp 0x12a4bb
movl $0x2, %esi
cmpl %edi, %r8d
jne 0x12a412
jmp 0x12a41a
xorl %r15d, %r15d
movq %r15, 0x10(%r11)
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq