// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/tovlp.cpp
// Function: pt_collect_anchors(ma_ug_t*, pt_mz1_v*, mz_ds_t*, unsigned int)
pt128_v *pt_collect_anchors(ma_ug_t *ug, pt_mz1_v *mz, mz_ds_t *mz_idx, uint32_t max_occ)
{
	uint32_t st, j;
    pt128_v *pa = NULL; CALLOC(pa, 1);
    pt128_t *p = NULL;
    mz_idx->pos.n = 0;
	///all minimizers
	for (j = 1, st = 0; j <= mz->n; ++j) {
		if (j == mz->n || mz->a[j].x != mz->a[st].x) {
			uint32_t k, l;
			// if (j - st == 1) ++info[mz->a[st].rid].cnt1; ///of size n_seg
			///max_occ is the frquency threshold of minimizer
			///if (j - st) == 1, means minimizer only occurs in one read, it is not useful
			if (j - st == 1 || j - st > max_occ) goto end_anchor;
			for (k = st; k < j; ++k) {
				// ++info[mz->a[k].rid].cnt2;
                kv_push(uint64_t, mz_idx->pos, (uint64_t)(mz->a[k].rid)<<32|(uint64_t)(mz->a[k].pos));
				for (l = k + 1; l < j; ++l) {
					///k is current minimizer
					uint32_t span, rev = (mz->a[k].rev != mz->a[l].rev);
					int32_t lk = ug->u.a[mz->a[k].rid].len, ll = ug->u.a[mz->a[l].rid].len;
                    kv_pushp(pt128_t, *pa, &p);
					span = mz->a[l].span;
                    p->x = (uint64_t)mz->a[k].rid << 33 | mz->a[l].rid << 1 | rev;
					p->y = (uint64_t)mz->a[k].pos << 32 | (rev? ll - (mz->a[l].pos + 1 - span) - 1 : mz->a[l].pos);
					kv_pushp(pt128_t, *pa, &p);
                    span = mz->a[k].span;
					p->x = (uint64_t)mz->a[l].rid << 33 | mz->a[k].rid << 1 | rev;
					p->y = (uint64_t)mz->a[l].pos << 32 | (rev? lk - (mz->a[k].pos + 1 - span) - 1 : mz->a[k].pos);
				}
			}
            end_anchor:	st = j;
		}
	}

	radix_sort_pt128x(pa->a, pa->a + pa->n);

    radix_sort_tb64(mz_idx->pos.a, mz_idx->pos.a + mz_idx->pos.n);
    CALLOC(mz_idx->idx, ug->u.n);
    for (st = 0, j = 1; j <= mz_idx->pos.n; ++j)
    {
        if (j == mz_idx->pos.n || (mz_idx->pos.a[j]>>32) != (mz_idx->pos.a[st]>>32))
        {
            mz_idx->idx[mz_idx->pos.a[st]>>32] = (uint64_t)st << 32 | (j - st), st = j;
        }
    }
	
    return pa;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, 0x2c(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, 0x30(%rsp)
movl $0x1, %edi
movl $0x10, %esi
callq 0x2120
movq %rax, %r14
movq $0x0, 0x8(%rbx)
movq %r15, 0x10(%rsp)
movl (%r15), %ecx
testl %ecx, %ecx
je 0x1935dd
movl $0x1, %esi
xorl %eax, %eax
movabsq $0x7fffffffffffffff, %r10 # imm = 0x7FFFFFFFFFFFFFFF
movq %rbx, 0x48(%rsp)
movq %r14, 0x40(%rsp)
cmpl %ecx, %esi
movl %esi, 0xc(%rsp)
je 0x1932fd
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rcx
movl %esi, %edx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx,8), %rdx
movl %eax, %esi
leaq (%rsi,%rsi,2), %rsi
cmpq (%rcx,%rsi,8), %rdx
movl 0xc(%rsp), %esi
jne 0x1932fd
movl %eax, %edx
jmp 0x1935ca
movl %esi, %ecx
subl %eax, %ecx
seta %dl
cmpl $0x1, %ecx
setne %sil
cmpl 0x2c(%rsp), %ecx
setbe %cl
andb %dl, %sil
andb %cl, %sil
movl 0xc(%rsp), %edx
cmpb $0x1, %sil
movl 0xc(%rsp), %esi
jne 0x1935ca
movl %eax, %r15d
movl %esi, %eax
movq %rax, 0x18(%rsp)
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rdi
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x19337f
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%rbx)
movq %rdi, %r12
movq 0x18(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %r12, %rdi
movabsq $0x7fffffffffffffff, %r10 # imm = 0x7FFFFFFFFFFFFFFF
movq %rax, 0x18(%rbx)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
leaq (%r15,%r15,2), %r11
incq %r15
movq 0x8(%rax,%r11,8), %rcx
shlq $0x20, %rcx
movq 0x10(%rax,%r11,8), %rax
andq %r10, %rax
orq %rcx, %rax
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movq %rax, (%rdx,%rcx,8)
cmpq 0x18(%rsp), %r15
jae 0x1935b5
movl (%r14), %r13d
movl 0x4(%r14), %ecx
movq 0x8(%r14), %rax
movq %rdi, 0x50(%rsp)
movq %rdi, %rbx
movq %r15, 0x58(%rsp)
movq 0x10(%rsp), %r14
movq %r11, 0x38(%rsp)
movl %r13d, %r8d
movq 0x8(%r14), %rdx
movq 0x28(%rdx,%rbx), %rbp
xorq 0x10(%rdx,%r11,8), %rbp
movq 0x30(%rsp), %rsi
movq 0x10(%rsi), %rsi
movl 0x8(%rdx,%r11,8), %edi
leaq (%rdi,%rdi,4), %rdi
movl (%rsi,%rdi,8), %edi
movq %rdi, 0x60(%rsp)
movl 0x20(%rdx,%rbx), %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rsi,%rdx,8), %r13d
movq %r8, 0x20(%rsp)
cmpl %ecx, %r8d
jne 0x193455
leal (%rcx,%rcx), %r12d
testl %ecx, %ecx
movl $0x2, %ecx
cmovel %ecx, %r12d
movq %r12, %rsi
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r11
movabsq $0x7fffffffffffffff, %r10 # imm = 0x7FFFFFFFFFFFFFFF
movl %r12d, %ecx
movq %rbp, %r12
shrq $0x3f, %r12
movl 0x20(%rsp), %edx
shlq $0x4, %rdx
movq 0x8(%r14), %r8
movq 0x20(%r8,%rbx), %rdi
movq 0x8(%r8,%r11,8), %rsi
shlq $0x21, %rsi
leal (%rdi,%rdi), %r9d
orq %rsi, %r9
orq %r12, %r9
movq %r9, (%rax,%rdx)
movq 0x10(%r8,%r11,8), %rsi
testq %rbp, %rbp
js 0x193498
movq 0x28(%r8,%rbx), %rdi
andq %r10, %rdi
jmp 0x1934b9
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andq %r9, %r13
shrq $0x20, %rdi
movq 0x28(%r8,%rbx), %r8
andq %r10, %r8
notq %r8
addq %r13, %rdi
addq %r8, %rdi
decq %rdi
movq 0x20(%rsp), %r8
leal 0x1(%r8), %r13d
addq %rax, %rdx
shlq $0x20, %rsi
orq %rdi, %rsi
movq %rsi, 0x8(%rdx)
cmpl %ecx, %r13d
jne 0x19350a
leal (%rcx,%rcx), %r14d
testl %ecx, %ecx
movl $0x2, %ecx
cmovel %ecx, %r14d
movq %r14, %rsi
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r11
movabsq $0x7fffffffffffffff, %r10 # imm = 0x7FFFFFFFFFFFFFFF
movl %r14d, %ecx
movq 0x10(%rsp), %r14
movl %r13d, %edx
shlq $0x4, %rdx
movq 0x8(%r14), %r8
movq 0x8(%r8,%r11,8), %rdi
movq 0x20(%r8,%rbx), %rsi
shlq $0x21, %rsi
leal (%rdi,%rdi), %r9d
orq %rsi, %r9
orq %r12, %r9
movq %r9, (%rax,%rdx)
movq 0x28(%r8,%rbx), %rsi
testq %rbp, %rbp
js 0x193545
movq 0x10(%r8,%r11,8), %rdi
andq %r10, %rdi
jmp 0x19356b
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq 0x60(%rsp), %r12
andq %r9, %r12
shrq $0x20, %rdi
movq 0x10(%r8,%r11,8), %r8
andq %r10, %r8
notq %r8
addq %r12, %rdi
addq %r8, %rdi
decq %rdi
addq %rax, %rdx
shlq $0x20, %rsi
orq %rdi, %rsi
movq %rsi, 0x8(%rdx)
incq %r15
addq $0x18, %rbx
incl %r13d
cmpq 0x18(%rsp), %r15
jb 0x1933e4
movq 0x20(%rsp), %rdx
addl $0x2, %edx
movq 0x40(%rsp), %r14
movl %edx, (%r14)
movl %ecx, 0x4(%r14)
movq %rax, 0x8(%r14)
movq 0x48(%rsp), %rbx
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %rdi
addq $0x18, %rdi
cmpq 0x18(%rsp), %r15
jne 0x193340
movl 0xc(%rsp), %esi
movl %esi, %edx
incl %esi
movq 0x10(%rsp), %rax
movl (%rax), %ecx
movl %edx, %eax
cmpl %ecx, %esi
jbe 0x1932cb
movq 0x8(%r14), %rdi
movl (%r14), %esi
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0x19249e
movq 0x8(%rbx), %rax
movq 0x18(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1926ca
movq 0x30(%rsp), %rax
movq (%rax), %rdi
movl $0x8, %esi
callq 0x2120
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x19366e
movl $0x1, %edx
xorl %esi, %esi
movl $0x1, %edi
movl %esi, %r8d
cmpq %rdi, %rcx
je 0x193645
movq 0x18(%rbx), %r9
movq (%r9,%r8,8), %r10
xorq (%r9,%rdi,8), %r10
shrq $0x20, %r10
je 0x193664
movq %r8, %rdi
shlq $0x20, %rdi
movl %edx, %r9d
subl %esi, %r9d
orq %rdi, %r9
movq 0x18(%rbx), %rsi
movl 0x4(%rsi,%r8,8), %esi
movq %r9, (%rax,%rsi,8)
movl %edx, %esi
incl %edx
movq %rdx, %rdi
cmpq %rdx, %rcx
jae 0x19362b
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq