// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, -0xb0(%rbp)
movq %rdi, %rax
movq %rax, -0xa8(%rbp)
movq 0x10(%rbp), %rax
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movl %r8d, -0x44(%rbp)
movq %r9, -0x50(%rbp)
movl $0xc0000000, -0x74(%rbp) # imm = 0xC0000000
xorl %esi, %esi
movl $0x38, %edx
callq 0x3130
cmpq $0x0, 0x10(%rbp)
je 0x2b68c
movq 0x10(%rbp), %rax
cmpq $0x0, (%rax)
jl 0x2b68c
movq 0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jge 0x2b6af
movq -0xb0(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movq $-0x1, (%rax)
movl 0xc(%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0xc(%rax)
jmp 0x2be44
movq 0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x60(%rbp)
movq 0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x64(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movslq -0x44(%rbp), %rdi
leaq 0xefcd(%rip), %rsi      # 0x3a6b8
movl $0x49e, %edx            # imm = 0x49E
leaq 0xf73c(%rip), %rcx      # 0x3ae33
callq 0x31ca0
movq %rax, -0x98(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x2b778
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x16, %eax
jge 0x2b737
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movl %eax, -0xb4(%rbp)
jmp 0x2b757
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0x154e3(%rip), %rax     # 0x40c30
movzbl (%rax,%rcx), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movb %al, %dl
movq -0x98(%rbp), %rax
movslq -0x54(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2b70a
movq 0x10(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jge 0x2b797
movq -0x30(%rbp), %rdi
movq 0x10(%rbp), %rsi
callq 0x2ad90
movl %eax, -0xb8(%rbp)
jmp 0x2b7a1
xorl %eax, %eax
movl %eax, -0xb8(%rbp)
jmp 0x2b7a1
movq -0xb0(%rbp), %rax
movl -0xb8(%rbp), %edx
movl 0x10(%rax), %ecx
andl $0xff, %edx
shll $0x2, %edx
andl $0xfffffc03, %ecx       # imm = 0xFFFFFC03
orl %edx, %ecx
movl %ecx, 0x10(%rax)
movq 0x10(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jl 0x2b7e2
movq -0xb0(%rbp), %rax
movl 0xc(%rax), %ecx
orl $0x100, %ecx            # imm = 0x100
movl %ecx, 0xc(%rax)
movl -0x64(%rbp), %edi
subl -0x60(%rbp), %edi
movq -0x90(%rbp), %rax
subq -0x88(%rbp), %rax
movl %eax, %esi
movq 0x10(%rbp), %rax
movl 0x20(%rax), %edx
movq -0x30(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl 0x8(%rax), %r8d
movq -0x30(%rbp), %rax
movl 0xc(%rax), %r9d
callq 0x2c1c0
movl %eax, -0x5c(%rbp)
movl -0x64(%rbp), %edi
subl -0x60(%rbp), %edi
movq -0x90(%rbp), %rax
subq -0x88(%rbp), %rax
movl %eax, %esi
movq 0x10(%rbp), %rax
movl 0x20(%rax), %edx
movq -0x30(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl 0x10(%rax), %r8d
movq -0x30(%rbp), %rax
movl 0x14(%rax), %r9d
callq 0x2c1c0
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jle 0x2b86b
movl -0x58(%rbp), %eax
movl %eax, -0xbc(%rbp)
jmp 0x2b874
movl -0x5c(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq 0x155ac(%rip), %rax     # 0x40e30
cmpl $0x4, (%rax)
jl 0x2b8a8
movl -0x58(%rbp), %esi
movq -0x30(%rbp), %rax
movl 0x24(%rax), %edx
movq 0x10(%rbp), %rax
movl 0x34(%rax), %ecx
leaq 0xf59e(%rip), %rdi      # 0x3ae3f
movb $0x0, %al
callq 0x3060
movl -0x58(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jle 0x2b8e1
movl -0x58(%rbp), %eax
movq 0x10(%rbp), %rcx
cmpl 0x34(%rcx), %eax
jge 0x2b8cb
movl -0x58(%rbp), %eax
movl %eax, -0xc0(%rbp)
jmp 0x2b8d8
movq 0x10(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0xb0(%rbp), %rax
movl $0x0, -0x54(%rbp)
movq $0x0, 0x18(%rax)
movq -0xb0(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x3380
movl -0x58(%rbp), %eax
movq -0x30(%rbp), %rcx
movl 0x24(%rcx), %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jge 0x2b923
movl -0x58(%rbp), %eax
movl %eax, -0xc4(%rbp)
jmp 0x2b933
movq -0x30(%rbp), %rax
movl 0x24(%rax), %eax
shll $0x2, %eax
movl %eax, -0xc4(%rbp)
movq -0xb0(%rbp), %r10
movl -0xc4(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x30(%rbp), %rdi
addq $0xc4, %rdi
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x30(%rbp), %rax
movl 0xc(%rax), %edx
movq -0x30(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x30(%rbp), %rax
movl 0x14(%rax), %r8d
movl -0x58(%rbp), %r9d
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
movq -0x40(%rbp), %r13
movl -0x64(%rbp), %r12d
subl -0x60(%rbp), %r12d
movq -0x98(%rbp), %r15
movslq -0x60(%rbp), %rax
addq %rax, %r15
movq -0x88(%rbp), %r14
movq -0x90(%rbp), %rbx
addq $0x14, %r10
leaq -0x6c(%rbp), %r11
leaq -0x68(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, (%rsp)
movq -0xd0(%rbp), %rax
movq %r13, 0x8(%rsp)
movl %r12d, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
callq 0x18f60
movq %rax, %rcx
movq -0xb0(%rbp), %rax
movq %rcx, 0x18(%rax)
leaq 0x15424(%rip), %rax     # 0x40e30
cmpl $0x4, (%rax)
jl 0x2ba2c
movl -0x58(%rbp), %esi
movl -0x6c(%rbp), %edx
movq 0x10(%rbp), %rax
movl 0x20(%rax), %ecx
leaq 0xf44c(%rip), %rdi      # 0x3ae71
movb $0x0, %al
callq 0x3060
movl -0x6c(%rbp), %eax
cmpl -0x74(%rbp), %eax
je 0x2ba45
movl -0x58(%rbp), %eax
movq -0x30(%rbp), %rcx
movl 0x24(%rcx), %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jne 0x2ba47
jmp 0x2ba94
movl -0x6c(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x58(%rbp), %eax
shll %eax
movl %eax, -0x58(%rbp)
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x54(%rbp)
xorl %eax, %eax
cmpl $0x3, %ecx
movb %al, -0xd9(%rbp)
jge 0x2ba86
movl -0x6c(%rbp), %eax
movq 0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
movq -0x30(%rbp), %rdx
subl (%rdx), %ecx
cmpl %ecx, %eax
setl %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x2b8f7
movq -0xb0(%rbp), %rax
movq 0x18(%rax), %rdi
movslq 0x14(%rax), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x30f0
movq %rax, %rcx
movq -0xb0(%rbp), %rax
addq $0x1, %rcx
movl %ecx, -0x78(%rbp)
movl -0x68(%rbp), %edx
movl 0x10(%rax), %ecx
andl $0x3fffff, %edx         # imm = 0x3FFFFF
shll $0xa, %edx
andl $0x3ff, %ecx            # imm = 0x3FF
orl %edx, %ecx
movl %ecx, 0x10(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x88(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x2bb05
movq -0x88(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x2bb17
movq -0x90(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x1ce60
movq %rax, %rcx
movq -0xb0(%rbp), %rax
movq %rcx, -0x80(%rbp)
movl -0x70(%rbp), %edx
movl 0x10(%rax), %ecx
andl $0x1, %edx
andl $-0x2, %ecx
orl %edx, %ecx
movl %ecx, 0x10(%rax)
cmpl $0x0, 0x14(%rax)
jle 0x2bc0c
movq -0xb0(%rbp), %rax
movq 0x18(%rax), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x2bbae
movq -0xb0(%rbp), %rax
movq 0x18(%rax), %rcx
movl (%rcx), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
addq -0x80(%rbp), %rcx
movq %rcx, -0x80(%rbp)
movl 0x14(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x14(%rax)
movq 0x18(%rax), %rdi
movq 0x18(%rax), %rsi
addq $0x4, %rsi
movl 0x14(%rax), %eax
shll $0x2, %eax
addl -0x78(%rbp), %eax
movslq %eax, %rdx
callq 0x35e0
jmp 0x2bc0a
movq -0xb0(%rbp), %rcx
movq 0x18(%rcx), %rax
movl 0x14(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x2bc08
movq -0xb0(%rbp), %rax
movl 0x14(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x14(%rax)
movq 0x18(%rax), %rdi
movslq 0x14(%rax), %rcx
shlq $0x2, %rcx
addq %rcx, %rdi
movq 0x18(%rax), %rsi
movslq 0x14(%rax), %rax
shlq $0x2, %rax
addq %rax, %rsi
addq $0x4, %rsi
movslq -0x78(%rbp), %rdx
callq 0x35e0
jmp 0x2bc0a
jmp 0x2bc0c
cmpl $0x0, -0x60(%rbp)
jne 0x2bc1e
movl -0x64(%rbp), %eax
cmpl -0x44(%rbp), %eax
je 0x2bd64
cmpl $0x0, -0x70(%rbp)
je 0x2bc32
movl -0x44(%rbp), %eax
subl -0x64(%rbp), %eax
movl %eax, -0xf4(%rbp)
jmp 0x2bc3b
movl -0x60(%rbp), %eax
movl %eax, -0xf4(%rbp)
movl -0xf4(%rbp), %eax
movl %eax, -0x9c(%rbp)
cmpl $0x0, -0x70(%rbp)
je 0x2bc58
movl -0x60(%rbp), %eax
movl %eax, -0xf8(%rbp)
jmp 0x2bc64
movl -0x44(%rbp), %eax
subl -0x64(%rbp), %eax
movl %eax, -0xf8(%rbp)
movq -0xb0(%rbp), %rax
movl -0xf8(%rbp), %ecx
movl %ecx, -0xa0(%rbp)
movq 0x18(%rax), %rdi
movl 0x14(%rax), %eax
addl $0x2, %eax
shll $0x2, %eax
addl -0x78(%rbp), %eax
movslq %eax, %rsi
leaq 0xea27(%rip), %rdx      # 0x3a6b8
movl $0x4c7, %ecx            # imm = 0x4C7
leaq 0xf196(%rip), %r8       # 0x3ae33
callq 0x31d40
movq %rax, %rcx
movq -0xb0(%rbp), %rax
movq %rcx, 0x18(%rax)
cmpl $0x0, -0x9c(%rbp)
je 0x2bcff
movq -0xb0(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x4, %rdi
movq 0x18(%rax), %rsi
movl 0x14(%rax), %eax
shll $0x2, %eax
addl -0x78(%rbp), %eax
movslq %eax, %rdx
callq 0x35e0
movq -0xb0(%rbp), %rax
movl -0x9c(%rbp), %edx
shll $0x4, %edx
orl $0x3, %edx
movq 0x18(%rax), %rcx
movl %edx, (%rcx)
movl 0x14(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x14(%rax)
cmpl $0x0, -0xa0(%rbp)
je 0x2bd62
movq -0xb0(%rbp), %rax
movq 0x18(%rax), %rdi
movslq 0x14(%rax), %rcx
shlq $0x2, %rcx
addq %rcx, %rdi
addq $0x4, %rdi
movq 0x18(%rax), %rsi
movslq 0x14(%rax), %rax
shlq $0x2, %rax
addq %rax, %rsi
movslq -0x78(%rbp), %rdx
callq 0x35e0
movq -0xb0(%rbp), %rsi
movl -0xa0(%rbp), %edx
shll $0x4, %edx
orl $0x3, %edx
movq 0x18(%rsi), %rax
movl 0x14(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x14(%rsi)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x2bd64
movq -0x38(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x179d0
movl %eax, %ecx
movq -0xb0(%rbp), %rax
movl %ecx, 0x8(%rax)
movl 0x8(%rax), %eax
movq 0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x2bd8b
jmp 0x2bdaa
leaq 0xf114(%rip), %rdi      # 0x3aea6
leaq 0xe91f(%rip), %rsi      # 0x3a6b8
movl $0x4d3, %edx            # imm = 0x4D3
leaq 0xf112(%rip), %rcx      # 0x3aeb7
callq 0x3190
movq -0xb0(%rbp), %rax
movq -0x80(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq 0x10(%rdx), %rdx
movslq 0x8(%rax), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
subq (%rdx), %rcx
movq %rcx, (%rax)
movq 0x10(%rbp), %rcx
movl 0x1c(%rcx), %ecx
movl %ecx, 0x28(%rax)
movq 0x10(%rbp), %rax
movl 0x24(%rax), %eax
movq 0x10(%rbp), %rcx
cmpl 0x2c(%rcx), %eax
jle 0x2bdf7
movq 0x10(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0xfc(%rbp)
jmp 0x2be04
movq 0x10(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0xfc(%rbp)
movq -0xb0(%rbp), %rax
movl -0xfc(%rbp), %ecx
movl %ecx, 0x2c(%rax)
movq 0x10(%rbp), %rcx
movl 0x48(%rcx), %edx
sarl $0x1e, %edx
movl 0x10(%rax), %ecx
andl $0x1, %edx
shll %edx
andl $-0x3, %ecx
orl %edx, %ecx
movl %ecx, 0x10(%rax)
movq 0x10(%rbp), %rcx
movl 0x28(%rcx), %ecx
movl %ecx, 0x30(%rax)
movq -0x98(%rbp), %rdi
callq 0x3380
movq -0xa8(%rbp), %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)