// File: /workspace/github_c_makefile_star2_O2/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
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movups %xmm0, (%rdi)
andq $0x0, 0x30(%rdi)
testq %r12, %r12
je 0x1a5a1
movq (%r12), %rcx
movq %rcx, 0x8(%rsp)
testq %rcx, %rcx
js 0x1a5a1
movq 0x8(%r12), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
js 0x1a5a1
movq %r9, %r14
movq %rdx, %r13
movq %rsi, %rbp
movslq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movl 0x14(%r12), %eax
movl %eax, (%rsp)
movslq %r8d, %r15
leaq 0x9303(%rip), %rsi      # 0x23850
leaq 0x9a77(%rip), %rcx      # 0x23fcb
movq %r15, %rdi
movl $0x49e, %edx            # imm = 0x49E
movl %r8d, 0x4(%rsp)
callq 0x1e045
movq %rax, 0x40(%rsp)
movq %rax, %rdi
xorl %eax, %eax
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl 0x4(%rsp), %ecx
leaq 0x126bc(%rip), %rdx     # 0x2cc40
cmpq %rax, %rcx
je 0x1a5b8
movsbq (%r14,%rax), %rsi
cmpq $0x16, %rsi
jl 0x1a598
movb (%rdx,%rsi), %sil
movb %sil, (%rdi,%rax)
incq %rax
jmp 0x1a584
movabsq $0x4ffffffff, %rax      # imm = 0x4FFFFFFFF
movq %rax, 0x8(%rbx)
orq $-0x1, (%rbx)
jmp 0x1a9b1
movq %rdi, 0x48(%rsp)
cmpl $0x0, 0x3c(%r12)
movq %r13, 0x30(%rsp)
js 0x1a5d5
andl $0x0, 0x10(%rbx)
movq 0x10(%rsp), %rcx
jmp 0x1a5f6
movq %rbp, %rdi
movq %r12, %rsi
callq 0x19f48
movzbl %al, %eax
shll $0x2, %eax
cmpl $0x0, 0x3c(%r12)
movl %eax, 0x10(%rbx)
movq 0x10(%rsp), %rcx
js 0x1a5fd
movl $0x100, 0xc(%rbx)       # imm = 0x100
movl (%rsp), %eax
movl %eax, %r14d
subl %ecx, %r14d
movq 0x20(%rsp), %rax
movl %eax, %r15d
subl 0x8(%rsp), %r15d
movl 0x20(%r12), %r13d
movl (%rbp), %ecx
movl %ecx, 0x18(%rsp)
movl 0x8(%rbp), %r8d
movl 0xc(%rbp), %r9d
movl %r14d, %edi
movl %r15d, %esi
movl %r13d, %edx
callq 0x1ac36
movl %eax, %r12d
movl 0x10(%rbp), %r8d
movl 0x14(%rbp), %r9d
movq %r14, 0x58(%rsp)
movl %r14d, %edi
movl %r15d, %esi
movl %r13d, %edx
movl 0x18(%rsp), %ecx
callq 0x1ac36
cmpl %r12d, %eax
cmoval %eax, %r12d
leaq 0x127db(%rip), %rax     # 0x2ce40
cmpl $0x4, (%rax)
jl 0x1a689
movl 0x24(%rbp), %edx
movq 0xa0(%rsp), %rax
movl 0x34(%rax), %ecx
leaq 0x9958(%rip), %rdi      # 0x23fd7
movl %r12d, %esi
xorl %eax, %eax
callq 0x3060
cmpl 0x24(%rbp), %r12d
jle 0x1a6a1
movq 0xa0(%rsp), %rax
movl 0x34(%rax), %eax
cmpl %eax, %r12d
cmovgel %eax, %r12d
movq 0x10(%rsp), %rcx
andq $0x0, 0x18(%rbx)
leaq 0xc4(%rbp), %rax
movq %rax, 0x50(%rsp)
addq %rcx, 0x40(%rsp)
leaq 0x14(%rbx), %rax
movq %rax, 0x18(%rsp)
movl $0xc0000000, %r13d      # imm = 0xC0000000
xorl %r14d, %r14d
movq 0x18(%rbx), %rdi
callq 0x3390
movl 0x24(%rbp), %r15d
shll $0x2, %r15d
cmpl %r15d, %r12d
cmovll %r12d, %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 0x28(%rsp)
leaq 0x44(%rsp), %rax
pushq %rax
pushq 0x40(%rsp)
pushq 0x30(%rsp)
pushq 0x70(%rsp)
pushq 0x90(%rsp)
pushq 0xa0(%rsp)
movq 0x78(%rsp), %rax
pushq (%rax)
callq 0x10572
addq $0x50, %rsp
movq %rax, 0x18(%rbx)
leaq 0x126fc(%rip), %rax     # 0x2ce40
cmpl $0x4, (%rax)
jl 0x1a769
movl 0x2c(%rsp), %edx
movq 0xa0(%rsp), %rax
movl 0x20(%rax), %ecx
leaq 0x98aa(%rip), %rdi      # 0x24009
movl %r15d, %esi
xorl %eax, %eax
callq 0x3060
movl 0x2c(%rsp), %eax
cmpl %r13d, %eax
je 0x1a7a5
movl 0x24(%rbp), %ecx
shll $0x2, %ecx
cmpl %ecx, %r15d
je 0x1a7a5
cmpl $0x1, %r14d
ja 0x1a7a5
incl %r14d
movq 0xa0(%rsp), %rcx
movl 0x20(%rcx), %ecx
subl (%rbp), %ecx
addl %r15d, %r15d
movl %eax, %r13d
movl %r15d, %r12d
cmpl %ecx, %eax
jl 0x1a6ce
movq 0x18(%rbx), %rbp
movslq 0x14(%rbx), %r14
leaq (,%r14,4), %rdi
addq %rbp, %rdi
callq 0x3100
movq %rax, %r13
incl %eax
movl 0x3c(%rsp), %ecx
shll $0xa, %ecx
movl $0x3ff, %edx            # imm = 0x3FF
andl 0x10(%rbx), %edx
orl %ecx, %edx
movl %edx, 0x10(%rbx)
movq 0x30(%rsp), %rcx
movq (%rcx), %r12
movq 0x20(%rsp), %rdi
decq %rdi
movq 0x8(%rsp), %rcx
cmpq %r12, %rcx
cmovlq %rcx, %rdi
xorl %ecx, %ecx
cmpq %rdi, %r12
setle %cl
movq %rdi, %rsi
notq %rsi
leaq (%rsi,%r12,2), %r15
movq %rdi, 0x20(%rsp)
cmovgq %rdi, %r15
andl $-0x2, %edx
orl %ecx, %edx
movl %edx, 0x10(%rbx)
movslq %eax, %rcx
movq %rcx, 0x8(%rsp)
testq %r14, %r14
jle 0x1a88b
movl (%rbp), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x2, %edx
jne 0x1a853
shrl $0x4, %ecx
addq %rcx, %r15
leal -0x1(%r14), %ecx
movq 0x18(%rsp), %rdx
movl %ecx, (%rdx)
leaq 0x4(%rbp), %rsi
leal (%rax,%r14,4), %eax
addl $-0x4, %eax
movslq %eax, %rdx
movq %rbp, %rdi
jmp 0x1a886
movl -0x4(%rbp,%r14,4), %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x1a88b
decl %r14d
movq 0x18(%rsp), %rax
movl %r14d, (%rax)
leaq (,%r14,4), %rdi
addq %rbp, %rdi
leaq 0x4(,%r14,4), %rsi
addq %rbp, %rsi
movq 0x8(%rsp), %rdx
callq 0x3600
movl 0x4(%rsp), %ecx
movl (%rsp), %esi
movl %ecx, %eax
xorl %esi, %eax
movq 0x10(%rsp), %rdx
orl %edx, %eax
je 0x1a948
subl %esi, %ecx
cmpq 0x20(%rsp), %r12
movl %edx, %ebp
cmovlel %ecx, %ebp
cmovlel %edx, %ecx
movl %ecx, %r14d
movq 0x18(%rbx), %rdi
movl 0x14(%rbx), %eax
leal 0x9(%r13,%rax,4), %eax
movslq %eax, %rsi
leaq 0x8f85(%rip), %rdx      # 0x23850
leaq 0x96f9(%rip), %r8       # 0x23fcb
movl $0x4c7, %ecx            # imm = 0x4C7
callq 0x1e0b4
movq %rax, 0x18(%rbx)
testl %ebp, %ebp
je 0x1a90f
movq %rax, %rdi
addq $0x4, %rdi
movslq 0x14(%rbx), %rcx
movq 0x8(%rsp), %rdx
leaq (%rdx,%rcx,4), %rdx
movq %rax, %rsi
callq 0x3600
shll $0x4, %ebp
orl $0x3, %ebp
movq 0x18(%rbx), %rax
movl %ebp, (%rax)
incl 0x14(%rbx)
testl %r14d, %r14d
je 0x1a948
movslq 0x14(%rbx), %rcx
leaq (%rax,%rcx,4), %rsi
leaq (%rax,%rcx,4), %rdi
addq $0x4, %rdi
movq 0x8(%rsp), %rdx
callq 0x3600
shll $0x4, %r14d
orl $0x3, %r14d
movq 0x18(%rbx), %rax
movslq 0x14(%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x14(%rbx)
movl %r14d, (%rax,%rcx,4)
movq 0x30(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0xf933
movl %eax, 0x8(%rbx)
movq 0xa0(%rsp), %rdx
cmpl 0x18(%rdx), %eax
jne 0x1a9c3
movq 0x10(%r14), %rcx
cltq
imulq $0x28, %rax, %rax
subq (%rcx,%rax), %r15
movq %r15, (%rbx)
movl 0x1c(%rdx), %eax
movl %eax, 0x28(%rbx)
movl 0x24(%rdx), %eax
movl 0x2c(%rdx), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, 0x2c(%rbx)
movl 0x48(%rdx), %eax
movl 0x10(%rbx), %ecx
shrl $0x1d, %eax
andl $0x2, %eax
andl $-0x3, %ecx
orl %eax, %ecx
movl %ecx, 0x10(%rbx)
movl 0x28(%rdx), %eax
movl %eax, 0x30(%rbx)
movq 0x48(%rsp), %rdi
callq 0x3390
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9674(%rip), %rdi      # 0x2403e
leaq 0x8e7f(%rip), %rsi      # 0x23850
leaq 0x9677(%rip), %rcx      # 0x2404f
movl $0x4d3, %edx            # imm = 0x4D3
callq 0x31a0