// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/bwamem_pair.c
// Function: mem_sam_pe
int mem_sam_pe(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, const mem_pestat_t pes[4], uint64_t id, bseq1_t s[2], mem_alnreg_v a[2])
{
	extern int mem_mark_primary_se(const mem_opt_t *opt, int n, mem_alnreg_t *a, int64_t id);
	extern int mem_approx_mapq_se(const mem_opt_t *opt, const mem_alnreg_t *a);
	extern void mem_reg2sam(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, bseq1_t *s, mem_alnreg_v *a, int extra_flag, const mem_aln_t *m);
	extern char **mem_gen_alt(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, const mem_alnreg_v *a, int l_query, const char *query);

	int n = 0, i, j, z[2], o, subo, n_sub, extra_flag = 1, n_pri[2], n_aa[2];
	kstring_t str;
	mem_aln_t h[2], g[2], aa[2][2];

	str.l = str.m = 0; str.s = 0;
	memset(h, 0, sizeof(mem_aln_t) * 2);
	memset(g, 0, sizeof(mem_aln_t) * 2);
	n_aa[0] = n_aa[1] = 0;
	if (!(opt->flag & MEM_F_NO_RESCUE)) { // then perform SW for the best alignment
		mem_alnreg_v b[2];
		kv_init(b[0]); kv_init(b[1]);
		for (i = 0; i < 2; ++i)
			for (j = 0; j < a[i].n; ++j)
				if (a[i].a[j].score >= a[i].a[0].score  - opt->pen_unpaired)
					kv_push(mem_alnreg_t, b[i], a[i].a[j]);
		for (i = 0; i < 2; ++i)
			for (j = 0; j < b[i].n && j < opt->max_matesw; ++j)
				n += mem_matesw(opt, bns, pac, pes, &b[i].a[j], s[!i].l_seq, (uint8_t*)s[!i].seq, &a[!i]);
		free(b[0].a); free(b[1].a);
	}
	n_pri[0] = mem_mark_primary_se(opt, a[0].n, a[0].a, id<<1|0);
	n_pri[1] = mem_mark_primary_se(opt, a[1].n, a[1].a, id<<1|1);
	if (opt->flag&MEM_F_NOPAIRING) goto no_pairing;
	// pairing single-end hits
	if (n_pri[0] && n_pri[1] && (o = mem_pair(opt, bns, pac, pes, s, a, id, &subo, &n_sub, z, n_pri)) > 0) {
		int is_multi[2], q_pe, score_un, q_se[2];
		char **XA[2];
		// check if an end has multiple hits even after mate-SW
		for (i = 0; i < 2; ++i) {
			for (j = 1; j < n_pri[i]; ++j)
				if (a[i].a[j].secondary < 0 && a[i].a[j].score >= opt->T) break;
			is_multi[i] = j < n_pri[i]? 1 : 0;
		}
		if (is_multi[0] || is_multi[1]) goto no_pairing; // TODO: in rare cases, the true hit may be long but with low score
		// compute mapQ for the best SE hit
		score_un = a[0].a[0].score + a[1].a[0].score - opt->pen_unpaired;
		//q_pe = o && subo < o? (int)(MEM_MAPQ_COEF * (1. - (double)subo / o) * log(a[0].a[z[0]].seedcov + a[1].a[z[1]].seedcov) + .499) : 0;
		subo = subo > score_un? subo : score_un;
		q_pe = raw_mapq(o - subo, opt->a);
		if (n_sub > 0) q_pe -= (int)(4.343 * log(n_sub+1) + .499);
		if (q_pe < 0) q_pe = 0;
		if (q_pe > 60) q_pe = 60;
		q_pe = (int)(q_pe * (1. - .5 * (a[0].a[0].frac_rep + a[1].a[0].frac_rep)) + .499);
		// the following assumes no split hits
		if (o > score_un) { // paired alignment is preferred
			mem_alnreg_t *c[2];
			c[0] = &a[0].a[z[0]]; c[1] = &a[1].a[z[1]];
			for (i = 0; i < 2; ++i) {
				if (c[i]->secondary >= 0)
					c[i]->sub = a[i].a[c[i]->secondary].score, c[i]->secondary = -2;
				q_se[i] = mem_approx_mapq_se(opt, c[i]);
			}
			q_se[0] = q_se[0] > q_pe? q_se[0] : q_pe < q_se[0] + 40? q_pe : q_se[0] + 40;
			q_se[1] = q_se[1] > q_pe? q_se[1] : q_pe < q_se[1] + 40? q_pe : q_se[1] + 40;
			extra_flag |= 2;
			// cap at the tandem repeat score
			q_se[0] = q_se[0] < raw_mapq(c[0]->score - c[0]->csub, opt->a)? q_se[0] : raw_mapq(c[0]->score - c[0]->csub, opt->a);
			q_se[1] = q_se[1] < raw_mapq(c[1]->score - c[1]->csub, opt->a)? q_se[1] : raw_mapq(c[1]->score - c[1]->csub, opt->a);
		} else { // the unpaired alignment is preferred
			z[0] = z[1] = 0;
			q_se[0] = mem_approx_mapq_se(opt, &a[0].a[0]);
			q_se[1] = mem_approx_mapq_se(opt, &a[1].a[0]);
		}
		for (i = 0; i < 2; ++i) {
			int k = a[i].a[z[i]].secondary_all;
			if (k >= 0 && k < n_pri[i]) { // switch secondary and primary if both of them are non-ALT
				assert(a[i].a[k].secondary_all < 0);
				for (j = 0; j < a[i].n; ++j)
					if (a[i].a[j].secondary_all == k || j == k)
						a[i].a[j].secondary_all = z[i];
				a[i].a[z[i]].secondary_all = -1;
			}
		}
		if (!(opt->flag & MEM_F_ALL)) {
			for (i = 0; i < 2; ++i)
				XA[i] = mem_gen_alt(opt, bns, pac, &a[i], s[i].l_seq, s[i].seq);
		} else XA[0] = XA[1] = 0;
		// write SAM
		for (i = 0; i < 2; ++i) {
			h[i] = mem_reg2aln(opt, bns, pac, s[i].l_seq, s[i].seq, &a[i].a[z[i]]);
			h[i].mapq = q_se[i];
			h[i].flag |= 0x40<<i | extra_flag;
			h[i].XA = XA[i]? XA[i][z[i]] : 0;
			aa[i][n_aa[i]++] = h[i];
			if (n_pri[i] < a[i].n) { // the read has ALT hits
				mem_alnreg_t *p = &a[i].a[n_pri[i]];
				if (p->score < opt->T || p->secondary >= 0 || !p->is_alt) continue;
				g[i] = mem_reg2aln(opt, bns, pac, s[i].l_seq, s[i].seq, p);
				g[i].flag |= 0x800 | 0x40<<i | extra_flag;
				g[i].XA = XA[i]? XA[i][n_pri[i]] : 0;
				aa[i][n_aa[i]++] = g[i];
			}
		}
		for (i = 0; i < n_aa[0]; ++i)
			mem_aln2sam(opt, bns, &str, &s[0], n_aa[0], aa[0], i, &h[1]); // write read1 hits
		s[0].sam = strdup(str.s); str.l = 0;
		for (i = 0; i < n_aa[1]; ++i)
			mem_aln2sam(opt, bns, &str, &s[1], n_aa[1], aa[1], i, &h[0]); // write read2 hits
		s[1].sam = str.s;
		if (strcmp(s[0].name, s[1].name) != 0) err_fatal(__func__, "paired reads have different names: \"%s\", \"%s\"\n", s[0].name, s[1].name);
		// free
		for (i = 0; i < 2; ++i) {
			free(h[i].cigar); free(g[i].cigar);
			if (XA[i] == 0) continue;
			for (j = 0; j < a[i].n; ++j) free(XA[i][j]);
			free(XA[i]);
		}
	} else goto no_pairing;
	return n;

no_pairing:
	for (i = 0; i < 2; ++i) {
		int which = -1;
		if (a[i].n) {
			if (a[i].a[0].score >= opt->T) which = 0;
			else if (n_pri[i] < a[i].n && a[i].a[n_pri[i]].score >= opt->T)
				which = n_pri[i];
		}
		if (which >= 0) h[i] = mem_reg2aln(opt, bns, pac, s[i].l_seq, s[i].seq, &a[i].a[which]);
		else h[i] = mem_reg2aln(opt, bns, pac, s[i].l_seq, s[i].seq, 0);
	}
	if (!(opt->flag & MEM_F_NOPAIRING) && h[0].rid == h[1].rid && h[0].rid >= 0) { // if the top hits from the two ends constitute a proper pair, flag it.
		int64_t dist;
		int d;
		d = mem_infer_dir(bns->l_pac, a[0].a[0].rb, a[1].a[0].rb, &dist);
		if (!pes[d].failed && dist >= pes[d].low && dist <= pes[d].high) extra_flag |= 2;
	}
	mem_reg2sam(opt, bns, pac, &s[0], &a[0], 0x41|extra_flag, &h[1]);
	mem_reg2sam(opt, bns, pac, &s[1], &a[1], 0x81|extra_flag, &h[0]);
	if (strcmp(s[0].name, s[1].name) != 0) err_fatal(__func__, "paired reads have different names: \"%s\", \"%s\"\n", s[0].name, s[1].name);
	free(h[0].cigar); free(h[1].cigar);
	return n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2b8, %rsp            # imm = 0x2B8
movq %r9, 0x10(%rsp)
movq %r8, %r15
movq %rcx, %r14
movq %rdx, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %r10
movq 0x2f0(%rsp), %r12
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xd0(%rsp)
movapd %xmm0, 0xe0(%rsp)
movapd %xmm0, 0xf0(%rsp)
movapd %xmm0, 0x100(%rsp)
movapd %xmm0, 0x110(%rsp)
movapd %xmm0, 0x160(%rsp)
movapd %xmm0, 0x170(%rsp)
movapd %xmm0, 0x180(%rsp)
movapd %xmm0, 0x190(%rsp)
movapd %xmm0, 0x1a0(%rsp)
movapd %xmm0, 0x1b0(%rsp)
andq $0x0, 0x48(%rsp)
movapd %xmm0, 0x1c0(%rsp)
movl $0x0, 0x38(%rsp)
testb $0x20, 0x48(%rdi)
movq %rdi, (%rsp)
jne 0x1c9ec
movq %r15, 0x58(%rsp)
movq %r14, 0x18(%rsp)
andq $0x0, 0x208(%rsp)
movapd %xmm0, 0x1d0(%rsp)
movupd %xmm0, 0x1e8(%rsp)
andq $0x0, 0x1f8(%rsp)
xorl %eax, %eax
pushq $0xc
popq %r15
cmpq $0x2, %rax
je 0x1c917
movq %rax, 0x38(%rsp)
shlq $0x5, %rax
leaq (%r12,%rax), %r14
leaq (%rsp,%rax), %rbx
addq $0x1d0, %rbx            # imm = 0x1D0
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq %r13, (%r14)
jbe 0x1c902
movq 0x18(%r14), %rsi
movl 0x1c(%rsi), %eax
subl 0x18(%r10), %eax
cmpl %eax, 0x1c(%rsi,%rbp)
jl 0x1c8f6
movq (%rbx), %r12
movq 0x18(%rbx), %rax
cmpq 0x8(%rbx), %r12
jne 0x1c8de
leaq (%r12,%r12), %rcx
testq %r12, %r12
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x8(%rbx)
imulq $0x60, %rcx, %rsi
movq %rax, %rdi
leaq 0x7a2f(%rip), %rdx      # 0x242f0
movl $0x10f, %ecx            # imm = 0x10F
leaq 0x7c1d(%rip), %r8       # 0x244ea
callq 0x1e0b4
movq (%rsp), %r10
movq %rax, 0x18(%rbx)
movq 0x18(%r14), %rsi
leaq 0x1(%r12), %rcx
movq %rcx, (%rbx)
imulq $0x60, %r12, %rdi
addq %rax, %rdi
addq %rbp, %rsi
movq %r15, %rcx
rep  movsq (%rsi), %es:(%rdi)
incq %r13
addq $0x60, %rbp
jmp 0x1c87e
movq 0x38(%rsp), %rax
incq %rax
movq 0x2f0(%rsp), %r12
jmp 0x1c857
xorl %ecx, %ecx
movl $0x0, 0x38(%rsp)
cmpq $0x2, %rcx
je 0x1c9c4
movq %rcx, %rax
shlq $0x5, %rax
leaq (%rsp,%rax), %r14
addq $0x1d0, %r14            # imm = 0x1D0
movq (%r14), %r15
movq %rcx, 0x30(%rsp)
movl %ecx, %r13d
xorq $0x1, %r13
imulq $0x30, %r13, %rbp
addq 0x10(%rsp), %rbp
shlq $0x5, %r13
addq %r12, %r13
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x1c9af
movslq 0xb8(%r10), %rax
cmpq %rax, %r12
jge 0x1c9af
movq 0x18(%r14), %r8
addq %rbx, %r8
movl (%rbp), %r9d
movq %r10, %rdi
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rcx
pushq %r13
pushq 0x18(%rbp)
callq 0x1bb86
movq 0x10(%rsp), %r10
addq $0x10, %rsp
addl %eax, 0x38(%rsp)
incq %r12
addq $0x60, %rbx
jmp 0x1c961
movq 0x30(%rsp), %rcx
incq %rcx
movq 0x2f0(%rsp), %r12
jmp 0x1c921
movq 0x1e8(%rsp), %rdi
callq 0x3390
movq 0x208(%rsp), %rdi
callq 0x3390
movq (%rsp), %r10
movq 0x18(%rsp), %r14
movq 0x58(%rsp), %r15
movl (%r12), %esi
movq 0x18(%r12), %rdx
leaq (%r15,%r15), %rcx
movq %r10, %rdi
callq 0x17c4f
movl %eax, %ebx
movl %eax, 0x28(%rsp)
movl 0x20(%r12), %esi
movq 0x38(%r12), %rdx
leaq 0x1(,%r15,2), %rcx
movq (%rsp), %rdi
callq 0x17c4f
movq (%rsp), %r11
movl %eax, 0x2c(%rsp)
testb $0x4, 0x48(%r11)
jne 0x1caf6
testl %ebx, %ebx
setne %cl
testl %eax, %eax
setne %al
andb %cl, %al
cmpb $0x1, %al
jne 0x1caf6
subq $0x8, %rsp
leaq 0x30(%rsp), %rax
leaq 0x58(%rsp), %rdx
leaq 0x68(%rsp), %r8
leaq 0x6c(%rsp), %r10
movq %r11, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rcx
movq %r12, %r9
pushq %rax
pushq %rdx
pushq %r8
pushq %r10
pushq %r15
callq 0x1c210
movq 0x30(%rsp), %r11
addq $0x30, %rsp
testl %eax, %eax
jle 0x1caf6
movl %eax, %r15d
xorl %eax, %eax
pushq $0x1
popq %rcx
cmpq $0x2, %rax
je 0x1cae8
movslq 0x28(%rsp,%rax,4), %rdx
movq %rax, %rsi
shlq $0x5, %rsi
addq %r12, %rsi
addq $0x18, %rsi
movl $0x9c, %r8d
movq %rcx, %rdi
cmpq %rdx, %rdi
jge 0x1cad6
movq (%rsi), %r9
cmpl $0x0, (%r9,%r8)
jns 0x1cacd
movl -0x20(%r9,%r8), %r9d
cmpl 0x44(%r11), %r9d
jge 0x1cad6
incq %rdi
addq $0x60, %r8
jmp 0x1cab3
xorl %esi, %esi
cmpq %rdx, %rdi
setl %sil
movl %esi, 0x78(%rsp,%rax,4)
incq %rax
jmp 0x1ca91
movl 0x7c(%rsp), %eax
orl 0x78(%rsp), %eax
je 0x1cd24
leaq 0x20(%r12), %rax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %rbp
leaq 0xb0(%rsp), %r15
xorl %ebx, %ebx
movq 0x10(%rsp), %r13
cmpq $0x8, %rbx
je 0x1cbf3
movq (%r12,%rbx,8), %rsi
testq %rsi, %rsi
je 0x1cb7c
movq 0x18(%r12,%rbx,8), %rax
movl 0x44(%r11), %edx
xorl %ecx, %ecx
cmpl %edx, 0x1c(%rax)
jge 0x1cb4f
movl 0x28(%rsp,%rbx), %ecx
movslq %ecx, %rdi
cmpq %rdi, %rsi
jbe 0x1cb7c
testl %ecx, %ecx
js 0x1cb7c
imulq $0x60, %rdi, %rsi
cmpl %edx, 0x1c(%rax,%rsi)
jl 0x1cb7c
movl (%r13), %r8d
movq 0x18(%r13), %r9
movl %ecx, %ecx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
subq $0x8, %rsp
leaq 0x1d8(%rsp), %rdi
movq %r11, %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
pushq %rax
jmp 0x1cb9f
movl (%r13), %r8d
movq 0x18(%r13), %r9
subq $0x8, %rsp
leaq 0x1d8(%rsp), %rdi
movq %r11, %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
pushq $0x0
callq 0x1a4cd
popq %rax
popq %rcx
movq 0x200(%rsp), %rax
movq %rax, 0x30(%r15)
movupd 0x1d0(%rsp), %xmm0
movupd 0x1e0(%rsp), %xmm1
movupd 0x1f0(%rsp), %xmm2
movupd %xmm2, 0x20(%r15)
movupd %xmm1, 0x10(%r15)
movupd %xmm0, (%r15)
addq $0x4, %rbx
addq $0x30, %r13
addq $0x38, %r15
movq (%rsp), %r11
jmp 0x1cb12
testb $0x4, 0x48(%r11)
jne 0x1cc6e
movl 0xb8(%rsp), %eax
testl %eax, %eax
js 0x1cc6e
cmpl 0xf0(%rsp), %eax
jne 0x1cc6e
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%r12), %rax
movq 0x38(%r12), %rcx
movq (%rax), %rsi
movq (%rcx), %rdx
leaq 0x1d0(%rsp), %rcx
callq 0x1bb45
movl %eax, %eax
shlq $0x5, %rax
pushq $0x1
popq %rbp
cmpl $0x0, 0x8(%r14,%rax)
jne 0x1cc6a
addq %rax, %r14
movq 0x1d0(%rsp), %rax
movslq (%r14), %rcx
cmpq %rcx, %rax
jl 0x1cc6a
movslq 0x4(%r14), %rcx
xorl %edx, %edx
cmpq %rcx, %rax
setle %dl
leal 0x1(,%rdx,2), %ebp
movq (%rsp), %r11
movl %ebp, %r9d
orl $0x41, %r9d
leaq 0xe8(%rsp), %rax
subq $0x8, %rsp
movq %r11, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq 0x28(%rsp), %r13
movq %r13, %rdx
movq 0x18(%rsp), %r14
movq %r14, %rcx
movq %r12, %r8
pushq %rax
movq %r11, %r15
callq 0x1a112
popq %rax
popq %rcx
leaq 0x30(%r14), %rcx
orl $0x81, %ebp
subq $0x8, %rsp
leaq 0xb8(%rsp), %rax
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq 0x20(%rsp), %r8
movl %ebp, %r9d
pushq %rax
callq 0x1a112
popq %rax
popq %rcx
movq 0x8(%r14), %rbx
movq 0x38(%r14), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x34d0
testl %eax, %eax
jne 0x1d438
movq 0xc8(%rsp), %rdi
callq 0x3390
movq 0x100(%rsp), %rdi
callq 0x3390
movl 0x38(%rsp), %eax
addq $0x2b8, %rsp            # imm = 0x2B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r12), %rsi
movq 0x38(%r12), %rax
movl 0x1c(%rax), %ebx
addl 0x1c(%rsi), %ebx
subl 0x18(%r11), %ebx
movl 0x64(%rsp), %ecx
cmpl %ebx, %ecx
cmovlel %ebx, %ecx
movl %r15d, %edx
subl %ecx, %edx
cvtsi2sd %edx, %xmm0
mulsd 0x752e(%rip), %xmm0     # 0x24280
cvtsi2sdl (%r11), %xmm1
divsd %xmm1, %xmm0
addsd 0x3a3d(%rip), %xmm0     # 0x207a0
cvttsd2si %xmm0, %ebp
movl 0x60(%rsp), %ecx
testl %ecx, %ecx
jle 0x1cd9e
incl %ecx
cvtsi2sd %ecx, %xmm0
callq 0x3250
movq (%rsp), %r11
mulsd 0x6ab2(%rip), %xmm0     # 0x23838
addsd 0x3a12(%rip), %xmm0     # 0x207a0
cvttsd2si %xmm0, %eax
subl %eax, %ebp
movq 0x18(%r12), %rsi
movq 0x38(%r12), %rax
xorl %edx, %edx
testl %ebp, %ebp
cmovgl %ebp, %edx
cmpl $0x3c, %edx
pushq $0x3c
popq %rcx
cmovll %edx, %ecx
cmpl %ebx, %r15d
jle 0x1ce62
cvtsi2sd %ecx, %xmm0
movss 0x50(%rsi), %xmm1
addss 0x50(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd 0x74b7(%rip), %xmm1     # 0x24288
addsd 0x66ef(%rip), %xmm1     # 0x234c8
mulsd %xmm0, %xmm1
addsd 0x39bb(%rip), %xmm1     # 0x207a0
cvttsd2si %xmm1, %ebp
movslq 0x50(%rsp), %rcx
imulq $0x60, %rcx, %rbx
addq %rsi, %rbx
movq %rbx, 0x1d0(%rsp)
movslq 0x54(%rsp), %rcx
imulq $0x60, %rcx, %r14
addq %rax, %r14
movq %r14, 0x1d8(%rsp)
xorl %r15d, %r15d
cmpq $0x8, %r15
je 0x1ce9e
movq 0x1d0(%rsp,%r15,2), %rsi
movslq 0x3c(%rsi), %rax
testq %rax, %rax
movq (%rsp), %rdi
js 0x1ce52
movq 0x2f0(%rsp), %rcx
movq 0x18(%rcx,%r15,8), %rcx
imulq $0x60, %rax, %rax
movl 0x1c(%rcx,%rax), %eax
movl %eax, 0x24(%rsi)
movl $0xfffffffe, 0x3c(%rsi) # imm = 0xFFFFFFFE
callq 0x19f48
movl %eax, 0x40(%rsp,%r15)
addq $0x4, %r15
jmp 0x1ce14
andq $0x0, 0x50(%rsp)
movq %r11, %rdi
callq 0x19f48
movl %eax, 0x40(%rsp)
movq 0x2f0(%rsp), %r12
movq 0x38(%r12), %rsi
movq (%rsp), %rdi
callq 0x19f48
movq (%rsp), %r10
movl %eax, 0x44(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x30(%rsp)
jmp 0x1cf55
movq (%rsp), %r10
cvtsi2sdl (%r10), %xmm2
movq 0x40(%rsp), %xmm1
movd %ebp, %xmm0
pshufd $0x50, %xmm0, %xmm3     # xmm3 = xmm0[0,0,1,1]
movdqa %xmm1, %xmm0
pcmpgtd %xmm3, %xmm0
movdqa 0x740a(%rip), %xmm4     # 0x242d0
paddd %xmm1, %xmm4
movdqa %xmm3, %xmm5
pcmpgtd %xmm4, %xmm5
pand %xmm5, %xmm4
pandn %xmm3, %xmm5
por %xmm4, %xmm5
pand %xmm0, %xmm1
pandn %xmm5, %xmm0
movd 0x1c(%r14), %xmm3
movd 0x1c(%rbx), %xmm4
punpckldq %xmm3, %xmm4    # xmm4 = xmm4[0],xmm3[0],xmm4[1],xmm3[1]
movd 0x2c(%r14), %xmm3
movd 0x2c(%rbx), %xmm5
punpckldq %xmm3, %xmm5    # xmm5 = xmm5[0],xmm3[0],xmm5[1],xmm3[1]
psubd %xmm5, %xmm4
cvtdq2pd %xmm4, %xmm3
mulpd 0x73cc(%rip), %xmm3     # 0x242e0
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
divpd %xmm2, %xmm3
addpd 0x737c(%rip), %xmm3     # 0x242a0
cvttpd2dq %xmm3, %xmm2
por %xmm1, %xmm0
movapd %xmm2, %xmm1
pcmpgtd %xmm0, %xmm1
pand %xmm1, %xmm0
pandn %xmm2, %xmm1
por %xmm0, %xmm1
movq %xmm1, 0x40(%rsp)
pushq $0x3
popq %rax
movl %eax, 0x30(%rsp)
movq 0x2f0(%rsp), %r12
xorl %eax, %eax
cmpq $0x2, %rax
je 0x1cfc7
movq %rax, %r8
shlq $0x5, %r8
movq 0x18(%r12,%r8), %rcx
movslq 0x50(%rsp,%rax,4), %rdx
imulq $0x60, %rdx, %rdi
movslq 0x40(%rcx,%rdi), %rsi
testq %rsi, %rsi
js 0x1cfc2
cmpl 0x28(%rsp,%rax,4), %esi
jge 0x1cfc2
imulq $0x60, %rsi, %r9
cmpl $0x0, 0x40(%rcx,%r9)
jns 0x1d453
addq %r12, %r8
addq %rcx, %rdi
addq $0x40, %rdi
movq (%r8), %r8
addq $0x40, %rcx
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x1cfbf
cmpq %r9, %rsi
je 0x1cfb4
cmpl %esi, (%rcx)
jne 0x1cfb6
movl %edx, (%rcx)
incq %r9
addq $0x60, %rcx
jmp 0x1cfa6
orl $-0x1, (%rdi)
incq %rax
jmp 0x1cf57
testb $0x8, 0x48(%r10)
jne 0x1d014
xorl %r14d, %r14d
movq %r12, %rbx
movq 0x10(%rsp), %r15
cmpq $0x2, %r14
je 0x1d021
movl (%r15), %r8d
movq 0x18(%r15), %r9
movq %r10, %rdi
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %rbx, %rcx
callq 0x1dac9
movq (%rsp), %r10
movq %rax, 0x80(%rsp,%r14,8)
incq %r14
addq $0x30, %r15
addq $0x20, %rbx
jmp 0x1cfd9
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
xorl %esi, %esi
xorl %r14d, %r14d
cmpq $0x8, %r14
je 0x1d2c1
movl (%rax), %r8d
movq %rax, 0x18(%rsp)
movq 0x18(%rax), %r9
movslq 0x50(%rsp,%r14), %r15
imulq $0x60, %r15, %rax
addq 0x18(%r12,%r14,8), %rax
subq $0x8, %rsp
leaq 0x130(%rsp), %rdi
movl %esi, %ebp
movq %r10, %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
pushq %rax
callq 0x1a4cd
movl %ebp, %esi
addq $0x10, %rsp
movq 0x158(%rsp), %rax
movq %rax, 0xe0(%rsp,%rbx)
movups 0x128(%rsp), %xmm0
movups 0x138(%rsp), %xmm1
movups 0x148(%rsp), %xmm2
movups %xmm2, 0xd0(%rsp,%rbx)
movups %xmm1, 0xc0(%rsp,%rbx)
movups %xmm0, 0xb0(%rsp,%rbx)
movl 0x40(%rsp,%r14), %eax
movzbl %al, %eax
movl 0xc0(%rsp,%rbx), %edx
movl $0xfffffc03, %ecx       # imm = 0xFFFFFC03
andl %ecx, %edx
pushq $0x40
popq %r13
movl %esi, %ecx
shll %cl, %r13d
leal (%rdx,%rax,4), %eax
movl %eax, 0xc0(%rsp,%rbx)
movl 0xbc(%rsp,%rbx), %eax
orl 0x30(%rsp), %eax
orl %r13d, %eax
movl %eax, 0xbc(%rsp,%rbx)
movq 0x80(%rsp,%r14,2), %rdi
testq %rdi, %rdi
je 0x1d109
movq (%rdi,%r15,8), %rax
jmp 0x1d10b
xorl %eax, %eax
leaq (%rsp,%rbx), %rcx
addq $0xb0, %rcx
movq %rax, 0x20(%rcx)
movslq 0x48(%rsp,%r14), %r9
leaq 0x1(%r9), %r8
movl %r8d, 0x48(%rsp,%r14)
imulq $0x38, %r9, %rax
addq %rsp, %rax
addq $0x1d0, %rax            # imm = 0x1D0
movq 0x30(%rcx), %rdx
movq %rdx, 0x30(%rax,%rbx,2)
movupd (%rcx), %xmm0
movupd 0x10(%rcx), %xmm1
movupd 0x20(%rcx), %xmm2
movupd %xmm2, 0x20(%rax,%rbx,2)
movupd %xmm1, 0x10(%rax,%rbx,2)
movupd %xmm0, (%rax,%rbx,2)
movslq 0x28(%rsp,%r14), %r15
cmpq %r15, (%r12,%r14,8)
movq (%rsp), %r10
jbe 0x1d2a9
movq 0x18(%r12,%r14,8), %rax
imulq $0x60, %r15, %rcx
movl 0x1c(%rax,%rcx), %edx
cmpl 0x44(%r10), %edx
jl 0x1d2a9
addq %rcx, %rax
cmpl $0x0, 0x3c(%rax)
jns 0x1d2a9
cmpl $0x40000000, 0x48(%rax) # imm = 0x40000000
jb 0x1d2a9
movq %r9, 0x68(%rsp)
movq %r8, 0x70(%rsp)
movq 0x18(%rsp), %rcx
movl (%rcx), %r8d
movq 0x18(%rcx), %r9
subq $0x8, %rsp
movq %rdi, 0x60(%rsp)
leaq 0x130(%rsp), %rdi
movq %r10, %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
pushq %rax
callq 0x1a4cd
movq 0x68(%rsp), %rcx
addq $0x10, %rsp
movups 0x128(%rsp), %xmm0
movups 0x138(%rsp), %xmm1
movups 0x148(%rsp), %xmm2
movups %xmm0, 0x160(%rsp,%rbx)
orl 0x16c(%rsp,%rbx), %r13d
orl 0x30(%rsp), %r13d
orl $0x800, %r13d           # imm = 0x800
movq 0x158(%rsp), %rax
movq %rax, 0x190(%rsp,%rbx)
movups %xmm2, 0x180(%rsp,%rbx)
movups %xmm1, 0x170(%rsp,%rbx)
movl %r13d, 0x16c(%rsp,%rbx)
testq %rcx, %rcx
je 0x1d24b
movq (%rcx,%r15,8), %rax
jmp 0x1d24d
xorl %eax, %eax
movq 0x70(%rsp), %rdx
movq 0x68(%rsp), %rsi
leaq (%rsp,%rbx), %rcx
addq $0x160, %rcx            # imm = 0x160
movq %rax, 0x20(%rcx)
addl $0x2, %esi
movl %esi, 0x48(%rsp,%r14)
imulq $0x38, %rdx, %rax
addq %rsp, %rax
addq $0x1d0, %rax            # imm = 0x1D0
movq 0x30(%rcx), %rdx
movq %rdx, 0x30(%rax,%rbx,2)
movupd (%rcx), %xmm0
movupd 0x10(%rcx), %xmm1
movupd 0x20(%rcx), %xmm2
movupd %xmm2, 0x20(%rax,%rbx,2)
movupd %xmm1, 0x10(%rax,%rbx,2)
movupd %xmm0, (%rax,%rbx,2)
movq (%rsp), %r10
movl %ebp, %esi
addq $0x4, %r14
incl %esi
addq $0x38, %rbx
movq 0x18(%rsp), %rax
addq $0x30, %rax
jmp 0x1d02d
movl 0x48(%rsp), %ebx
leaq 0xe8(%rsp), %r12
xorl %r13d, %r13d
testl %ebx, %ebx
movl $0x0, %ebp
cmovgl %ebx, %ebp
leaq 0x90(%rsp), %r14
leaq 0x1d0(%rsp), %r15
cmpl %r13d, %ebp
je 0x1d31c
movq %r10, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
movq 0x10(%rsp), %rcx
movl %ebx, %r8d
movq %r15, %r9
pushq %r12
pushq %r13
callq 0x1915c
movq 0x10(%rsp), %r10
addq $0x10, %rsp
incl %r13d
jmp 0x1d2ea
leaq 0x90(%rsp), %rbx
movq 0x10(%rbx), %rdi
leaq 0x6fc1(%rip), %rsi      # 0x242f0
leaq 0x71b4(%rip), %rcx      # 0x244ea
movl $0x160, %edx            # imm = 0x160
callq 0x1e123
movq 0x10(%rsp), %rcx
movq %rax, 0x28(%rcx)
andq $0x0, (%rbx)
movl 0x4c(%rsp), %r14d
leaq 0x30(%rcx), %r15
leaq 0x240(%rsp), %r13
xorl %r12d, %r12d
testl %r14d, %r14d
movl $0x0, %ebp
cmovgl %r14d, %ebp
leaq 0xb0(%rsp), %rbx
cmpl %r12d, %ebp
movq (%rsp), %rdi
je 0x1d3a3
movq 0x8(%rsp), %rsi
leaq 0x90(%rsp), %rdx
movq %r15, %rcx
movl %r14d, %r8d
movq %r13, %r9
pushq %rbx
pushq %r12
callq 0x1915c
popq %rax
popq %rcx
incl %r12d
jmp 0x1d375
movq 0xa0(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, 0x58(%rcx)
movq 0x8(%rcx), %rbx
movq 0x38(%rcx), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x34d0
testl %eax, %eax
jne 0x1d438
xorl %r14d, %r14d
movq 0x2f0(%rsp), %r13
cmpq $0x2, %r14
je 0x1cd0e
imulq $0x38, %r14, %rbx
movq 0xc8(%rsp,%rbx), %rdi
callq 0x3390
movq 0x178(%rsp,%rbx), %rdi
callq 0x3390
movq 0x80(%rsp,%r14,8), %rbx
testq %rbx, %rbx
je 0x1d433
movq %r14, %r15
shlq $0x5, %r15
addq %r13, %r15
xorl %r12d, %r12d
cmpq %r12, (%r15)
jbe 0x1d42b
movq (%rbx,%r12,8), %rdi
callq 0x3390
incq %r12
jmp 0x1d418
movq %rbx, %rdi
callq 0x3390
incq %r14
jmp 0x1d3d6
leaq 0x70ab(%rip), %rdi      # 0x244ea
leaq 0x714b(%rip), %rsi      # 0x24591
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0xc9c1
leaq 0x709b(%rip), %rdi      # 0x244f5
leaq 0x6e8f(%rip), %rsi      # 0x242f0
leaq 0x70a9(%rip), %rcx      # 0x24511
movl $0x143, %edx            # imm = 0x143
callq 0x31a0
nop