// File: /workspace/github_c_makefile_star2_O1/ToniWestbrook[P]paladin/bwamem.c
// Function: mem_reg2aln
mem_aln_t mem_reg2aln(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, int l_query, const char *query_, const mem_alnreg_t *ar)
{
	mem_aln_t a;
	int i, w2, tmp, qb, qe, NM, score, is_rev, last_sc = -(1<<30), l_MD;
	int64_t pos, rb, re;
	uint8_t *query;

	memset(&a, 0, sizeof(mem_aln_t));
	if (ar == 0 || ar->rb < 0 || ar->re < 0) { // generate an unmapped record
		a.rid = -1; a.pos = -1; a.flag |= 0x4;
		return a;
	}
	qb = ar->qb, qe = ar->qe;
	rb = ar->rb, re = ar->re;
	query = malloc(l_query);

	for (i = 0; i < l_query; ++i){
		query[i] = query_[i] < VALUE_DEFINED ? query_[i] : aa_encode_hash[(int)query_[i]];
	}

	a.mapq = ar->secondary < 0? mem_approx_mapq_se(opt, ar) : 0;
	if (ar->secondary >= 0) a.flag |= 0x100; // secondary alignment
	tmp = infer_bw(qe - qb, re - rb, ar->truesc, opt->a, opt->o_del, opt->e_del);
	w2  = infer_bw(qe - qb, re - rb, ar->truesc, opt->a, opt->o_ins, opt->e_ins);
	w2 = w2 > tmp? w2 : tmp;
	if (bwa_verbose >= 4) printf("* Band width: inferred=%d, cmd_opt=%d, alnreg=%d\n", w2, opt->w, ar->w);
	if (w2 > opt->w) w2 = w2 < ar->w? w2 : ar->w;
	i = 0; a.cigar = 0;
	do {
		free(a.cigar);
		w2 = w2 < opt->w<<2? w2 : opt->w<<2;
		a.cigar = bwa_gen_cigar2(opt->mat, opt->o_del, opt->e_del, opt->o_ins, opt->e_ins, w2, bns->l_pac, pac, qe - qb, (uint8_t*)&query[qb], rb, re, &score, &a.n_cigar, &NM);
		if (bwa_verbose >= 4) printf("* Final alignment: w2=%d, global_sc=%d, local_sc=%d\n", w2, score, ar->truesc);
		if (score == last_sc || w2 == opt->w<<2) break; // it is possible that global alignment and local alignment give different scores
		last_sc = score;
		w2 <<= 1;
	} while (++i < 3 && score < ar->truesc - opt->a);
	l_MD = strlen((char*)(a.cigar + a.n_cigar)) + 1;
	a.NM = NM;
	pos = bns_depos(bns, rb < bns->l_pac? rb : re - 1, &is_rev);
	a.is_rev = is_rev;
	if (a.n_cigar > 0) { // squeeze out leading or trailing deletions
		if ((a.cigar[0]&0xf) == 2) {
			pos += a.cigar[0]>>4;
			--a.n_cigar;
			memmove(a.cigar, a.cigar + 1, a.n_cigar * 4 + l_MD);
		} else if ((a.cigar[a.n_cigar-1]&0xf) == 2) {
			--a.n_cigar;
			memmove(a.cigar + a.n_cigar, a.cigar + a.n_cigar + 1, l_MD); // MD needs to be moved accordingly
		}
	}
	if (qb != 0 || qe != l_query) { // add clipping to CIGAR
		int clip5, clip3;
		clip5 = is_rev? l_query - qe : qb;
		clip3 = is_rev? qb : l_query - qe;
		a.cigar = realloc(a.cigar, 4 * (a.n_cigar + 2) + l_MD);
		if (clip5) {
			memmove(a.cigar+1, a.cigar, a.n_cigar * 4 + l_MD); // make room for 5'-end clipping
			a.cigar[0] = clip5<<4 | 3;
			++a.n_cigar;
		}
		if (clip3) {
			memmove(a.cigar + a.n_cigar + 1, a.cigar + a.n_cigar, l_MD); // make room for 3'-end clipping
			a.cigar[a.n_cigar++] = clip3<<4 | 3;
		}
	}
	a.rid = bns_pos2rid(bns, pos);
	assert(a.rid == ar->rid);
	a.pos = pos - bns->anns[a.rid].offset;
	a.score = ar->score; a.sub = ar->sub > ar->csub? ar->sub : ar->csub;

	a.is_alt = ar->is_alt; a.alt_sc = ar->alt_sc;
	free(query);
	return a;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, 0x60(%rsp)
movq %rdi, %rbx
movq 0xa0(%rsp), %r12
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%rdi)
movupd %xmm0, 0x10(%rdi)
movupd %xmm0, (%rdi)
movq $0x0, 0x30(%rdi)
testq %r12, %r12
je 0x1e6f6
movq (%r12), %rcx
movq %rcx, 0x30(%rsp)
testq %rcx, %rcx
js 0x1e6f6
movq 0x8(%r12), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
js 0x1e6f6
movq %r9, %r14
movq %rsi, %rbp
movq %rdx, 0x28(%rsp)
movslq 0x10(%r12), %rax
movq %rax, 0x18(%rsp)
movl 0x14(%r12), %r13d
movl %r8d, 0x4(%rsp)
movslq %r8d, %r15
leaq 0xa44a(%rip), %rsi      # 0x287d0
leaq 0xabbe(%rip), %rcx      # 0x28f4b
movq %r15, %rdi
movl $0x49e, %edx            # imm = 0x49E
callq 0x22d2b
movq %rax, %r11
testl %r15d, %r15d
jle 0x1e3ca
movl 0x4(%rsp), %eax
xorl %ecx, %ecx
leaq 0x12891(%rip), %rdx     # 0x30c40
movsbq (%r14,%rcx), %rsi
cmpq $0x16, %rsi
jl 0x1e3be
movb (%rdx,%rsi), %sil
movb %sil, (%r11,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x1e3af
xorl %eax, %eax
cmpl $0x0, 0x3c(%r12)
jns 0x1e3eb
movq %rbp, %rdi
movq %r12, %rsi
movq %r11, %r14
callq 0x1dd1f
movq %r14, %r11
movzbl %al, %eax
shll $0x2, %eax
movl $0xfffffc03, %ecx       # imm = 0xFFFFFC03
andl 0x10(%rbx), %ecx
orl %eax, %ecx
movl %ecx, 0x10(%rbx)
cmpl $0x0, 0x3c(%r12)
js 0x1e404
orb $0x1, 0xd(%rbx)
movl %r13d, 0x38(%rsp)
movl %r13d, %r15d
movq 0x18(%rsp), %r14
subl %r14d, %r15d
movq 0x8(%rsp), %rax
movl %eax, %edx
subl 0x30(%rsp), %edx
movl 0x20(%r12), %eax
movl (%rbp), %ecx
movl 0x8(%rbp), %edi
movl 0xc(%rbp), %esi
cmpl %edx, %r15d
jne 0x1e450
movl %ecx, %r8d
imull %r15d, %r8d
subl %eax, %r8d
movl %edi, %r9d
subl %ecx, %r9d
addl %esi, %r9d
addl %r9d, %r9d
xorl %r13d, %r13d
cmpl %r9d, %r8d
jl 0x1e493
movl %r15d, %r8d
subl %edx, %r8d
movl %edx, %r9d
cmovll %r15d, %r9d
imull %ecx, %r9d
addl %eax, %edi
subl %edi, %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
cvtsi2sd %esi, %xmm1
divsd %xmm1, %xmm0
addsd 0xa342(%rip), %xmm0     # 0x287c0
cvttsd2si %xmm0, %esi
movl %r8d, %r13d
negl %r13d
cmovsl %r8d, %r13d
cmpl %esi, %r13d
cmovlel %esi, %r13d
movl 0x10(%rbp), %edi
movl 0x14(%rbp), %esi
cmpl %edx, %r15d
jne 0x1e4bc
movl %ecx, %r9d
imull %r15d, %r9d
subl %eax, %r9d
movl %edi, %r10d
subl %ecx, %r10d
addl %esi, %r10d
addl %r10d, %r10d
xorl %r8d, %r8d
cmpl %r10d, %r9d
jl 0x1e4fc
movl %r15d, %r9d
subl %edx, %r9d
cmovll %r15d, %edx
imull %edx, %ecx
addl %edi, %eax
subl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
divsd %xmm1, %xmm0
addsd 0xa2d9(%rip), %xmm0     # 0x287c0
cvttsd2si %xmm0, %eax
movl %r9d, %r8d
negl %r8d
cmovsl %r9d, %r8d
cmpl %eax, %r8d
cmovlel %eax, %r8d
cmpl %r13d, %r8d
cmoval %r8d, %r13d
leaq 0x12936(%rip), %rax     # 0x30e40
cmpl $0x4, (%rax)
jl 0x1e533
movl 0x24(%rbp), %edx
movl 0x34(%r12), %ecx
leaq 0xaa39(%rip), %rdi      # 0x28f57
movl %r13d, %esi
xorl %eax, %eax
movq %r11, %r14
callq 0x3060
movq %r14, %r11
movq 0x18(%rsp), %r14
cmpl 0x24(%rbp), %r13d
movq %r15, 0x58(%rsp)
jle 0x1e54a
movl 0x34(%r12), %eax
cmpl %eax, %r13d
cmovgel %eax, %r13d
movq $0x0, 0x18(%rbx)
leaq 0xc4(%rbp), %rax
movq %rax, 0x50(%rsp)
movq %r11, 0x40(%rsp)
addq %r14, %r11
movq %r11, 0x48(%rsp)
leaq 0x14(%rbx), %rax
movq %rax, 0x20(%rsp)
movl $0xc0000000, %r12d      # imm = 0xC0000000
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
callq 0x3390
movl 0x24(%rbp), %r15d
shll $0x2, %r15d
cmpl %r15d, %r13d
cmovll %r13d, %r15d
movl 0x8(%rbp), %esi
movl 0xc(%rbp), %edx
movl 0x10(%rbp), %ecx
movl 0x14(%rbp), %r8d
subq $0x8, %rsp
movq 0x58(%rsp), %rdi
movl %r15d, %r9d
leaq 0x44(%rsp), %rax
pushq %rax
pushq 0x30(%rsp)
leaq 0x2c(%rsp), %rax
pushq %rax
pushq 0x28(%rsp)
pushq 0x58(%rsp)
pushq 0x78(%rsp)
pushq 0x90(%rsp)
pushq 0xa0(%rsp)
movq 0x70(%rsp), %rax
pushq (%rax)
callq 0x11f18
addq $0x50, %rsp
movq %rax, 0x18(%rbx)
leaq 0x1284d(%rip), %rax     # 0x30e40
cmpl $0x4, (%rax)
jl 0x1e618
movl 0x14(%rsp), %edx
movq 0xa0(%rsp), %rax
movl 0x20(%rax), %ecx
leaq 0xa97b(%rip), %rdi      # 0x28f89
movl %r15d, %esi
xorl %eax, %eax
callq 0x3060
movl 0x14(%rsp), %eax
cmpl %r12d, %eax
je 0x1e654
movl 0x24(%rbp), %ecx
shll $0x2, %ecx
cmpl %ecx, %r15d
je 0x1e654
cmpl $0x1, %r14d
ja 0x1e654
incl %r14d
movq 0xa0(%rsp), %rcx
movl 0x20(%rcx), %ecx
subl (%rbp), %ecx
addl %r15d, %r15d
movl %eax, %r12d
movl %r15d, %r13d
cmpl %ecx, %eax
jl 0x1e57d
movq 0x18(%rbx), %r14
movslq 0x14(%rbx), %r12
leaq (%r14,%r12,4), %rdi
callq 0x3100
movq %rax, %r15
leal 0x1(%rax), %r8d
movl 0x3c(%rsp), %eax
movl $0x3ff, %ecx            # imm = 0x3FF
andl 0x10(%rbx), %ecx
shll $0xa, %eax
orl %eax, %ecx
movl %ecx, 0x10(%rbx)
movq 0x28(%rsp), %rax
movq (%rax), %rbp
movq 0x8(%rsp), %rsi
decq %rsi
movq 0x30(%rsp), %rax
cmpq %rbp, %rax
cmovlq %rax, %rsi
xorl %eax, %eax
cmpq %rsi, %rbp
setle %al
movq %rsi, %rdx
notq %rdx
leaq (%rdx,%rbp,2), %r13
movq %rsi, 0x8(%rsp)
cmovgq %rsi, %r13
andl $-0x2, %ecx
orl %eax, %ecx
movl %ecx, 0x10(%rbx)
testq %r12, %r12
jle 0x1e73f
movl (%r14), %eax
movl %eax, %ecx
andl $0xf, %ecx
cmpl $0x2, %ecx
jne 0x1e70d
shrl $0x4, %eax
addq %rax, %r13
leal -0x1(%r12), %eax
movq 0x20(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x4(%r14), %rsi
leal (%r8,%r12,4), %eax
addl $-0x4, %eax
movslq %eax, %rdx
movq %r14, %rdi
jmp 0x1e734
movl $0xffffffff, 0x8(%rbx)  # imm = 0xFFFFFFFF
movq $-0x1, (%rbx)
orb $0x4, 0xc(%rbx)
jmp 0x1e87b
movl -0x4(%r14,%r12,4), %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x1e73f
decl %r12d
movq 0x20(%rsp), %rax
movl %r12d, (%rax)
leaq (%r14,%r12,4), %rdi
leaq (%r14,%r12,4), %rsi
addq $0x4, %rsi
movslq %r8d, %rdx
movq %r8, %r14
callq 0x3600
movq %r14, %r8
movl 0x4(%rsp), %ecx
movl %ecx, %eax
movl 0x38(%rsp), %esi
xorl %esi, %eax
movq 0x18(%rsp), %rdx
orl %edx, %eax
movq 0xa0(%rsp), %r12
je 0x1e80e
subl %esi, %ecx
cmpq 0x8(%rsp), %rbp
movl %edx, %ebp
cmovlel %ecx, %ebp
cmovlel %edx, %ecx
movl %ecx, 0x4(%rsp)
movq 0x18(%rbx), %rdi
movl 0x14(%rbx), %eax
leal 0x9(%r15,%rax,4), %eax
movslq %eax, %rsi
leaq 0xa047(%rip), %rdx      # 0x287d0
movq %r8, %r14
leaq 0xa7b8(%rip), %r8       # 0x28f4b
movl $0x4c7, %ecx            # imm = 0x4C7
callq 0x22d64
movq %rax, 0x18(%rbx)
movslq %r14d, %r14
testl %ebp, %ebp
je 0x1e7ce
movq %rax, %rdi
addq $0x4, %rdi
movslq 0x14(%rbx), %rcx
leaq (%r14,%rcx,4), %rdx
movq %rax, %rsi
callq 0x3600
shll $0x4, %ebp
orl $0x3, %ebp
movq 0x18(%rbx), %rax
movl %ebp, (%rax)
incl 0x14(%rbx)
movl 0x4(%rsp), %r8d
testl %r8d, %r8d
je 0x1e80e
movq 0x18(%rbx), %rax
movslq 0x14(%rbx), %rcx
leaq (%rax,%rcx,4), %rsi
leaq (%rax,%rcx,4), %rdi
addq $0x4, %rdi
movq %r14, %rdx
movl %r8d, %ebp
callq 0x3600
shll $0x4, %ebp
orl $0x3, %ebp
movq 0x18(%rbx), %rax
movslq 0x14(%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x14(%rbx)
movl %ebp, (%rax,%rcx,4)
movq 0x28(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x111bc
movl %eax, 0x8(%rbx)
cmpl 0x18(%r12), %eax
jne 0x1e88d
movq 0x10(%r14), %rcx
cltq
leaq (%rax,%rax,4), %rax
subq (%rcx,%rax,8), %r13
movq %r13, (%rbx)
movl 0x1c(%r12), %eax
movl %eax, 0x28(%rbx)
movl 0x24(%r12), %eax
movl 0x2c(%r12), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, 0x2c(%rbx)
movl 0x48(%r12), %eax
movl 0x10(%rbx), %ecx
shrl $0x1d, %eax
andl $0x2, %eax
andl $-0x3, %ecx
orl %eax, %ecx
movl %ecx, 0x10(%rbx)
movl 0x28(%r12), %eax
movl %eax, 0x30(%rbx)
movq 0x40(%rsp), %rdi
callq 0x3390
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa72a(%rip), %rdi      # 0x28fbe
leaq 0x9f35(%rip), %rsi      # 0x287d0
leaq 0xa72d(%rip), %rcx      # 0x28fcf
movl $0x4d3, %edx            # imm = 0x4D3
callq 0x31a0