// File: /workspace/github_cpp_makefile_star2_O1/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, 0x1c(%rsp)
movq %rsi, %r12
movq 0x8(%rdi), %r14
movl %r14d, %esi
shrl $0x18, %esi
leaq 0x16cc6(%rip), %rcx     # 0x300d0
movl 0x8(%rcx), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq 0x30(%rdi), %rsi
movl %edx, 0x40(%rsp)
movq (%rcx), %rcx
movq %rcx, 0x38(%rsp)
btq $0x22, %r14
jb 0x1995b
movq %rdi, %rbx
movq %rsi, 0x30(%rsp)
shrq $0x30, %r14
movl 0x10(%rdi), %eax
movl %eax, %ecx
incl %ecx
shrl $0x1f, %ecx
leal (%rax,%rcx), %ebp
incl %ebp
sarl %ebp
leaq 0xda79(%rip), %rsi      # 0x26eca
callq 0x1110a
movq %rax, %r15
leaq 0x1(%rax), %r13
testq %rax, %rax
cmoveq %rax, %r13
leaq 0xda62(%rip), %rsi      # 0x26ecd
movq %rbx, %rdi
callq 0x1110a
xorl %esi, %esi
testq %rax, %rax
je 0x19484
cmpb $0x5a, (%rax)
sete %sil
addq %rax, %rsi
movq 0x30(%rsp), %rax
leaq (%rax,%r14,4), %r14
movslq %ebp, %rbp
testq %r15, %r15
je 0x194a7
testq %rsi, %rsi
je 0x194a7
decq %rsi
movq %rbx, %rdi
callq 0x11205
xorl %esi, %esi
addq %rbp, %r14
movq %r15, %rax
orq %rsi, %rax
je 0x1953e
testq %rsi, %rsi
setne %cl
testq %r15, %r15
setne %dil
cmpl $0x0, 0x1c(%rsp)
setne %dl
sete %r8b
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
testb %dil, %r8b
jne 0x1995b
andb %cl, %dl
jne 0x1995b
movl 0x1c(%rsp), %edx
testl %edx, %edx
sete %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0x1992a
cmpl $0x0, 0x10(%rbx)
jle 0x19534
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 0x19506
movb $0x5a, -0x3(%r13)
jmp 0x1991f
movq %r12, 0x10(%rsp)
movq 0x8(%rbx), %rbp
movq %rbp, %r13
shrq $0x30, %r13
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 0x195e3
movl 0x4(%rbx), %edi
xorl %r8d, %r8d
leaq 0xd8b2(%rip), %r9       # 0x26e28
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 0x195cf
shrl $0x4, %r11d
movslq (%r9,%r15,4), %r15
addq %r9, %r15
jmpq *%r15
addl %r11d, %r10d
jmp 0x195cf
xorl %r11d, %r11d
jmp 0x195d2
testl %ecx, %ecx
cmovsl %r10d, %ecx
testl %edx, %edx
cmovsl %edi, %edx
addl %r11d, %r10d
addl %r11d, %edi
movb $0x1, %r11b
movl %edi, %eax
movl %r10d, %esi
jmp 0x195d2
addl %r11d, %edi
movb $0x1, %r11b
testb %r11b, %r11b
je 0x19923
incq %r8
cmpq %r8, %r13
jne 0x19588
movq %rbp, 0x28(%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 0x19662
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, 0x20(%rsp)
movl %edx, %eax
movq %rax, 0x8(%rsp)
jmp 0x1966b
movl %esi, 0x20(%rsp)
movq %r9, 0x8(%rsp)
movq 0x10(%rsp), %r12
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 0x196e8
movq 0x28(%rsp), %rcx
shrl $0x18, %ecx
addq %rcx, %r13
xorl %r8d, %r8d
leaq 0x15ca3(%rip), %rdx     # 0x2f360
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 0x196bf
movq %rax, 0x28(%rsp)
movl 0x20(%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, 0x10(%rsp)
jle 0x1974a
movslq 0x8(%rsp), %rcx
movslq %r13d, %r8
addq %rcx, %r12
subq %rcx, %r8
xorl %ecx, %ecx
leaq 0x164b6(%rip), %rdx     # 0x2fbe0
leaq 0x15c2f(%rip), %rsi     # 0x2f360
movsbq (%r12,%rcx), %rdi
movzbl (%rdx,%rdi), %edi
movb (%rsi,%rdi), %dil
movb %dil, (%rax,%rcx)
incq %rcx
cmpq %rcx, %r8
jne 0x19731
movl $0x4, %esi
movq %rbp, %rdi
movq %rax, %r12
callq 0x3530
movq %rax, %r13
movl $0x1, %esi
movq %rbp, %rdi
callq 0x3530
leaq 0x38(%rsp), %r9
movq %r12, 0x20(%rsp)
movq %r12, %rdi
movl 0x10(%rsp), %esi
movq 0x28(%rsp), %rdx
movl %ebp, %ecx
movq %r14, %r8
movq %rax, 0x10(%rsp)
pushq %rax
pushq %r13
callq 0x2104c
addq $0x10, %rsp
movq 0x8(%rbx), %rax
movabsq $0x1000000000000, %rcx  # imm = 0x1000000000000
cmpq %rcx, %rax
movq 0x30(%rsp), %r11
jb 0x19882
shrq $0x30, %rax
movl 0x4(%rbx), %edx
movq 0x8(%rsp), %rcx
negl %ecx
movq %rcx, 0x8(%rsp)
xorl %ecx, %ecx
leaq 0xd66e(%rip), %rsi      # 0x26e3c
xorl %r9d, %r9d
movl (%r11,%rcx,4), %r10d
movl %r10d, %edi
andl $0xf, %edi
cmpl $0x4, %edi
ja 0x19870
movl %r10d, %r8d
shrl $0x4, %r8d
movslq (%rsi,%rdi,4), %rdi
addq %rsi, %rdi
jmpq *%rdi
addl %r9d, %r8d
movl %r8d, %edi
jmp 0x19873
leal (%r8,%r9), %edi
cmpl $0x10, %r10d
jb 0x19858
movslq %r9d, %r9
movslq %edi, %r10
movq 0x8(%rsp), %r11
addl %edx, %r11d
movl (%r13,%r9,4), %ebp
testb $0x3, %bpl
jne 0x19848
sarl $0x2, %ebp
cmpl %ebp, %r11d
jne 0x19848
movzbl (%r15,%r9), %ebp
movq %rbx, %r12
movq 0x10(%rsp), %rbx
movzbl (%rbx,%r9), %ebx
cmpb %bl, %bpl
cmovbl %ebp, %ebx
movb %bl, (%r15,%r9)
movq %r12, %rbx
jmp 0x1984d
movb $0x0, (%r15,%r9)
incq %r9
incl %r11d
cmpq %r10, %r9
jl 0x19814
addl %edx, %r8d
movl %r8d, %edx
movq 0x30(%rsp), %r11
jmp 0x19873
addl %edx, %r8d
movl %r9d, %edi
movl %r8d, %edx
jmp 0x19873
movl %r9d, %edi
incq %rcx
movl %edi, %r9d
cmpq %rax, %rcx
jne 0x197d1
movslq 0x10(%rbx), %rcx
testq %rcx, %rcx
jle 0x198a4
xorl %eax, %eax
movb (%r14,%rax), %dl
subb (%r15,%rax), %dl
addb $0x40, %dl
movb %dl, (%r15,%rax)
incq %rax
cmpq %rax, %rcx
jne 0x1988d
cmpl $0x0, 0x1c(%rsp)
je 0x198d8
movl 0x10(%rbx), %ecx
testl %ecx, %ecx
jle 0x198cf
xorl %eax, %eax
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 0x198b4
leaq 0xd5f7(%rip), %rsi      # 0x26ecd
jmp 0x198df
leaq 0xd5eb(%rip), %rsi      # 0x26eca
incl %ecx
movq %rbx, %rdi
movl $0x5a, %edx
movq %r15, %r8
callq 0x11050
movq %r15, %rdi
callq 0x32c0
movq 0x28(%rsp), %rdi
callq 0x32c0
movq 0x20(%rsp), %rdi
callq 0x32c0
movq 0x10(%rsp), %rdi
callq 0x32c0
movq %r13, %rdi
callq 0x32c0
xorl %eax, %eax
jmp 0x1995b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1995b
xorl %eax, %eax
testl %edx, %edx
jne 0x1995b
testq %rsi, %rsi
je 0x1995b
cmpl $0x0, 0x10(%rbx)
jle 0x19957
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 0x1993d
movb $0x42, -0x3(%rsi)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq