// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/bam_md.c
// Function: bam_prob_realn_core
int bam_prob_realn_core(bam1_t *b, const char *ref, int flag)
{
	int k, i, bw, x, y, yb, ye, xb, xe, apply_baq = flag&1, extend_baq = flag>>1&1;
	uint32_t *cigar = bam1_cigar(b);
	bam1_core_t *c = &b->core;
	kpa_par_t conf = kpa_par_def;
	uint8_t *bq = 0, *zq = 0, *qual = bam1_qual(b);
	if ((c->flag & BAM_FUNMAP) || b->core.l_qseq == 0) return -1; // do nothing
	// test if BQ or ZQ is present
	if ((bq = bam_aux_get(b, "BQ")) != 0) ++bq;
	if ((zq = bam_aux_get(b, "ZQ")) != 0 && *zq == 'Z') ++zq;
	if (bq && zq) { // remove the ZQ tag
		bam_aux_del(b, zq-1);
		zq = 0;
	}
	if (bq || zq) {
		if ((apply_baq && zq) || (!apply_baq && bq)) return -3; // in both cases, do nothing
		if (bq && apply_baq) { // then convert BQ to ZQ
			for (i = 0; i < c->l_qseq; ++i)
				qual[i] = qual[i] + 64 < bq[i]? 0 : qual[i] - ((int)bq[i] - 64);
			*(bq - 3) = 'Z';
		} else if (zq && !apply_baq) { // then convert ZQ to BQ
			for (i = 0; i < c->l_qseq; ++i)
				qual[i] += (int)zq[i] - 64;
			*(zq - 3) = 'B';
		}
		return 0;
	}
	// find the start and end of the alignment	
	x = c->pos, y = 0, yb = ye = xb = xe = -1;
	for (k = 0; k < c->n_cigar; ++k) {
		int op, l;
		op = cigar[k]&0xf; l = cigar[k]>>4;
		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
			if (yb < 0) yb = y;
			if (xb < 0) xb = x;
			ye = y + l; xe = x + l;
			x += l; y += l;
		} else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
		else if (op == BAM_CDEL) x += l;
		else if (op == BAM_CREF_SKIP) return -1; // do nothing if there is a reference skip
	}
	// set bandwidth and the start and the end
	bw = 7;
	if (abs((xe - xb) - (ye - yb)) > bw)
		bw = abs((xe - xb) - (ye - yb)) + 3;
	conf.bw = bw;
	xb -= yb + bw/2; if (xb < 0) xb = 0;
	xe += c->l_qseq - ye + bw/2;
	if (xe - xb - c->l_qseq > bw)
		xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2;
	{ // glocal
		uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq;
		int *state;
		bq = calloc(c->l_qseq + 1, 1);
		memcpy(bq, qual, c->l_qseq);
		s = calloc(c->l_qseq, 1);
		for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)];
		r = calloc(xe - xb, 1);
		for (i = xb; i < xe; ++i) {
			if (ref[i] == 0) { xe = i; break; }
			r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]];
		}
		state = calloc(c->l_qseq, sizeof(int));
		q = calloc(c->l_qseq, 1);
		kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q);
		if (!extend_baq) { // in this block, bq[] is capped by base quality qual[]
			for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
				int op = cigar[k]&0xf, l = cigar[k]>>4;
				if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
					for (i = y; i < y + l; ++i) {
						if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) bq[i] = 0;
						else bq[i] = bq[i] < q[i]? bq[i] : q[i];
					}
					x += l; y += l;
				} else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
				else if (op == BAM_CDEL) x += l;
			}
			for (i = 0; i < c->l_qseq; ++i) bq[i] = qual[i] - bq[i] + 64; // finalize BQ
		} else { // in this block, bq[] is BAQ that can be larger than qual[] (different from the above!)
			uint8_t *left, *rght;
			left = calloc(c->l_qseq, 1); rght = calloc(c->l_qseq, 1);
			for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) {
				int op = cigar[k]&0xf, l = cigar[k]>>4;
				if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
					for (i = y; i < y + l; ++i)
						bq[i] = ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y))? 0 : q[i];
					for (left[y] = bq[y], i = y + 1; i < y + l; ++i)
						left[i] = bq[i] > left[i-1]? bq[i] : left[i-1];
					for (rght[y+l-1] = bq[y+l-1], i = y + l - 2; i >= y; --i)
						rght[i] = bq[i] > rght[i+1]? bq[i] : rght[i+1];
					for (i = y; i < y + l; ++i)
						bq[i] = left[i] < rght[i]? left[i] : rght[i];
					x += l; y += l;
				} else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l;
				else if (op == BAM_CDEL) x += l;
			}
			for (i = 0; i < c->l_qseq; ++i) bq[i] = 64 + (qual[i] <= bq[i]? 0 : qual[i] - bq[i]); // finalize BQ
			free(left); free(rght);
		}
		if (apply_baq) {
			for (i = 0; i < c->l_qseq; ++i) qual[i] -= bq[i] - 64; // modify qual
			bam_aux_append(b, "ZQ", 'Z', c->l_qseq + 1, bq);
		} else bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq);
		free(bq); free(s); free(r); free(q); free(state);
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x8(%rdi), %rax
movl %eax, %ebx
shrl $0x18, %ebx
leaq 0x1744a7(%rip), %rcx    # 0x411580
movq (%rcx), %rcx
addq 0x30(%rdi), %rbx
movq %rcx, 0x48(%rsp)
movl 0x10(%rdi), %ecx
movq %rax, %r8
shrq $0x22, %r8
andl $0x1, %r8d
testl %ecx, %ecx
sete %r9b
orb %r8b, %r9b
je 0x29d106
pushq $-0x1
popq %rax
jmp 0x29d7da
movq %rsi, 0x28(%rsp)
movl %edx, 0xc(%rsp)
shrq $0x30, %rax
leaq (%rbx,%rax,4), %rax
incl %ecx
sarl %ecx
movslq %ecx, %r14
addq %rax, %r14
leaq 0xbe6cf(%rip), %rsi     # 0x35b7f7
movq %rdi, %r13
callq 0x293fec
movq %rax, %r15
leaq 0x1(%rax), %r12
testq %rax, %rax
cmoveq %rax, %r12
leaq 0xbe6b5(%rip), %rsi     # 0x35b7fa
movq %r13, 0x10(%rsp)
movq %r13, %rdi
callq 0x293fec
testq %rax, %rax
je 0x29d179
xorl %esi, %esi
cmpb $0x5a, (%rax)
sete %sil
addq %rax, %rsi
testq %r15, %r15
je 0x29d188
decq %rsi
movq 0x10(%rsp), %rdi
callq 0x29410b
xorl %edx, %edx
jmp 0x29d184
xorl %edx, %edx
testq %r15, %r15
je 0x29d23d
xorl %esi, %esi
jmp 0x29d18a
movb $0x1, %dl
movl 0xc(%rsp), %r9d
testq %r15, %r15
setne %cl
testb $0x1, %r9b
sete %dil
pushq $-0x3
popq %rax
testb %cl, %dil
jne 0x29d7da
andb %r9b, %dl
movq 0x10(%rsp), %r8
jne 0x29d7da
testb $0x1, %r9b
sete %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0x29d1fb
xorl %eax, %eax
xorl %ecx, %ecx
movslq 0x10(%r8), %rdx
cmpq %rdx, %rcx
jge 0x29d232
movzbl (%r14,%rcx), %edx
movl %edx, %esi
addl $0x40, %esi
movzbl (%r12,%rcx), %edi
subb %dil, %dl
addb $0x40, %dl
cmpl %edi, %esi
movzbl %dl, %edx
cmovbl %eax, %edx
movb %dl, (%r14,%rcx)
incq %rcx
jmp 0x29d1cc
xorl %eax, %eax
testb $0x1, %r9b
jne 0x29d7da
testq %rsi, %rsi
je 0x29d7da
xorl %eax, %eax
movslq 0x10(%r8), %rcx
cmpq %rcx, %rax
jge 0x29d356
movb (%rsi,%rax), %cl
addb (%r14,%rax), %cl
addb $-0x40, %cl
movb %cl, (%r14,%rax)
incq %rax
jmp 0x29d212
movb $0x5a, -0x3(%r12)
jmp 0x29d7d8
movq 0x10(%rsp), %rax
movl 0x4(%rax), %r8d
movq 0x8(%rax), %rbp
movq %rbp, %rdi
shrq $0x30, %rdi
pushq $-0x1
popq %rsi
xorl %r10d, %r10d
movl %esi, %ecx
movl %esi, %r9d
movl %esi, %eax
cmpq %r10, %rdi
je 0x29d2ca
movl (%rbx,%r10,4), %r12d
movl %r12d, %r15d
andl $0xf, %r15d
movl %r12d, %r11d
shrl $0x4, %r11d
cmpl $0x7, %r15d
setne %r13b
testb $0x7, %r12b
setne %r12b
testb %r13b, %r12b
jne 0x29d2a3
testl %eax, %eax
cmovsl %edx, %eax
testl %ecx, %ecx
cmovsl %r8d, %ecx
addl %r11d, %edx
addl %r11d, %r8d
movl %r8d, %esi
movl %edx, %r9d
jmp 0x29d2c5
decl %r15d
cmpl $0x3, %r15d
ja 0x29d2c5
leaq 0xbe461(%rip), %r12     # 0x35b714
movslq (%r12,%r15,4), %r15
addq %r12, %r15
jmpq *%r15
addl %r11d, %edx
jmp 0x29d2c5
addl %r11d, %r8d
incq %r10
jmp 0x29d25e
movl %esi, %r8d
subl %ecx, %r8d
subl %r9d, %r8d
addl %eax, %r8d
movl %r8d, %edx
negl %edx
cmovsl %r8d, %edx
leal 0x3(%rdx), %r10d
cmpl $0x8, %edx
pushq $0x7
popq %r8
cmovael %r10d, %r8d
movl %r8d, %edx
shrl %edx
addl %edx, %eax
subl %eax, %ecx
xorl %r12d, %r12d
testl %ecx, %ecx
cmovlel %r12d, %ecx
movl %r8d, 0x50(%rsp)
movq 0x10(%rsp), %rax
movslq 0x10(%rax), %rax
subl %r9d, %esi
addl %edx, %esi
movq %rax, 0x38(%rsp)
leal (%rsi,%rax), %r11d
movl %esi, %eax
subl %ecx, %eax
subl %r8d, %eax
jle 0x29d35f
pushq $0x2
popq %r9
movq %rcx, %r10
cltd
idivl %r9d
movl %eax, %ecx
addl %r10d, %ecx
addl %ecx, %r8d
subl %r8d, %esi
pushq $-0x2
popq %r8
movl %esi, %eax
cltd
idivl %r8d
addl %eax, %r11d
movl %r11d, 0x1c(%rsp)
movl %ecx, %eax
movq %rax, 0x20(%rsp)
jmp 0x29d369
movb $0x42, -0x3(%rsi)
jmp 0x29d7d8
movl %r11d, 0x1c(%rsp)
movq %rcx, 0x20(%rsp)
shlq $0x2, %rdi
movq 0x10(%rsp), %rax
addq 0x30(%rax), %rdi
shrl $0x18, %ebp
addq %rdi, %rbp
movq 0x38(%rsp), %r13
leaq 0x1(%r13), %rdi
pushq $0x1
popq %rsi
callq 0x31f40
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x32ca0
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x31f40
testl %r13d, %r13d
movl $0x0, %edi
cmovgl %r13d, %edi
leaq 0x173fb4(%rip), %r13    # 0x411370
xorl %edx, %edx
cmpq %rdx, %rdi
je 0x29d3ea
movl %edx, %ecx
shrl %ecx
movzbl (%rbp,%rcx), %esi
movl %r12d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %esi
andl $0xf, %esi
movb (%r13,%rsi), %cl
movb %cl, (%rax,%rdx)
incq %rdx
addl $0x4, %r12d
jmp 0x29d3be
movq %rax, 0x40(%rsp)
movl 0x1c(%rsp), %ebp
movl %ebp, %eax
movq 0x20(%rsp), %r12
subl %r12d, %eax
movl %eax, 0x30(%rsp)
movslq %eax, %rdi
pushq $0x1
popq %rsi
callq 0x31f40
movslq %r12d, %r8
movq %rax, %r12
movslq %ebp, %rcx
movq 0x28(%rsp), %rax
addq %r8, %rax
xorl %edx, %edx
leaq 0x173c3a(%rip), %rsi    # 0x411060
leaq (%r8,%rdx), %rdi
cmpq %rcx, %rdi
jge 0x29d44f
movsbq (%rax,%rdx), %rdi
testq %rdi, %rdi
je 0x29d44b
movzbl (%rsi,%rdi), %edi
movb (%r13,%rdi), %dil
movb %dil, (%r12,%rdx)
incq %rdx
jmp 0x29d426
movl %edx, 0x30(%rsp)
pushq $0x4
popq %rsi
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
callq 0x31f40
movq %rax, %r13
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0x31f40
leaq 0x48(%rsp), %r9
movq %r12, %rdi
movl 0x30(%rsp), %esi
movq 0x40(%rsp), %rdx
movl %ebp, %ecx
movq %r14, %r8
movq %rax, 0x28(%rsp)
pushq %rax
pushq %r13
callq 0x2a5e48
popq %rcx
popq %rdx
testb $0x2, 0xc(%rsp)
jne 0x29d56d
movq 0x10(%rsp), %rax
movl 0x4(%rax), %ecx
movzwl 0xe(%rax), %eax
movq 0x20(%rsp), %rdx
negl %edx
movq %rdx, 0x20(%rsp)
xorl %edx, %edx
xorl %r8d, %r8d
cmpq %rax, %rdx
je 0x29d6df
movl (%rbx,%rdx,4), %r9d
movl %r9d, %edi
andl $0xf, %edi
movl %r9d, %esi
shrl $0x4, %esi
cmpl $0x7, %edi
setne %r10b
testb $0x7, %r9b
setne %r9b
testb %r10b, %r9b
jne 0x29d542
leal (%rsi,%r8), %edi
movslq %r8d, %r8
movslq %edi, %r9
movq 0x20(%rsp), %r10
addl %ecx, %r10d
cmpq %r9, %r8
jge 0x29d53c
movl (%r13,%r8,4), %r11d
testb $0x3, %r11b
jne 0x29d52f
sarl $0x2, %r11d
cmpl %r11d, %r10d
jne 0x29d52f
movzbl (%r15,%r8), %r11d
movq 0x28(%rsp), %rbp
movzbl (%rbp,%r8), %ebp
cmpb %bpl, %r11b
cmovbl %r11d, %ebp
movb %bpl, (%r15,%r8)
jmp 0x29d534
movb $0x0, (%r15,%r8)
incq %r8
incl %r10d
jmp 0x29d4f9
addl %ecx, %esi
movl %esi, %ecx
jmp 0x29d562
cmpl $0x4, %edi
je 0x29d551
cmpl $0x2, %edi
je 0x29d558
cmpl $0x1, %edi
jne 0x29d55f
addl %r8d, %esi
movl %esi, %edi
jmp 0x29d562
addl %ecx, %esi
movl %r8d, %edi
jmp 0x29d53e
movl %r8d, %edi
incq %rdx
movl %edi, %r8d
jmp 0x29d4ba
movq %r12, 0x30(%rsp)
movq 0x10(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x1c(%rsp)
movslq %eax, %rbp
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0x31f40
movq %rax, %r12
movq %rbp, %rdi
pushq $0x1
popq %rsi
callq 0x31f40
movq %rax, %rbp
movq 0x10(%rsp), %rax
movl 0x4(%rax), %ecx
movzwl 0xe(%rax), %r10d
movq 0x20(%rsp), %rax
negl %eax
movq %rax, 0x20(%rsp)
xorl %edx, %edx
xorl %r8d, %r8d
movq %r10, 0x38(%rsp)
cmpq %r10, %rdx
je 0x29d70c
movl (%rbx,%rdx,4), %eax
movl %eax, %edi
andl $0xf, %edi
movl %eax, %esi
shrl $0x4, %esi
cmpl $0x7, %edi
setne %r9b
testb $0x7, %al
setne %al
testb %r9b, %al
jne 0x29d6b2
leal (%rsi,%r8), %edi
movslq %r8d, %r8
movslq %edi, %r9
movq 0x20(%rsp), %rax
leal (%rax,%rcx), %r10d
movq %r8, %r11
cmpq %r9, %r11
jge 0x29d630
movl (%r13,%r11,4), %eax
testb $0x3, %al
jne 0x29d622
sarl $0x2, %eax
cmpl %eax, %r10d
jne 0x29d622
movq 0x28(%rsp), %rax
movb (%rax,%r11), %al
jmp 0x29d624
xorl %eax, %eax
movb %al, (%r15,%r11)
incq %r11
incl %r10d
jmp 0x29d601
movb (%r15,%r8), %r11b
leaq 0x1(%r8), %r10
movb %r11b, -0x1(%r12,%r10)
cmpq %r9, %r10
jge 0x29d657
movzbl (%r15,%r10), %eax
cmpb %r11b, %al
movzbl %r11b, %r11d
cmoval %eax, %r11d
incq %r10
jmp 0x29d638
movb -0x1(%r15,%r9), %al
movb %al, -0x1(%rbp,%r9)
leal -0x2(%r9), %eax
movslq %eax, %r10
cmpq %r8, %r10
jl 0x29d6a2
movzbl (%r15,%r10), %eax
movzbl 0x1(%rbp,%r10), %r11d
cmpb %r11b, %al
cmoval %eax, %r11d
movb %r11b, (%rbp,%r10)
decq %r10
jmp 0x29d668
movzbl (%r12,%r8), %eax
movzbl (%rbp,%r8), %r10d
cmpb %r10b, %al
cmovbl %eax, %r10d
movb %r10b, (%r15,%r8)
incq %r8
cmpq %r9, %r8
jl 0x29d689
addl %ecx, %esi
movl %esi, %ecx
movq 0x38(%rsp), %r10
jmp 0x29d6d4
cmpl $0x4, %edi
je 0x29d6c1
cmpl $0x2, %edi
je 0x29d6c8
cmpl $0x1, %edi
jne 0x29d6d1
addl %r8d, %esi
movl %esi, %edi
jmp 0x29d6d4
addl %ecx, %esi
movl %r8d, %edi
movl %esi, %ecx
jmp 0x29d6d4
movl %r8d, %edi
incq %rdx
movl %edi, %r8d
jmp 0x29d5c0
movq 0x10(%rsp), %rdi
movl 0x10(%rdi), %ebx
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
cmpq %rax, %rcx
je 0x29d763
movb (%r14,%rax), %dl
subb (%r15,%rax), %dl
addb $0x40, %dl
movb %dl, (%r15,%rax)
incq %rax
jmp 0x29d6f3
xorl %eax, %eax
movl 0x1c(%rsp), %ebx
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
pushq $0x40
popq %rdx
cmpq %rax, %rcx
je 0x29d749
movb (%r14,%rax), %sil
movb (%r15,%rax), %dil
movl %esi, %r8d
subb %dil, %r8b
addb $0x40, %r8b
cmpb %dil, %sil
movzbl %r8b, %esi
cmovbel %edx, %esi
movb %sil, (%r15,%rax)
incq %rax
jmp 0x29d71f
movq %r12, %rdi
callq 0x33058
movq %rbp, %rdi
callq 0x33058
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %r12
testb $0x1, 0xc(%rsp)
jne 0x29d775
incl %ebx
leaq 0xbe084(%rip), %rsi     # 0x35b7f7
jmp 0x29d79f
xorl %eax, %eax
movslq %ebx, %rcx
cmpq %rcx, %rax
jge 0x29d796
movb (%r14,%rax), %cl
subb (%r15,%rax), %cl
addb $0x40, %cl
movb %cl, (%r14,%rax)
incq %rax
movl 0x10(%rdi), %ebx
jmp 0x29d777
incl %ebx
leaq 0xbe05b(%rip), %rsi     # 0x35b7fa
pushq $0x5a
popq %rdx
movl %ebx, %ecx
movq %r15, %r8
callq 0x293f30
movq %r15, %rdi
callq 0x33058
movq 0x40(%rsp), %rdi
callq 0x33058
movq %r12, %rdi
callq 0x33058
movq 0x28(%rsp), %rdi
callq 0x33058
movq %r13, %rdi
callq 0x33058
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq