// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %r9, 0x30(%rsp)
movq %r8, 0x38(%rsp)
movq %rdx, (%rsp)
testq %rcx, %rcx
je 0x126d03
movq %rcx, %rbx
movq %rsi, %rbp
movq %rdi, 0x10(%rsp)
movq 0xa0(%rsp), %r15
movq 0x90(%rsp), %r12
cmpq $0x0, 0x30(%rsp)
je 0x1267ac
xorl %r14d, %r14d
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdi
movq 0x38(%rsp), %rax
movq (%rax,%rdi,8), %rsi
movq 0x8(%rax,%rdi,8), %rdx
leaq (%rsi,%rsi,2), %r8
movq 0x8(%r12,%r8,8), %r9
movq %r9, %r10
xorq %rdx, %r10
btrq $0x1f, %r10
testq %r10, %r10
jne 0x126d12
cmpq %r9, %rdx
jne 0x126d12
movq 0x38(%rsp), %rax
leaq (%rax,%rdi,8), %rdi
leaq (%r12,%r8,8), %r8
movb 0x10(%r8), %r9b
cmpb %r9b, 0x10(%rdi)
jne 0x126d12
movq %r14, %rax
leaq 0x1(%rsi), %r9
cmpq 0x98(%rsp), %r9
jge 0x126728
movq (%r8), %r10
xorl %edi, %edi
movq (%rsp), %r13
leaq (%r9,%r9,2), %r11
cmpq %r10, (%r12,%r11,8)
jne 0x12672e
cmpq %r15, %rdi
jae 0x12672e
leaq (%r12,%r11,8), %r11
movq 0x8(%r11), %r11
movl %r11d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmpq %rbp, %r14
je 0x126719
xorl %edx, %r11d
shrdl $0x1f, %r14d, %r11d
movq 0x10(%r13), %r14
xorl %r15d, %r15d
cmpq %r11, (%r14,%r15,8)
je 0x12670e
incq %r15
cmpq %r15, %rbx
jne 0x1266fe
jmp 0x126711
incq %rdi
movq 0xa0(%rsp), %r15
incq %r9
cmpq 0x98(%rsp), %r9
jne 0x1266c9
jmp 0x12672e
xorl %edi, %edi
movq (%rsp), %r13
testq %rsi, %rsi
jle 0x12678a
movq (%r8), %r8
movq %rsi, %r9
decq %rsi
leaq (%rsi,%rsi,2), %r10
cmpq %r8, (%r12,%r10,8)
jne 0x12678a
cmpq %r15, %rdi
jae 0x12678a
leaq (%r12,%r10,8), %r10
movq 0x8(%r10), %r10
movl %r10d, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpq %rbp, %r11
je 0x126784
xorl %edx, %r10d
shrdl $0x1f, %r11d, %r10d
movq 0x10(%r13), %r11
xorl %r14d, %r14d
cmpq %r10, (%r11,%r14,8)
je 0x126781
incq %r14
cmpq %r14, %rbx
jne 0x126771
jmp 0x126784
incq %rdi
cmpq $0x1, %r9
jg 0x126736
cmpq %r15, %rdi
movl $0x0, %edx
cmovaeq %rdx, %rdi
movq %rax, %r14
addq %rdi, %r14
incq %rcx
cmpq 0x30(%rsp), %rcx
jne 0x12665c
jmp 0x1267af
xorl %r14d, %r14d
testq %r14, %r14
movq 0x10(%rsp), %rcx
je 0x126d03
movq (%rcx), %rax
addq %r14, %rax
movq %rax, (%rcx)
cmpq 0x8(%rcx), %rax
jbe 0x1267ea
movq %rax, 0x8(%rcx)
movq 0x30(%rcx), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x10(%rsp), %rcx
movq %rax, 0x30(%rcx)
movq (%rcx), %rax
movq %rax, 0x48(%rsp)
movq %r14, 0x40(%rsp)
subq %r14, %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x30(%rsp)
je 0x126bc1
cmpq $0x1, %rbx
movq %rbx, %rdx
adcq $0x0, %rdx
leaq -0x1(%rdx), %rsi
xorl %eax, %eax
movq (%rsp), %r13
movq %rax, 0x50(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x18(%rsp)
movq (%rax), %r10
leaq (%r10,%r10,2), %rax
leaq (%r12,%rax,8), %rax
leaq 0x1(%r10), %r11
cmpq 0x98(%rsp), %r11
movq %rax, 0x20(%rsp)
jge 0x1268c2
movq (%rax), %rax
xorl %r8d, %r8d
leaq (%r11,%r11,2), %rcx
cmpq %rax, (%r12,%rcx,8)
jne 0x1268bb
cmpq %r15, %r8
jae 0x1268bb
leaq (%r12,%rcx,8), %rcx
movq 0x8(%rcx), %rcx
movl %ecx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
cmpq %rbp, %rdi
je 0x1268a6
movq 0x18(%rsp), %r9
xorl 0x8(%r9), %ecx
shrdl $0x1f, %edi, %ecx
movq 0x10(%r13), %rdi
xorl %r14d, %r14d
cmpq %rcx, (%rdi,%r14,8)
je 0x1268a3
incq %r14
cmpq %r14, %rdx
jne 0x126893
jmp 0x1268a6
incq %r8
incq %r11
cmpq 0x98(%rsp), %r11
jne 0x12685b
movq 0x98(%rsp), %r11
movq 0x20(%rsp), %rax
jmp 0x1268c5
xorl %r8d, %r8d
testq %r10, %r10
jle 0x12693a
movq (%rax), %rcx
movq %r10, %rdi
movq %rdi, %rax
decq %rdi
leaq (%rdi,%rdi,2), %r14
cmpq %rcx, (%r12,%r14,8)
jne 0x12693d
cmpq %r15, %r8
jae 0x12693d
leaq (%r12,%r14,8), %r9
movq 0x8(%r9), %r14
movl %r14d, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
cmpq %rbp, %r15
je 0x126928
movq 0x18(%rsp), %r9
xorl 0x8(%r9), %r14d
shrdl $0x1f, %r15d, %r14d
movq 0x10(%r13), %r15
xorl %r13d, %r13d
cmpq %r14, (%r15,%r13,8)
je 0x126921
incq %r13
cmpq %r13, %rdx
jne 0x126911
jmp 0x126924
incq %r8
movq (%rsp), %r13
cmpq $0x1, %rax
movq 0xa0(%rsp), %r15
jg 0x1268d0
xorl %eax, %eax
jmp 0x12693d
movq %r10, %rax
testq %r8, %r8
sete %cl
cmpq %r15, %r8
setae %dil
orb %cl, %dil
jne 0x126bae
subq %rax, %r11
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpq %rax, %r11
cmovaeq %rax, %r11
shll $0x8, %r11d
leaq 0x1(%r10), %r8
cmpq 0x98(%rsp), %r8
jge 0x126a93
movq 0x20(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x8(%rsp)
leaq (%r8,%r8,2), %rax
cmpq %rcx, (%r12,%rax,8)
jne 0x126a93
leaq (%r12,%rax,8), %rdi
movq 0x8(%rdi), %rax
movl %eax, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
cmpq %rbp, %r13
jne 0x1269b3
movq (%rsp), %r13
jmp 0x126a82
movq 0x18(%rsp), %rcx
movl 0x8(%rcx), %ecx
xorl %eax, %ecx
shldl $0x1, %ecx, %r13d
movq (%rsp), %r9
movq 0x10(%r9), %r14
cmpq %r13, (%r14)
jne 0x126a44
shrl $0x1f, %ecx
shrq $0x20, %rax
testq %rcx, %rcx
je 0x1269ea
movzbl 0x10(%rdi), %r9d
subl %eax, %r9d
addl $-0x3, %r9d
movl %r9d, %eax
movq (%rsp), %r13
movq 0x28(%rsp), %r14
leaq (%r14,%r14,2), %r9
incq %r14
movq %r14, 0x28(%rsp)
shlq $0x3, %r9
movq 0x10(%rsp), %r14
addq 0x30(%r14), %r9
movl %eax, 0xc(%r9)
movq 0x18(%rsp), %r14
movq 0x8(%r14), %rax
shrq $0x20, %rax
movl %eax, 0x8(%r9)
movzbl 0x10(%r14), %r14d
orl %r11d, %r14d
movl %r14d, 0x10(%r9)
movq 0x8(%rdi), %rdi
shlq $0x21, %rdi
shlq $0x20, %rcx
orq %rdi, %rcx
orq %rax, %rcx
movq %rcx, (%r9)
jmp 0x126a7d
xorl %r15d, %r15d
cmpq %r15, %rsi
je 0x126a71
leaq 0x1(%r15), %r9
cmpq %r13, 0x8(%r14,%r15,8)
movq %r9, %r15
jne 0x126a47
cmpq %rbx, %r9
movq 0xa0(%rsp), %r15
jb 0x1269cf
movq (%rsp), %r13
jmp 0x126a7d
movq (%rsp), %r13
movq 0xa0(%rsp), %r15
movq 0x8(%rsp), %rcx
incq %r8
cmpq 0x98(%rsp), %r8
jne 0x126985
testq %r10, %r10
jle 0x126bae
movq 0x20(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x8(%rsp)
movq %r10, %r8
decq %r10
leaq (%r10,%r10,2), %rax
cmpq %rcx, (%r12,%rax,8)
jne 0x126bae
leaq (%r12,%rax,8), %rdi
movq 0x8(%rdi), %r14
movl %r14d, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
cmpq %rbp, %r13
jne 0x126add
movq (%rsp), %r13
jmp 0x126ba4
movq 0x18(%rsp), %rax
movl 0x8(%rax), %ecx
xorl %r14d, %ecx
shldl $0x1, %ecx, %r13d
movq (%rsp), %rax
movq 0x10(%rax), %r15
cmpq %r13, (%r15)
jne 0x126b75
shrl $0x1f, %ecx
shrq $0x20, %r14
testq %rcx, %rcx
je 0x126b13
movzbl 0x10(%rdi), %eax
subl %r14d, %eax
addl $-0x3, %eax
movl %eax, %r14d
movq (%rsp), %r13
movq 0xa0(%rsp), %r15
movq 0x28(%rsp), %r9
leaq (%r9,%r9,2), %rax
incq %r9
movq %r9, 0x28(%rsp)
shlq $0x3, %rax
movq 0x10(%rsp), %r9
addq 0x30(%r9), %rax
movl %r14d, 0xc(%rax)
movq 0x18(%rsp), %r14
movq 0x8(%r14), %r9
shrq $0x20, %r9
movl %r9d, 0x8(%rax)
movzbl 0x10(%r14), %r14d
orl %r11d, %r14d
movl %r14d, 0x10(%rax)
movq 0x8(%rdi), %rdi
shlq $0x21, %rdi
shlq $0x20, %rcx
orq %rdi, %rcx
orq %r9, %rcx
movq %rcx, (%rax)
jmp 0x126b9f
xorl %eax, %eax
cmpq %rax, %rsi
je 0x126b93
leaq 0x1(%rax), %r9
cmpq %r13, 0x8(%r15,%rax,8)
movq %r9, %rax
jne 0x126b77
cmpq %rbx, %r9
jb 0x126afa
movq (%rsp), %r13
movq 0xa0(%rsp), %r15
movq 0x8(%rsp), %rcx
cmpq $0x1, %r8
jg 0x126aa9
movq 0x50(%rsp), %rax
incq %rax
cmpq 0x30(%rsp), %rax
jne 0x126820
movq 0x48(%rsp), %rax
cmpq %rax, 0x28(%rsp)
jne 0x126d31
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
movq 0x10(%rsp), %r14
addq 0x30(%r14), %rsi
movq 0x40(%rsp), %r15
movq %r15, %rax
negq %rax
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rdi
callq 0x11fe18
movq (%r14), %rax
movq %rax, %rbp
subq %r15, %rbp
leaq 0x1(%rbp), %r15
cmpq %rax, %r15
ja 0x126d03
movq %r14, %rsi
xorl %r13d, %r13d
movl $0x2, %r12d
movq (%rsp), %r14
cmpq %rax, %r15
je 0x126c4a
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 0x126cf4
cmpq %rbx, %r13
jae 0x126cbb
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x30(%rsi), %rax
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movl 0x4(%rax,%rcx,8), %eax
cmpq %rax, (%rdi,%r13,8)
jae 0x126c7d
incq %r13
cmpq %r13, %rbx
jne 0x126c6a
movq %rbx, %r13
jmp 0x126cbb
jne 0x126cbb
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x126ca9
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %rsi
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movq %rbp, (%rcx,%rax,8)
cmpq %rbx, %r13
jae 0x126cf1
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 0x126cf1
incq %r13
cmpq %r13, %rbx
jne 0x126cdb
movq %r15, %rbp
movq %rbx, %r13
jmp 0x126cf4
movq %r15, %rbp
incq %r15
movq (%rsi), %rax
cmpq %rax, %r15
jbe 0x126c20
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x107c8b(%rip), %rdi    # 0x22e9a4
leaq 0x107cd3(%rip), %rsi    # 0x22e9f3
leaq 0x107cd7(%rip), %rcx    # 0x22e9fe
movl $0x50b, %edx            # imm = 0x50B
callq 0x2110
leaq 0x107d3c(%rip), %rdi    # 0x22ea74
leaq 0x107cb4(%rip), %rsi    # 0x22e9f3
leaq 0x107cb8(%rip), %rcx    # 0x22e9fe
movl $0x567, %edx            # imm = 0x567
callq 0x2110