// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_md.c
// Function: bam_prob_realn_core
int bam_prob_realn_core(bam1_t *b, const char *ref, int apply_baq)
{
	int k, i, bw, x, y, yb, ye, xb, xe;
	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) 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) {
			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)
			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);
		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) {
				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
		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 $0x48, %rsp
movl %edx, %ebp
movq 0x8(%rdi), %rcx
movl %ecx, %r12d
shrl $0x18, %r12d
leaq 0x15621(%rip), %rax     # 0x2f0d0
movq (%rax), %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq 0x30(%rdi), %r12
movq %rdx, 0x38(%rsp)
btq $0x22, %rcx
jb 0x19fce
movq %rdi, %rbx
movq %rsi, 0x28(%rsp)
shrq $0x30, %rcx
leaq (%r12,%rcx,4), %rax
movl 0x10(%rdi), %ecx
movl %ecx, %edx
incl %edx
shrl $0x1f, %edx
addl %edx, %ecx
incl %ecx
sarl %ecx
movslq %ecx, %r14
addq %rax, %r14
leaq 0xc4d2(%rip), %rsi      # 0x25fca
callq 0x1175c
movq %rax, %r15
leaq 0x1(%rax), %r13
testq %rax, %rax
cmoveq %rax, %r13
leaq 0xc4bb(%rip), %rsi      # 0x25fcd
movq %rbx, %rdi
callq 0x1175c
testq %rax, %rax
je 0x19b3d
xorl %esi, %esi
cmpb $0x5a, (%rax)
sete %sil
addq %rax, %rsi
testq %r15, %r15
je 0x19b4c
decq %rsi
movq %rbx, %rdi
callq 0x11844
jmp 0x19b46
testq %r15, %r15
je 0x19bff
xorl %eax, %eax
xorl %esi, %esi
jmp 0x19b4e
movb $0x1, %al
testq %r15, %r15
setne %cl
testl %ebp, %ebp
setne %dl
sete %dil
andb %al, %dl
andb %cl, %dil
orb %dl, %dil
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jne 0x19fce
testl %ebp, %ebp
sete %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0x19bc1
cmpl $0x0, 0x10(%rbx)
jle 0x19bb7
xorl %eax, %eax
xorl %ecx, %ecx
movzbl (%r14,%rcx), %edx
movl %edx, %esi
addl $0x40, %esi
movzbl (%r13,%rcx), %edi
subb %dil, %dl
addb $0x40, %dl
cmpl %edi, %esi
movzbl %dl, %edx
cmovbl %eax, %edx
movb %dl, (%r14,%rcx)
incq %rcx
movslq 0x10(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x19b89
movb $0x5a, -0x3(%r13)
jmp 0x19fcc
xorl %eax, %eax
testl %ebp, %ebp
jne 0x19fce
testq %rsi, %rsi
je 0x19fce
cmpl $0x0, 0x10(%rbx)
jle 0x19bf6
xorl %ecx, %ecx
movb (%rsi,%rcx), %dl
addb (%r14,%rcx), %dl
addb $-0x40, %dl
movb %dl, (%r14,%rcx)
incq %rcx
movslq 0x10(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x19bdc
movb $0x42, -0x3(%rsi)
jmp 0x19fce
movq 0x8(%rbx), %r13
movq %r13, 0x20(%rsp)
shrq $0x30, %r13
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
je 0x19c88
movl 0x4(%rbx), %edi
xorl %r8d, %r8d
leaq 0xc2f9(%rip), %r9       # 0x25f28
xorl %r10d, %r10d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl (%r12,%r8,4), %r11d
movl %r11d, %r15d
andl $0xf, %r15d
cmpl $0x4, %r15d
ja 0x19c80
shrl $0x4, %r11d
movslq (%r9,%r15,4), %r15
addq %r9, %r15
jmpq *%r15
addl %r11d, %r10d
jmp 0x19c80
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 0x19c80
addl %r11d, %edi
incq %r8
cmpq %r8, %r13
jne 0x19c41
movl %ebp, 0x1c(%rsp)
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, 0x40(%rsp)
movslq 0x10(%rbx), %rbp
subl %esi, %eax
addl %r8d, %eax
leal (%rax,%rbp), %esi
movl %eax, %ecx
subl %r9d, %ecx
subl %edi, %ecx
jle 0x19d06
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, %esi
movl %esi, 0x10(%rsp)
movl %edx, %eax
movq %rax, 0x8(%rsp)
jmp 0x19d0f
movl %esi, 0x10(%rsp)
movq %r9, 0x8(%rsp)
shlq $0x2, %r13
addq 0x30(%rbx), %r13
leaq 0x1(%rbp), %rdi
movl $0x1, %esi
callq 0x3530
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x3680
movl $0x1, %esi
movq %rbp, %rdi
callq 0x3530
testl %ebp, %ebp
jle 0x19d87
movq 0x20(%rsp), %rcx
shrl $0x18, %ecx
addq %rcx, %r13
xorl %r8d, %r8d
leaq 0x14604(%rip), %rdx     # 0x2e360
xorl %esi, %esi
movl %esi, %ecx
shrl %ecx
movzbl (%r13,%rcx), %edi
movl %r8d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edi
andl $0xf, %edi
movb (%rdx,%rdi), %cl
movb %cl, (%rax,%rsi)
incq %rsi
addl $0x4, %r8d
cmpq %rsi, %rbp
jne 0x19d5e
movq %rax, 0x20(%rsp)
movl 0x10(%rsp), %r13d
movl %r13d, %eax
subl 0x8(%rsp), %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3530
movl %r13d, %ecx
subl 0x8(%rsp), %ecx
movl %ecx, 0x34(%rsp)
movq 0x28(%rsp), %r8
jle 0x19dee
movslq 0x8(%rsp), %rcx
movslq %r13d, %r9
addq %rcx, %r8
subq %rcx, %r9
xorl %ecx, %ecx
leaq 0x14e12(%rip), %rdx     # 0x2ebe0
leaq 0x1458b(%rip), %rsi     # 0x2e360
movsbq (%r8,%rcx), %rdi
movzbl (%rdx,%rdi), %edi
movb (%rsi,%rdi), %dil
movb %dil, (%rax,%rcx)
incq %rcx
cmpq %rcx, %r9
jne 0x19dd5
movl $0x4, %esi
movq %rbp, %rdi
movq %rax, 0x10(%rsp)
callq 0x3530
movq %rax, %r13
movl $0x1, %esi
movq %rbp, %rdi
callq 0x3530
leaq 0x38(%rsp), %r9
movq 0x10(%rsp), %rdi
movl 0x34(%rsp), %esi
movq 0x20(%rsp), %rdx
movl %ebp, %ecx
movq %r14, %r8
movq %rax, 0x28(%rsp)
pushq %rax
pushq %r13
callq 0x20b7c
addq $0x10, %rsp
movq 0x8(%rbx), %rax
movabsq $0x1000000000000, %rcx  # imm = 0x1000000000000
cmpq %rcx, %rax
movl 0x1c(%rsp), %esi
jb 0x19f25
shrq $0x30, %rax
movl 0x4(%rbx), %edx
movq 0x8(%rsp), %rcx
negl %ecx
movq %rcx, 0x8(%rsp)
xorl %ecx, %ecx
leaq 0xc0cc(%rip), %r11      # 0x25f3c
xorl %r9d, %r9d
movl (%r12,%rcx,4), %r10d
movl %r10d, %edi
andl $0xf, %edi
cmpl $0x4, %edi
ja 0x19f13
movl %r10d, %r8d
shrl $0x4, %r8d
movslq (%r11,%rdi,4), %rdi
addq %r11, %rdi
jmpq *%rdi
addl %r9d, %r8d
movl %r8d, %edi
jmp 0x19f16
leal (%r8,%r9), %edi
cmpl $0x10, %r10d
jb 0x19ef5
movslq %r9d, %r9
movslq %edi, %r10
movq 0x8(%rsp), %rsi
leal (%rsi,%rdx), %r11d
movl (%r13,%r9,4), %ebp
testb $0x3, %bpl
jne 0x19ee5
sarl $0x2, %ebp
cmpl %ebp, %r11d
jne 0x19ee5
movzbl (%r15,%r9), %ebp
movq 0x28(%rsp), %rsi
movzbl (%rsi,%r9), %esi
cmpb %sil, %bpl
cmovbl %ebp, %esi
movb %sil, (%r15,%r9)
jmp 0x19eea
movb $0x0, (%r15,%r9)
incq %r9
incl %r11d
cmpq %r10, %r9
jl 0x19eb7
addl %edx, %r8d
movl %r8d, %edx
movl 0x1c(%rsp), %esi
leaq 0xc036(%rip), %r11      # 0x25f3c
jmp 0x19f16
addl %edx, %r8d
movl %r9d, %edi
movl %r8d, %edx
jmp 0x19f16
movl %r9d, %edi
incq %rcx
movl %edi, %r9d
cmpq %rax, %rcx
jne 0x19e73
movslq 0x10(%rbx), %rcx
testq %rcx, %rcx
jle 0x19f78
xorl %eax, %eax
movb (%r14,%rax), %dl
subb (%r15,%rax), %dl
addb $0x40, %dl
movb %dl, (%r15,%rax)
incq %rax
cmpq %rax, %rcx
jne 0x19f30
testl %esi, %esi
je 0x19f85
testl %ecx, %ecx
jle 0x19f7c
xorl %eax, %eax
leaq 0xc075(%rip), %rdx      # 0x25fcd
movb (%r14,%rax), %cl
subb (%r15,%rax), %cl
addb $0x40, %cl
movb %cl, (%r14,%rax)
incq %rax
movslq 0x10(%rbx), %rcx
cmpq %rcx, %rax
jl 0x19f58
movq %rdx, %rsi
jmp 0x19f8c
testl %esi, %esi
je 0x19f85
leaq 0xc04a(%rip), %rsi      # 0x25fcd
jmp 0x19f8c
leaq 0xc03e(%rip), %rsi      # 0x25fca
incl %ecx
movq %rbx, %rdi
movl $0x5a, %edx
movq %r15, %r8
callq 0x116a0
movq %r15, %rdi
callq 0x32c0
movq 0x20(%rsp), %rdi
callq 0x32c0
movq 0x10(%rsp), %rdi
callq 0x32c0
movq 0x28(%rsp), %rdi
callq 0x32c0
movq %r13, %rdi
callq 0x32c0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x19fce