// File: /workspace/github_c_makefile_star2_O3/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 0x1ead1
movq (%r12), %rcx
movq %rcx, 0x30(%rsp)
testq %rcx, %rcx
js 0x1ead1
movq 0x8(%r12), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
js 0x1ead1
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 0x9e44(%rip), %rsi      # 0x288c0
leaq 0xa5b8(%rip), %rcx      # 0x2903b
movq %r15, %rdi
movl $0x49e, %edx            # imm = 0x49E
callq 0x23213
movq %rax, %r11
testl %r15d, %r15d
jle 0x1eac0
movl 0x4(%rsp), %eax
xorl %ecx, %ecx
leaq 0x1219b(%rip), %rdx     # 0x30c40
movsbq (%r14,%rcx), %rsi
cmpq $0x16, %rsi
jl 0x1eab4
movb (%rdx,%rsi), %sil
movb %sil, (%r11,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x1eaa5
cmpl $0x0, 0x3c(%r12)
js 0x1eaeb
movl $0x0, 0x10(%rbx)
jmp 0x1eb0d
movabsq $0x4ffffffff, %rax      # imm = 0x4FFFFFFFF
movq %rax, 0x8(%rbx)
movq $-0x1, (%rbx)
jmp 0x1ef6c
movq %rbp, %rdi
movq %r12, %rsi
movq %r11, %r14
callq 0x1e409
movq %r14, %r11
movzbl %al, %eax
shll $0x2, %eax
cmpl $0x0, 0x3c(%r12)
movl %eax, 0x10(%rbx)
js 0x1eb14
movl $0x100, 0xc(%rbx)       # imm = 0x100
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 0x1eb68
movl %ecx, %r8d
imull %r15d, %r8d
subl %eax, %r8d
movl %edi, %r9d
subl %ecx, %r9d
addl %esi, %r9d
addl %r9d, %r9d
cmpl %r9d, %r8d
jge 0x1eb68
movl 0x10(%rbp), %edi
movl 0x14(%rbp), %esi
xorl %r13d, %r13d
jmp 0x1ebc4
movl %r15d, %r10d
subl %edx, %r10d
movl %edx, %r8d
cmovll %r15d, %r8d
imull %ecx, %r8d
addl %eax, %edi
movl %r8d, %r9d
subl %edi, %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
cvtsi2sd %esi, %xmm1
divsd %xmm1, %xmm0
addsd 0x9d17(%rip), %xmm0     # 0x288b0
cvttsd2si %xmm0, %r13d
movl %r10d, %r9d
negl %r9d
cmovsl %r10d, %r9d
cmpl %r13d, %r9d
cmovgl %r9d, %r13d
movl 0x10(%rbp), %edi
movl 0x14(%rbp), %esi
cmpl %edx, %r15d
jne 0x1ebf2
movl %ecx, %r8d
imull %r15d, %r8d
subl %eax, %r8d
movl %esi, %r9d
subl %ecx, %r9d
addl %edi, %r9d
addl %r9d, %r9d
xorl %r10d, %r10d
cmpl %r9d, %r8d
jl 0x1ec1e
movl %r15d, %r8d
subl %edx, %r8d
cmovll %r15d, %edx
imull %edx, %ecx
movl %r8d, %r9d
negl %r9d
cmovsl %r8d, %r9d
movl %ecx, %r8d
addl %edi, %eax
subl %eax, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
divsd %xmm1, %xmm0
addsd 0x9c9e(%rip), %xmm0     # 0x288b0
cvttsd2si %xmm0, %r10d
cmpl %r10d, %r9d
cmovgl %r9d, %r10d
cmpl %r13d, %r10d
cmoval %r10d, %r13d
leaq 0x12214(%rip), %rax     # 0x30e40
cmpl $0x4, (%rax)
jl 0x1ec55
movl 0x24(%rbp), %edx
movl 0x34(%r12), %ecx
leaq 0xa407(%rip), %rdi      # 0x29047
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 0x1ec6c
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 0x122ac
addq $0x50, %rsp
movq %rax, 0x18(%rbx)
leaq 0x1212b(%rip), %rax     # 0x30e40
cmpl $0x4, (%rax)
jl 0x1ed3a
movl 0x14(%rsp), %edx
movq 0xa0(%rsp), %rax
movl 0x20(%rax), %ecx
leaq 0xa349(%rip), %rdi      # 0x29079
movl %r15d, %esi
xorl %eax, %eax
callq 0x3060
movl 0x14(%rsp), %eax
cmpl %r12d, %eax
je 0x1ed76
movl 0x24(%rbp), %ecx
shll $0x2, %ecx
cmpl %ecx, %r15d
je 0x1ed76
cmpl $0x1, %r14d
ja 0x1ed76
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 0x1ec9f
movq 0x18(%rbx), %r14
movslq 0x14(%rbx), %rbp
leaq (%r14,%rbp,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), %r12
movq 0x8(%rsp), %rsi
decq %rsi
movq 0x30(%rsp), %rax
cmpq %r12, %rax
cmovlq %rax, %rsi
xorl %eax, %eax
cmpq %rsi, %r12
setle %al
movq %rsi, %rdx
notq %rdx
leaq (%rdx,%r12,2), %r13
movq %rsi, 0x8(%rsp)
cmovgq %rsi, %r13
andl $-0x2, %ecx
orl %eax, %ecx
movl %ecx, 0x10(%rbx)
testq %rbp, %rbp
jle 0x1ee46
movl (%r14), %eax
movl %eax, %ecx
andl $0xf, %ecx
cmpl $0x2, %ecx
jne 0x1ee16
shrl $0x4, %eax
addq %rax, %r13
leal -0x1(%rbp), %eax
movq 0x20(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x4(%r14), %rsi
leal (%r8,%rbp,4), %eax
addl $-0x4, %eax
movslq %eax, %rdx
movq %r14, %rdi
jmp 0x1ee3b
movl -0x4(%r14,%rbp,4), %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x1ee46
decl %ebp
movq 0x20(%rsp), %rax
movl %ebp, (%rax)
leaq (%r14,%rbp,4), %rdi
leaq (%r14,%rbp,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
je 0x1ef03
subl %esi, %ecx
cmpq 0x8(%rsp), %r12
movl %edx, %ebp
cmovlel %ecx, %ebp
cmovlel %edx, %ecx
movl %ecx, %r12d
movq 0x18(%rbx), %rdi
movl 0x14(%rbx), %eax
leal 0x9(%r15,%rax,4), %eax
movslq %eax, %rsi
leaq 0x9a39(%rip), %rdx      # 0x288c0
movq %r8, %r14
leaq 0xa1aa(%rip), %r8       # 0x2903b
movl $0x4c7, %ecx            # imm = 0x4C7
callq 0x2324c
movq %rax, 0x18(%rbx)
movslq %r14d, %r14
testl %ebp, %ebp
je 0x1eecc
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)
testl %r12d, %r12d
je 0x1ef03
movslq 0x14(%rbx), %rcx
leaq (%rax,%rcx,4), %rsi
leaq (%rax,%rcx,4), %rdi
addq $0x4, %rdi
movq %r14, %rdx
callq 0x3600
shll $0x4, %r12d
orl $0x3, %r12d
movq 0x18(%rbx), %rax
movslq 0x14(%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0x14(%rbx)
movl %r12d, (%rax,%rcx,4)
movq 0x28(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x11494
movl %eax, 0x8(%rbx)
movq 0xa0(%rsp), %rdx
cmpl 0x18(%rdx), %eax
jne 0x1ef7e
movq 0x10(%r14), %rcx
cltq
leaq (%rax,%rax,4), %rax
subq (%rcx,%rax,8), %r13
movq %r13, (%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 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 0xa129(%rip), %rdi      # 0x290ae
leaq 0x9934(%rip), %rsi      # 0x288c0
leaq 0xa12c(%rip), %rcx      # 0x290bf
movl $0x4d3, %edx            # imm = 0x4D3
callq 0x31a0