// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/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, redo_baq = flag&4;
	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 && redo_baq)
	{
	    bam_aux_del(b, bq-1);
	    bq = 0;
	}
	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 $0x68, %rsp
movq 0x8(%rdi), %rcx
movl %ecx, %r10d
shrl $0x18, %r10d
leaq 0x231f4(%rip), %r8      # 0x51300
movl 0x8(%r8), %r9d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq 0x30(%rdi), %r10
movl %r9d, 0x60(%rsp)
movq (%r8), %r8
movq %r8, 0x58(%rsp)
movl 0x10(%rdi), %r9d
movq %rcx, %r11
shrq $0x22, %r11
andl $0x1, %r11d
testl %r9d, %r9d
sete %r8b
orb %r11b, %r8b
jne 0x2e760
movq %rsi, 0x20(%rsp)
movl %edx, 0x4(%rsp)
shrq $0x30, %rcx
movq %r10, 0x30(%rsp)
leaq (%r10,%rcx,4), %r14
incl %r9d
sarl %r9d
movq %rdi, %rbx
movslq %r9d, %r13
leaq 0x166b4(%rip), %rsi     # 0x44822
callq 0x22374
movq %rax, %r15
leaq 0x1(%rax), %r12
testq %rax, %rax
setne %bpl
cmoveq %rax, %r12
leaq 0x13b4a(%rip), %rsi     # 0x41cd6
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
callq 0x22374
xorl %ebx, %ebx
testq %rax, %rax
je 0x2e1a9
cmpb $0x5a, (%rax)
sete %bl
addq %rax, %rbx
addq %r13, %r14
testq %r15, %r15
sete %al
testb $0x4, 0x4(%rsp)
sete %cl
orb %al, %cl
movq %r14, 0x10(%rsp)
jne 0x2e31c
movq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x224e7
xorl %ebp, %ebp
xorl %r12d, %r12d
movl 0x4(%rsp), %esi
testq %rbx, %rbx
setne %cl
testb %bpl, %bpl
jne 0x2e34c
testq %rbx, %rbx
jne 0x2e34c
movq 0x8(%r13), %rbp
movq %rbp, 0x28(%rsp)
shrq $0x30, %rbp
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x30(%rsp), %r12
je 0x2e29d
movl 0x4(%r13), %edi
xorl %r8d, %r8d
leaq 0x1651a(%rip), %r9      # 0x44748
xorl %r10d, %r10d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl (%r12,%r8,4), %r14d
movl %r14d, %ebx
andl $0xf, %ebx
movl %r14d, %r11d
shrl $0x4, %r11d
cmpl $0x7, %ebx
setne %r15b
testb $0x7, %r14b
setne %r14b
testb %r15b, %r14b
jne 0x2e27d
testl %ecx, %ecx
cmovsl %r10d, %ecx
testl %edx, %edx
cmovsl %edi, %edx
addl %r11d, %r10d
addl %r11d, %edi
movl %edi, %eax
movl %r10d, %esi
jmp 0x2e295
decl %ebx
cmpl $0x3, %ebx
ja 0x2e295
movslq (%r9,%rbx,4), %rbx
addq %r9, %rbx
jmpq *%rbx
addl %r11d, %r10d
jmp 0x2e295
addl %r11d, %edi
incq %r8
cmpq %r8, %rbp
jne 0x2e240
movl %eax, %r8d
subl %edx, %r8d
subl %esi, %r8d
addl %ecx, %r8d
movl %r8d, %edi
negl %edi
cmovsl %r8d, %edi
leal 0x3(%rdi), %r8d
cmpl $0x8, %edi
movl $0x7, %edi
cmovael %r8d, %edi
movl %edi, %r8d
shrl %r8d
addl %r8d, %ecx
subl %ecx, %edx
xorl %r9d, %r9d
testl %edx, %edx
cmovgl %edx, %r9d
movl %edi, 0x60(%rsp)
movslq 0x10(%r13), %rbx
subl %esi, %eax
addl %r8d, %eax
leal (%rax,%rbx), %r12d
movl %eax, %ecx
subl %r9d, %ecx
subl %edi, %ecx
jle 0x2e3c9
movl %ecx, %edx
shrl $0x1f, %edx
addl %ecx, %edx
sarl %edx
addl %r9d, %edx
addl %edx, %edi
subl %edi, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
subl %ecx, %r12d
movl %edx, %eax
movq %rax, 0x8(%rsp)
jmp 0x2e3ce
testq %r15, %r15
movq 0x18(%rsp), %r13
je 0x2e1dc
testq %rbx, %rbx
je 0x2e1dc
decq %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x224e7
movb $0x1, %bpl
xorl %ecx, %ecx
xorl %ebx, %ebx
movl 0x4(%rsp), %esi
testb $0x1, %sil
sete %dl
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
testb %bpl, %dl
jne 0x2e760
andb %sil, %cl
testb $0x1, %cl
jne 0x2e760
testb $0x1, %sil
sete %al
xorb $0x1, %bpl
orb %al, %bpl
jne 0x2e4a6
cmpl $0x0, 0x10(%r13)
movq 0x10(%rsp), %r8
jle 0x2e3be
xorl %eax, %eax
xorl %ecx, %ecx
movzbl (%r8,%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, (%r8,%rcx)
incq %rcx
movslq 0x10(%r13), %rdx
cmpq %rdx, %rcx
jl 0x2e391
movb $0x5a, -0x3(%r12)
jmp 0x2e75e
movq %r9, 0x8(%rsp)
shlq $0x2, %rbp
addq 0x30(%r13), %rbp
leaq 0x1(%rbx), %rdi
movl $0x1, %esi
callq 0x3610
movq %rax, %r15
movq %rax, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x37d0
movl $0x1, %esi
movq %rbx, %rdi
callq 0x3610
leaq 0x22ce5(%rip), %r13     # 0x510f0
testl %ebx, %ebx
movl 0x4(%rsp), %r14d
jle 0x2e44a
movq 0x28(%rsp), %rcx
shrl $0x18, %ecx
addq %rcx, %rbp
xorl %edi, %edi
xorl %edx, %edx
movl %edx, %ecx
shrl %ecx
movzbl (%rbp,%rcx), %esi
movl %edi, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %esi
andl $0xf, %esi
movb (%rsi,%r13), %cl
movb %cl, (%rax,%rdx)
incq %rdx
addl $0x4, %edi
cmpq %rdx, %rbx
jne 0x2e423
movq %rax, 0x50(%rsp)
movl %r12d, %eax
movq 0x8(%rsp), %rbp
subl %ebp, %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3610
subl %ebp, %r12d
movq %rax, 0x48(%rsp)
jle 0x2e4ed
movslq %ebp, %rcx
movq 0x20(%rsp), %r8
addq %rcx, %r8
movl %r12d, %ecx
xorl %edi, %edi
leaq 0x22959(%rip), %rdx     # 0x50de0
movsbq (%r8,%rdi), %rsi
testq %rsi, %rsi
je 0x2e4ea
movzbl (%rdx,%rsi), %esi
movb (%rsi,%r13), %sil
movb %sil, (%rax,%rdi)
incq %rdi
cmpl %edi, %ecx
jne 0x2e487
jmp 0x2e4ed
xorl %eax, %eax
testb $0x1, %sil
movq 0x10(%rsp), %rsi
jne 0x2e760
testq %rbx, %rbx
je 0x2e760
cmpl $0x0, 0x10(%r13)
jle 0x2e4e1
xorl %ecx, %ecx
movb (%rbx,%rcx), %dl
addb (%rsi,%rcx), %dl
addb $-0x40, %dl
movb %dl, (%rsi,%rcx)
incq %rcx
movslq 0x10(%r13), %rdx
cmpq %rdx, %rcx
jl 0x2e4c9
movb $0x42, -0x3(%rbx)
jmp 0x2e760
movl %edi, %r12d
movabsq $0x1000000000000, %r13  # imm = 0x1000000000000
movl $0x4, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, %rbp
movl $0x1, %esi
movq %rbx, %rdi
callq 0x3610
leaq 0x58(%rsp), %r9
movq 0x48(%rsp), %rdi
movl %r12d, %esi
movq 0x50(%rsp), %rdx
movl %ebx, %ecx
movq 0x10(%rsp), %r8
movq %rax, 0x20(%rsp)
pushq %rax
pushq %rbp
callq 0x38274
addq $0x10, %rsp
testb $0x2, %r14b
movq 0x18(%rsp), %r14
jne 0x2e64f
movq 0x8(%r14), %rax
cmpq %r13, %rax
movq 0x30(%rsp), %r12
movq 0x8(%rsp), %r13
jb 0x2e620
shrq $0x30, %rax
movl 0x4(%r14), %edx
negl %r13d
xorl %ecx, %ecx
xorl %r8d, %r8d
movl (%r12,%rcx,4), %r9d
movl %r9d, %edi
andl $0xf, %edi
movl %r9d, %esi
shrl $0x4, %esi
cmpl $0x7, %edi
setne %r10b
testb $0x7, %r9b
setne %r11b
testb %r10b, %r11b
jne 0x2e5f1
leal (%rsi,%r8), %edi
cmpl $0x10, %r9d
jb 0x2e5eb
movslq %r8d, %r8
movslq %edi, %r9
leal (%rdx,%r13), %r10d
movl (%rbp,%r8,4), %r11d
testb $0x3, %r11b
jne 0x2e5db
sarl $0x2, %r11d
cmpl %r11d, %r10d
jne 0x2e5db
movzbl (%r15,%r8), %r11d
movq 0x20(%rsp), %rbx
movzbl (%rbx,%r8), %ebx
cmpb %bl, %r11b
cmovbl %r11d, %ebx
movb %bl, (%r15,%r8)
jmp 0x2e5e0
movb $0x0, (%r15,%r8)
incq %r8
incl %r10d
cmpq %r9, %r8
jl 0x2e5ab
addl %edx, %esi
movl %esi, %edx
jmp 0x2e611
cmpl $0x4, %edi
je 0x2e600
cmpl $0x2, %edi
je 0x2e607
cmpl $0x1, %edi
jne 0x2e60e
addl %r8d, %esi
movl %esi, %edi
jmp 0x2e611
addl %edx, %esi
movl %r8d, %edi
jmp 0x2e5ed
movl %r8d, %edi
incq %rcx
movl %edi, %r8d
cmpq %rax, %rcx
jne 0x2e573
movslq 0x10(%r14), %r12
testq %r12, %r12
movq 0x10(%rsp), %rdx
jle 0x2e6e0
xorl %eax, %eax
movb (%rdx,%rax), %cl
subb (%r15,%rax), %cl
addb $0x40, %cl
movb %cl, (%r15,%rax)
incq %rax
cmpq %rax, %r12
jne 0x2e634
jmp 0x2e6e0
movl 0x10(%r14), %r12d
movslq %r12d, %rbx
movl $0x1, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, %r13
movl $0x1, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, %rbx
movq 0x8(%r14), %r14
movabsq $0x1000000000000, %rax  # imm = 0x1000000000000
cmpq %rax, %r14
jae 0x2e76f
testl %r12d, %r12d
movq 0x10(%rsp), %r8
jle 0x2e6c5
xorl %eax, %eax
movl $0x40, %ecx
movb (%r8,%rax), %dl
movb (%r15,%rax), %sil
movl %edx, %edi
subb %sil, %dil
addb $0x40, %dil
cmpb %sil, %dl
movzbl %dil, %edx
cmovbel %ecx, %edx
movb %dl, (%r15,%rax)
incq %rax
cmpq %rax, %r12
jne 0x2e69e
movq %r13, %rdi
movq %r8, %r14
callq 0x3350
movq %rbx, %rdi
callq 0x3350
movq %r14, %rdx
movq 0x18(%rsp), %r14
testb $0x1, 0x4(%rsp)
jne 0x2e6f3
incl %r12d
leaq 0x16131(%rip), %rsi     # 0x44822
jmp 0x2e71d
testl %r12d, %r12d
jle 0x2e713
xorl %eax, %eax
movb (%rdx,%rax), %cl
subb (%r15,%rax), %cl
addb $0x40, %cl
movb %cl, (%rdx,%rax)
incq %rax
movslq 0x10(%r14), %r12
cmpq %r12, %rax
jl 0x2e6fa
incl %r12d
leaq 0x135b9(%rip), %rsi     # 0x41cd6
movq %r14, %rdi
movl $0x5a, %edx
movl %r12d, %ecx
movq %r15, %r8
callq 0x222b8
movq %r15, %rdi
callq 0x3350
movq 0x50(%rsp), %rdi
callq 0x3350
movq 0x48(%rsp), %rdi
callq 0x3350
movq 0x20(%rsp), %rdi
callq 0x3350
movq %rbp, %rdi
callq 0x3350
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
shrq $0x30, %r14
movq 0x18(%rsp), %rax
movl 0x4(%rax), %r10d
movq 0x8(%rsp), %rax
negl %eax
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
xorl %r11d, %r11d
movq %r14, 0x40(%rsp)
movq 0x30(%rsp), %rax
movl (%rax,%rcx,4), %r9d
movl %r9d, %eax
andl $0xf, %eax
movl %r9d, %esi
shrl $0x4, %esi
cmpl $0x7, %eax
setne %dl
testb $0x7, %r9b
setne %dil
testb %dl, %dil
jne 0x2e7e1
leal (%rsi,%r11), %eax
movl %eax, 0x3c(%rsp)
movslq %eax, %r8
cmpl $0xf, %r9d
movq %r10, 0x28(%rsp)
movslq %r11d, %rdx
ja 0x2e802
movb (%r15,%rdx), %al
movb %al, (%r13,%rdx)
jmp 0x2e87f
cmpl $0x4, %eax
je 0x2e7f8
cmpl $0x2, %eax
je 0x2e8f9
cmpl $0x1, %eax
jne 0x2e904
addl %r11d, %esi
movl %esi, %eax
jmp 0x2e8e5
movq 0x8(%rsp), %rax
addl %r10d, %eax
movq %rdx, %r10
movl (%rbp,%r10,4), %r14d
testb $0x3, %r14b
jne 0x2e82c
sarl $0x2, %r14d
cmpl %r14d, %eax
jne 0x2e82c
movq 0x20(%rsp), %rdi
movb (%rdi,%r10), %r14b
jmp 0x2e82f
xorl %r14d, %r14d
movb %r14b, (%r15,%r10)
incq %r10
incl %eax
cmpq %r8, %r10
jl 0x2e80d
movb (%r15,%rdx), %al
movb %al, (%r13,%rdx)
cmpl $0x20, %r9d
jb 0x2e87a
leal 0x1(%r11), %eax
cltq
movl %r11d, %r10d
movzbl (%r15,%rax), %r14d
movslq %r10d, %r10
movzbl (%r13,%r10), %edi
cmpb %dil, %r14b
cmoval %r14d, %edi
movb %dil, (%r13,%rax)
incq %rax
incl %r10d
cmpq %r8, %rax
jl 0x2e855
movq 0x40(%rsp), %r14
movb -0x1(%r15,%r8), %al
movb %al, -0x1(%rbx,%r8)
leaq -0x2(%r8), %rax
cmpl %r11d, %eax
jl 0x2e8b8
movb 0x1(%rbx,%rax), %r10b
incq %rax
movzbl -0x1(%r15,%rax), %edi
movzbl %r10b, %r10d
cmpb %r10b, %dil
cmoval %edi, %r10d
movb %r10b, -0x1(%rbx,%rax)
decq %rax
cmpq %rdx, %rax
jg 0x2e89a
cmpl $0x10, %r9d
jb 0x2e8da
movzbl (%r13,%rdx), %eax
movzbl (%rbx,%rdx), %edi
cmpb %dil, %al
cmovbl %eax, %edi
movb %dil, (%r15,%rdx)
incq %rdx
cmpq %r8, %rdx
jl 0x2e8be
addl 0x28(%rsp), %esi
movl %esi, %r10d
movl 0x3c(%rsp), %eax
incq %rcx
movl %eax, %r11d
cmpq %r14, %rcx
jne 0x2e792
jmp 0x2e68d
addl %r10d, %esi
movl %r11d, %eax
movl %esi, %r10d
jmp 0x2e8e5
movl %r11d, %eax
jmp 0x2e8e5
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2e760