// File: /workspace/github_cpp_makefile_star2_O1/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, 0x30(%rsp)
movq %rcx, 0x10(%rsp)
movq 0x190(%rsp), %rcx
movq %rdx, 0x8(%rsp)
movl (%rdx), %eax
movl %eax, 0x4(%rcx)
movl %eax, (%rcx)
movl %esi, 0x28(%rsp)
movl %esi, %eax
movq %rax, 0x18(%rsp)
testl %r8d, %r8d
je 0x1e0e1b
movq %rdi, %r12
movl $0xff, %eax
cmpl %eax, %r8d
cmovbl %r8d, %eax
incl %eax
movq %rax, 0x20(%rsp)
movl $0x1, %r13d
movq %rdi, 0x38(%rsp)
leaq 0x40(%rsp), %rdi
movl $0x4e, %esi
movq %r13, %rdx
callq 0x20e0
leaq 0x4cd47(%rip), %r9      # 0x22da10
movl 0x188(%rsp), %eax
cmpq %rax, %r13
movl 0x30(%rsp), %r10d
cmoval 0x180(%rsp), %r10d
cmpl $0x0, 0x28(%rsp)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r8
je 0x1e0e0d
imull %r13d, %r10d
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebx, %ebx
movzbl (%r12,%r14), %ecx
movb (%r9,%rcx), %bpl
cmpb $0x3, %bpl
ja 0x1e0d49
movl %r15d, %esi
cmpq %rsi, %r13
jbe 0x1e0d33
leal (%r15,%rbx), %eax
xorl %edx, %edx
divl %r13d
movb %cl, 0x40(%rsp,%rdx)
incl %r15d
movl %ebx, %edx
movl %r15d, %ecx
jmp 0x1e0dfc
leal (%r15,%rbx), %eax
xorl %edx, %edx
divl %r13d
cmpb %cl, 0x40(%rsp,%rdx)
jne 0x1e0d49
cmpq %rsi, %r13
ja 0x1e0d19
jmp 0x1e0d26
cmpl %r10d, %r15d
jb 0x1e0dc3
movq 0x8(%rdi), %rax
cmpq %rax, (%rdi)
jne 0x1e0d9c
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rdi)
movq 0x10(%rdi), %rdi
shlq $0x2, %rcx
leaq (%rcx,%rcx,2), %rsi
movl %r10d, %r12d
callq 0x2370
movl %r12d, %r10d
movq 0x38(%rsp), %r12
leaq 0x4cc82(%rip), %r9      # 0x22da10
movq 0x18(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movl %r15d, %eax
negl %eax
addq %r14, %rax
movq (%rdi), %rcx
movq 0x10(%rdi), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
leaq (%rcx,%rcx,2), %rcx
movl %eax, (%rdx,%rcx,4)
movl %r14d, 0x4(%rdx,%rcx,4)
movb %r13b, 0x8(%rdx,%rcx,4)
xorl %edx, %edx
movl $0x0, %ecx
cmpb $0x3, %bpl
ja 0x1e0dfc
movb (%r12,%r14), %cl
leal (%r15,%rbx), %eax
xorl %edx, %edx
divl %r13d
movb %cl, 0x40(%rsp,%rdx)
leal 0x1(%r15), %eax
cmpq %rax, %r13
movl %r15d, %ecx
cmovbl %r13d, %ecx
leal (%r15,%rbx), %eax
incl %eax
subl %ecx, %eax
xorl %edx, %edx
divl %r13d
incq %r14
movl %ecx, %r15d
movl %edx, %ebx
cmpq %r14, %r8
jne 0x1e0d02
incq %r13
cmpq 0x20(%rsp), %r13
jne 0x1e0cb0
movq 0x8(%rsp), %rbx
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x190(%rsp), %r15
movl (%r15), %edx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,4), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,4), %rsi
callq 0x196b9d
movq %rbx, %r8
movl (%r15), %eax
movl %eax, 0x4(%r15)
cmpq %rax, (%rbx)
jbe 0x1e1001
movl %eax, %edx
movl %eax, %ecx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
cmpl (%r15), %edx
jbe 0x1e0e8d
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rsi
movl %ecx, %edi
leaq (%rdi,%rdi,2), %rdi
movl (%rsi,%rdi,4), %edi
leal -0x1(%rdx), %r8d
leaq (%r8,%r8,2), %r8
movl 0x4(%rsi,%r8,4), %r10d
cmpl $-0x1, %edi
je 0x1e0f92
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rbx
leaq (%rax,%rax,2), %rsi
movl (%rbx,%rsi,4), %r8d
cmpl %r8d, %r10d
jbe 0x1e0f92
cmpl %edi, %r8d
jb 0x1e12b1
leaq (%rbx,%rsi,4), %rdi
movl 0x4(%rdi), %r9d
cmpl %r10d, %r9d
ja 0x1e0ef9
cmpl %edx, %ecx
jae 0x1e0ef9
movl %ecx, %r14d
movl %edx, %r11d
leaq (%r14,%r14,2), %r10
leaq (%rbx,%r10,4), %r10
subq %r14, %r11
movl (%r10), %ebx
cmpl %ebx, %r8d
jb 0x1e0ef0
movl 0x4(%r10), %ebp
cmpl %ebp, %r9d
jbe 0x1e0fd2
addq $0xc, %r10
decq %r11
jne 0x1e0edb
movl %ecx, %edi
cmpl %edx, %ecx
jae 0x1e0f5c
movl %ecx, %edi
movl %edx, %edx
leaq (,%rdi,4), %r8
leaq (%r8,%r8,2), %r8
subq %rdi, %rdx
movl %ecx, %edi
movq 0x8(%rsp), %r9
movq 0x10(%r9), %r9
movl (%r9,%rsi,4), %r10d
cmpl (%r9,%r8), %r10d
ja 0x1e0f36
leaq (%r9,%rsi,4), %r10
movl 0x4(%r10), %r10d
cmpl 0x4(%r9,%r8), %r10d
jae 0x1e0f53
leaq (%r9,%r8), %r10
movl %edi, %r11d
incl %edi
leaq (%r11,%r11,2), %r11
movl 0x8(%r10), %ebx
movl %ebx, 0x8(%r9,%r11,4)
movq (%r10), %r10
movq %r10, (%r9,%r11,4)
addq $0xc, %r8
decq %rdx
jne 0x1e0f14
movl %edi, 0x4(%r15)
movq 0x8(%rsp), %r9
movq 0x10(%r9), %rdx
leal 0x1(%rdi), %r8d
movl %r8d, 0x4(%r15)
movl %edi, %edi
leaq (%rdi,%rdi,2), %rdi
movl 0x8(%rdx,%rsi,4), %r8d
movl %r8d, 0x8(%rdx,%rdi,4)
movq %r9, %r8
movq (%rdx,%rsi,4), %rsi
movq %rsi, (%rdx,%rdi,4)
movl 0x4(%r15), %edx
jmp 0x1e0fc5
movq 0x8(%rsp), %r8
movq 0x10(%r8), %rcx
leaq (%rax,%rax,2), %rdx
movl 0x4(%r15), %esi
leal 0x1(%rsi), %edi
movl %edi, 0x4(%r15)
leaq (%rsi,%rsi,2), %rsi
movl 0x8(%rcx,%rdx,4), %edi
movl %edi, 0x8(%rcx,%rsi,4)
movq (%rcx,%rdx,4), %rdx
movq %rdx, (%rcx,%rsi,4)
movl 0x4(%r15), %edx
leal -0x1(%rdx), %ecx
incl %eax
cmpq %rax, (%r8)
ja 0x1e0e5e
jmp 0x1e1001
cmpl %ebx, %r8d
jne 0x1e0ffa
cmpl %ebp, %r9d
movq 0x8(%rsp), %r8
jne 0x1e0fc5
movb 0x8(%rdi), %sil
cmpb 0x8(%r10), %sil
jae 0x1e0fc5
movl 0x8(%rdi), %esi
movl %esi, 0x8(%r10)
movq (%rdi), %rsi
movq %rsi, (%r10)
jmp 0x1e0fc5
movq 0x8(%rsp), %r8
jmp 0x1e0fc5
movl 0x4(%r15), %eax
movq %rax, (%r8)
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq 0x18(%rsp), %rcx
shrq $0x25, %rcx
cmpl $0xb, %ecx
movl $0xa, %edx
cmovael %ecx, %edx
movl (%r15), %ecx
movl %eax, %esi
subl %ecx, %esi
cmpl %edx, %esi
movq 0x10(%rsp), %r9
jbe 0x1e1280
subq %rcx, %rax
cmpq %rax, 0x8(%r9)
jae 0x1e109a
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(%r9)
movq 0x10(%r9), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r9)
movq 0x10(%r8), %r14
movl (%r15), %ecx
movl 0x4(%r15), %eax
leaq (%rcx,%rcx,2), %rbp
movl %eax, 0x38(%rsp)
movq $0x0, (%r9)
movq %rcx, 0x18(%rsp)
subl %ecx, %eax
movl %eax, %r15d
je 0x1e1128
leaq (%r14,%rbp,4), %r12
addq $0x8, %r12
xorl %ebx, %ebx
movl $0x2, %r13d
movq 0x8(%r9), %rax
cmpq %rax, (%r9)
jne 0x1e10fe
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x8(%r9)
movq 0x10(%r9), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r9
movq %rax, 0x10(%r9)
movzbl (%r12), %eax
shlq $0x20, %rax
addq %rbx, %rax
movq (%r9), %rcx
movq 0x10(%r9), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r9)
movq %rax, (%rdx,%rcx,8)
incq %rbx
addq $0xc, %r12
cmpq %rbx, %r15
jne 0x1e10d0
leaq (%r14,%rbp,4), %r12
movq (%r9), %rax
movq 0x10(%r9), %rdi
leaq (%rdi,%rax,8), %rsi
movq %r9, %rbx
callq 0x194fef
movq (%rbx), %rax
movl %eax, %r10d
movq %rax, 0x20(%rsp)
testl %eax, %eax
je 0x1e121c
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdx
leaq (%r14,%rbp,4), %rax
addq $0x4, %rax
movq %rax, 0x28(%rsp)
xorl %edi, %edi
movq %r10, 0x30(%rsp)
movl (%rdx,%rdi,8), %r8d
leaq (%r8,%r8,2), %rax
leaq (%r12,%rax,4), %r10
movl (%r10), %ebp
movl 0x4(%r10), %eax
movl 0x38(%rsp), %ecx
cmpl 0x18(%rsp), %ecx
jne 0x1e1190
movl %eax, %ebx
jmp 0x1e11fa
movq 0x28(%rsp), %r13
xorl %ecx, %ecx
movl %eax, %ebx
movl -0x4(%r13), %esi
cmpl %eax, %esi
jae 0x1e11fa
cmpl %ecx, %r8d
je 0x1e11ee
cmpq $0x0, (%rdx,%rcx,8)
jns 0x1e11ee
cmpl %ebp, %esi
movl %ebp, %r9d
cmoval %esi, %r9d
movl (%r13), %r14d
cmpl %ebx, %r14d
movl %ebx, %r11d
cmovbl %r14d, %r11d
cmpl %r9d, %r11d
jbe 0x1e11ee
cmpl %esi, %ebp
jae 0x1e11d6
cmpl %r14d, %ebx
ja 0x1e1292
cmpl %esi, %ebp
cmovael %r11d, %ebp
cmpl %r14d, %ebx
cmovbel %r9d, %ebx
movzbl 0x8(%r10), %esi
addl %ebp, %esi
cmpl %esi, %ebx
jb 0x1e11fa
incq %rcx
addq $0xc, %r13
cmpl %ecx, %r15d
jne 0x1e1199
movzbl 0x8(%r10), %eax
addl %ebp, %eax
cmpl %eax, %ebx
jb 0x1e120b
orb $-0x80, 0x7(%rdx,%r8,8)
incq %rdi
movq 0x30(%rsp), %r10
cmpq %r10, %rdi
jne 0x1e116f
movq 0x190(%rsp), %r9
movl (%r9), %eax
movl %eax, 0x4(%r9)
cmpl $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r8
je 0x1e1279
xorl %ecx, %ecx
movq 0x10(%r8), %rax
cmpq $0x0, (%rax,%rcx,8)
jns 0x1e126d
movq 0x10(%rdi), %rax
movl 0x4(%r9), %edx
leal 0x1(%rdx), %esi
movl %esi, 0x4(%r9)
leaq (%rdx,%rdx,2), %rdx
movl 0x8(%r12), %esi
movl %esi, 0x8(%rax,%rdx,4)
movq (%r12), %rsi
movq %rsi, (%rax,%rdx,4)
incq %rcx
addq $0xc, %r12
cmpq %rcx, %r10
jne 0x1e123e
movl 0x4(%r9), %eax
movq %rax, (%rdi)
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x57852(%rip), %rdi     # 0x238aeb
leaq 0x526eb(%rip), %rsi     # 0x23398b
leaq 0x577c4(%rip), %rcx     # 0x238a6b
movl $0x58c6, %edx           # imm = 0x58C6
callq 0x2110
leaq 0x577a1(%rip), %rdi     # 0x238a59
leaq 0x526cc(%rip), %rsi     # 0x23398b
leaq 0x577a5(%rip), %rcx     # 0x238a6b
movl $0x589c, %edx           # imm = 0x589C
callq 0x2110