// File: /workspace/github_c_makefile_star2_O2/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, 0x78(%rsp)
movq 0x4f0(%rsp), %rax
andq $0x0, (%rax)
movq %rdi, 0x58(%rsp)
movslq %ecx, %rax
movq %rdx, 0x68(%rsp)
movzbl (%rdx,%rax), %r9d
cmpq $0x16, %r9
jb 0xddc8
incl %ecx
movl %ecx, %eax
jmp 0xe35e
movq 0x4f8(%rsp), %r10
cmpl $0x2, %r8d
pushq $0x1
popq %rax
cmovgel %r8d, %eax
leaq 0x98(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x8(%rbp)
movaps %xmm0, -0x8(%rbp)
movaps %xmm0, -0x18(%rbp)
testq %r10, %r10
je 0xde1b
movq (%r10), %rdx
testq %rdx, %rdx
leaq 0x80(%rsp), %rdi
cmovneq %rdx, %rdi
movq %rdi, 0x8(%rsp)
movq 0x8(%r10), %rdx
testq %rdx, %rdx
cmovneq %rdx, %rbp
jmp 0xde28
leaq 0x80(%rsp), %rdx
movq %rdx, 0x8(%rsp)
movq 0x58(%rsp), %rdi
movq 0x8(%rdi,%r9,8), %rdx
leaq 0x1(%rdx), %r8
leaq 0x20(%rsp), %r10
movq %r8, (%r10)
movq 0x10(%rdi,%r9,8), %r8
subq %rdx, %r8
movq %r8, 0x10(%r10)
pushq $0x15
popq %rdx
subl %r9d, %edx
shll $0x3, %edx
movq 0x8(%rdi,%rdx), %rdx
incq %rdx
movq %rdx, 0x8(%r10)
movq %rcx, 0x10(%rsp)
incl %ecx
movslq %ecx, %r13
movq %r13, 0x18(%r10)
andq $0x0, (%rbp)
movl %eax, %eax
movq %rax, 0x70(%rsp)
movslq %esi, %r14
cmpl %r13d, %r14d
movl %r13d, %eax
movl %esi, 0x50(%rsp)
cmovgl %esi, %eax
movl %eax, 0x48(%rsp)
cmpq %r14, %r13
jge 0xdf86
movq 0x30(%rsp), %r12
cmpq 0x78(%rsp), %r12
jb 0xdf92
movq 0x68(%rsp), %rax
movzbl (%rax,%r13), %eax
cmpq $0x15, %rax
ja 0xdff1
pushq $0x15
popq %rcx
movl %ecx, %r15d
subl %eax, %r15d
movq 0x58(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
xorl %ecx, %ecx
callq 0xdc9b
shll $0x5, %r15d
movq 0xc0(%rsp,%r15), %rbx
cmpq %r12, %rbx
je 0xdf5e
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xdf34
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x5, %rsi
movq %rax, %rdi
leaq 0x15272(%rip), %rdx     # 0x2318d
movl $0x164, %ecx            # imm = 0x164
leaq 0x1526c(%rip), %r8      # 0x23193
callq 0x1e0b4
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
shlq $0x5, %rcx
movups 0x20(%rsp), %xmm0
movups 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
cmpq 0x70(%rsp), %rbx
jb 0xe057
leaq (%rsp,%r15), %rax
addq $0xb0, %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x30(%rsp)
movups (%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
incq %r13
movq %r13, 0x38(%rsp)
jmp 0xde8d
movl 0x48(%rsp), %eax
movl %eax, %r13d
jmp 0xe057
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xdfd9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x5, %rsi
leaq 0x151d0(%rip), %rdx     # 0x2318d
leaq 0x151cf(%rip), %r8      # 0x23193
movq %rax, %rdi
movl $0x15d, %ecx            # imm = 0x15D
callq 0x1e0b4
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
shlq $0x5, %rcx
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
jmp 0xe04e
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xe038
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x5, %rsi
leaq 0x15171(%rip), %rdx     # 0x2318d
leaq 0x15170(%rip), %r8      # 0x23193
movq %rax, %rdi
movl $0x169, %ecx            # imm = 0x169
callq 0x1e0b4
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
shlq $0x5, %rcx
movups 0x20(%rsp), %xmm0
movups 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
cmpl 0x50(%rsp), %r13d
movq 0x10(%rsp), %rbx
jne 0xe0c9
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xe0aa
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x5, %rsi
leaq 0x150ff(%rip), %rdx     # 0x2318d
leaq 0x150fe(%rip), %r8      # 0x23193
movq %rax, %rdi
movl $0x16e, %ecx            # imm = 0x16E
callq 0x1e0b4
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
shlq $0x5, %rcx
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movq %rbp, %rdi
callq 0xe370
movq 0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x1c(%rsp)
pushq $-0x1
popq %rdx
movq 0x8(%rsp), %rdi
leal -0x1(%rbx), %esi
testl %ebx, %ebx
js 0xe30a
movq %rbp, %r12
movl %edx, %r13d
je 0xe10b
movl %esi, %eax
movq 0x68(%rsp), %rcx
movzbl (%rcx,%rax), %r13d
cmpl $0x16, %r13d
cmovael %edx, %r13d
movl %esi, 0x64(%rsp)
andq $0x0, (%rdi)
movl %r13d, %eax
shlq $0x5, %rax
addq %rsp, %rax
addq $0xb0, %rax
movq %rax, 0x10(%rsp)
movl %ebx, %eax
movq %rax, 0x48(%rsp)
shlq $0x20, %rax
movq %rax, 0x50(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %rdi, 0x8(%rsp)
cmpq %r14, (%r12)
jbe 0xe2f1
movq 0x10(%r12), %r15
leaq (%r15,%rbx), %rbp
testl %r13d, %r13d
js 0xe22a
movq 0x78(%rsp), %rax
cmpq %rax, 0x30(%rsp)
jb 0xe22a
movq 0x58(%rsp), %rdi
movq %rbp, %rsi
leaq 0xb0(%rsp), %rdx
pushq $0x1
popq %rcx
callq 0xdc9b
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdx
movq (%rdi), %rcx
cmpq 0x70(%rsp), %rdx
jb 0xe22d
movq 0x10(%rdi), %rax
testq %rcx, %rcx
je 0xe1bd
movq %rcx, %rsi
shlq $0x5, %rsi
cmpq -0x10(%rax,%rsi), %rdx
je 0xe2e5
movq 0x18(%r15,%rbx), %rdx
movq 0x10(%rsp), %rsi
movq %rdx, 0x18(%rsi)
cmpq 0x8(%rdi), %rcx
jne 0xe20e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x5, %rsi
movq %rax, %rdi
leaq 0x14f9c(%rip), %rdx     # 0x2318d
movl $0x182, %ecx            # imm = 0x182
leaq 0x14f96(%rip), %r8      # 0x23193
callq 0x1e0b4
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
shlq $0x5, %rcx
movq 0x10(%rsp), %rdx
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
jmp 0xe2dc
movq (%rdi), %rcx
testq %rcx, %rcx
jne 0xe2e5
movq 0x4f0(%rsp), %rax
movq (%rax), %rcx
movq 0x10(%rax), %rax
testq %rcx, %rcx
je 0xe260
movq %rcx, %rdx
shlq $0x5, %rdx
movq 0x48(%rsp), %rsi
cmpl %esi, -0x4(%rax,%rdx)
jbe 0xe2e5
movups (%rbp), %xmm0
movups 0x10(%rbp), %xmm1
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq 0x50(%rsp), %rdx
orq %rdx, 0x38(%rsp)
movq 0x4f0(%rsp), %r15
cmpq 0x8(%r15), %rcx
jne 0xe2c7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x5, %rsi
movq %rax, %rdi
leaq 0x14ee3(%rip), %rdx     # 0x2318d
movl $0x17d, %ecx            # imm = 0x17D
leaq 0x14edd(%rip), %r8      # 0x23193
callq 0x1e0b4
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
shlq $0x5, %rcx
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %r14
addq $0x20, %rbx
jmp 0xe142
cmpq $0x0, (%rdi)
movl 0x64(%rsp), %eax
movl %eax, %ebx
movq %rdi, %rbp
movq %r12, %rdi
pushq $-0x1
popq %rdx
jne 0xe0e4
movq 0x4f0(%rsp), %rdi
callq 0xe370
movq 0x4f8(%rsp), %rbx
testq %rbx, %rbx
je 0xe340
cmpq $0x0, (%rbx)
jne 0xe337
movq 0x90(%rsp), %rdi
callq 0x3390
cmpq $0x0, 0x8(%rbx)
jne 0xe35a
jmp 0xe34d
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