// File: /workspace/github_cpp_makefile_star2_O2/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 $0x138, %rsp            # imm = 0x138
movl %r9d, 0x24(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %r9
movq %rdi, %r12
movq 0x180(%rsp), %rcx
movl (%rdx), %eax
movl %eax, 0x4(%rcx)
movl $0xff, %edx
cmpl %edx, %r8d
cmovbl %r8d, %edx
movl %eax, (%rcx)
incl %edx
movq %rdx, 0x28(%rsp)
movl %esi, 0x20(%rsp)
movl %esi, %edi
pushq $0x1
popq %r13
movq %r9, (%rsp)
movq %rdi, 0x8(%rsp)
movq %r12, 0x18(%rsp)
cmpq 0x28(%rsp), %r13
je 0x1a078a
leaq 0x30(%rsp), %rdi
pushq $0x4e
popq %rsi
movq %r13, %rdx
callq 0x20e0
leaq 0x4b49a(%rip), %r8      # 0x1ebb00
movq 0x8(%rsp), %rdi
movq (%rsp), %r9
movl 0x178(%rsp), %eax
cmpq %rax, %r13
movl 0x24(%rsp), %r10d
cmoval 0x170(%rsp), %r10d
imull %r13d, %r10d
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq %r14, %rdi
je 0x1a0782
movzbl (%r12,%r14), %ecx
movb (%r8,%rcx), %r15b
cmpb $0x3, %r15b
ja 0x1a06d0
movl %ebp, %esi
leal (%rbx,%rbp), %eax
xorl %edx, %edx
divl %r13d
cmpq %rsi, %r13
jbe 0x1a06bf
movb %cl, 0x30(%rsp,%rdx)
jmp 0x1a06c5
cmpb %cl, 0x30(%rsp,%rdx)
jne 0x1a06d0
incl %ebp
movl %ebx, %edx
movl %ebp, %ecx
jmp 0x1a0776
cmpl %r10d, %ebp
jb 0x1a0741
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x1a0722
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r9)
imulq $0xc, %rdx, %rsi
movq %rax, %rdi
movl %r10d, %r12d
callq 0x2370
movl %r12d, %r10d
movq 0x18(%rsp), %r12
leaq 0x4b3ee(%rip), %r8      # 0x1ebb00
movq 0x8(%rsp), %rdi
movq (%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
movl %ebp, %edx
negl %edx
addq %r14, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r9)
imulq $0xc, %rcx, %rcx
movl %edx, (%rax,%rcx)
movl %r14d, 0x4(%rax,%rcx)
movb %r13b, 0x8(%rax,%rcx)
xorl %edx, %edx
movl $0x0, %ecx
cmpb $0x3, %r15b
ja 0x1a0776
movb (%r12,%r14), %cl
leal (%rbx,%rbp), %eax
xorl %edx, %edx
divl %r13d
movb %cl, 0x30(%rsp,%rdx)
leal 0x1(%rbp), %eax
cmpq %rax, %r13
movl %ebp, %ecx
cmovbl %r13d, %ecx
leal (%rbx,%rbp), %eax
incl %eax
subl %ecx, %eax
xorl %edx, %edx
divl %r13d
incq %r14
movl %edx, %ebx
movl %ecx, %ebp
jmp 0x1a0692
incq %r13
jmp 0x1a0644
movq 0x10(%r9), %rax
movq 0x180(%rsp), %rbx
movl (%rbx), %ecx
imulq $0xc, %rcx, %rdi
addq %rax, %rdi
imulq $0xc, (%r9), %rsi
addq %rax, %rsi
callq 0x15859f
movq (%rsp), %r11
movl (%rbx), %eax
movl %eax, 0x4(%rbx)
movl %eax, %ecx
movl %eax, %edx
movl %eax, %eax
cmpq %rax, (%r11)
jbe 0x1a091e
movq 0x10(%r11), %r9
movq 0x180(%rsp), %rsi
cmpl (%rsi), %edx
jbe 0x1a0870
movl %ecx, %esi
imulq $0xc, %rsi, %rdi
movl (%r9,%rdi), %r10d
cmpl $-0x1, %r10d
je 0x1a0870
leal -0x1(%rdx), %r8d
imulq $0xc, %r8, %r8
movl 0x4(%r9,%r8), %r14d
imulq $0xc, %rax, %r8
movl (%r9,%r8), %ebx
cmpl %ebx, %r14d
jbe 0x1a0870
cmpl %r10d, %ebx
jb 0x1a0bc1
leaq (%r9,%r8), %r11
movl 0x4(%r11), %ebp
movl %edx, %r10d
cmpl %r14d, %ebp
jbe 0x1a08d1
movl %ecx, %edx
movq (%rsp), %r9
movq 0x10(%r9), %r9
leaq (%r9,%r8), %rbx
movl %edx, %r11d
cmpq %r10, %rsi
jae 0x1a08a8
movl (%rbx), %ebp
cmpl (%r9,%rdi), %ebp
ja 0x1a084a
movl 0x4(%rbx), %ebx
cmpl 0x4(%r9,%rdi), %ebx
jae 0x1a0863
leaq (%r9,%rdi), %rbx
incl %edx
imulq $0xc, %r11, %r11
movl 0x8(%rbx), %ebp
movl %ebp, 0x8(%r9,%r11)
movq (%rbx), %rbx
movq %rbx, (%r9,%r11)
movq (%rsp), %r9
incq %rsi
addq $0xc, %rdi
jmp 0x1a0828
imulq $0xc, %rax, %rcx
movq 0x180(%rsp), %rdi
movl 0x4(%rdi), %edx
leal 0x1(%rdx), %esi
movl %esi, 0x4(%rdi)
imulq $0xc, %rdx, %rdx
movl 0x8(%r9,%rcx), %esi
movl %esi, 0x8(%r9,%rdx)
movq (%r9,%rcx), %rcx
movq %rcx, (%r9,%rdx)
movl 0x4(%rdi), %edx
leal -0x1(%rdx), %ecx
incl %eax
jmp 0x1a07b8
incl %edx
movq 0x180(%rsp), %rdi
movl %edx, 0x4(%rdi)
imulq $0xc, %r11, %rdx
movl 0x8(%rbx), %esi
movl %esi, 0x8(%r9,%rdx)
movq (%rbx), %rsi
movq %rsi, (%r9,%rdx)
movl 0x4(%rdi), %edx
movq (%rsp), %r11
jmp 0x1a08a1
addq %rdi, %r9
movq %rsi, %r14
cmpq %r10, %r14
jae 0x1a0822
movl (%r9), %r15d
cmpl %r15d, %ebx
jb 0x1a08f1
movl 0x4(%r9), %r12d
cmpl %r12d, %ebp
jbe 0x1a08fa
incq %r14
addq $0xc, %r9
jmp 0x1a08d7
cmpl %r15d, %ebx
jne 0x1a08cb
cmpl %r12d, %ebp
jne 0x1a08cb
movb 0x8(%r11), %sil
cmpb 0x8(%r9), %sil
jae 0x1a08cb
movl 0x8(%r11), %esi
movl %esi, 0x8(%r9)
movq (%r11), %rsi
movq %rsi, (%r9)
jmp 0x1a08cb
movq 0x180(%rsp), %rdi
movl 0x4(%rdi), %ecx
movq %rcx, (%r11)
pushq $0x30
popq %rsi
movl 0x20(%rsp), %eax
xorl %edx, %edx
divl %esi
cmpl $0xb, %eax
pushq $0xa
popq %rsi
cmovael %eax, %esi
movl (%rdi), %edx
movl %ecx, %eax
subl %edx, %eax
cmpl %esi, %eax
jbe 0x1a0b90
subq %rdx, %rcx
movq 0x10(%rsp), %rbx
cmpq %rcx, 0x8(%rbx)
jae 0x1a09c5
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq 0x10(%rsp), %rbx
movq (%rsp), %r11
movq %rax, 0x10(%rbx)
movq 0x180(%rsp), %rax
movl (%rax), %edx
movl 0x4(%rax), %eax
subl %edx, %eax
movq 0x10(%r11), %rcx
imulq $0xc, %rdx, %r14
andq $0x0, (%rbx)
leaq (%rcx,%r14), %r15
movl %eax, %r12d
movq %rcx, 0x8(%rsp)
leaq (%rcx,%r14), %r13
addq $0x8, %r13
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x1a0a3e
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x1a0a1e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movzbl (%r13), %edx
shlq $0x20, %rdx
addq %rbp, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
incq %rbp
addq $0xc, %r13
jmp 0x1a09e7
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
movq 0x10(%rsp), %rsi
movq (%rsp), %rdi
movl (%rsi), %r9d
movq 0x8(%rsp), %rax
addq %r14, %rax
addq $0x4, %rax
movq %rax, 0x8(%rsp)
xorl %edx, %edx
movq %r9, 0x18(%rsp)
cmpq %r9, %rdx
je 0x1a0b2b
movq 0x10(%rsi), %rsi
movl (%rsi,%rdx,8), %edi
imulq $0xc, %rdi, %rax
leaq (%r15,%rax), %r9
movl (%r15,%rax), %r8d
movl 0x4(%r15,%rax), %eax
movq 0x8(%rsp), %rbx
xorl %r14d, %r14d
movl %eax, %r11d
cmpq %r14, %r12
je 0x1a0b03
movl -0x4(%rbx), %ebp
cmpl %eax, %ebp
jae 0x1a0b03
cmpq %r14, %rdi
je 0x1a0afa
cmpq $0x0, (%rsi,%r14,8)
jns 0x1a0afa
cmpl %r8d, %ebp
movl %r8d, %r13d
cmoval %ebp, %r13d
movl (%rbx), %ecx
cmpl %r11d, %ecx
movl %r11d, %r10d
cmovbl %ecx, %r10d
cmpl %r13d, %r10d
jbe 0x1a0afa
cmpl %ebp, %r8d
jae 0x1a0adf
cmpl %ecx, %r11d
ja 0x1a0ba2
cmpl %ebp, %r8d
cmovael %r10d, %r8d
cmpl %ecx, %r11d
cmovbel %r13d, %r11d
movzbl 0x8(%r9), %ecx
addl %r8d, %ecx
cmpl %ecx, %r11d
jb 0x1a0b03
incq %r14
addq $0xc, %rbx
jmp 0x1a0a9e
movzbl 0x8(%r9), %eax
addl %r8d, %eax
cmpl %eax, %r11d
jb 0x1a0b15
orb $-0x80, 0x7(%rsi,%rdi,8)
incq %rdx
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r9
jmp 0x1a0a72
movq 0x180(%rsp), %rcx
movl (%rcx), %eax
movl %eax, 0x4(%rcx)
xorl %ecx, %ecx
cmpq %rcx, %r9
je 0x1a0b82
movq 0x10(%rsi), %rax
cmpq $0x0, (%rax,%rcx,8)
jns 0x1a0b79
movq 0x10(%rdi), %rax
movq 0x180(%rsp), %r8
movl 0x4(%r8), %edx
leal 0x1(%rdx), %esi
movl %esi, 0x4(%r8)
imulq $0xc, %rdx, %rdx
movl 0x8(%r15), %esi
movl %esi, 0x8(%rax,%rdx)
movq (%r15), %rsi
movq %rsi, (%rax,%rdx)
movq 0x10(%rsp), %rsi
incq %rcx
addq $0xc, %r15
jmp 0x1a0b3a
movq 0x180(%rsp), %rax
movl 0x4(%rax), %eax
movq %rax, (%rdi)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x560da(%rip), %rdi     # 0x1f6c83
leaq 0x50f73(%rip), %rsi     # 0x1f1b23
leaq 0x5604c(%rip), %rcx     # 0x1f6c03
movl $0x58c6, %edx           # imm = 0x58C6
callq 0x2110
leaq 0x56029(%rip), %rdi     # 0x1f6bf1
leaq 0x50f54(%rip), %rsi     # 0x1f1b23
leaq 0x5602d(%rip), %rcx     # 0x1f6c03
movl $0x589c, %edx           # imm = 0x589C
callq 0x2110