// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gen_microsatellite(char const*, unsigned int, ma_utg_t*, hpc_t*, unsigned int)
void gen_microsatellite(const char *in, uint32_t in_len, ma_utg_t *idx, hpc_t *res, uint32_t mcs_len)
{
	uint32_t k, i, l, c, o; char sk[256]; 
	idx->start = idx->end = res->n; 
	if(mcs_len > 256) mcs_len = 256;
	for (k = 1; k <= mcs_len; k++) {
		memset(sk, 'N', k); ///k->length of k-mer
		o = k + (k>>1) + 1;
		for (i = l = 0; i < in_len; i++) {
			c = seq_nt4_table[(uint8_t)in[i]]; 
			if((c < 4) && (((l >= k) && (sk[l%k] == in[i])) || (l < k))) {
				if(l < k) sk[l] = in[i]; 
				l++;
			} else {
				if(l >= o) {
					kv_push(uint64_t, *res, (((uint64_t)(i-l))<<1));
					kv_push(uint64_t, *res, ((((uint64_t)i)<<1)|1));
				}
				l = 0;
			}
		}
	}

	radix_sort_gfa64(res->a+idx->start, res->a+res->n);
	int64_t dp, old_dp; uint64_t st, en;
	for (k = st = idx->start, dp = 0; k < res->n; k++) {
		old_dp = dp;
        if (res->a[k]&1) --dp;//qe
        else ++dp;
		if (old_dp < 1 && dp >= 1) {///qs
            st = res->a[k]>>1;
        } else if (old_dp >= 1 && dp < 1) {///qe
            en = res->a[k]>>1;
			res->a[idx->end++] = ((st<<32)|(en));
        }
	}
	res->n = idx->end;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rcx, %rbx
movl 0x8(%rcx), %eax
movl %eax, 0x8(%rdx)
movq %rdx, 0x18(%rsp)
movl %eax, 0x4(%rdx)
movl $0x100, %eax            # imm = 0x100
cmpl %eax, %r8d
cmovbl %r8d, %eax
incl %eax
movq %rax, 0x20(%rsp)
movl %esi, %ebp
pushq $0x1
popq %rdx
movq %rdi, 0x8(%rsp)
cmpq 0x20(%rsp), %rdx
je 0x197f7e
leaq 0x30(%rsp), %rdi
pushq $0x4e
popq %rsi
movq %rdx, 0x10(%rsp)
callq 0x20e0
movq 0x10(%rsp), %r8
leaq 0x53ca6(%rip), %rsi     # 0x1ebb00
movq 0x8(%rsp), %rdi
movl %r8d, %eax
shrl %eax
leaq 0x1(%r8), %rcx
movq %rcx, 0x28(%rsp)
leal (%r8,%rax), %r9d
incl %r9d
xorl %r12d, %r12d
pushq $0x1
popq %r15
xorl %r14d, %r14d
movl %r9d, 0x4(%rsp)
cmpq %r12, %rbp
je 0x197f74
movzbl (%rdi,%r12), %ecx
cmpb $0x3, (%rsi,%rcx)
ja 0x197ebe
movl %r14d, %eax
cmpq %rax, %r8
jbe 0x197ea5
movb %cl, 0x30(%rsp,%rax)
jmp 0x197eb3
movl %r14d, %eax
xorl %edx, %edx
divl %r8d
cmpb %cl, 0x30(%rsp,%rdx)
jne 0x197ebe
incl %r14d
movl %r14d, %r13d
jmp 0x197f65
xorl %r13d, %r13d
cmpl %r9d, %r14d
jb 0x197f65
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x197efe
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %r12d, %edx
subl %r14d, %edx
addq %rdx, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movq %rdx, (%rax,%rcx,8)
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x197f43
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movq 0x8(%rsp), %rdi
leaq 0x53bb1(%rip), %rsi     # 0x1ebb00
movq 0x10(%rsp), %r8
movl 0x4(%rsp), %r9d
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movq %r15, (%rax,%rcx,8)
incq %r12
addq $0x2, %r15
movl %r13d, %r14d
jmp 0x197e83
movq 0x28(%rsp), %rdx
jmp 0x197e31
movq 0x8(%rbx), %rax
movq 0x18(%rbx), %rcx
movq 0x18(%rsp), %r14
movl 0x4(%r14), %edx
leaq (%rcx,%rdx,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x156b0d
movl 0x4(%r14), %eax
xorl %edi, %edi
movq %rax, %rcx
movl %eax, %eax
cmpq %rax, 0x8(%rbx)
jbe 0x19801f
movq 0x18(%rbx), %rsi
movq (%rsi,%rax,8), %r8
movl %r8d, %edx
andl $0x1, %edx
negq %rdx
orq $0x1, %rdx
addq %rdi, %rdx
testq %rdi, %rdi
setle %r9b
testq %rdx, %rdx
setg %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x197fe4
shrq %r8
movq %r8, %rcx
jmp 0x198018
testq %rdi, %rdi
setg %dil
testq %rdx, %rdx
setle %r9b
andb %dil, %r9b
cmpb $0x1, %r9b
jne 0x198018
shrq %r8
movq %rcx, %rdi
shlq $0x20, %rdi
orq %r8, %rdi
movl 0x8(%r14), %r8d
leal 0x1(%r8), %r9d
movl %r9d, 0x8(%r14)
movq %rdi, (%rsi,%r8,8)
incl %eax
movq %rdx, %rdi
jmp 0x197fa5
movl 0x8(%r14), %eax
movq %rax, 0x8(%rbx)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq