// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/bwt.c
// Function: bwt_smem1a
int bwt_smem1a(const bwt_t *bwt, int len, const uint8_t *q, int x, int min_intv, uint64_t max_intv, bwtintv_v *mem, bwtintv_v *tmpvec[2])
{
	int i, j, c, ret;
	bwtintv_t ik, ok[VALUE_DOMAIN];
	bwtintv_v a[2], *prev, *curr, *swap;

	mem->n = 0;
	if (q[x] >= VALUE_DEFINED) return x + 1;
	//if (q[x] > 3) return x + 1;
	if (min_intv < 1) min_intv = 1; // the interval size should be at least 1
	kv_init(a[0]); kv_init(a[1]);
	prev = tmpvec && tmpvec[0]? tmpvec[0] : &a[0]; // use the temporary vector if provided
	curr = tmpvec && tmpvec[1]? tmpvec[1] : &a[1];

	bwt_set_intv(bwt, q[x], ik); // the initial interval of a single base

	ik.info = x + 1;

	for (i = x + 1, curr->n = 0; i < len; ++i) { // forward search
		if (ik.x[2] < max_intv) { // an interval small enough
			kv_push(bwtintv_t, *curr, ik);
			break;
		} else if (q[i] < VALUE_DEFINED) { // an amino acid
			c = VALUE_DEFINED - 1 - q[i]; // complement of q[i]
			bwt_extend(bwt, &ik, ok, 0);

			if (ok[c].x[2] != ik.x[2]) { // change of the interval size
				kv_push(bwtintv_t, *curr, ik);
				if (ok[c].x[2] < min_intv) break; // the interval size is too small to be extended further
			}
			ik = ok[c]; ik.info = i + 1;
		} else { // an ambiguous base
			kv_push(bwtintv_t, *curr, ik);
			break; // always terminate extension at an ambiguous base; in this case, i<len always stands
		}
	}

	if (i == len) kv_push(bwtintv_t, *curr, ik); // push the last interval if we reach the end
	bwt_reverse_intvs(curr); // s.t. smaller intervals (i.e. longer matches) visited first
	ret = curr->a[0].info; // this will be the returned value
	swap = curr; curr = prev; prev = swap;

	for (i = x - 1; i >= -1; --i) { // backward search for MEMs
		c = i < 0? -1 : q[i] < VALUE_DEFINED? q[i] : -1; // c==-1 if i<0 or q[i] is an ambiguous base
		//c = i < 0? -1 : q[i] < 4? q[i] : -1; // c==-1 if i<0 or q[i] is an ambiguous base
		for (j = 0, curr->n = 0; j < prev->n; ++j) {
			bwtintv_t *p = &prev->a[j];
			if (c >= 0 && ik.x[2] >= max_intv) bwt_extend(bwt, p, ok, 1);
			if (c < 0 || ik.x[2] < max_intv || ok[c].x[2] < min_intv) { // keep the hit if reaching the beginning or an ambiguous base or the intv is small enough
				if (curr->n == 0) { // test curr->n>0 to make sure there are no longer matches
					if (mem->n == 0 || i + 1 < mem->a[mem->n-1].info>>32) { // skip contained matches
						ik = *p; ik.info |= (uint64_t)(i + 1)<<32;
						kv_push(bwtintv_t, *mem, ik);
					}
				} // otherwise the match is contained in another longer match
			} else if (curr->n == 0 || ok[c].x[2] != curr->a[curr->n-1].x[2]) {
				ok[c].info = p->info;
				kv_push(bwtintv_t, *curr, ok[c]);
			}
		}
		if (curr->n == 0) break;
		swap = curr; curr = prev; prev = swap;
	}

	bwt_reverse_intvs(mem); // s.t. sorted by the start coordinate

	if (tmpvec == 0 || tmpvec[0] == 0) free(a[0].a);
	if (tmpvec == 0 || tmpvec[1] == 0) free(a[1].a);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
movq %r9, 0x70(%rsp)
movl %esi, 0xc(%rsp)
movq %rdi, 0x50(%rsp)
movq 0x4f0(%rsp), %rax
movq $0x0, (%rax)
movslq %ecx, %rax
movq %rdx, 0x60(%rsp)
movzbl (%rdx,%rax), %r9d
cmpq $0x16, %r9
jb 0xf7ff
incl %ecx
movl %ecx, %eax
jmp 0xfda8
movq 0x4f8(%rsp), %rsi
cmpl $0x2, %r8d
movl $0x1, %eax
cmovgel %r8d, %eax
leaq 0x98(%rsp), %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x8(%r13)
movaps %xmm0, -0x8(%r13)
movaps %xmm0, -0x18(%r13)
testq %rsi, %rsi
je 0xf857
movq (%rsi), %rdx
testq %rdx, %rdx
leaq 0x80(%rsp), %rdi
cmovneq %rdx, %rdi
movq %rdi, 0x10(%rsp)
movq 0x8(%rsi), %rdx
testq %rdx, %rdx
cmovneq %rdx, %r13
jmp 0xf864
leaq 0x80(%rsp), %rdx
movq %rdx, 0x10(%rsp)
movq 0x50(%rsp), %rdi
movq 0x8(%rdi,%r9,8), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x20(%rsp)
movq 0x10(%rdi,%r9,8), %rsi
subq %rdx, %rsi
movq %rsi, 0x30(%rsp)
movl $0x15, %edx
subl %r9d, %edx
shll $0x3, %edx
movq 0x8(%rdi,%rdx), %rdx
incq %rdx
movq %rdx, 0x28(%rsp)
movq %rcx, 0x48(%rsp)
leal 0x1(%rcx), %r14d
movslq %r14d, %rbx
movq %rbx, 0x38(%rsp)
movq $0x0, (%r13)
movl %eax, %eax
movq %rax, 0x68(%rsp)
cmpl 0xc(%rsp), %ebx
jge 0xfa92
incq %rbx
movq 0x30(%rsp), %r15
cmpq 0x70(%rsp), %r15
jb 0xf9c9
movq 0x60(%rsp), %rax
movzbl -0x1(%rax,%rbx), %eax
cmpq $0x15, %rax
ja 0xfa2a
movl $0x15, %r12d
subl %eax, %r12d
movq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
xorl %ecx, %ecx
callq 0xf6ba
shll $0x5, %r12d
movq 0xc0(%rsp,%r12), %rbp
cmpq %r15, %rbp
je 0xf993
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
jne 0xf969
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x5, %rsi
movq %rax, %rdi
leaq 0x188a2(%rip), %rdx     # 0x281f2
movl $0x164, %ecx            # imm = 0x164
leaq 0x1889c(%rip), %r8      # 0x281f8
callq 0x2324c
movq %rax, 0x10(%r13)
movq (%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
shlq $0x5, %rcx
movups 0x20(%rsp), %xmm0
movups 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
cmpq 0x68(%rsp), %rbp
jb 0xfa92
leaq (%rsp,%r12), %rax
addq $0xb0, %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x30(%rsp)
movups (%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
movq %rbx, 0x38(%rsp)
incl %r14d
incq %rbx
cmpl %r14d, 0xc(%rsp)
jne 0xf8c9
jmp 0xfa99
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
jne 0xfa12
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x5, %rsi
leaq 0x187fc(%rip), %rdx     # 0x281f2
leaq 0x187fb(%rip), %r8      # 0x281f8
movq %rax, %rdi
movl $0x15d, %ecx            # imm = 0x15D
callq 0x2324c
movq %rax, 0x10(%r13)
movq (%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
shlq $0x5, %rcx
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
jmp 0xfa89
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
jne 0xfa73
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x5, %rsi
leaq 0x1879b(%rip), %rdx     # 0x281f2
leaq 0x1879a(%rip), %r8      # 0x281f8
movq %rax, %rdi
movl $0x169, %ecx            # imm = 0x169
callq 0x2324c
movq %rax, 0x10(%r13)
movq (%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
shlq $0x5, %rcx
movups 0x20(%rsp), %xmm0
movups 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
cmpl 0xc(%rsp), %r14d
jne 0xfb04
movq (%r13), %rcx
cmpq 0x8(%r13), %rcx
jne 0xfae1
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
shlq $0x5, %rsi
leaq 0x1872c(%rip), %rdx     # 0x281f2
leaq 0x1872b(%rip), %r8      # 0x281f8
movl $0x16e, %ecx            # imm = 0x16E
callq 0x2324c
movq %rax, 0x10(%r13)
movq (%r13), %rcx
jmp 0xfae5
movq 0x10(%r13), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
shlq $0x5, %rcx
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movq %r13, %rdi
callq 0xfdba
movq 0x10(%r13), %rax
movl 0x18(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %rdx
leal -0x1(%rdx), %esi
testl %edx, %edx
js 0xfd54
movq %r13, %rbp
je 0xfb4d
movl %esi, %eax
movq 0x60(%rsp), %rcx
movzbl (%rcx,%rax), %ecx
cmpl $0x16, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovael %eax, %ecx
movl %ecx, 0xc(%rsp)
jmp 0xfb55
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq $0x0, (%rdi)
cmpq $0x0, (%rbp)
je 0xfd54
movl %esi, 0x5c(%rsp)
movl 0xc(%rsp), %eax
shlq $0x5, %rax
leaq (%rsp,%rax), %r15
addq $0xb0, %r15
movl %edx, %eax
movq %rax, 0x78(%rsp)
shlq $0x20, %rax
movq %rax, 0x48(%rsp)
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %rdi, 0x10(%rsp)
movq 0x10(%rbp), %r14
leaq (%r14,%rbx), %r13
cmpl $0x0, 0xc(%rsp)
js 0xfc6e
movq 0x70(%rsp), %rax
cmpq %rax, 0x30(%rsp)
jb 0xfc6e
movq 0x50(%rsp), %rdi
movq %r13, %rsi
leaq 0xb0(%rsp), %rdx
movl $0x1, %ecx
callq 0xf6ba
movq 0x10(%rsp), %rdi
movq 0x10(%r15), %rdx
movq (%rdi), %rcx
cmpq 0x68(%rsp), %rdx
jb 0xfc71
movq 0x10(%rdi), %rax
testq %rcx, %rcx
je 0xfc07
movq %rcx, %rsi
shlq $0x5, %rsi
cmpq -0x10(%rax,%rsi), %rdx
je 0xfd2d
movq 0x18(%r14,%rbx), %rdx
movq %rdx, 0x18(%r15)
cmpq 0x8(%rdi), %rcx
jne 0xfc55
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x5, %rsi
movq %rax, %rdi
leaq 0x185ba(%rip), %rdx     # 0x281f2
movl $0x182, %ecx            # imm = 0x182
leaq 0x185b4(%rip), %r8      # 0x281f8
callq 0x2324c
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
shlq $0x5, %rcx
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
jmp 0xfd24
movq (%rdi), %rcx
testq %rcx, %rcx
jne 0xfd2d
movq 0x4f0(%rsp), %rax
movq (%rax), %rcx
movq 0x10(%rax), %rax
testq %rcx, %rcx
je 0xfca4
movq %rcx, %rdx
shlq $0x5, %rdx
movq 0x78(%rsp), %rsi
cmpl %esi, -0x4(%rax,%rdx)
jbe 0xfd2d
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq 0x48(%rsp), %rdx
orq %rdx, 0x38(%rsp)
movq 0x4f0(%rsp), %r14
cmpq 0x8(%r14), %rcx
jne 0xfd0f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x5, %rsi
movq %rax, %rdi
leaq 0x18500(%rip), %rdx     # 0x281f2
movl $0x17d, %ecx            # imm = 0x17D
leaq 0x184fa(%rip), %r8      # 0x281f8
callq 0x2324c
movq 0x10(%rsp), %rdi
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
shlq $0x5, %rcx
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %r12
addq $0x20, %rbx
cmpq %r12, (%rbp)
ja 0xfb98
cmpq $0x0, (%rdi)
movl 0x5c(%rsp), %eax
movl %eax, %edx
movq %rdi, %r13
movq %rbp, %rdi
jne 0xfb21
movq 0x4f0(%rsp), %rdi
callq 0xfdba
movq 0x4f8(%rsp), %rbx
testq %rbx, %rbx
je 0xfd8a
cmpq $0x0, (%rbx)
jne 0xfd81
movq 0x90(%rsp), %rdi
callq 0x3390
cmpq $0x0, 0x8(%rbx)
jne 0xfda4
jmp 0xfd97
movq 0x90(%rsp), %rdi
callq 0x3390
movq 0xa8(%rsp), %rdi
callq 0x3390
movl 0x1c(%rsp), %eax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq