// File: /workspace/github_c_makefile_star2_O1/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
movl %ecx, %r14d
movl %esi, 0x8(%rsp)
movq %rdi, 0x50(%rsp)
movq 0x4f0(%rsp), %rax
movq $0x0, (%rax)
movslq %ecx, %rax
movq %rdx, 0x60(%rsp)
movzbl (%rdx,%rax), %ecx
cmpq $0x16, %rcx
jb 0xf4c4
incl %r14d
movl %r14d, %eax
jmp 0xfa6f
movq 0x4f8(%rsp), %rdx
cmpl $0x2, %r8d
movl $0x1, %eax
cmovgel %r8d, %eax
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
testq %rdx, %rdx
je 0xf506
movq (%rdx), %rsi
movq %rsi, 0x20(%rsp)
testq %rsi, %rsi
jne 0xf513
leaq 0x80(%rsp), %rsi
movq %rsi, 0x20(%rsp)
testq %rdx, %rdx
je 0xf521
movq 0x8(%rdx), %r13
testq %r13, %r13
jne 0xf529
leaq 0x98(%rsp), %r13
movq 0x50(%rsp), %rdi
movq 0x8(%rdi,%rcx,8), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x30(%rsp)
movq 0x10(%rdi,%rcx,8), %rsi
subq %rdx, %rsi
movq %rsi, 0x40(%rsp)
movl $0x15, %edx
subq %rcx, %rdx
movq 0x8(%rdi,%rdx,8), %rcx
incq %rcx
movq %rcx, 0x38(%rsp)
leal 0x1(%r14), %r12d
movslq %r12d, %rbx
movq %rbx, 0x48(%rsp)
movq $0x0, (%r13)
movl %eax, %eax
movq %rax, 0x68(%rsp)
cmpl 0x8(%rsp), %ebx
movq %r9, 0x18(%rsp)
jge 0xf714
movq %r14, 0x10(%rsp)
incq %rbx
movq 0x40(%rsp), %rbp
cmpq %r9, %rbp
jb 0xf69b
movq 0x60(%rsp), %rax
movzbl -0x1(%rax,%rbx), %eax
cmpq $0x15, %rax
ja 0xf6a6
movl $0x15, %r15d
subl %eax, %r15d
movq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
xorl %ecx, %ecx
callq 0xf380
movq 0x18(%rsp), %r9
shlq $0x5, %r15
movq 0xc0(%rsp,%r15), %r14
cmpq %rbp, %r14
je 0xf663
movq 0x8(%r13), %rax
cmpq %rax, (%r13)
jne 0xf631
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
shlq $0x5, %rsi
leaq 0x18b46(%rip), %rdx     # 0x2815d
movl $0x164, %ecx            # imm = 0x164
leaq 0x18b40(%rip), %r8      # 0x28163
callq 0x22d64
movq 0x18(%rsp), %r9
movq %rax, 0x10(%r13)
movq (%r13), %rax
movq 0x10(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
shlq $0x5, %rax
movups 0x30(%rsp), %xmm0
movups 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rcx,%rax)
movups %xmm0, (%rcx,%rax)
cmpq 0x68(%rsp), %r14
jb 0xf70f
leaq (%rsp,%r15), %rax
addq $0xb0, %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %rbx, 0x48(%rsp)
incl %r12d
incq %rbx
cmpl %r12d, 0x8(%rsp)
jne 0xf590
movl 0x8(%rsp), %r12d
jmp 0xf70f
movq 0x8(%r13), %rax
movl $0x15d, %ecx            # imm = 0x15D
jmp 0xf6af
movq 0x8(%r13), %rax
movl $0x169, %ecx            # imm = 0x169
cmpq %rax, (%r13)
jne 0xf6e8
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
shlq $0x5, %rsi
leaq 0x18a85(%rip), %rdx     # 0x2815d
leaq 0x18a84(%rip), %r8      # 0x28163
callq 0x22d64
movq %rax, 0x10(%r13)
movq (%r13), %rax
movq 0x10(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
shlq $0x5, %rax
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rcx,%rax)
movups %xmm0, (%rcx,%rax)
movq 0x10(%rsp), %r14
cmpl 0x8(%rsp), %r12d
jne 0xf784
movq 0x8(%r13), %rax
cmpq %rax, (%r13)
jne 0xf75d
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r13)
movq 0x10(%r13), %rdi
shlq $0x5, %rsi
leaq 0x18a15(%rip), %rdx     # 0x2815d
leaq 0x18a14(%rip), %r8      # 0x28163
movl $0x16e, %ecx            # imm = 0x16E
callq 0x22d64
movq %rax, 0x10(%r13)
movq (%r13), %rax
movq 0x10(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
shlq $0x5, %rax
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rcx,%rax)
movups %xmm0, (%rcx,%rax)
movq %r13, %rdi
callq 0xfa81
movq 0x10(%r13), %rax
movl 0x18(%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r8
leal -0x1(%r14), %eax
testl %r14d, %r14d
js 0xfa25
movq %r13, %r9
movl %eax, 0x5c(%rsp)
je 0xf7d2
movl %eax, %eax
movq 0x60(%rsp), %rcx
movzbl (%rcx,%rax), %r12d
cmpl $0x16, %r12d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovael %eax, %r12d
jmp 0xf7d8
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, (%r8)
cmpq $0x0, (%r9)
je 0xfa0e
movl %r12d, %eax
shlq $0x5, %rax
addq %rsp, %rax
addq $0xb0, %rax
movq %rax, 0x8(%rsp)
movl %r14d, %eax
movq %rax, 0x70(%rsp)
shlq $0x20, %rax
movq %rax, 0x78(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r8, 0x20(%rsp)
movq %r9, 0x10(%rsp)
testl %r12d, %r12d
sets %al
movq 0x10(%r9), %r15
leaq (%r15,%rbx), %r13
movq 0x40(%rsp), %rbp
cmpq %rdi, %rbp
setb %cl
orb %al, %cl
jne 0xf864
movq 0x50(%rsp), %rdi
movq %r13, %rsi
leaq 0xb0(%rsp), %rdx
movl $0x1, %ecx
callq 0xf380
movq 0x10(%rsp), %r9
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %rdi
cmpq %rdi, %rbp
setb %al
testl %r12d, %r12d
sets %cl
orb %al, %cl
jne 0xf888
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rcx
cmpq 0x68(%rsp), %rcx
jae 0xf95b
cmpq $0x0, (%r8)
jne 0xf9fe
movq 0x4f0(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0xf8c4
movq 0x4f0(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %rax, %rdx
shlq $0x5, %rdx
movq 0x70(%rsp), %rsi
cmpl %esi, -0x4(%rcx,%rdx)
jbe 0xf9fe
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq 0x78(%rsp), %rcx
orq %rcx, 0x48(%rsp)
movq 0x4f0(%rsp), %r15
movq 0x8(%r15), %rcx
cmpq %rcx, %rax
jne 0xf93a
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x5, %rsi
leaq 0x18847(%rip), %rdx     # 0x2815d
movl $0x17d, %ecx            # imm = 0x17D
leaq 0x18841(%rip), %r8      # 0x28163
callq 0x22d64
movq 0x10(%rsp), %r9
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%r15)
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
shlq $0x5, %rax
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
jmp 0xf9f5
movq (%r8), %rax
testq %rax, %rax
je 0xf979
movq 0x10(%r8), %rdx
movq %rax, %rsi
shlq $0x5, %rsi
cmpq -0x10(%rdx,%rsi), %rcx
je 0xf9fe
movq 0x18(%r15,%rbx), %rcx
movq 0x8(%rsp), %rdx
movq %rcx, 0x18(%rdx)
movq 0x8(%r8), %rcx
cmpq %rcx, %rax
jne 0xf9d7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
shlq $0x5, %rsi
leaq 0x187aa(%rip), %rdx     # 0x2815d
movl $0x182, %ecx            # imm = 0x182
leaq 0x187a4(%rip), %r8      # 0x28163
callq 0x22d64
movq 0x10(%rsp), %r9
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
shlq $0x5, %rax
movq 0x8(%rsp), %rdx
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups %xmm1, 0x10(%rcx,%rax)
movups %xmm0, (%rcx,%rax)
incq %r14
addq $0x20, %rbx
cmpq %r14, (%r9)
ja 0xf81e
cmpq $0x0, (%r8)
movl 0x5c(%rsp), %eax
movl %eax, %r14d
movq %r8, %r13
movq %r9, %r8
jne 0xf7a1
movq 0x4f0(%rsp), %rdi
callq 0xfa81
movq 0x4f8(%rsp), %rbx
testq %rbx, %rbx
je 0xfa45
cmpq $0x0, (%rbx)
jne 0xfa52
movq 0x90(%rsp), %rdi
callq 0x3390
testq %rbx, %rbx
je 0xfa5e
cmpq $0x0, 0x8(%rbx)
jne 0xfa6b
movq 0xa8(%rsp), %rdi
callq 0x3390
movl 0x2c(%rsp), %eax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq