// File: /workspace/github_c_makefile_star2_O1/ToniWestbrook[P]paladin/bwamem.c
// Function: mem_chain2aln
void mem_chain2aln(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, int l_query, const uint8_t *query, const mem_chain_t *c, mem_alnreg_v *av)
{
	int i, k, rid, max_off[2], aw[2]; // aw: actual bandwidth used in extension
	int64_t l_pac = bns->l_pac, rmax[2], tmp, max = 0;
	const mem_seed_t *s;
	uint8_t *rseq = 0;
	uint64_t *srt;

	if (c->n == 0) return;
	// get the max possible span
	rmax[0] = l_pac<<1; rmax[1] = 0;
	for (i = 0; i < c->n; ++i) {
		int64_t b, e;
		const mem_seed_t *t = &c->seeds[i];
		b = t->rbeg - (t->qbeg + cal_max_gap(opt, t->qbeg));
		e = t->rbeg + t->len + ((l_query - t->qbeg - t->len) + cal_max_gap(opt, l_query - t->qbeg - t->len));
		rmax[0] = rmax[0] < b? rmax[0] : b;
		rmax[1] = rmax[1] > e? rmax[1] : e;
		if (t->len > max) max = t->len;
	}
	rmax[0] = rmax[0] > 0? rmax[0] : 0;
	rmax[1] = rmax[1] < l_pac<<1? rmax[1] : l_pac<<1;
	if (rmax[0] < l_pac && l_pac < rmax[1]) { // crossing the forward-reverse boundary; then choose one side
		if (c->seeds[0].rbeg < l_pac) rmax[1] = l_pac; // this works because all seeds are guaranteed to be on the same strand
		else rmax[0] = l_pac;
	}
	// retrieve the reference sequence
	rseq = bns_fetch_seq(bns, pac, &rmax[0], c->seeds[0].rbeg, &rmax[1], &rid);
	assert(c->rid == rid);

	srt = malloc(c->n * 8);
	for (i = 0; i < c->n; ++i)
		srt[i] = (uint64_t)c->seeds[i].score<<32 | i;
	ks_introsort_64(c->n, srt);

	for (k = c->n - 1; k >= 0; --k) {
		mem_alnreg_t *a;
		s = &c->seeds[(uint32_t)srt[k]];

		for (i = 0; i < av->n; ++i) { // test whether extension has been made before
			mem_alnreg_t *p = &av->a[i];
			int64_t rd;
			int qd, w, max_gap;
			if (s->rbeg < p->rb || s->rbeg + s->len > p->re || s->qbeg < p->qb || s->qbeg + s->len > p->qe) continue; // not fully contained
			if (s->len - p->seedlen0 > .1 * l_query) continue; // this seed may give a better alignment
			// qd: distance ahead of the seed on query; rd: on reference
			qd = s->qbeg - p->qb; rd = s->rbeg - p->rb;
			max_gap = cal_max_gap(opt, qd < rd? qd : rd); // the maximal gap allowed in regions ahead of the seed
			w = max_gap < p->w? max_gap : p->w; // bounded by the band width
			if (qd - rd < w && rd - qd < w) break; // the seed is "around" a previous hit
			// similar to the previous four lines, but this time we look at the region behind
			qd = p->qe - (s->qbeg + s->len); rd = p->re - (s->rbeg + s->len);
			max_gap = cal_max_gap(opt, qd < rd? qd : rd);
			w = max_gap < p->w? max_gap : p->w;
			if (qd - rd < w && rd - qd < w) break;
		}
		if (i < av->n) { // the seed is (almost) contained in an existing alignment; further testing is needed to confirm it is not leading to a different aln
			if (bwa_verbose >= 4)
				printf("** Seed(%d) [%ld;%ld,%ld] is almost contained in an existing alignment [%d,%d) <=> [%ld,%ld)\n",
					   k, (long)s->len, (long)s->qbeg, (long)s->rbeg, av->a[i].qb, av->a[i].qe, (long)av->a[i].rb, (long)av->a[i].re);
			for (i = k + 1; i < c->n; ++i) { // check overlapping seeds in the same chain
				const mem_seed_t *t;
				if (srt[i] == 0) continue;
				t = &c->seeds[(uint32_t)srt[i]];
				if (t->len < s->len * .95) continue; // only check overlapping if t is long enough; TODO: more efficient by early stopping
				if (s->qbeg <= t->qbeg && s->qbeg + s->len - t->qbeg >= s->len>>2 && t->qbeg - s->qbeg != t->rbeg - s->rbeg) break;
				if (t->qbeg <= s->qbeg && t->qbeg + t->len - s->qbeg >= s->len>>2 && s->qbeg - t->qbeg != s->rbeg - t->rbeg) break;
			}
			if (i == c->n) { // no overlapping seeds; then skip extension
				srt[k] = 0; // mark that seed extension has not been performed
				continue;
			}
			if (bwa_verbose >= 4)
				printf("** Seed(%d) might lead to a different alignment even though it is contained. Extension will be performed.\n", k);
		}

		a = kv_pushp(mem_alnreg_t, *av);
		memset(a, 0, sizeof(mem_alnreg_t));
		a->w = aw[0] = aw[1] = opt->w;
		a->score = a->truesc = -1;
		a->rid = c->rid;

		if (bwa_verbose >= 4) err_printf("** ---> Extending from seed(%d) [%ld;%ld,%ld] @ %s <---\n", k, (long)s->len, (long)s->qbeg, (long)s->rbeg, bns->anns[c->rid].name);
		if (s->qbeg) { // left extension
			uint8_t *rs, *qs;
			int qle, tle, gtle, gscore;
			qs = malloc(s->qbeg);
			for (i = 0; i < s->qbeg; ++i) qs[i] = query[s->qbeg - 1 - i];
			tmp = s->rbeg - rmax[0];
			rs = malloc(tmp);
			for (i = 0; i < tmp; ++i) rs[i] = rseq[tmp - 1 - i];
			for (i = 0; i < MAX_BAND_TRY; ++i) {
				int prev = a->score;
				aw[0] = opt->w << i;
				if (bwa_verbose >= 4) {
					int j;
					printf("*** Left ref:   "); for (j = 0; j < tmp; ++j) putchar("ACGTN"[(int)rs[j]]); putchar('\n');
					printf("*** Left query: "); for (j = 0; j < s->qbeg; ++j) putchar("ACGTN"[(int)qs[j]]); putchar('\n');
				}
				a->score = ksw_extend2(s->qbeg, qs, tmp, rs, VALUE_DEFINED, opt->mat, opt->o_del, opt->e_del, opt->o_ins, opt->e_ins, aw[0], opt->pen_clip5, opt->zdrop, s->len * opt->a, &qle, &tle, &gtle, &gscore, &max_off[0]);
				if (bwa_verbose >= 4) { printf("*** Left extension: prev_score=%d; score=%d; bandwidth=%d; max_off_diagonal_dist=%d\n", prev, a->score, aw[0], max_off[0]); fflush(stdout); }
				if (a->score == prev || max_off[0] < (aw[0]>>1) + (aw[0]>>2)) break;
			}
			// check whether we prefer to reach the end of the query
			if (gscore <= 0 || gscore <= a->score - opt->pen_clip5) { // local extension
				a->qb = s->qbeg - qle, a->rb = s->rbeg - tle;
				a->truesc = a->score;
			} else { // to-end extension
				a->qb = 0, a->rb = s->rbeg - gtle;
				a->truesc = gscore;
			}
			free(qs); free(rs);
		} else a->score = a->truesc = s->len * opt->a, a->qb = 0, a->rb = s->rbeg;

		if (s->qbeg + s->len != l_query) { // right extension
			int qle, tle, qe, re, gtle, gscore, sc0 = a->score;
			qe = s->qbeg + s->len;
			re = s->rbeg + s->len - rmax[0];
			assert(re >= 0);
			for (i = 0; i < MAX_BAND_TRY; ++i) {
				int prev = a->score;
				aw[1] = opt->w << i;
				if (bwa_verbose >= 4) {
					int j;
					printf("*** Right ref:   "); for (j = 0; j < rmax[1] - rmax[0] - re; ++j) putchar("ACGTN"[(int)rseq[re+j]]); putchar('\n');
					printf("*** Right query: "); for (j = 0; j < l_query - qe; ++j) putchar("ACGTN"[(int)query[qe+j]]); putchar('\n');
				}

				a->score = ksw_extend2(l_query - qe, query + qe, rmax[1] - rmax[0] - re, rseq + re, VALUE_DEFINED, opt->mat, opt->o_del, opt->e_del, opt->o_ins, opt->e_ins, aw[1], opt->pen_clip3, opt->zdrop, sc0, &qle, &tle, &gtle, &gscore, &max_off[1]);
				if (bwa_verbose >= 4) { printf("*** Right extension: prev_score=%d; score=%d; bandwidth=%d; max_off_diagonal_dist=%d\n", prev, a->score, aw[1], max_off[1]); fflush(stdout); }
				if (a->score == prev || max_off[1] < (aw[1]>>1) + (aw[1]>>2)) break;
			}
			// similar to the above
			if (gscore <= 0 || gscore <= a->score - opt->pen_clip3) { // local extension
				a->qe = qe + qle, a->re = rmax[0] + re + tle;
				a->truesc += a->score - sc0;
			} else { // to-end extension
				a->qe = l_query, a->re = rmax[0] + re + gtle;
				a->truesc += gscore - sc0;
			}
		} else a->qe = l_query, a->re = s->rbeg + s->len;
		if (bwa_verbose >= 4) printf("*** Added alignment region: [%d,%d) <=> [%ld,%ld); score=%d; {left,right}_bandwidth={%d,%d}\n", a->qb, a->qe, (long)a->rb, (long)a->re, a->score, aw[0], aw[1]);

		// compute seedcov
		for (i = 0, a->seedcov = 0; i < c->n; ++i) {
			const mem_seed_t *t = &c->seeds[i];
			if (t->qbeg >= a->qb && t->qbeg + t->len <= a->qe && t->rbeg >= a->rb && t->rbeg + t->len <= a->re) // seed fully contained
				a->seedcov += t->len; // this is not very accurate, but for approx. mapQ, this is good enough
		}
		a->w = aw[0] > aw[1]? aw[0] : aw[1];
		a->seedlen0 = s->len;

		a->frac_rep = c->frac_rep;
	}
	free(srt); free(rseq);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, 0x10(%rsp)
movl (%r9), %eax
testq %rax, %rax
je 0x1b66b
movq %rdx, 0x18(%rsp)
movl %ecx, 0x44(%rsp)
movq %r8, 0xc0(%rsp)
movq %r9, 0x50(%rsp)
movq %rsi, 0xb8(%rsp)
movq (%rsi), %rcx
movq %rcx, 0x48(%rsp)
addq %rcx, %rcx
movq %rcx, 0x8(%rsp)
movq %rcx, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
testl %eax, %eax
jle 0x1a95f
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdi
movq 0x50(%rsp), %rcx
movq 0x20(%rcx), %r8
movq 0x10(%rsp), %rcx
movl (%rcx), %r9d
movl 0x8(%rcx), %r10d
cvtsi2sdl 0xc(%rcx), %xmm0
movl 0x10(%rcx), %r11d
cvtsi2sdl 0x14(%rcx), %xmm1
movl 0x24(%rcx), %edx
addl %edx, %edx
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
movsd 0xdc22(%rip), %xmm2     # 0x28498
movl 0x8(%r8,%r15), %r12d
movl %r9d, %eax
imull %r12d, %eax
movl %eax, %ebx
subl %r10d, %ebx
xorps %xmm3, %xmm3
cvtsi2sd %ebx, %xmm3
divsd %xmm0, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %ebx
subl %r11d, %eax
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
divsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %ebp
cmpl %ebp, %ebx
cmovgl %ebx, %ebp
cmpl $0x2, %ebp
jge 0x1a8bf
movl $0x1, %ebp
cmpl %edx, %ebp
movl 0x44(%rsp), %ecx
jl 0x1a8c9
movl %edx, %ebp
movslq 0xc(%r8,%r15), %r13
subl %r12d, %ecx
subl %r13d, %ecx
movl %ecx, %eax
imull %r9d, %eax
movl %eax, %ebx
subl %r10d, %ebx
xorps %xmm3, %xmm3
cvtsi2sd %ebx, %xmm3
divsd %xmm0, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %r14d
subl %r11d, %eax
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
divsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %ebx
cmpl %ebx, %r14d
cmovgl %r14d, %ebx
cmpl $0x2, %ebx
jge 0x1a91a
movl $0x1, %ebx
cmpl %edx, %ebx
jl 0x1a920
movl %edx, %ebx
movq (%r8,%r15), %rax
addl %r12d, %ebp
movslq %ebp, %r14
addq %rax, %r13
subq %r14, %rax
addl %ecx, %ebx
movslq %ebx, %rcx
addq %r13, %rcx
cmpq %rax, %rsi
cmovgeq %rax, %rsi
cmpq %rcx, %rdi
cmovleq %rcx, %rdi
movq %rsi, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
addq $0x18, %r15
cmpq %r15, 0x28(%rsp)
jne 0x1a876
leaq 0x38(%rsp), %r8
movq -0x8(%r8), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovgq %rax, %rcx
movq (%r8), %rax
movq 0x8(%rsp), %rsi
cmpq %rsi, %rax
cmovlq %rax, %rsi
movq %rcx, -0x8(%r8)
movq %rsi, (%r8)
movq 0x48(%rsp), %rdx
cmpq %rdx, %rcx
setl %al
cmpq %rsi, %rdx
setl %cl
andb %al, %cl
cmpb $0x1, %cl
movq 0x50(%rsp), %r15
movq 0xb8(%rsp), %rdi
movq 0x18(%rsp), %rsi
jne 0x1a9c6
movq 0x20(%r15), %rax
cmpq %rdx, (%rax)
jge 0x1a9c1
movq %rdx, 0x38(%rsp)
jmp 0x1a9c6
movq %rdx, 0x30(%rsp)
movq 0x20(%r15), %rax
movq (%rax), %rcx
leaq 0x30(%rsp), %rdx
leaq 0xf4(%rsp), %rbx
movq %rbx, %r9
callq 0x11404
movq %rax, 0x90(%rsp)
movl 0xc(%r15), %eax
cmpl (%rbx), %eax
jne 0x1b69c
movslq (%r15), %rdi
shlq $0x3, %rdi
leaq 0xddcc(%rip), %rsi      # 0x287d0
leaq 0xe177(%rip), %rcx      # 0x28b82
movl $0x2eb, %edx            # imm = 0x2EB
callq 0x22d2b
movslq (%r15), %rdi
testq %rdi, %rdi
jle 0x1aa40
movq 0x20(%r15), %rsi
addq $0x10, %rsi
xorl %ecx, %ecx
movl (%rsi), %edx
shlq $0x20, %rdx
addq %rcx, %rdx
movq %rdx, (%rax,%rcx,8)
incq %rcx
addq $0x18, %rsi
cmpq %rcx, %rdi
jne 0x1aa27
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rsi
callq 0xd241
movq 0x88(%rsp), %rdi
movslq (%r15), %rcx
testq %rcx, %rcx
jle 0x1b659
cvtsi2sdl 0x44(%rsp), %xmm0
mulsd 0xad19(%rip), %xmm0     # 0x25790
movsd %xmm0, 0xe8(%rsp)
movq 0x10(%rsp), %rax
addq $0xc4, %rax
movq %rax, 0xd8(%rsp)
leaq (%rdi,%rcx,8), %rax
movq %rax, 0xc8(%rsp)
movl -0x8(%rdi,%rcx,8), %eax
leaq (%rax,%rax,2), %rax
shlq $0x3, %rax
addq 0x20(%r15), %rax
movq %rax, 0x8(%rsp)
movq %rcx, 0x70(%rsp)
leaq -0x1(%rcx), %rax
movq %rax, 0x78(%rsp)
movq 0x130(%rsp), %rax
movq (%rax), %rcx
testq %rcx, %rcx
setne %dl
je 0x1acbb
movq 0x18(%rax), %rsi
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %rcx, %r8
negq %r8
movl $0x1, %r9d
xorl %eax, %eax
movq %rdi, %rbx
subq (%rsi,%rax), %rbx
jl 0x1ac9d
movq 0x8(%rsp), %r10
movslq 0xc(%r10), %r14
leaq (%rdi,%r14), %r11
movq 0x8(%rsi,%rax), %r10
subq %r11, %r10
jl 0x1ac9d
movq 0x8(%rsp), %r11
movl 0x8(%r11), %r15d
movl %r15d, %ebp
subl 0x10(%rsi,%rax), %ebp
jl 0x1ac9d
addl %r14d, %r15d
movl 0x14(%rsi,%rax), %r11d
subl %r15d, %r11d
jl 0x1ac9d
subl 0x44(%rsi,%rax), %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
ucomisd 0xe8(%rsp), %xmm0
ja 0x1ac9d
movslq %ebp, %r15
movq %rbx, %r12
subq %r15, %r12
cmovlq %rbx, %r15
movq 0x10(%rsp), %r14
movl (%r14), %ebx
movl 0x8(%r14), %r13d
imull %ebx, %r15d
movl %r15d, %ebp
movl %r13d, 0x28(%rsp)
subl %r13d, %ebp
cvtsi2sd %ebp, %xmm1
xorps %xmm0, %xmm0
cvtsi2sdl 0xc(%r14), %xmm0
divsd %xmm0, %xmm1
movsd 0xd8f9(%rip), %xmm3     # 0x28498
addsd %xmm3, %xmm1
cvttsd2si %xmm1, %r13d
movl 0x10(%r14), %ebp
movl %ebp, 0x18(%rsp)
subl %ebp, %r15d
cvtsi2sd %r15d, %xmm2
xorps %xmm1, %xmm1
cvtsi2sdl 0x14(%r14), %xmm1
divsd %xmm1, %xmm2
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %ebp
cmpl %ebp, %r13d
cmovgl %r13d, %ebp
cmpl $0x2, %ebp
jge 0x1abde
movl $0x1, %ebp
movq 0x10(%rsp), %r14
movl 0x24(%r14), %r15d
addl %r15d, %r15d
cmpl %r15d, %ebp
jl 0x1abf2
movl %r15d, %ebp
movl 0x34(%rsi,%rax), %r13d
cmpl %r13d, %ebp
jl 0x1abff
movl %r13d, %ebp
movq %r12, %r14
negq %r14
movslq %ebp, %rbp
cmpq %rbp, %r14
jge 0x1ac16
cmpq %rbp, %r12
jl 0x1b4d6
movslq %r11d, %r14
movq %r10, %r11
subq %r14, %r11
cmovlq %r10, %r14
imull %r14d, %ebx
movl %ebx, %r10d
subl 0x28(%rsp), %r10d
xorps %xmm2, %xmm2
cvtsi2sd %r10d, %xmm2
divsd %xmm0, %xmm2
movsd 0xd855(%rip), %xmm3     # 0x28498
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %ebp
subl 0x18(%rsp), %ebx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
divsd %xmm1, %xmm0
addsd %xmm3, %xmm0
cvttsd2si %xmm0, %r10d
cmpl %r10d, %ebp
cmovgl %ebp, %r10d
cmpl $0x2, %r10d
jge 0x1ac76
movl $0x1, %r10d
cmpl %r15d, %r10d
jl 0x1ac7e
movl %r15d, %r10d
cmpl %r13d, %r10d
jl 0x1ac86
movl %r13d, %r10d
movq %r11, %rbx
negq %rbx
movslq %r10d, %r10
cmpq %r10, %rbx
jge 0x1ac9d
cmpq %r10, %r11
jl 0x1b4d6
cmpq %r9, %rcx
seta %dl
addq $0x60, %rax
leaq (%r8,%r9), %r10
incq %r10
incq %r9
cmpq $0x1, %r10
jne 0x1aaf3
movq 0x130(%rsp), %r14
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x1ad08
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r14)
movq 0x18(%r14), %rdi
shlq $0x5, %rcx
leaq (%rcx,%rcx,2), %rsi
leaq 0xdadd(%rip), %rdx      # 0x287d0
movl $0x319, %ecx            # imm = 0x319
leaq 0xde83(%rip), %r8       # 0x28b82
callq 0x22d64
movq %rax, 0x18(%r14)
movq (%r14), %rax
movq 0x18(%r14), %rbx
leaq 0x1(%rax), %rcx
movq %rcx, (%r14)
leaq (%rax,%rax,2), %r14
shlq $0x5, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, 0x50(%rbx,%r14)
movupd %xmm0, 0x40(%rbx,%r14)
movupd %xmm0, 0x30(%rbx,%r14)
movupd %xmm0, 0x20(%rbx,%r14)
movupd %xmm0, 0x10(%rbx,%r14)
movupd %xmm0, (%rbx,%r14)
movq 0x10(%rsp), %rax
movl 0x24(%rax), %eax
movq %rax, 0x18(%rsp)
movl %eax, 0x34(%rbx,%r14)
movq $-0x1, 0x1c(%rbx,%r14)
movq 0x50(%rsp), %r15
movslq 0xc(%r15), %rax
movl %eax, 0x18(%rbx,%r14)
leaq 0x160c5(%rip), %rbp     # 0x30e40
cmpl $0x4, (%rbp)
jl 0x1adb9
movq 0x8(%rsp), %rsi
movslq 0xc(%rsi), %rdx
movslq 0x8(%rsi), %rcx
movq (%rsi), %r8
movq 0xb8(%rsp), %rsi
movq 0x10(%rsi), %rsi
leaq (%rax,%rax,4), %rax
movq 0x18(%rsi,%rax,8), %r9
leaq 0xdeac(%rip), %rdi      # 0x28c59
movq 0x78(%rsp), %rsi
xorl %eax, %eax
callq 0xe215
leaq (%rbx,%r14), %rcx
leaq (%rbx,%r14), %rax
addq $0x1c, %rax
movq %rax, 0x48(%rsp)
movq 0x8(%rsp), %r12
cmpl $0x0, 0x8(%r12)
movq %rcx, 0x68(%rsp)
je 0x1b0c1
movslq 0x8(%r12), %rdi
leaq 0xd9e4(%rip), %rsi      # 0x287d0
movl $0x323, %edx            # imm = 0x323
leaq 0xdd8a(%rip), %rcx      # 0x28b82
callq 0x22d2b
movq %rax, %r14
movl 0x8(%r12), %eax
testl %eax, %eax
movq 0xc0(%rsp), %rsi
jle 0x1ae32
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %edx, %edx
addl %ecx, %eax
cltq
movb (%rsi,%rax), %al
movb %al, (%r14,%rdx)
incq %rdx
movslq 0x8(%r12), %rax
decl %ecx
cmpq %rax, %rdx
jl 0x1ae18
movq (%r12), %r15
movq 0x30(%rsp), %rbx
movq %r15, %rdi
subq %rbx, %rdi
leaq 0xd988(%rip), %rsi      # 0x287d0
movl $0x326, %edx            # imm = 0x326
leaq 0xdd2e(%rip), %rcx      # 0x28b82
callq 0x22d2b
movq %rax, %r13
subq %rbx, %r15
movq %r15, %rcx
movq 0x90(%rsp), %rsi
jle 0x1ae7a
movb -0x1(%rsi,%rcx), %dl
movb %dl, (%rax)
incq %rax
decq %rcx
jne 0x1ae6c
xorl %ecx, %ecx
leaq 0xd6c9(%rip), %rbx      # 0x2854c
movq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rax
movl 0x24(%rax), %eax
movq %rcx, 0xb0(%rsp)
shll %cl, %eax
movq %rax, 0x28(%rsp)
movl (%rdi), %eax
movl %eax, 0x58(%rsp)
cmpl $0x4, (%rbp)
movq 0x8(%rsp), %rbp
jl 0x1af22
leaq 0xdddb(%rip), %rdi      # 0x28c92
xorl %eax, %eax
callq 0x3060
testq %r15, %r15
jle 0x1aedd
xorl %r12d, %r12d
movzbl (%r13,%r12), %eax
movsbl (%rax,%rbx), %edi
callq 0x34c0
incq %r12
cmpq %r12, %r15
jne 0x1aec6
movl $0xa, %edi
callq 0x34c0
leaq 0xddb5(%rip), %rdi      # 0x28ca3
xorl %eax, %eax
callq 0x3060
cmpl $0x0, 0x8(%rbp)
jle 0x1af18
xorl %r12d, %r12d
movzbl (%r14,%r12), %eax
movsbl (%rax,%rbx), %edi
callq 0x34c0
incq %r12
movslq 0x8(%rbp), %rax
cmpq %rax, %r12
jl 0x1aefe
movl $0xa, %edi
callq 0x34c0
movl 0x8(%rbp), %edi
movq 0x10(%rsp), %rcx
movq %rbp, %rdx
movl (%rcx), %ebp
movl 0x8(%rcx), %eax
movq %rax, 0xa8(%rsp)
movl 0xc(%rcx), %eax
movq %rax, 0xa0(%rsp)
movl 0x10(%rcx), %r11d
movl 0x14(%rcx), %r12d
movl 0x1c(%rcx), %eax
movl 0x28(%rcx), %ebx
imull 0xc(%rdx), %ebp
subq $0x8, %rsp
movq %r14, %rsi
movl %r15d, %edx
movq %r13, %rcx
movl $0x16, %r8d
movq 0xe0(%rsp), %r9
leaq 0x88(%rsp), %r10
pushq %r10
leaq 0x34(%rsp), %r10
pushq %r10
leaq 0x74(%rsp), %r10
pushq %r10
leaq 0x80(%rsp), %r10
pushq %r10
leaq 0x8c(%rsp), %r10
pushq %r10
pushq %rbp
pushq %rbx
pushq %rax
pushq 0x70(%rsp)
pushq %r12
pushq %r11
pushq 0x100(%rsp)
pushq 0x110(%rsp)
callq 0x23cae
addq $0x70, %rsp
movq 0x48(%rsp), %rdi
movl %eax, (%rdi)
leaq 0x15e72(%rip), %rbp     # 0x30e40
cmpl $0x4, (%rbp)
movl 0x58(%rsp), %ebx
jl 0x1b00b
movl 0x80(%rsp), %r8d
leaq 0xdccd(%rip), %rdi      # 0x28cb4
movl %ebx, %esi
movl %eax, %edx
movq 0x28(%rsp), %rcx
xorl %eax, %eax
callq 0x3060
movq 0x14faa(%rip), %rax     # 0x2ffa8
movq (%rax), %rdi
callq 0x3370
movq 0x48(%rsp), %rdi
cmpl %ebx, (%rdi)
leaq 0xd538(%rip), %rbx      # 0x2854c
movq 0xb0(%rsp), %rdx
je 0x1b042
movq 0x28(%rsp), %rcx
movl %ecx, %eax
sarl %eax
sarl $0x2, %ecx
addl %eax, %ecx
cmpl %ecx, 0x80(%rsp)
jl 0x1b042
leal 0x1(%rdx), %eax
testl %edx, %edx
movl %eax, %ecx
je 0x1ae88
movl 0x24(%rsp), %eax
testl %eax, %eax
jle 0x1b07a
movl (%rdi), %ecx
movq 0x10(%rsp), %rdx
subl 0x1c(%rdx), %ecx
cmpl %ecx, %eax
jle 0x1b07a
movq 0x68(%rsp), %rsi
movl $0x0, 0x10(%rsi)
leaq 0x5c(%rsp), %rcx
leaq 0x24(%rsp), %rax
movq 0x50(%rsp), %r15
movq 0x8(%rsp), %r12
jmp 0x1b09d
movq 0x8(%rsp), %r12
movl 0x8(%r12), %eax
subl 0x64(%rsp), %eax
movq 0x68(%rsp), %rsi
movl %eax, 0x10(%rsi)
leaq 0x60(%rsp), %rcx
movq %rdi, %rax
movq 0x50(%rsp), %r15
movq (%r12), %rdx
movslq (%rcx), %rcx
subq %rcx, %rdx
movq %rdx, (%rsi)
movl (%rax), %eax
movl %eax, 0x20(%rsi)
movq %r14, %rdi
callq 0x3390
movq %r13, %rdi
callq 0x3390
jmp 0x1b0f3
movq 0x10(%rsp), %rax
movl (%rax), %eax
imull 0xc(%r12), %eax
movl %eax, 0x20(%rcx)
movl %eax, 0x1c(%rcx)
movl $0x0, 0x10(%rcx)
movq (%r12), %rax
movq %rax, (%rcx)
movq 0x18(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0xd459(%rip), %rbx      # 0x2854c
movslq 0xc(%r12), %rax
movl 0x8(%r12), %ecx
addl %eax, %ecx
movl 0x44(%rsp), %edx
cmpl %edx, %ecx
movl %edx, %ecx
movq 0x90(%rsp), %rdx
jne 0x1b127
movq 0x68(%rsp), %r10
movl %ecx, 0x14(%r10)
addq (%r12), %rax
movq %rax, 0x8(%r10)
jmp 0x1b3ef
movl 0xc(%r12), %r8d
movq (%r12), %rdi
addq %r8, %rdi
subq 0x30(%rsp), %rdi
testl %edi, %edi
js 0x1b67d
movq 0x48(%rsp), %rsi
movl (%rsi), %eax
movq %rax, 0xd0(%rsp)
addl 0x8(%r12), %r8d
movq %rdi, 0x98(%rsp)
movl %edi, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl %ecx, %r14d
subl %r8d, %r14d
movq %r8, 0xe0(%rsp)
movslq %r8d, %r15
addq 0xc0(%rsp), %r15
leaq (%rdx,%r12), %r13
xorl %ecx, %ecx
movq 0x10(%rsp), %rax
movl 0x24(%rax), %eax
movq %rcx, 0xb0(%rsp)
shll %cl, %eax
movq %rax, 0x18(%rsp)
movl (%rsi), %eax
movl %eax, 0x58(%rsp)
cmpl $0x4, (%rbp)
jl 0x1b237
leaq 0xdb5e(%rip), %rdi      # 0x28d11
xorl %eax, %eax
callq 0x3060
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
addq %r12, %rcx
subq %rcx, %rax
testq %rax, %rax
jle 0x1b1f8
xorl %ebp, %ebp
movzbl (%r13,%rbp), %eax
movsbl (%rax,%rbx), %edi
callq 0x34c0
incq %rbp
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
addq %r12, %rcx
subq %rcx, %rax
cmpq %rbp, %rax
jg 0x1b1d1
movl $0xa, %edi
callq 0x34c0
leaq 0xdb1a(%rip), %rdi      # 0x28d23
xorl %eax, %eax
callq 0x3060
testl %r14d, %r14d
jle 0x1b22d
xorl %ebp, %ebp
movzbl (%r15,%rbp), %eax
movsbl (%rax,%rbx), %edi
callq 0x34c0
incq %rbp
cmpq %rbp, %r14
jne 0x1b217
movl $0xa, %edi
callq 0x34c0
movl 0x38(%rsp), %edx
movl 0x30(%rsp), %eax
addl 0x98(%rsp), %eax
subl %eax, %edx
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ecx
movq %rcx, 0xa8(%rsp)
movl 0xc(%rax), %ecx
movq %rcx, 0xa0(%rsp)
movl 0x10(%rax), %r11d
movl 0x14(%rax), %ebp
movl 0x20(%rax), %ebx
movl 0x28(%rax), %eax
subq $0x8, %rsp
movl %r14d, %edi
movq %r15, %rsi
movq %r13, %rcx
movl $0x16, %r8d
movq 0xe0(%rsp), %r9
leaq 0x8c(%rsp), %r10
pushq %r10
leaq 0x34(%rsp), %r10
pushq %r10
leaq 0x74(%rsp), %r10
pushq %r10
leaq 0x80(%rsp), %r10
pushq %r10
leaq 0x8c(%rsp), %r10
pushq %r10
pushq 0x100(%rsp)
pushq %rax
pushq %rbx
pushq 0x60(%rsp)
pushq %rbp
pushq %r11
pushq 0x100(%rsp)
pushq 0x110(%rsp)
callq 0x23cae
addq $0x70, %rsp
movq 0x48(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x15b54(%rip), %rbp     # 0x30e40
cmpl $0x4, (%rbp)
movl 0x58(%rsp), %ebx
jl 0x1b329
movl 0x84(%rsp), %r8d
leaq 0xda30(%rip), %rdi      # 0x28d35
movl %ebx, %esi
movl %eax, %edx
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x3060
movq 0x14c8c(%rip), %rax     # 0x2ffa8
movq (%rax), %rdi
callq 0x3370
movq 0x48(%rsp), %rsi
cmpl %ebx, (%rsi)
leaq 0xd21a(%rip), %rbx      # 0x2854c
movq 0xb0(%rsp), %rdx
je 0x1b360
movq 0x18(%rsp), %rcx
movl %ecx, %eax
sarl %eax
sarl $0x2, %ecx
addl %eax, %ecx
cmpl %ecx, 0x84(%rsp)
jl 0x1b360
leal 0x1(%rdx), %eax
testl %edx, %edx
movl %eax, %ecx
je 0x1b185
movl 0x24(%rsp), %eax
testl %eax, %eax
movq 0x50(%rsp), %r15
jle 0x1b3ae
movl (%rsi), %ecx
movq 0x10(%rsp), %rdx
subl 0x20(%rdx), %ecx
cmpl %ecx, %eax
jle 0x1b3ae
movl 0x44(%rsp), %eax
movq 0x68(%rsp), %r10
movl %eax, 0x14(%r10)
movq 0x98(%rsp), %rcx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq 0x30(%rsp), %rcx
movslq 0x5c(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x8(%r10)
leaq 0x24(%rsp), %rsi
jmp 0x1b3e2
movq 0xe0(%rsp), %rax
addl 0x64(%rsp), %eax
movq 0x68(%rsp), %r10
movl %eax, 0x14(%r10)
movq 0x98(%rsp), %rcx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq 0x30(%rsp), %rcx
movslq 0x60(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x8(%r10)
movl (%rsi), %eax
subl 0xd0(%rsp), %eax
addl %eax, 0x20(%r10)
cmpl $0x4, (%rbp)
jl 0x1b427
movl 0x10(%r10), %esi
movl 0x14(%r10), %edx
movq (%r10), %rcx
movq 0x8(%r10), %r8
movl 0x1c(%r10), %r9d
leaq 0xd97c(%rip), %rdi      # 0x28d8b
xorl %eax, %eax
pushq 0x18(%rsp)
pushq 0x30(%rsp)
callq 0x3060
movq 0x78(%rsp), %r10
addq $0x10, %rsp
movl $0x0, 0x38(%r10)
movslq (%r15), %rdx
testq %rdx, %rdx
movq 0x8(%rsp), %r9
jle 0x1b485
movq 0x20(%r15), %rax
movl 0x10(%r10), %ecx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
movl 0x8(%rax,%rsi), %r8d
cmpl %ecx, %r8d
jl 0x1b47c
movslq 0xc(%rax,%rsi), %rdi
addl %edi, %r8d
cmpl 0x14(%r10), %r8d
jg 0x1b47c
movq (%rax,%rsi), %r8
cmpq (%r10), %r8
jl 0x1b47c
addq %rdi, %r8
cmpq 0x8(%r10), %r8
jg 0x1b47c
addl %edi, 0x38(%r10)
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x1b44e
movq 0x18(%rsp), %rax
movq 0x28(%rsp), %rcx
cmpl %eax, %ecx
cmovgl %ecx, %eax
movl %eax, 0x34(%r10)
movl 0xc(%r9), %eax
movl %eax, 0x44(%r10)
movss 0x14(%r15), %xmm0
movss %xmm0, 0x50(%r10)
movq 0x88(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq $-0x8, 0xc8(%rsp)
cmpq $0x1, %rsi
movq 0x78(%rsp), %rcx
jg 0x1aa9f
jmp 0x1b659
testb $0x1, %dl
je 0x1acbb
leaq 0x1595a(%rip), %r14     # 0x30e40
cmpl $0x4, (%r14)
movq 0x8(%rsp), %r12
jl 0x1b540
movslq 0xc(%r12), %rdx
movslq 0x8(%r12), %rcx
movq (%r12), %r8
movq 0x130(%rsp), %rsi
movq 0x18(%rsi), %rsi
movl 0x10(%rsi,%rax), %r9d
movl 0x14(%rsi,%rax), %edi
movupd (%rsi,%rax), %xmm0
subq $0x20, %rsp
movupd %xmm0, 0x8(%rsp)
movl %edi, (%rsp)
leaq 0xd663(%rip), %rdi      # 0x28b90
movq 0x98(%rsp), %rsi
xorl %eax, %eax
callq 0x3060
addq $0x20, %rsp
movq 0x50(%rsp), %r15
movslq (%r15), %rax
movq 0x70(%rsp), %rsi
movl %esi, %ecx
cmpq %rax, %rsi
jge 0x1b610
xorl %ecx, %ecx
movq 0xc8(%rsp), %rdx
movq (%rdx,%rcx,8), %rdx
testq %rdx, %rdx
je 0x1b5fb
movq 0x20(%r15), %rdi
movl %edx, %edx
leaq (%rdx,%rdx,2), %r8
movl 0xc(%rdi,%r8,8), %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movl 0xc(%r12), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
mulsd 0xd207(%rip), %xmm1     # 0x287a0
ucomisd %xmm0, %xmm1
ja 0x1b5fb
leaq (%rdi,%r8,8), %rdi
movl 0x8(%r12), %r10d
movl 0x8(%rdi), %r9d
movl %r10d, %r8d
subl %r9d, %r8d
jg 0x1b5da
leal (%r10,%rsi), %r11d
subl %r9d, %r11d
movl %esi, %ebx
sarl $0x2, %ebx
cmpl %ebx, %r11d
jl 0x1b5da
movl %r9d, %r11d
subl %r10d, %r11d
movslq %r11d, %r11
movq (%rdi), %rbx
subq (%r12), %rbx
cmpq %r11, %rbx
jne 0x1b650
cmpl %r10d, %r9d
jg 0x1b5fb
subl %r10d, %edx
addl %r9d, %edx
sarl $0x2, %esi
cmpl %esi, %edx
jl 0x1b5fb
movslq %r8d, %rdx
movq (%r12), %rsi
subq (%rdi), %rsi
cmpq %rdx, %rsi
jne 0x1b650
incq %rcx
movq 0x70(%rsp), %rsi
leal (%rcx,%rsi), %edx
cmpl %edx, %eax
jg 0x1b55a
movl %eax, %ecx
cmpl %eax, %ecx
jne 0x1b62e
movq 0x88(%rsp), %rdi
movq 0x78(%rsp), %rax
movq $0x0, (%rdi,%rax,8)
jmp 0x1b4b9
cmpl $0x4, (%r14)
jl 0x1acbb
leaq 0xd5af(%rip), %rdi      # 0x28bee
movq 0x78(%rsp), %rsi
xorl %eax, %eax
callq 0x3060
jmp 0x1acbb
movq 0x70(%rsp), %rsi
addl %esi, %ecx
jmp 0x1b610
callq 0x3390
movq 0x90(%rsp), %rdi
callq 0x3390
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd685(%rip), %rdi      # 0x28d09
leaq 0xd145(%rip), %rsi      # 0x287d0
leaq 0xd46c(%rip), %rcx      # 0x28afe
movl $0x343, %edx            # imm = 0x343
callq 0x31a0
leaq 0xd44d(%rip), %rdi      # 0x28af0
leaq 0xd126(%rip), %rsi      # 0x287d0
leaq 0xd44d(%rip), %rcx      # 0x28afe
movl $0x2e9, %edx            # imm = 0x2E9
callq 0x31a0