// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/anchor.cpp
// Function: gen_pair_chain(ha_abufl_s*, unsigned long, st_mt_t*, unsigned long, ha_mzl_t*, unsigned long, ha_mzl_t*, long, unsigned long)
void gen_pair_chain(ha_abufl_t *ab, uint64_t rid, st_mt_t *tid, uint64_t tid_n, ha_mzl_t *in, uint64_t in_n, ha_mzl_t *idx, int64_t idx_n, uint64_t mzl_cutoff)
{
	if(!tid_n) return;
	uint64_t i, l, k, n, m, rev, x, tn, kn; ha_mzl_t *z, *p, *y; int64_t zi, ns, ne; anchor1_t *an;
	for (i = tn = 0; i < in_n; ++i) {
		///z is one of the minimizer
		z = &in[i]; p = &(idx[z->x]); n = 0;
		assert(z->pos == p->pos && z->rid == p->rid && z->span == p->span && z->rev == p->rev);
		for (zi = z->x+1; zi < idx_n && idx[zi].x == p->x && n < mzl_cutoff; zi++) {
			if(idx[zi].rid == rid) continue; 
			x = idx[zi].rid; x <<= 1; x |= ((uint64_t)((z->rev == idx[zi].rev)?0:1));
			for (m = 0; m < tid_n; m++) {
				if(tid->a[m] == x) {
					n++; break;
				}
			}
		}
		for (zi = ((int64_t)z->x)-1; zi >= 0 && idx[zi].x == p->x && n < mzl_cutoff; zi--) {
			if(idx[zi].rid == rid) continue; 
			x = idx[zi].rid; x <<= 1; x |= ((uint64_t)((z->rev == idx[zi].rev)?0:1));
			for (m = 0; m < tid_n; m++) {
				if(tid->a[m] == x) {
					n++; break;
				}
			}
		}
		if((!n) || (n >= mzl_cutoff)) continue;
		tn += n;
	}
	if(!tn) return;
	ab->n_a += tn;
	if (ab->n_a > ab->m_a) {
		ab->m_a = ab->n_a;
		REALLOC(ab->a, ab->m_a);
	}


	for (i = 0, k = ab->n_a - tn; i < in_n; ++i) {
		///z is one of the minimizer
		z = &in[i]; p = &(idx[z->x]); n = 0; ns = 0; ne = -1;
		for (zi = z->x+1; zi < idx_n && idx[zi].x == p->x && n < mzl_cutoff; zi++) {
			if(idx[zi].rid == rid) continue; 
			x = idx[zi].rid; x <<= 1; x |= ((uint64_t)((z->rev == idx[zi].rev)?0:1));
			for (m = 0; m < tid_n; m++) {
				if(tid->a[m] == x) {
					n++; break;
				}
			}
		}
		ne = zi;
		for (zi = ((int64_t)z->x)-1; zi >= 0 && idx[zi].x == p->x && n < mzl_cutoff; zi--) {
			if(idx[zi].rid == rid) continue; 
			x = idx[zi].rid; x <<= 1; x |= ((uint64_t)((z->rev == idx[zi].rev)?0:1));
			for (m = 0; m < tid_n; m++) {
				if(tid->a[m] == x) {
					n++; break;
				}
			}
		}
		ns = zi + 1; 
		if((!n) || (n >= mzl_cutoff)) continue;
		n = ne - ns; if(n > ((uint32_t)(0xffffffu))) n = 0xffffffu; n<<=8;

		for (zi = z->x+1; zi < idx_n && idx[zi].x == p->x; zi++) {
			if(idx[zi].rid == rid) continue; 
			x = idx[zi].rid; x <<= 1; x |= ((uint64_t)((z->rev == idx[zi].rev)?0:1));
			for (m = 0; m < tid_n; m++) {
				if(tid->a[m] == x) break;
			}
			if(m >= tid_n) continue; 


			y = &(idx[zi]); an = &ab->a[k++];
			rev = z->rev == y->rev? 0 : 1;
			an->other_off = rev?((uint32_t)-1)-1-(y->pos+1-y->span):y->pos;
			an->self_off = z->pos;
			///an->cnt: cnt<<8|span
			an->cnt = ((z->span <= ((uint32_t)(0xffu)))?z->span:((uint32_t)(0xffu))); an->cnt |= n;
			an->srt = (uint64_t)y->rid<<33 | (uint64_t)rev<<32 | an->self_off;
		}
		for (zi = ((int64_t)z->x)-1; zi >= 0 && idx[zi].x == p->x; zi--) {
			if(idx[zi].rid == rid) continue; 
			x = idx[zi].rid; x <<= 1; x |= ((uint64_t)((z->rev == idx[zi].rev)?0:1));
			for (m = 0; m < tid_n; m++) {
				if(tid->a[m] == x) break;
			}
			if(m >= tid_n) continue; 



			y = &(idx[zi]); an = &ab->a[k++];
			rev = z->rev == y->rev? 0 : 1;
			an->other_off = rev?((uint32_t)-1)-1-(y->pos+1-y->span):y->pos;
			an->self_off = z->pos;
			///an->cnt: cnt<<8|span
			an->cnt = ((z->span <= ((uint32_t)(0xffu)))?z->span:((uint32_t)(0xffu))); an->cnt |= n;
			an->srt = (uint64_t)y->rid<<33 | (uint64_t)rev<<32 | an->self_off;
		}
	}
	assert(k == ab->n_a);
	radix_sort_ha_an1(ab->a + ab->n_a - tn, ab->a + ab->n_a); kn = 0;
	for (k = ab->n_a - tn + 1, l = ab->n_a - tn; k <= ab->n_a; ++k) {
		if (k == ab->n_a || (ab->a[k].srt>>32) != (ab->a[l].srt>>32)) {
			for (; kn < tid_n && tid->a[kn] < (ab->a[l].srt>>32); kn++);
			if(kn < tid_n && tid->a[kn] == (ab->a[l].srt>>32)) kv_push(uint64_t, *tid, l);
			for (; kn < tid_n && tid->a[kn] == (ab->a[l].srt>>32); kn++);
			l = k;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x28(%rsp)
movq %rcx, %rbx
movq %rdx, (%rsp)
testq %rcx, %rcx
sete %al
movq %r9, 0x30(%rsp)
testq %r9, %r9
sete %cl
orb %al, %cl
jne 0x12cbe9
movq %rsi, %rbp
movq %rdi, 0x10(%rsp)
movq 0x90(%rsp), %r15
movq 0x80(%rsp), %r12
xorl %edx, %edx
xorl %ecx, %ecx
movq %rdx, 0x20(%rsp)
leaq (%rcx,%rcx,2), %rdi
movq 0x28(%rsp), %rax
movq (%rax,%rdi,8), %rsi
movq 0x8(%rax,%rdi,8), %rdx
leaq (%rsi,%rsi,2), %r8
movq 0x8(%r12,%r8,8), %rax
movq %rax, %r9
xorq %rdx, %r9
btrq $0x1f, %r9
testq %r9, %r9
jne 0x12cbf8
cmpq %rax, %rdx
jne 0x12cbf8
movq 0x28(%rsp), %rax
leaq (%rax,%rdi,8), %rax
leaq (%r12,%r8,8), %r8
movb 0x10(%r8), %dil
cmpb %dil, 0x10(%rax)
jne 0x12cbf8
leaq 0x1(%rsi), %r9
cmpq 0x88(%rsp), %r9
jge 0x12c6d2
movq (%r8), %r10
xorl %edi, %edi
movq (%rsp), %r14
leaq (%r9,%r9,2), %rax
cmpq %r10, (%r12,%rax,8)
jne 0x12c6d8
cmpq %r15, %rdi
jae 0x12c6d8
leaq (%r12,%rax,8), %rax
movq 0x8(%rax), %r11
movl %r11d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rbp, %rax
je 0x12c6c3
xorl %edx, %r11d
shrdl $0x1f, %eax, %r11d
movq 0x10(%r14), %rax
xorl %r13d, %r13d
cmpq %r11, (%rax,%r13,8)
je 0x12c6bc
incq %r13
cmpq %r13, %rbx
jne 0x12c6ac
jmp 0x12c6bf
incq %rdi
movq (%rsp), %r14
incq %r9
cmpq 0x88(%rsp), %r9
jne 0x12c679
jmp 0x12c6d8
xorl %edi, %edi
movq (%rsp), %r14
testq %rsi, %rsi
jle 0x12c732
movq (%r8), %r8
movq %rsi, %r9
decq %rsi
leaq (%rsi,%rsi,2), %rax
cmpq %r8, (%r12,%rax,8)
jne 0x12c732
cmpq %r15, %rdi
jae 0x12c732
leaq (%r12,%rax,8), %rax
movq 0x8(%rax), %r10
movl %r10d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rbp, %rax
je 0x12c72c
xorl %edx, %r10d
shrdl $0x1f, %eax, %r10d
movq 0x10(%r14), %rax
xorl %r11d, %r11d
cmpq %r10, (%rax,%r11,8)
je 0x12c729
incq %r11
cmpq %r11, %rbx
jne 0x12c719
jmp 0x12c72c
incq %rdi
cmpq $0x1, %r9
jg 0x12c6e0
cmpq %r15, %rdi
movl $0x0, %eax
cmovaeq %rax, %rdi
movq 0x20(%rsp), %rdx
addq %rdi, %rdx
incq %rcx
cmpq 0x30(%rsp), %rcx
jne 0x12c60a
testq %rdx, %rdx
movq 0x10(%rsp), %rax
je 0x12cbe9
movq (%rax), %rcx
addq %rdx, %rcx
movq %rcx, (%rax)
cmpq 0x8(%rax), %rcx
jbe 0x12c796
movq %rcx, 0x8(%rax)
movq %rax, %r14
movq %rdx, %r13
movq 0x30(%rax), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %r13, %rdx
movq %rax, 0x30(%r14)
movq (%r14), %rcx
movq %rcx, 0x38(%rsp)
movq %rdx, 0x20(%rsp)
subq %rdx, %rcx
movq %rcx, 0x18(%rsp)
xorl %eax, %eax
movq (%rsp), %r13
movq %rax, 0x40(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,8), %rdi
movq (%rdi), %rcx
leaq (%rcx,%rcx,2), %rax
leaq (%r12,%rax,8), %rdx
leaq 0x1(%rcx), %r11
cmpq 0x88(%rsp), %r11
movq %rdx, 0x8(%rsp)
jge 0x12c847
movq (%rdx), %r8
movq %r11, %r9
xorl %eax, %eax
leaq (%r9,%r9,2), %rdx
cmpq %r8, (%r12,%rdx,8)
jne 0x12c840
cmpq %r15, %rax
jae 0x12c840
leaq (%r12,%rdx,8), %rdx
movq 0x8(%rdx), %rdx
movl %edx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rbp, %rsi
je 0x12c82b
xorl 0x8(%rdi), %edx
shrdl $0x1f, %esi, %edx
movq 0x10(%r13), %rsi
xorl %r10d, %r10d
cmpq %rdx, (%rsi,%r10,8)
je 0x12c828
incq %r10
cmpq %r10, %rbx
jne 0x12c818
jmp 0x12c82b
incq %rax
incq %r9
cmpq 0x88(%rsp), %r9
jne 0x12c7e6
movq 0x88(%rsp), %r9
movq 0x8(%rsp), %rdx
jmp 0x12c84c
xorl %eax, %eax
movq %r11, %r9
testq %rcx, %rcx
jle 0x12c8b4
movq (%rdx), %r10
movq %rcx, %r8
movq %r8, %rsi
decq %r8
leaq (%r8,%r8,2), %rdx
cmpq %r10, (%r12,%rdx,8)
jne 0x12c8b7
cmpq %r15, %rax
jae 0x12c8b7
leaq (%r12,%rdx,8), %rdx
movq 0x8(%rdx), %rdx
movl %edx, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmpq %rbp, %r14
je 0x12c8aa
xorl 0x8(%rdi), %edx
shrdl $0x1f, %r14d, %edx
movq 0x10(%r13), %r13
xorl %r14d, %r14d
cmpq %rdx, (%r13,%r14,8)
je 0x12c8a3
incq %r14
cmpq %r14, %rbx
jne 0x12c892
jmp 0x12c8a6
incq %rax
movq (%rsp), %r13
cmpq $0x1, %rsi
jg 0x12c857
xorl %esi, %esi
jmp 0x12c8b7
movq %rcx, %rsi
testq %rax, %rax
sete %dl
cmpq %r15, %rax
setae %al
orb %dl, %al
jne 0x12ca98
subq %rsi, %r9
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpq %rax, %r9
cmovaeq %rax, %r9
shll $0x8, %r9d
cmpq 0x88(%rsp), %r11
jge 0x12c9be
movq 0x8(%rsp), %rax
movq (%rax), %rax
leaq (%r11,%r11,2), %rdx
cmpq %rax, (%r12,%rdx,8)
jne 0x12c9be
leaq (%r12,%rdx,8), %r8
movq 0x8(%r8), %rdx
movl %edx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rbp, %rsi
je 0x12c9ad
movl 0x8(%rdi), %r10d
xorl %edx, %r10d
shldl $0x1, %r10d, %esi
shrl $0x1f, %r10d
movq 0x10(%r13), %r13
xorl %r14d, %r14d
cmpq %rsi, (%r13,%r14,8)
je 0x12c947
incq %r14
cmpq %r14, %rbx
jne 0x12c932
movq (%rsp), %r13
jmp 0x12c9ad
shrq $0x20, %rdx
testq %r10, %r10
je 0x12c95c
movzbl 0x10(%r8), %esi
subl %edx, %esi
addl $-0x3, %esi
movl %esi, %edx
movq (%rsp), %r13
movq 0x18(%rsp), %r14
leaq (%r14,%r14,2), %rsi
incq %r14
movq %r14, 0x18(%rsp)
shlq $0x3, %rsi
movq 0x10(%rsp), %r14
addq 0x30(%r14), %rsi
movl %edx, 0xc(%rsi)
movq 0x8(%rdi), %rdx
shrq $0x20, %rdx
movl %edx, 0x8(%rsi)
movzbl 0x10(%rdi), %r14d
orl %r9d, %r14d
movl %r14d, 0x10(%rsi)
movq 0x8(%r8), %r8
shlq $0x21, %r8
shlq $0x20, %r10
orq %r8, %r10
orq %rdx, %r10
movq %r10, (%rsi)
incq %r11
cmpq 0x88(%rsp), %r11
jne 0x12c8f4
testq %rcx, %rcx
jle 0x12ca98
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rcx, %r10
decq %rcx
leaq (%rcx,%rcx,2), %rdx
cmpq %rax, (%r12,%rdx,8)
jne 0x12ca98
leaq (%r12,%rdx,8), %r8
movq 0x8(%r8), %rsi
movl %esi, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rbp, %rdx
je 0x12ca8e
movl 0x8(%rdi), %r11d
xorl %esi, %r11d
shldl $0x1, %r11d, %edx
shrl $0x1f, %r11d
movq 0x10(%r13), %r13
xorl %r14d, %r14d
cmpq %rdx, (%r13,%r14,8)
je 0x12ca28
incq %r14
cmpq %r14, %rbx
jne 0x12ca13
movq (%rsp), %r13
jmp 0x12ca8e
shrq $0x20, %rsi
testq %r11, %r11
je 0x12ca3d
movzbl 0x10(%r8), %edx
subl %esi, %edx
addl $-0x3, %edx
movl %edx, %esi
movq (%rsp), %r13
movq 0x18(%rsp), %r14
leaq (%r14,%r14,2), %rdx
incq %r14
movq %r14, 0x18(%rsp)
shlq $0x3, %rdx
movq 0x10(%rsp), %r14
addq 0x30(%r14), %rdx
movl %esi, 0xc(%rdx)
movq 0x8(%rdi), %rsi
shrq $0x20, %rsi
movl %esi, 0x8(%rdx)
movzbl 0x10(%rdi), %r14d
orl %r9d, %r14d
movl %r14d, 0x10(%rdx)
movq 0x8(%r8), %r8
shlq $0x21, %r8
shlq $0x20, %r11
orq %r8, %r11
orq %rsi, %r11
movq %r11, (%rdx)
cmpq $0x1, %r10
jg 0x12c9cf
movq 0x40(%rsp), %rax
incq %rax
cmpq 0x30(%rsp), %rax
jne 0x12c7ae
movq 0x38(%rsp), %rax
cmpq %rax, 0x18(%rsp)
jne 0x12cc17
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
movq 0x10(%rsp), %r14
addq 0x30(%r14), %rsi
movq 0x20(%rsp), %r15
movq %r15, %rax
negq %rax
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rdi
callq 0x125ce1
movq (%r14), %rax
movq %rax, %rbp
subq %r15, %rbp
leaq 0x1(%rbp), %r15
cmpq %rax, %r15
ja 0x12cbe9
movq %r14, %rsi
xorl %r13d, %r13d
movl $0x2, %r12d
movq (%rsp), %r14
cmpq %rax, %r15
je 0x12cb34
movq 0x30(%rsi), %rax
leaq (%r15,%r15,2), %rcx
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movq (%rax,%rdx,8), %rdx
xorq (%rax,%rcx,8), %rdx
shrq $0x20, %rdx
je 0x12cbda
cmpq %rbx, %r13
jae 0x12cba1
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq 0x30(%rsi), %rcx
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movl 0x4(%rcx,%rdx,8), %ecx
cmpq %rcx, (%rax,%r13,8)
jae 0x12cb64
incq %r13
cmpq %r13, %rbx
jne 0x12cb54
jmp 0x12cbcf
jne 0x12cba1
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x12cb96
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x10(%rsp), %rsi
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rbp, (%rax,%rcx,8)
cmpq %rbx, %r13
jae 0x12cbd7
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq 0x30(%rsi), %rcx
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movl 0x4(%rcx,%rdx,8), %ecx
cmpq %rcx, (%rax,%r13,8)
jne 0x12cbd7
incq %r13
cmpq %r13, %rbx
jne 0x12cbc1
movq %r15, %rbp
movq %rbx, %r13
jmp 0x12cbda
movq %r15, %rbp
incq %r15
movq (%rsi), %rax
cmpq %rax, %r15
jbe 0x12cb0a
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10dfb5(%rip), %rdi    # 0x23abb4
leaq 0x10dffd(%rip), %rsi    # 0x23ac03
leaq 0x10e001(%rip), %rcx    # 0x23ac0e
movl $0x50b, %edx            # imm = 0x50B
callq 0x2110
leaq 0x10e066(%rip), %rdi    # 0x23ac84
leaq 0x10dfde(%rip), %rsi    # 0x23ac03
leaq 0x10dfe2(%rip), %rcx    # 0x23ac0e
movl $0x567, %edx            # imm = 0x567
callq 0x2110