// File: /workspace/github_cpp_makefile_star2_O1/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
movsd %xmm1, 0x38(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %r8, %rbx
movq %rcx, 0x28(%rsp)
movq %rdx, %r14
movq %rsi, 0x20(%rsp)
movq %rdi, %rbp
movq %r9, 0x18(%rsp)
movq %r9, %rdi
callq 0x3fd1c
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x3db37
movl $0x0, 0x18(%rbp)
movq $0x0, (%rbp)
movl 0x1c(%rbp), %esi
cmpl 0x10(%rbp), %esi
jbe 0x124070
movl %esi, 0x10(%rbp)
movq 0x28(%rbp), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%rbp)
movq $0x0, (%rbp)
cmpl $0x0, 0x18(%rbp)
je 0x1240cb
movq 0x178(%rsp), %r15
xorl %ebx, %ebx
leaq 0x70(%rsp), %r13
xorl %r12d, %r12d
movq 0x20(%rbp), %rax
movq (%rax,%rbx), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x1139ee
movq 0x28(%rbp), %rcx
movq %rax, 0x8(%rcx,%rbx)
movl 0x70(%rsp), %eax
movl %eax, (%rcx,%rbx)
movslq 0x70(%rsp), %rax
addq %rax, (%rbp)
incq %r12
movl 0x18(%rbp), %eax
addq $0x10, %rbx
cmpq %rax, %r12
jb 0x124090
movq (%rbp), %rax
cmpq 0x8(%rbp), %rax
jbe 0x124134
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), %eax
movq %rax, 0x50(%rsp)
testq %rax, %rax
movq 0x18(%rsp), %r13
je 0x124262
movq 0x20(%rbp), %rax
movq %rax, 0x48(%rsp)
movq 0x28(%rbp), %rcx
xorl %esi, %esi
movabsq $0x100000000, %rdi      # imm = 0x100000000
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %r9d, %r9d
movq %rcx, 0x40(%rsp)
movq %rsi, %rax
shlq $0x4, %rax
movslq (%rcx,%rax), %r10
testq %r10, %r10
jle 0x124254
addq %rax, %rcx
movq 0x8(%rcx), %r11
movq 0x30(%rbp), %rcx
movq 0x48(%rsp), %rdx
leaq 0x8(%rdx,%rax), %rbx
movq %r9, 0x58(%rsp)
leaq (%r9,%r9,2), %rax
leaq (%rcx,%rax,8), %r15
addq $0x10, %r15
xorl %r13d, %r13d
movq (%r11,%r13,8), %r12
movq (%rbx), %rcx
xorq %r12, %rcx
shrq $0x1c, %r12
movl %r12d, %eax
andl $0x7ffffff, %eax        # imm = 0x7FFFFFF
movl %eax, -0x4(%r15)
btq $0x37, %rcx
jb 0x1241dd
movq (%rbx), %rax
shrq $0x1c, %rax
andl $0x7ffffff, %eax        # imm = 0x7FFFFFF
jmp 0x124202
movl (%r14), %eax
andl %r8d, %eax
movq (%rbx), %r9
movq %r9, %rdx
shrq $0x1c, %rdx
notl %edx
orl $0xf8000000, %edx       # imm = 0xF8000000
shrq $0x38, %r9
addl %eax, %r9d
leal (%rdx,%r9), %eax
decl %eax
movl %eax, -0x8(%r15)
movl $0x1, (%r15)
movq (%r11,%r13,8), %rax
andl $0xfffffff, %eax        # imm = 0xFFFFFFF
shlq $0x21, %rax
shrq $0x17, %rcx
andq %rdi, %rcx
andl $0x7ffffff, %r12d       # imm = 0x7FFFFFF
orq %rcx, %r12
orq %rax, %r12
movq %r12, -0x10(%r15)
incq %r13
addq $0x18, %r15
cmpq %r13, %r10
jne 0x1241ae
movq 0x58(%rsp), %r9
addq %r13, %r9
movq 0x18(%rsp), %r13
movq 0x40(%rsp), %rcx
incq %rsi
cmpq 0x50(%rsp), %rsi
jne 0x124171
movq (%rbp), %rax
movq 0x30(%rbp), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x11fe18
movq (%rbp), %rcx
testq %rcx, %rcx
movq 0x10(%rsp), %r12
je 0x1242e7
movl $0x1, %ebx
movl $0x18, %r15d
xorl %eax, %eax
cmpq %rcx, %rbx
je 0x1242ae
movq 0x30(%rbp), %rcx
movq (%rcx,%r15), %rdx
leaq (%rax,%rax,2), %rsi
cmpq (%rcx,%rsi,8), %rdx
jne 0x1242ae
movq %rax, %rdx
jmp 0x1242d4
movq %rbx, %rcx
subq %rax, %rcx
movq %rbx, %rdx
cmpq $0x2, %rcx
jb 0x1242d4
movq 0x30(%rbp), %rsi
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rdi
addq %r15, %rsi
callq 0x1200ba
movq %rbx, %rdx
incq %rbx
movq (%rbp), %rcx
addq $0x18, %r15
movq %rdx, %rax
cmpq %rcx, %rbx
jbe 0x124292
movq 0x8(%rbp), %rsi
cmpq 0x10(%r13), %rsi
jb 0x124306
movq %rsi, 0x10(%r13)
movq (%r13), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%r13)
movq 0x180(%rsp), %rax
movq 0x168(%rsp), %rdx
movl 0x158(%rsp), %r9d
movl 0x150(%rsp), %r15d
movq (%rbp), %r10
testq %r10, %r10
je 0x1243a1
xorl %ecx, %ecx
movl $0x80000000, %esi       # imm = 0x80000000
xorl %edi, %edi
xorl %r8d, %r8d
movq (%r13), %r10
movq 0x30(%rbp), %r11
movq (%r11,%rdi), %r11
shrq $0x21, %r11
movl (%r10,%rcx), %ebx
andl %esi, %ebx
orl %r11d, %ebx
movl %ebx, (%r10,%rcx)
movq 0x30(%rbp), %rbx
movq (%rbx,%rdi), %rbx
shrq %rbx
andl %esi, %ebx
orl %r11d, %ebx
movl %ebx, (%r10,%rcx)
movq 0x30(%rbp), %r11
movl 0xc(%r11,%rdi), %ebx
movl %ebx, 0x4(%r10,%rcx)
movl 0x8(%r11,%rdi), %r11d
movl %r11d, 0x8(%r10,%rcx)
movl $0x1, 0xc(%r10,%rcx)
incq %r8
movq (%rbp), %r10
addq $0x18, %rdi
addq $0x10, %rcx
cmpq %r10, %r8
jb 0x12433b
movq %r10, 0x8(%r13)
movl 0x18(%rbp), %r10d
movq %r13, %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %r8
movsd 0x30(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
pushq %r10
pushq %rax
callq 0x123864
addq $0x10, %rsp
movq 0x10(%r12), %rdi
cmpl %r15d, %edi
jle 0x124569
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, (%rsp)
movq (%r12), %rsi
callq 0x121388
movl 0x10(%r12), %edx
testq %rdx, %rdx
je 0x12446c
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl (%r14), %eax
movq (%r12), %rcx
decl %eax
imulq $0xa8, %rdx, %rdx
xorl %esi, %esi
movl 0x4(%rcx,%rsi), %r8d
movl 0x8(%rcx,%rsi), %r9d
testl %r8d, %r8d
je 0x124460
movl $0x3, %edi
cmpl %eax, %r9d
jb 0x124437
xorl %edi, %edi
testl %r8d, %r8d
setne %dil
movl 0x60(%rsp,%rdi,4), %r8d
incl %r8d
movl %r8d, 0x60(%rsp,%rdi,4)
cmpl %r15d, %r8d
jne 0x124452
movl 0x24(%rcx,%rsi), %r8d
movl %r8d, (%rsp,%rdi,4)
addq $0xa8, %rsi
cmpq %rsi, %rdx
jne 0x124415
jmp 0x12446c
movl $0x2, %edi
cmpl %eax, %r9d
jne 0x12442e
jmp 0x124437
cmpl $0x0, (%rsp)
jg 0x12448b
cmpl $0x0, 0x4(%rsp)
jg 0x12448b
cmpl $0x0, 0x8(%rsp)
jg 0x12448b
cmpl $0x0, 0xc(%rsp)
jle 0x124569
cmpl $0x0, 0x10(%r12)
je 0x124561
xorl %ebp, %ebp
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %ebx, %ebx
xorl %r15d, %r15d
movq (%r12), %rax
movl (%r14), %esi
andl %r8d, %esi
movl 0x4(%rax,%rbp), %ecx
movl 0x8(%rax,%rbp), %edi
decl %esi
testl %ecx, %ecx
je 0x12454f
movl $0x3, %edx
cmpl %esi, %edi
jb 0x1244d0
xorl %edx, %edx
testl %ecx, %ecx
setne %dl
movl 0x24(%rax,%rbp), %ecx
cmpl (%rsp,%rdx,4), %ecx
jl 0x124535
movl %r15d, %ecx
cmpq %rcx, %rbx
je 0x124532
leaq (%rax,%rbp), %r12
imulq $0xa8, %r15, %r13
addq %rax, %r13
movl $0xa8, %edx
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r13, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %r12
callq 0x2160
movq (%r12), %rdi
addq %rbp, %rdi
movl $0xa8, %edx
leaq 0x70(%rsp), %rsi
callq 0x2160
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
incq %r15
incq %rbx
movl 0x10(%r12), %eax
addq $0xa8, %rbp
cmpq %rax, %rbx
jb 0x1244a4
jmp 0x124564
movl $0x2, %edx
cmpl %esi, %edi
jne 0x1244c9
jmp 0x1244d0
xorl %r15d, %r15d
movq %r15, 0x10(%r12)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq