// File: /workspace/github_cpp_makefile_star2_O2/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, 0x38(%rsp)
movq %rdx, %r14
movq %rsi, 0x10(%rsp)
movq %rdi, 0x40(%rsp)
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x2120
movq %rax, %r9
andq $0x0, 0x8(%r14)
pushq $0x1
popq %rdx
xorl %r11d, %r11d
xorl %r8d, %r8d
xorl %r10d, %r10d
xorl %ebp, %ebp
movq %rax, 0x20(%rsp)
movq %r14, 0x50(%rsp)
movq 0x10(%rsp), %rax
cmpl (%rax), %edx
ja 0x1551a8
movl %edx, 0xc(%rsp)
movl %edx, %r15d
movl %ebp, %ebx
je 0x154ea9
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
imulq $0x18, %r15, %rcx
movq (%rax,%rcx), %rcx
imulq $0x18, %rbx, %rdx
cmpq (%rax,%rdx), %rcx
jne 0x154ea9
movl 0xc(%rsp), %edx
jmp 0x1551a1
movl 0xc(%rsp), %edx
movl %edx, %eax
subl %ebp, %eax
cmpl $0x1, %eax
sete %cl
cmpl 0x38(%rsp), %eax
seta %al
orb %cl, %al
jne 0x15519f
incl %ebp
movq %r15, 0x28(%rsp)
cmpq %r15, %rbx
jae 0x15519b
movq %r8, 0x30(%rsp)
movq %r10, %r13
movq 0x8(%r14), %rdx
movq 0x18(%r14), %rax
cmpq 0x10(%r14), %rdx
jne 0x154f22
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r11, %r15
callq 0x2370
movq %r15, %r11
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %r9
movq %rax, 0x18(%r14)
movq 0x8(%r14), %rdx
movq %r14, %r8
movl %ebp, 0x3c(%rsp)
movl %ebp, %ecx
imulq $0x18, %rcx, %r12
addq $0x10, %r12
leaq 0x1(%rbx), %rcx
movq %rcx, 0x58(%rsp)
movl %ecx, %r14d
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rcx
imulq $0x18, %rbx, %rbp
movq 0x8(%rcx,%rbp), %rsi
shlq $0x20, %rsi
movq 0x10(%rcx,%rbp), %rdi
movabsq $0x7fffffffffffffff, %r10 # imm = 0x7FFFFFFFFFFFFFFF
andq %r10, %rdi
orq %rsi, %rdi
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r8)
movq %rdi, (%rax,%rdx,8)
movq %r13, %r10
leal 0x2(,%r13,2), %eax
movl %eax, 0x1c(%rsp)
movq 0x30(%rsp), %r8
movq %rbp, 0x48(%rsp)
cmpq %r15, %r14
jae 0x155186
movq (%rcx,%r12), %rbx
xorq 0x10(%rcx,%rbp), %rbx
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x8(%rcx,%rbp), %edx
imulq $0x28, %rdx, %rdx
movl (%rax,%rdx), %edx
movq %rdx, 0x30(%rsp)
movl -0x8(%rcx,%r12), %edx
imulq $0x28, %rdx, %rdx
movl (%rax,%rdx), %ebp
cmpl %r8d, %r10d
jne 0x155003
leal (%r8,%r8), %esi
testl %r8d, %r8d
pushq $0x2
popq %rax
cmovel %eax, %esi
movl %esi, 0x4(%r9)
shlq $0x4, %rsi
movq %r11, %rdi
movq %r10, %r15
callq 0x2370
movq %r15, %r10
movq 0x20(%rsp), %r9
movq %rax, %r11
movq %rax, 0x8(%r9)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rcx
movq %rbx, %r15
shrq $0x3f, %r15
leal 0x1(%r10), %r13d
movl %r13d, (%r9)
movl %r10d, %eax
shlq $0x4, %rax
movq -0x8(%rcx,%r12), %rsi
movq 0x48(%rsp), %r8
movq 0x8(%rcx,%r8), %rdx
shlq $0x21, %rdx
leal (%rsi,%rsi), %edi
orq %rdx, %rdi
orq %r15, %rdi
movq %rdi, (%r11,%rax)
movq 0x10(%rcx,%r8), %rdx
testq %rbx, %rbx
js 0x155055
movq (%rcx,%r12), %rsi
movabsq $0x7fffffffffffffff, %rdi # imm = 0x7FFFFFFFFFFFFFFF
andq %rdi, %rsi
jmp 0x15507e
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andq %rdi, %rbp
shrq $0x20, %rsi
movq (%rcx,%r12), %rdi
movabsq $0x7fffffffffffffff, %r8 # imm = 0x7FFFFFFFFFFFFFFF
andq %r8, %rdi
notq %rdi
addq %rbp, %rsi
addq %rdi, %rsi
decq %rsi
addq %r11, %rax
shlq $0x20, %rdx
orq %rsi, %rdx
movq %rdx, 0x8(%rax)
movl 0x4(%r9), %r8d
cmpl %r8d, %r13d
jne 0x1550e2
cmpl $-0x1, %r10d
movq %r14, 0x60(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, %r14d
pushq $0x2
popq %rax
cmovel %eax, %r14d
movl %r14d, 0x4(%r9)
movq %r14, %rsi
shlq $0x4, %rsi
movq %r11, %rdi
movq %r10, %rbp
callq 0x2370
movq %r14, %r8
movq 0x60(%rsp), %r14
movq %rbp, %r10
movq 0x20(%rsp), %r9
movq %rax, %r11
movq %rax, 0x8(%r9)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rcx
addl $0x2, %r10d
movl %r10d, (%r9)
movl %r13d, %eax
shlq $0x4, %rax
movq 0x48(%rsp), %rbp
movq 0x8(%rcx,%rbp), %rsi
movq -0x8(%rcx,%r12), %rdx
shlq $0x21, %rdx
leal (%rsi,%rsi), %edi
orq %rdx, %rdi
orq %r15, %rdi
movq %rdi, (%r11,%rax)
movq (%rcx,%r12), %rdx
testq %rbx, %rbx
js 0x15512d
movq 0x10(%rcx,%rbp), %rsi
movabsq $0x7fffffffffffffff, %rdi # imm = 0x7FFFFFFFFFFFFFFF
andq %rdi, %rsi
jmp 0x155162
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x30(%rsp), %r15
andq %rdi, %r15
shrq $0x20, %rsi
movq 0x10(%rcx,%rbp), %rdi
movq %r8, %rbx
movabsq $0x7fffffffffffffff, %r8 # imm = 0x7FFFFFFFFFFFFFFF
andq %r8, %rdi
movq %rbx, %r8
notq %rdi
addq %r15, %rsi
addq %rdi, %rsi
decq %rsi
movq 0x28(%rsp), %r15
addq %r11, %rax
shlq $0x20, %rdx
orq %rsi, %rdx
movq %rdx, 0x8(%rax)
incq %r14
addl $0x4, 0x1c(%rsp)
addq $0x18, %r12
jmp 0x154f8f
movl 0x3c(%rsp), %ebp
incl %ebp
movq 0x58(%rsp), %rbx
movq 0x50(%rsp), %r14
jmp 0x154ecd
movl 0xc(%rsp), %edx
movl %edx, %ebp
incl %edx
jmp 0x154e6d
movl %r10d, %esi
shlq $0x4, %rsi
addq %r11, %rsi
movq %r11, %rdi
callq 0x154142
movq 0x8(%r14), %rax
movq 0x18(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x15432b
movq 0x40(%rsp), %rax
movq (%rax), %rdi
pushq $0x8
popq %rsi
callq 0x2120
movq %rax, (%r14)
movq 0x8(%r14), %rcx
xorl %esi, %esi
pushq $0x1
popq %rdx
movl %edx, %r9d
cmpq %r9, %rcx
jb 0x155229
movq 0x18(%r14), %r10
movl %esi, %edi
jne 0x1551fd
movq (%r10,%rdi,8), %r8
jmp 0x15520e
movq (%r10,%rdi,8), %r8
movq (%r10,%r9,8), %r9
xorq %r8, %r9
shrq $0x20, %r9
je 0x155225
shlq $0x20, %rdi
movl %edx, %r9d
subl %esi, %r9d
orq %rdi, %r9
shrq $0x20, %r8
movq %r9, (%rax,%r8,8)
movl %edx, %esi
incl %edx
jmp 0x1551e7
movq 0x20(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq