// File: /workspace/github_c_makefile_star2_O2/lh3[P]fermi2/match.c
// Function: discover
static void discover(const rld_t *e, const fmdsmem_t *q, const fmdsmem_t *p, int l_seq, const char *seq, const char *qual, kstring_t *s, kstring_t cmp[2])
{
	int start, end, i, ext[2], left, right, tmp_l, pos;
	int64_t occ[2];
	rldintv_t ovlp;

	// find the coordinate from which the extension will be applied
	if (q == 0 && p == 0) {
		start = 0; end = l_seq;
	} else if (q == 0) {
		if (p->ik.info>>32 == 0) return; // no novel allele
		start = 0; end = p->ik.info>>32;
	} else if (p == 0) {
		if ((uint32_t)q->ik.info == l_seq) return; // no novel allele
		start = (uint32_t)q->ik.info; end = l_seq;
	} else {
		start = (uint32_t)q->ik.info, end = p->ik.info>>32;
		if (start >= end && q->ok[1][0].x[2] == q->ik.x[2] && p->ik.x[2] == p->ok[0][0].x[2]) return;
	}
	// find the SAI for the overlap (if applicable)
	memset(&ovlp, 0, sizeof(rldintv_t));
	ovlp.x[2] = e->mcnt[0];
	if (start <= end) { // no overlap
		left = start - 1, right = end;
		for (i = left + 1; i < right; ++i)
			if (seq[i] < 5) break;
		if (i == end) return; // the gap is filled with "N"
	} else {
		rldintv_t ok[6];
		left = end - 1, right = start;
		for (i = right - 1; i > left; --i) {
			rld_extend(e, &ovlp, ok, 1);
			ovlp = ok[(int)seq[i]];
			assert(ovlp.x[2] > 0);
		}
	}
	occ[0] = occ[1] = 0;
	ext[0] = ext[1] = 0;
	// left extension
	if (left >= 0) {
		int64_t l = ovlp.x[0], u = l + ovlp.x[2];
		uint64_t ol[6], ou[6];
		for (i = left; i >= 0; --i) {
			int c = seq[i];
			rld_rank1a(e, l, ol);
			rld_rank1a(e, u, ou);
			l = e->cnt[c] + ol[c];
			u = e->cnt[c] + ou[c];
			if (u - l <= q->ik.x[2]) break;
		}
		assert(i >= 0 && u - l == q->ik.x[2]);
		ext[0] = left + 1 - i;
		occ[0] = u - l;
	}
	// right extension
	if (right < l_seq) {
		int64_t l = ovlp.x[1], u = l + ovlp.x[2];
		uint64_t ol[6], ou[6];
		for (i = right; i < l_seq; ++i) {
			int c = fmd_comp(seq[i]);
			rld_rank1a(e, l, ol);
			rld_rank1a(e, u, ou);
			l = e->cnt[c] + ol[c];
			u = e->cnt[c] + ou[c];
			if (u - l <= p->ik.x[2]) break;
		}
		assert(i < l_seq && u - l == p->ik.x[2]);
		ext[1] = i + 1 - right;
		occ[1] = u - l;
	}
	if (ovlp.x[2] == occ[0] + occ[1]) return;
	// cut sequence
	pos = left + 1 - ext[0];
	cmp[0].l = cmp[1].l = 0;
	kputsn(&seq[pos], (right + ext[1]) - (left + 1 - ext[0]), &cmp[0]);
	kputsn(cmp[0].s, cmp[0].l, &cmp[1]);
	seq_revcomp6(cmp[1].l, (uint8_t*)cmp[1].s);
	// print
	if (ovlp.x[2] == e->mcnt[0]) ovlp.x[2] = 0;
	ksprintf(s, "NS\t%d\t", pos);
	tmp_l = end < start? start - end : 0;
	if (strcmp(cmp[0].s, cmp[1].s) <= 0) {
		ksprintf(s, "+\t%d\t%d\t%d\t%ld\t%ld\t%ld\t", ext[0] + tmp_l, end - start, ext[1] + tmp_l, (long)occ[0], (long)ovlp.x[2], (long)occ[1]);
		for (i = 0; i < cmp[0].l; ++i)
			kputc("$ACGTN"[(int)cmp[0].s[i]], s);
		kputc('\t', s);
		if (qual) kputsn(&qual[pos], cmp[0].l, s);
		else kputc('*', s);
	} else {
		ksprintf(s, "-\t%d\t%d\t%d\t%ld\t%ld\t%ld\t", ext[1] + tmp_l, end - start, ext[0] + tmp_l, (long)occ[1], (long)ovlp.x[2], (long)occ[0]);
		for (i = 0; i < cmp[0].l; ++i)
			kputc("$ACGTN"[(int)cmp[1].s[i]], s);
		kputc('\t', s);
		if (qual) {
			for (i = pos + cmp[0].l - 1; i >= pos; --i)
				kputc(qual[i], s);
		} else kputc('*', s);
	}
	kputc('\n', s);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %r8, 0x18(%rsp)
movl %ecx, %r8d
movq %rdi, %r15
movq %rsi, %rax
orq %rdx, %rax
movq %r9, 0x68(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, 0x28(%rsp)
jne 0x1760c
movl $0x0, 0x4(%rsp)
movl %r8d, %eax
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq 0x28(%r15), %rax
movq (%rax), %rbx
movq %rbx, 0x50(%rsp)
movq 0x8(%rsp), %rax
movl %eax, %edx
movl 0x4(%rsp), %ecx
movslq %ecx, %r14
movslq %eax, %r13
subl %ecx, %edx
movl %edx, 0x34(%rsp)
jge 0x17659
movl %r8d, 0x14(%rsp)
pushq $0x1
popq %rbp
cmpq %r13, %r14
jle 0x1767c
leaq -0x1(%r14), %r12
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
movl %ebp, %ecx
callq 0x39db
movq 0x18(%rsp), %rax
movsbq -0x1(%rax,%r14), %rax
shlq $0x5, %rax
movups 0xa0(%rsp,%rax), %xmm0
movups 0xb0(%rsp,%rax), %xmm1
movaps %xmm1, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq 0x50(%rsp), %rbx
movq %r12, %r14
testq %rbx, %rbx
jne 0x17593
leaq 0x3b0f(%rip), %rdi      # 0x1b103
leaq 0x3b16(%rip), %rsi      # 0x1b111
leaq 0x3b17(%rip), %rcx      # 0x1b119
movl $0xae, %edx
callq 0x2120
testq %rsi, %rsi
je 0x1794a
movl 0x18(%rsi), %ecx
testq %rdx, %rdx
movl %ecx, 0x4(%rsp)
je 0x17a32
movl 0x1c(%rdx), %eax
cmpl %eax, %ecx
movq %rax, 0x8(%rsp)
jl 0x17555
movq 0xf0(%rsi), %rax
cmpq 0x10(%rsi), %rax
jne 0x17555
movq 0x10(%rdx), %rax
cmpq 0x30(%rdx), %rax
jne 0x17555
jmp 0x17a5b
subq %r14, %r13
addq 0x18(%rsp), %r14
xorl %eax, %eax
cmpq %rax, %r13
je 0x17a5b
cmpb $0x5, (%r14,%rax)
jl 0x17730
incq %rax
jmp 0x17663
movq 0x8(%rsp), %rax
movl %eax, %edx
movl 0x4(%rsp), %esi
movl 0x14(%rsp), %r8d
xorl %r14d, %r14d
testl %edx, %edx
movl %edx, 0x24(%rsp)
movl %esi, 0x30(%rsp)
jle 0x1774a
movl %r8d, 0x14(%rsp)
movq 0x40(%rsp), %rsi
addq %rsi, %rbx
movl %edx, %r12d
xorl %r13d, %r13d
testq %r12, %r12
jle 0x17a6d
movq 0x18(%rsp), %rax
movsbq -0x1(%rax,%r12), %rbp
decq %r12
movq %r15, %rdi
leaq 0xa0(%rsp), %rdx
callq 0x346c
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
callq 0x346c
movq 0x20(%r15), %rax
movq (%rax,%rbp,8), %rbx
movq 0xa0(%rsp,%rbp,8), %rax
leaq (%rax,%rbx), %rsi
movq 0x70(%rsp,%rbp,8), %rbp
addq %rbp, %rbx
subq %rax, %rbp
incl %r13d
movq 0x28(%rsp), %rax
cmpq 0x10(%rax), %rbp
ja 0x176b2
jne 0x17a6d
movq 0x50(%rsp), %rbx
movl 0x24(%rsp), %edx
movl 0x14(%rsp), %r8d
movl 0x30(%rsp), %esi
jmp 0x1774f
movl 0x4(%rsp), %edx
movq 0x8(%rsp), %rcx
movl %ecx, %esi
cmpl %eax, 0x34(%rsp)
jne 0x1768c
jmp 0x17a5b
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpl %r8d, %esi
jge 0x177f9
movq 0x48(%rsp), %rsi
addq %rsi, %rbx
movq 0x8(%rsp), %rax
movl 0x4(%rsp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, 0x14(%rsp)
movslq %ecx, %r14
movslq %r8d, %rax
movq %rax, 0x28(%rsp)
cmpq 0x28(%rsp), %r14
jge 0x17a8c
movq 0x18(%rsp), %rax
movzbl (%rax,%r14), %eax
movb $0x5, %cl
subb %al, %cl
leal -0x1(%rax), %edx
cmpb $0x4, %dl
movzbl %cl, %r12d
cmovael %eax, %r12d
movq %r15, %rdi
leaq 0xa0(%rsp), %rdx
callq 0x346c
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
callq 0x346c
movsbq %r12b, %rax
movq 0xa0(%rsp,%rax,8), %rsi
movq 0x70(%rsp,%rax,8), %rbx
movq %rbx, %r12
subq %rsi, %r12
movq 0x38(%rsp), %rcx
cmpq 0x10(%rcx), %r12
jbe 0x177fe
movq 0x20(%r15), %rcx
movq (%rcx,%rax,8), %rax
addq %rax, %rsi
addq %rax, %rbx
incq %r14
jmp 0x1777d
xorl %r12d, %r12d
jmp 0x17819
jne 0x17a8c
subl 0x14(%rsp), %r14d
incl %r14d
movq 0x50(%rsp), %rbx
movl 0x24(%rsp), %edx
movl 0x30(%rsp), %esi
leaq (%r12,%rbp), %rax
cmpq %rax, %rbx
je 0x17a5b
movq 0x1a8(%rsp), %rbx
subl %r13d, %edx
andl $0x0, 0x10(%rbx)
leaq 0x10(%rbx), %rax
movq %rax, 0x38(%rsp)
andl $0x0, (%rbx)
movl %edx, 0x28(%rsp)
movslq %edx, %rax
movq 0x18(%rsp), %rdi
addq %rax, %rdi
movq %rax, 0x18(%rsp)
subl %eax, %esi
addl %r14d, %esi
movq %rbx, %rdx
callq 0x17aab
movq 0x8(%rbx), %rdi
movl (%rbx), %esi
movq 0x38(%rsp), %rdx
callq 0x17aab
movl 0x10(%rbx), %edi
movq 0x18(%rbx), %rsi
callq 0xc599
movq 0x50(%rsp), %rax
movq 0x28(%r15), %rcx
cmpq (%rcx), %rax
jne 0x17892
andq $0x0, 0x50(%rsp)
movl 0x34(%rsp), %r15d
negl %r15d
xorl %eax, %eax
movq 0x8(%rsp), %rcx
subl 0x4(%rsp), %ecx
movq %rcx, 0x8(%rsp)
leaq 0x3925(%rip), %rsi      # 0x1b1d6
cmovgel %eax, %r15d
movq 0x1a0(%rsp), %rdi
movl 0x28(%rsp), %edx
xorl %eax, %eax
callq 0x16308
movq 0x8(%rbx), %rdi
movq 0x18(%rbx), %rsi
callq 0x21a0
movq 0x50(%rsp), %r10
testl %eax, %eax
jg 0x1796a
addl %r15d, %r13d
addl %r15d, %r14d
leaq 0x38ee(%rip), %rsi      # 0x1b1dd
xorl %r15d, %r15d
movq 0x1a0(%rsp), %rdi
movl %r13d, %edx
movq 0x8(%rsp), %rcx
movl %r14d, %r8d
movq %rbp, %r9
xorl %eax, %eax
pushq %r12
pushq %r10
callq 0x16308
popq %rcx
popq %rdx
leaq 0x1ab8(%rip), %r14      # 0x193d4
movq 0x1a0(%rsp), %r12
movl (%rbx), %eax
cmpq %rax, %r15
jae 0x179cd
movq 0x8(%rbx), %rax
movsbq (%rax,%r15), %rax
movsbl (%rax,%r14), %edi
movq %r12, %rsi
callq 0x1749d
incq %r15
jmp 0x17924
movl $0x0, 0x4(%rsp)
movq 0x18(%rdx), %rax
shrq $0x20, %rax
movq %rax, 0x8(%rsp)
jne 0x17555
jmp 0x17a5b
addl %r15d, %r14d
addl %r13d, %r15d
leaq 0x387e(%rip), %rsi      # 0x1b1f5
movl %r15d, %r8d
xorl %r15d, %r15d
movq 0x1a0(%rsp), %rdi
movl %r14d, %edx
movq 0x8(%rsp), %rcx
movq %r12, %r9
xorl %eax, %eax
pushq %rbp
pushq %r10
callq 0x16308
popq %rcx
popq %rdx
leaq 0x1a31(%rip), %r14      # 0x193d4
movq 0x1a0(%rsp), %r12
movl (%rbx), %eax
cmpq %rax, %r15
jae 0x179f3
movq 0x18(%rbx), %rax
movsbq (%rax,%r15), %rax
movsbl (%rax,%r14), %edi
movq %r12, %rsi
callq 0x1749d
incq %r15
jmp 0x179ab
pushq $0x9
popq %rdi
movq %r12, %rsi
callq 0x1749d
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x17a45
addq 0x18(%rsp), %rdi
movl (%rbx), %esi
movq %r12, %rdx
callq 0x17aab
jmp 0x17a50
pushq $0x9
popq %rdi
movq %r12, %rsi
callq 0x1749d
movq 0x68(%rsp), %r14
testq %r14, %r14
je 0x17a45
movl 0x24(%rsp), %eax
addl (%rbx), %eax
notl %r13d
addl %eax, %r13d
movl 0x28(%rsp), %ebx
cmpl %ebx, %r13d
jl 0x17a50
movslq %r13d, %rax
movsbl (%r14,%rax), %edi
movq %r12, %rsi
callq 0x1749d
decl %r13d
jmp 0x17a18
movl %r8d, %eax
movq %rax, 0x8(%rsp)
cmpl %r8d, %ecx
jne 0x17555
jmp 0x17a5b
pushq $0x2a
popq %rdi
movq %r12, %rsi
callq 0x1749d
pushq $0xa
popq %rdi
movq %r12, %rsi
callq 0x1749d
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3723(%rip), %rdi      # 0x1b197
leaq 0x3696(%rip), %rsi      # 0x1b111
leaq 0x3697(%rip), %rcx      # 0x1b119
movl $0xbf, %edx
callq 0x2120
leaq 0x3722(%rip), %rdi      # 0x1b1b5
leaq 0x3677(%rip), %rsi      # 0x1b111
leaq 0x3678(%rip), %rcx      # 0x1b119
movl $0xcf, %edx
callq 0x2120