// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: extract_microsatellite(char const*, unsigned int, hpc_re_t*, asg64_v*, unsigned int, unsigned int, unsigned int, unsigned int, hpc_idx_t*)
void extract_microsatellite(const char *in, uint32_t in_len, hpc_re_t *res, asg64_v *buf, uint32_t mcs_len, 
uint32_t min_klen_simple, uint32_t min_klen_complex, uint32_t ksimple_cut, hpc_idx_t *idx)
{
	uint32_t k, i, l, c, o, ls; char sk[256]; hpc_ss_t z, *za; 
	uint32_t m, cutoff, zn, bn, ovlp, os, oe, oks, oke;
	idx->s = idx->e = res->n;
	if(mcs_len > 255) mcs_len = 255;
	for (k = 1; k <= mcs_len; k++) {
		memset(sk, 'N', k); ///k->length of k-mer
		o = ((k<=ksimple_cut)?(k*min_klen_simple):(k*min_klen_complex));
		for (i = l = ls = 0; i < in_len; i++) {
			c = seq_nt4_table[(uint8_t)in[i]]; 
			if((c < 4) && (((l >= k) && (sk[(l+ls)%k] == in[i])) || (l < k))) {
				if(l < k) sk[(l+ls)%k] = in[i]; 
				l++;
			} else {
				if(l >= o) {
					z.s = i-l; z.e = i; z.k = k;
					kv_push(hpc_ss_t, *res, z);
				}

				if(c >= 4) {
					l = ls = 0;
				} else if(l >= 0) {
					sk[(l+ls)%k] = in[i]; l++;
					m = ((l<=k)?(l-1):(k));
					ls = (ls+(l-m))%k; l = m;
				}
			}
		}
	}

	uint32_t ks, ke, fs, fe;
	radix_sort_hpc_ss_t_s(res->a+idx->s, res->a+res->n);
	for (k = ks = ke = idx->e = idx->s; k < res->n; k++) {
		fs = fe = (uint32_t)-1;
		if(ke > idx->s) {
			fs = res->a[ks].s; fe = res->a[ke-1].e;
		}
		///new interval is not overlapped with existing one
		if((fs == ((uint32_t)-1)) || (fe <= res->a[k].s)) {
			res->a[idx->e++] = res->a[k]; ks = idx->e - 1; ke = idx->e;
		} else {
			assert(res->a[k].s >= fs);
			///new interval might be contained in an exist one
			if(res->a[k].s >= fs && res->a[k].e <= fe) {
				for (i = ks; i < ke; i++) {
					if(res->a[k].s >= res->a[i].s && res->a[k].e <= res->a[i].e) {///new interval is contained in an exist one
						if((res->a[k].s == res->a[i].s) && (res->a[k].e == res->a[i].e) 
															&& (res->a[k].k < res->a[i].k)) {
							res->a[i] = res->a[k];
						}
						break;
					}
				}
				if(i < ke) continue;
			}

			for (i = m = ks; i < ke; i++) {
				///exist interval is contained in new interval
				if(res->a[k].s <= res->a[i].s && res->a[k].e >= res->a[i].e) continue;
				res->a[m++] = res->a[i];
			}
			ke = idx->e = m;
			res->a[idx->e++] = res->a[k]; ke = idx->e;
		}
	}
	res->n = idx->e;
	cutoff = (in_len/sizeof((*(res->a))))/4; if(cutoff < 10) cutoff = 10;
	if(idx->e-idx->s > cutoff) {///compress if there are too many intervals
		kv_resize(uint64_t, *buf, res->n-idx->s);
		za = res->a + idx->s; zn = idx->e - idx->s;
		for (k = buf->n = 0; k < zn; k++) {
			kv_push(uint64_t, *buf, ((((uint64_t)za[k].k)<<32)|((uint64_t)(k))));
		}
		radix_sort_gfa64(buf->a, buf->a+buf->n);///sorted by the k-mer length
		for (i = 0, bn = buf->n; i < bn; i++) {
			k = (uint32_t)buf->a[i]; oks = za[k].s; oke = za[k].e;
			for (m = 0; m < zn && za[m].s < za[k].e; m++) {
				if(!(buf->a[m]&((uint64_t)0x8000000000000000))) continue;
				if(m == k) continue;
				///now has overlap
				os = MAX(za[m].s, oks); oe = MIN(za[m].e, oke);
				ovlp = ((oe > os)?(oe - os):(0));
				if(!ovlp) continue;
				assert(os == oks || oe == oke);
				if(os == oks) oks = oe;
				if(oe == oke) oke = os;
				if(oke < oks + za[k].k) break;
			}
			if(oke >= oks + za[k].k) {
				buf->a[k] |= ((uint64_t)0x8000000000000000);
			}
		}
		for (i = 0, idx->e = idx->s; i < bn; i++) {
			if(!(buf->a[i]&((uint64_t)0x8000000000000000))) continue;
			res->a[idx->e++] = za[i];
		}
		res->n = idx->e;
	}
	// for (i = idx->s; i < idx->e; i++) {
	// 	fprintf(stderr, "k::%u\tin::[%u,\t%u)\n", res->a[i].k, res->a[i].s, res->a[i].e);
	// }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movl %r9d, 0x1c(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdi, %r12
movq %rdx, %rdi
movq 0x190(%rsp), %r14
movq (%rdx), %rax
movl %eax, 0x4(%r14)
movl %eax, (%r14)
movl %esi, 0x30(%rsp)
movl %esi, %ecx
movq %rcx, 0x8(%rsp)
movl %eax, %ecx
testl %r8d, %r8d
movq %rdx, 0x10(%rsp)
je 0x1eb912
movl $0xff, %eax
cmpl %eax, %r8d
cmovbl %r8d, %eax
incl %eax
movq %rax, 0x38(%rsp)
movl $0x1, %r13d
movq %r12, 0x28(%rsp)
leaq 0x40(%rsp), %rdi
movl $0x4e, %esi
movq %r13, %rdx
callq 0x20e0
leaq 0x4e45a(%rip), %r9      # 0x239c20
movq 0x10(%rsp), %rdi
movl 0x188(%rsp), %eax
cmpq %rax, %r13
movl 0x1c(%rsp), %r10d
cmoval 0x180(%rsp), %r10d
cmpl $0x0, 0x30(%rsp)
movq 0x8(%rsp), %r8
je 0x1eb8f6
imull %r13d, %r10d
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %r15d, %r15d
movzbl (%r12,%r14), %ecx
movb (%r9,%rcx), %bpl
cmpb $0x3, %bpl
ja 0x1eb836
movl %ebx, %esi
leal (%rbx,%r15), %eax
xorl %edx, %edx
divl %r13d
cmpq %rsi, %r13
jbe 0x1eb824
movb %cl, 0x40(%rsp,%rdx)
jmp 0x1eb82a
cmpb %cl, 0x40(%rsp,%rdx)
jne 0x1eb836
incl %ebx
movl %r15d, %edx
movl %ebx, %ecx
jmp 0x1eb8e5
cmpl %r10d, %ebx
jb 0x1eb8ae
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x1eb88f
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rdi)
shlq $0x2, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
movl %r10d, %r12d
callq 0x2370
movl %r12d, %r10d
movq 0x28(%rsp), %r12
leaq 0x4e3a2(%rip), %r9      # 0x239c20
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movl %ebx, %edx
negl %edx
addq %r14, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
leaq (%rcx,%rcx,2), %rcx
movl %edx, (%rax,%rcx,4)
movl %r14d, 0x4(%rax,%rcx,4)
movb %r13b, 0x8(%rax,%rcx,4)
xorl %edx, %edx
movl $0x0, %ecx
cmpb $0x3, %bpl
ja 0x1eb8e5
movb (%r12,%r14), %cl
leal (%rbx,%r15), %eax
xorl %edx, %edx
divl %r13d
movb %cl, 0x40(%rsp,%rdx)
leal 0x1(%rbx), %eax
cmpq %rax, %r13
movl %ebx, %ecx
cmovbl %r13d, %ecx
leal (%rbx,%r15), %eax
incl %eax
subl %ecx, %eax
xorl %edx, %edx
divl %r13d
incq %r14
movl %ecx, %ebx
movl %edx, %r15d
cmpq %r14, %r8
jne 0x1eb7ff
incq %r13
cmpq 0x38(%rsp), %r13
jne 0x1eb7ad
movq 0x190(%rsp), %r14
movl (%r14), %ecx
movq (%rdi), %rax
movq 0x10(%rdi), %rdx
movl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,4), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,4), %rsi
callq 0x19f928
movq 0x10(%rsp), %rdi
movl (%r14), %eax
movl %eax, 0x4(%r14)
cmpq %rax, (%rdi)
jbe 0x1ebaf3
movl %eax, %edx
movl %eax, %ecx
movq 0x10(%rdi), %rdi
cmpl (%r14), %edx
jbe 0x1eba3b
movl %ecx, %r8d
leaq (%r8,%r8,2), %r11
movl (%rdi,%r11,4), %r9d
cmpl $-0x1, %r9d
je 0x1eba3b
leal -0x1(%rdx), %esi
leaq (%rsi,%rsi,2), %rsi
movl 0x4(%rdi,%rsi,4), %ebp
leaq (%rax,%rax,2), %rsi
movl (%rdi,%rsi,4), %r10d
cmpl %r10d, %ebp
jbe 0x1eba3b
cmpl %r9d, %r10d
jb 0x1ebdec
leaq (%rdi,%rsi,4), %r9
movl 0x4(%r9), %ebx
cmpl %ebp, %ebx
ja 0x1eb9c2
cmpl %edx, %ecx
jae 0x1eb9c2
leaq (%rdi,%r11,4), %r11
movl %edx, %ebp
movl (%r11), %r14d
cmpl %r14d, %r10d
jb 0x1eb9b8
movl 0x4(%r11), %r15d
cmpl %r15d, %ebx
jbe 0x1ebaab
addq $0xc, %r11
decl %ebp
cmpl %ebp, %ecx
jne 0x1eb9a3
cmpl %edx, %ecx
jae 0x1eba7f
movl %edx, %edi
leaq (,%r8,4), %rdx
leaq (%rdx,%rdx,2), %r9
subq %r8, %rdi
movl %ecx, %edx
movq 0x190(%rsp), %r14
movq 0x10(%rsp), %r8
movq 0x10(%r8), %r8
movl (%r8,%rsi,4), %r10d
cmpl (%r8,%r9), %r10d
ja 0x1eba07
leaq (%r8,%rsi,4), %r10
movl 0x4(%r10), %r10d
cmpl 0x4(%r8,%r9), %r10d
jae 0x1eba24
leaq (%r8,%r9), %r10
movl %edx, %r11d
incl %edx
leaq (%r11,%r11,2), %r11
movl 0x8(%r10), %ebx
movl %ebx, 0x8(%r8,%r11,4)
movq (%r10), %r10
movq %r10, (%r8,%r11,4)
addq $0xc, %r9
decq %rdi
jne 0x1eb9e5
movq 0x10(%rsp), %rdi
movq 0x10(%rdi), %rdi
movl %edx, %r8d
jmp 0x1eba89
leaq (%rax,%rax,2), %rcx
movl 0x4(%r14), %edx
leal 0x1(%rdx), %esi
movl %esi, 0x4(%r14)
leaq (%rdx,%rdx,2), %rdx
movl 0x8(%rdi,%rcx,4), %esi
movl %esi, 0x8(%rdi,%rdx,4)
movq (%rdi,%rcx,4), %rcx
movq %rcx, (%rdi,%rdx,4)
movl 0x4(%r14), %edx
leal -0x1(%rdx), %ecx
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r8
incl %eax
cmpq %rax, (%rdi)
ja 0x1eb946
jmp 0x1ebaff
movl %ecx, %edx
movq 0x190(%rsp), %r14
incl %edx
movl %edx, 0x4(%r14)
leaq (%r8,%r8,2), %rdx
movl 0x8(%rdi,%rsi,4), %r8d
movl %r8d, 0x8(%rdi,%rdx,4)
movq (%rdi,%rsi,4), %rsi
movq %rsi, (%rdi,%rdx,4)
movl 0x4(%r14), %edx
jmp 0x1eba65
cmpl %r14d, %r10d
jne 0x1ebae1
cmpl %r15d, %ebx
movq 0x10(%rsp), %rdi
movq 0x190(%rsp), %r14
movq 0x8(%rsp), %r8
jne 0x1eba6f
movb 0x8(%r9), %sil
cmpb 0x8(%r11), %sil
jae 0x1eba6f
movl 0x8(%r9), %esi
movl %esi, 0x8(%r11)
movq (%r9), %rsi
movq %rsi, (%r11)
jmp 0x1eba6f
movq 0x10(%rsp), %rdi
movq 0x190(%rsp), %r14
jmp 0x1eba6a
movl %eax, %r15d
movl %eax, %ebp
movq 0x8(%rsp), %r8
jmp 0x1ebb08
movl (%r14), %r15d
movl 0x4(%r14), %eax
movl %eax, %ebp
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %r8, %rcx
shrq $0x25, %rcx
cmpl $0xb, %ecx
movl $0xa, %edx
cmovael %ecx, %edx
movq %rax, (%rdi)
movl %ebp, %ecx
subl %r15d, %ecx
cmpl %edx, %ecx
movq 0x20(%rsp), %rsi
jbe 0x1ebdbb
movl %r15d, %edx
subq %rdx, %rax
cmpq %rax, 0x8(%rsi)
jae 0x1ebbaa
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rsi)
movq 0x10(%rsi), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rsi)
movl (%r14), %edx
movl 0x4(%r14), %ebp
movl %ebp, %ecx
subl %edx, %ecx
movl %edx, %r15d
movq 0x10(%rdi), %r12
leaq (%rdx,%rdx,2), %r13
movq $0x0, (%rsi)
movl %ecx, %r14d
cmpl %r15d, %ebp
jne 0x1ebbcf
movq 0x10(%rsi), %rdi
xorl %eax, %eax
movq %rsi, %rbx
jmp 0x1ebc55
movq 0x10(%rsi), %rdi
movq %r12, 0x8(%rsp)
movq %r13, 0x28(%rsp)
leaq (%r12,%r13,4), %r13
addq $0x8, %r13
xorl %ebx, %ebx
movl $0x2, %r12d
movq (%rsi), %rax
cmpq 0x8(%rsi), %rax
jne 0x1ebc22
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq 0x20(%rsp), %rax
movq %rsi, 0x8(%rax)
shlq $0x3, %rsi
callq 0x2370
movq 0x20(%rsp), %rsi
movq %rax, %rdi
movq %rax, 0x10(%rsi)
movq (%rsi), %rax
movzbl (%r13), %ecx
shlq $0x20, %rcx
addq %rbx, %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rsi)
movq %rcx, (%rdi,%rax,8)
incq %rbx
addq $0xc, %r13
cmpq %rbx, %r14
jne 0x1ebbed
movq (%rsi), %rax
movq %rsi, %rbx
movq 0x8(%rsp), %r12
movq 0x28(%rsp), %r13
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
movl (%rbx), %eax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x1ebda4
movl %ebp, 0x28(%rsp)
leaq (%r12,%r13,4), %rcx
movq 0x10(%rbx), %rdx
leaq (%r12,%r13,4), %rax
addq $0x4, %rax
movq %rax, 0x30(%rsp)
xorl %edi, %edi
movl %r15d, 0x1c(%rsp)
movl (%rdx,%rdi,8), %r8d
leaq (%r8,%r8,2), %rax
leaq (%rcx,%rax,4), %r10
movl (%r10), %ebp
movl 0x4(%r10), %eax
cmpl %r15d, 0x28(%rsp)
jne 0x1ebcac
movl %eax, %ebx
jmp 0x1ebd18
movq 0x30(%rsp), %r12
xorl %r13d, %r13d
movl %eax, %ebx
movl -0x4(%r12), %esi
cmpl %eax, %esi
jae 0x1ebd18
cmpq %r13, %r8
je 0x1ebd0c
cmpq $0x0, (%rdx,%r13,8)
jns 0x1ebd0c
cmpl %ebp, %esi
movl %ebp, %r9d
cmoval %esi, %r9d
movl (%r12), %r15d
cmpl %ebx, %r15d
movl %ebx, %r11d
cmovbl %r15d, %r11d
cmpl %r9d, %r11d
jbe 0x1ebd0c
cmpl %esi, %ebp
jae 0x1ebcf4
cmpl %r15d, %ebx
ja 0x1ebdcd
cmpl %esi, %ebp
cmovael %r11d, %ebp
cmpl %r15d, %ebx
cmovbel %r9d, %ebx
movzbl 0x8(%r10), %esi
addl %ebp, %esi
cmpl %esi, %ebx
jb 0x1ebd18
incq %r13
addq $0xc, %r12
cmpq %r13, %r14
jne 0x1ebcb6
movzbl 0x8(%r10), %eax
addl %ebp, %eax
cmpl %eax, %ebx
jb 0x1ebd29
orb $-0x80, 0x7(%rdx,%r8,8)
incq %rdi
cmpq 0x8(%rsp), %rdi
movl 0x1c(%rsp), %r15d
jne 0x1ebc8e
movq 0x190(%rsp), %r8
movl (%r8), %eax
movl %eax, 0x4(%r8)
cmpq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rsi
je 0x1ebdb6
xorl %edx, %edx
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, (%rax,%rdx,8)
jns 0x1ebd90
movq 0x10(%rsi), %rax
movl 0x4(%r8), %esi
leal 0x1(%rsi), %edi
movl %edi, 0x4(%r8)
leaq (%rsi,%rsi,2), %rsi
movl 0x8(%rcx), %edi
movl %edi, 0x8(%rax,%rsi,4)
movq (%rcx), %rdi
movq %rdi, (%rax,%rsi,4)
movq 0x10(%rsp), %rsi
incq %rdx
addq $0xc, %rcx
cmpq %rdx, 0x8(%rsp)
jne 0x1ebd5a
movl 0x4(%r8), %eax
jmp 0x1ebdb6
movq 0x190(%rsp), %rcx
movl (%rcx), %eax
movl %eax, 0x4(%rcx)
movq 0x10(%rsp), %rsi
movl %eax, %eax
movq %rax, (%rsi)
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x58fe7(%rip), %rdi     # 0x244dbb
leaq 0x53e80(%rip), %rsi     # 0x23fc5b
leaq 0x58f59(%rip), %rcx     # 0x244d3b
movl $0x58c6, %edx           # imm = 0x58C6
callq 0x2110
leaq 0x58f36(%rip), %rdi     # 0x244d29
leaq 0x53e61(%rip), %rsi     # 0x23fc5b
leaq 0x58f3a(%rip), %rcx     # 0x244d3b
movl $0x589c, %edx           # imm = 0x589C
callq 0x2110