// File: /workspace/github_cpp_makefile_star2_O2/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 %r9, 0x28(%rsp)
movq %r8, 0x20(%rsp)
testq %rcx, %rcx
je 0xe79f1
movq %rcx, %rbx
movq %rsi, %rbp
movq 0x90(%rsp), %r12
movq 0x80(%rsp), %r13
xorl %ecx, %ecx
xorl %r14d, %r14d
movq %rdx, 0x8(%rsp)
movq %rdi, (%rsp)
cmpq 0x28(%rsp), %rcx
je 0xe7596
imulq $0x18, %rcx, %r8
movq 0x20(%rsp), %rax
movq (%rax,%r8), %rdx
movq 0x8(%rax,%r8), %rsi
imulq $0x18, %rdx, %rdi
movq 0x8(%r13,%rdi), %rax
movq %rax, %r9
xorq %rsi, %r9
btrq $0x1f, %r9
testq %r9, %r9
jne 0xe7a00
cmpq %rax, %rsi
jne 0xe7a00
addq 0x20(%rsp), %r8
addq %r13, %rdi
movb 0x10(%rdi), %al
cmpb %al, 0x10(%r8)
jne 0xe7a00
xorl %r8d, %r8d
movq %rdx, %r9
incq %r9
cmpq 0x88(%rsp), %r9
jge 0xe7560
imulq $0x18, %r9, %r10
movq (%r13,%r10), %rax
cmpq (%rdi), %rax
jne 0xe7560
cmpq %r12, %r8
jae 0xe7560
addq %r13, %r10
movq 0x8(%r10), %r10
movl %r10d, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpq %rbp, %r11
je 0xe74ba
xorl %esi, %r10d
shrdl $0x1f, %r11d, %r10d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r11
xorl %eax, %eax
cmpq %rax, %rbx
je 0xe74ba
leaq 0x1(%rax), %r15
cmpq %r10, (%r11,%rax,8)
movq %r15, %rax
jne 0xe750b
incq %r8
jmp 0xe74ba
addq %r13, %r9
movq 0x8(%r9), %r9
movl %r9d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
cmpq %rbp, %r10
je 0xe7560
xorl %esi, %r9d
shrdl $0x1f, %r10d, %r9d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r10
xorl %eax, %eax
cmpq %rax, %rbx
je 0xe7560
leaq 0x1(%rax), %r11
cmpq %r9, (%r10,%rax,8)
movq %r11, %rax
jne 0xe754b
incq %r8
testq %rdx, %rdx
jle 0xe757b
decq %rdx
imulq $0x18, %rdx, %r9
movq (%r13,%r9), %rax
cmpq (%rdi), %rax
jne 0xe757b
cmpq %r12, %r8
jb 0xe7522
cmpq %r12, %r8
movl $0x0, %eax
cmovaeq %rax, %r8
addq %r8, %r14
incq %rcx
movq (%rsp), %rdi
jmp 0xe745c
testq %r14, %r14
je 0xe79f1
movq (%rdi), %rcx
addq %r14, %rcx
movq %rcx, (%rdi)
cmpq 0x8(%rdi), %rcx
jbe 0xe75ce
movq %rcx, 0x8(%rdi)
movq (%rsp), %rax
movq 0x30(%rax), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq (%rsp), %rdi
movq %rax, 0x30(%rdi)
movq (%rdi), %rcx
movq %rcx, 0x38(%rsp)
subq %r14, %rcx
movq %rcx, 0x10(%rsp)
xorl %eax, %eax
movq %r14, 0x40(%rsp)
cmpq 0x28(%rsp), %rax
je 0xe78be
movq %rax, 0x18(%rsp)
imulq $0x18, %rax, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movq (%rcx,%rax), %r8
imulq $0x18, %r8, %r9
addq %r13, %r9
leaq 0x1(%r8), %r14
movq 0x88(%rsp), %rax
cmpq %r14, %rax
cmovgq %rax, %r14
xorl %r11d, %r11d
movq %r8, %rax
incq %rax
cmpq 0x88(%rsp), %rax
jge 0xe7683
imulq $0x18, %rax, %rcx
movq (%r13,%rcx), %rdx
cmpq (%r9), %rdx
jne 0xe7680
cmpq %r12, %r11
jae 0xe7680
addq %r13, %rcx
movq 0x8(%rcx), %rcx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rbp, %rdx
je 0xe7623
xorl 0x8(%rdi), %ecx
shrdl $0x1f, %edx, %ecx
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
xorl %esi, %esi
cmpq %rsi, %rbx
je 0xe7623
leaq 0x1(%rsi), %r10
cmpq %rcx, (%rdx,%rsi,8)
movq %r10, %rsi
jne 0xe7669
incq %r11
jmp 0xe7623
movq %rax, %r14
movq %r8, %rax
sarq $0x3f, %rax
andq %r8, %rax
movq %r8, %rsi
movq %rsi, %rdx
testq %rdx, %rdx
jle 0xe76f3
leaq -0x1(%rdx), %rsi
imulq $0x18, %rsi, %rcx
movq (%r13,%rcx), %r10
cmpq (%r9), %r10
jne 0xe76f6
cmpq %r12, %r11
jae 0xe76f6
addq %r13, %rcx
movq 0x8(%rcx), %rcx
movl %ecx, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %rsi, %rdx
cmpq %rbp, %r10
je 0xe7693
xorl 0x8(%rdi), %ecx
shrdl $0x1f, %r10d, %ecx
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
xorl %r10d, %r10d
cmpq %r10, %rbx
je 0xe7690
leaq 0x1(%r10), %r15
cmpq %rcx, (%rdx,%r10,8)
movq %r15, %r10
jne 0xe76dc
incq %r11
jmp 0xe7690
movq %rax, %rdx
testq %r11, %r11
sete %al
cmpq %r12, %r11
setae %cl
orb %al, %cl
jne 0xe78a8
subq %rdx, %r14
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpq %rax, %r14
cmovaeq %rax, %r14
shll $0x8, %r14d
movq %r14, 0x30(%rsp)
movq %r8, %r11
incq %r11
cmpq 0x88(%rsp), %r11
jge 0xe788e
imulq $0x18, %r11, %rax
movq (%r13,%rax), %rcx
cmpq (%r9), %rcx
jne 0xe788e
addq %r13, %rax
movq 0x8(%rax), %rcx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rbp, %rdx
je 0xe7725
movl 0x8(%rdi), %esi
xorl %ecx, %esi
shldl $0x1, %esi, %edx
shrl $0x1f, %esi
movq 0x8(%rsp), %r10
movq 0x10(%r10), %r10
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0xe7725
leaq 0x1(%r15), %r14
cmpq %rdx, (%r10,%r15,8)
movq %r14, %r15
jne 0xe7774
shrq $0x20, %rcx
testq %rsi, %rsi
je 0xe779a
movzbl 0x10(%rax), %edx
subl %ecx, %edx
addl $-0x3, %edx
movl %edx, %ecx
movq 0x10(%rsp), %r10
imulq $0x18, %r10, %rdx
incq %r10
movq %r10, 0x10(%rsp)
movq (%rsp), %r10
addq 0x30(%r10), %rdx
movl %ecx, 0xc(%rdx)
movq 0x8(%rdi), %rcx
shrq $0x20, %rcx
movl %ecx, 0x8(%rdx)
movzbl 0x10(%rdi), %r10d
orl 0x30(%rsp), %r10d
movl %r10d, 0x10(%rdx)
movq 0x8(%rax), %rax
shlq $0x21, %rax
shlq $0x20, %rsi
orq %rax, %rsi
orq %rcx, %rsi
movq %rsi, (%rdx)
jmp 0xe7725
addq %r13, %rax
movq 0x8(%rax), %r11
movl %r11d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rbp, %rcx
je 0xe788e
movl 0x8(%rdi), %esi
xorl %r11d, %esi
shldl $0x1, %esi, %ecx
shrl $0x1f, %esi
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
xorl %r10d, %r10d
cmpq %r10, %rbx
je 0xe788e
leaq 0x1(%r10), %r14
cmpq %rcx, (%rdx,%r10,8)
movq %r14, %r10
jne 0xe781b
shrq $0x20, %r11
testq %rsi, %rsi
je 0xe7843
movzbl 0x10(%rax), %ecx
subl %r11d, %ecx
addl $-0x3, %ecx
movl %ecx, %r11d
movq 0x10(%rsp), %rdx
imulq $0x18, %rdx, %rcx
incq %rdx
movq %rdx, 0x10(%rsp)
movq (%rsp), %rdx
addq 0x30(%rdx), %rcx
movl %r11d, 0xc(%rcx)
movq 0x8(%rdi), %rdx
shrq $0x20, %rdx
movl %edx, 0x8(%rcx)
movzbl 0x10(%rdi), %r10d
orl 0x30(%rsp), %r10d
movl %r10d, 0x10(%rcx)
movq 0x8(%rax), %rax
shlq $0x21, %rax
shlq $0x20, %rsi
orq %rax, %rsi
orq %rdx, %rsi
movq %rsi, (%rcx)
testq %r8, %r8
jle 0xe78a8
decq %r8
imulq $0x18, %r8, %rax
movq (%r13,%rax), %rcx
cmpq (%r9), %rcx
je 0xe77e9
movq 0x18(%rsp), %rax
incq %rax
movq (%rsp), %rdi
movq 0x40(%rsp), %r14
jmp 0xe75e2
movq 0x38(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jne 0xe7a1f
imulq $0x18, %rax, %rsi
addq 0x30(%rdi), %rsi
movq %r14, %rax
negq %rax
imulq $0x18, %rax, %rdi
addq %rsi, %rdi
callq 0xe1068
movq (%rsp), %rdi
movq (%rdi), %rax
movq %rax, %r15
subq %r14, %r15
xorl %r12d, %r12d
pushq $0x2
popq %r14
movq 0x8(%rsp), %r9
leaq 0x1(%r15), %rsi
movq %rax, %rcx
negq %rcx
imulq $0x18, %r15, %rbp
leaq 0x18(%rbp), %rdx
cmpq %rax, %rsi
ja 0xe79f1
movq %rsi, %r13
addq %rcx, %rsi
incq %rsi
cmpq $0x1, %rsi
je 0xe794d
leaq 0x1(%r13), %rsi
movq (%rsp), %rdi
movq 0x30(%rdi), %rdi
movq (%rdi,%rbp), %r8
xorq (%rdi,%rdx), %r8
movq (%rsp), %rdi
addq $0x18, %rdx
shrq $0x20, %r8
je 0xe7913
cmpq %rbx, %r12
movq %rbx, %rcx
cmovaq %r12, %rcx
cmpq %rbx, %r12
jae 0xe7977
movq 0x10(%r9), %rax
movq 0x30(%rdi), %rdx
movq (%rdx,%rbp), %rdx
shrq $0x20, %rdx
cmpq %rdx, (%rax,%r12,8)
jae 0xe797c
incq %r12
jmp 0xe7957
movq %rcx, %r12
jmp 0xe79bd
jne 0xe79bd
movq (%r9), %rcx
cmpq 0x8(%r9), %rcx
jne 0xe79b2
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r14, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %rdi
movq 0x8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
movq %r15, (%rax,%rcx,8)
cmpq %rbx, %r12
movq %rbx, %rax
cmovaq %r12, %rax
cmpq %rbx, %r12
jae 0xe79e3
movq 0x10(%r9), %rcx
movq 0x30(%rdi), %rdx
movl 0x4(%rdx,%rbp), %edx
cmpq %rdx, (%rcx,%r12,8)
jne 0xe79e6
incq %r12
jmp 0xe79c7
movq %rax, %r12
movq (%rdi), %rax
movq %r13, %r15
jmp 0xe7901
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10509d(%rip), %rdi    # 0x1ecaa4
leaq 0x1050e5(%rip), %rsi    # 0x1ecaf3
leaq 0x1050e9(%rip), %rcx    # 0x1ecafe
movl $0x50b, %edx            # imm = 0x50B
callq 0x2110
leaq 0x10514e(%rip), %rdi    # 0x1ecb74
leaq 0x1050c6(%rip), %rsi    # 0x1ecaf3
leaq 0x1050ca(%rip), %rcx    # 0x1ecafe
movl $0x567, %edx            # imm = 0x567
callq 0x2110