// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_md.c
// Function: bam_fillmd1_core
void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm)
{
	uint8_t *seq = bam1_seq(b);
	uint32_t *cigar = bam1_cigar(b);
	bam1_core_t *c = &b->core;
	int i, x, y, u = 0;
	kstring_t *str;
	uint8_t *old_md, *old_nm;
	int32_t old_nm_i = -1, nm = 0;

	str = (kstring_t*)calloc(1, sizeof(kstring_t));
	for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
		int j, l = cigar[i]>>4, op = cigar[i]&0xf;
		if (op == BAM_CMATCH) {
			for (j = 0; j < l; ++j) {
				int z = y + j;
				int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
				if (ref[x+j] == 0) break; // out of boundary
				if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
					if (is_equal) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
					++u;
				} else {
					ksprintf(str, "%d", u);
					kputc(ref[x+j], str);
					u = 0; ++nm;
				}
			}
			if (j < l) break;
			x += l; y += l;
		} else if (op == BAM_CDEL) {
			ksprintf(str, "%d", u);
			kputc('^', str);
			for (j = 0; j < l; ++j) {
				if (ref[x+j] == 0) break;
				kputc(ref[x+j], str);
			}
			u = 0;
			if (j < l) break;
			x += l; nm += l;
		} else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
			y += l;
			if (op == BAM_CINS) nm += l;
		} else if (op == BAM_CREF_SKIP) {
			x += l;
		}
	}
	ksprintf(str, "%d", u);
	// apply max_nm
	if (max_nm > 0 && nm >= max_nm) {
		for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
			int j, l = cigar[i]>>4, op = cigar[i]&0xf;
			if (op == BAM_CMATCH) {
				for (j = 0; j < l; ++j) {
					int z = y + j;
					int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
					if (ref[x+j] == 0) break; // out of boundary
					if ((c1 == c2 && c1 != 15 && c2 != 15) || c1 == 0) { // a match
						seq[z/2] |= (z&1)? 0x0f : 0xf0;
						bam1_qual(b)[z] = 0;
					}
				}
				if (j < l) break;
				x += l; y += l;
			} else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
			else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
		}
	}
	// update NM
	old_nm = bam_aux_get(b, "NM");
	if (c->flag & BAM_FUNMAP) return;
	if (old_nm) old_nm_i = bam_aux2i(old_nm);
	if (!old_nm) bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
	else if (nm != old_nm_i) {
		fprintf(stderr, "[bam_fillmd1] different NM for read '%s': %d -> %d\n", bam1_qname(b), old_nm_i, nm);
		bam_aux_del(b, old_nm);
		bam_aux_append(b, "NM", 'i', 4, (uint8_t*)&nm);
	}
	// update MD
	old_md = bam_aux_get(b, "MD");
	if (!old_md) bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
	else {
		int is_diff = 0;
		if (strlen((char*)old_md+1) == str->l) {
			for (i = 0; i < str->l; ++i)
				if (toupper(old_md[i+1]) != toupper(str->s[i]))
					break;
			if (i < str->l) is_diff = 1;
		} else is_diff = 1;
		if (is_diff) {
			fprintf(stderr, "[bam_fillmd1] different MD for read '%s': '%s' -> '%s'\n", bam1_qname(b), old_md+1, str->s);
			bam_aux_del(b, old_md);
			bam_aux_append(b, "MD", 'Z', str->l + 1, (uint8_t*)str->s);
		}
	}
	free(str->s); free(str);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, 0x44(%rsp)
movl %edx, 0xc(%rsp)
movq %rdi, %r15
movq 0x8(%rdi), %r14
movl %r14d, %eax
shrl $0x18, %eax
addq 0x30(%rdi), %rax
movq %rsi, 0x38(%rsp)
shrq $0x30, %r14
movq %rax, 0x48(%rsp)
leaq (%rax,%r14,4), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3530
movq %rax, %rbx
testq %r14, %r14
movq %r15, 0x18(%rsp)
jne 0x19233
movl $0x0, 0x8(%rsp)
xorl %edx, %edx
leaq 0xd179(%rip), %rsi      # 0x26290
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10fc0
movl 0x44(%rsp), %eax
testl %eax, %eax
jle 0x1914b
cmpl %eax, 0x8(%rsp)
jl 0x1914b
movabsq $0x1000000000000, %rax  # imm = 0x1000000000000
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
cmpq %rax, %rcx
jae 0x195a4
leaq 0xce06(%rip), %rsi      # 0x25f58
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x1175c
testb $0x4, 0xc(%r14)
jne 0x1978a
movq %rax, %r15
testq %rax, %rax
je 0x191ad
movq %r15, %rdi
callq 0x121e2
movl 0x8(%rsp), %r8d
cmpl %eax, %r8d
je 0x191cb
movq 0x14e5d(%rip), %rcx     # 0x2dfe8
movq (%rcx), %rdi
movq 0x30(%r14), %rdx
leaq 0xcdc2(%rip), %rsi      # 0x25f5b
movl %eax, %ecx
xorl %eax, %eax
callq 0x3630
movq %r14, %rdi
movq %r15, %rsi
callq 0x11844
leaq 0xcda4(%rip), %rsi      # 0x25f58
leaq 0x14(%rsp), %r8
movq %r14, %rdi
movl $0x69, %edx
movl $0x4, %ecx
callq 0x116a0
leaq 0xcdbd(%rip), %rsi      # 0x25f8f
movq %r14, %rdi
callq 0x1175c
testq %rax, %rax
je 0x1975d
movq %rax, %r15
movq %rax, %r12
incq %r12
movq %r12, %rdi
callq 0x32d0
cmpq (%rbx), %rax
jne 0x1972a
movq %rax, %r13
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x19779
callq 0x3280
movq (%rax), %rax
movzbl 0x1(%r15,%r14), %ecx
movq 0x10(%rbx), %r8
movsbq (%r8,%r14), %rdx
incq %r14
movl (%rax,%rcx,4), %ecx
cmpl (%rax,%rdx,4), %ecx
je 0x19203
jmp 0x1972e
movl 0x4(%r15), %r12d
leaq 0xccbe(%rip), %rsi      # 0x25efc
movl $0x0, 0x8(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x24(%rsp)
xorl %edx, %edx
movq %r15, %r14
movq 0x48(%rsp), %rax
movl (%rax,%r13,4), %ebp
movl %ebp, %eax
andl $0xf, %eax
cmpl $0x4, %eax
ja 0x1958e
movl %ebp, %r15d
shrl $0x4, %r15d
movslq (%rsi,%rax,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
addl %r15d, 0x24(%rsp)
cmpl $0x1, %eax
jne 0x1958e
movl 0x8(%rsp), %eax
addl %r15d, %eax
movl %eax, 0x8(%rsp)
movl %eax, 0x14(%rsp)
jmp 0x1958e
addl %r12d, %r15d
movl %r15d, %r12d
jmp 0x1958e
cmpl $0x10, %ebp
movl %r12d, 0x10(%rsp)
jb 0x19412
movslq %r12d, %rbp
addq 0x38(%rsp), %rbp
movl 0x24(%rsp), %eax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
movsbq (%rbp,%r12), %rcx
testq %rcx, %rcx
je 0x19110
movq 0x28(%rsp), %rax
addq %r12, %rax
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
movl %esi, %edi
andl $-0x2, %edi
movl %eax, %r9d
subl %edi, %r9d
sarl %esi
movslq %esi, %rsi
movq 0x30(%rsp), %rdi
movzbl (%rdi,%rsi), %edi
leaq 0x158d6(%rip), %r8      # 0x2ebe0
movzbl (%r8,%rcx), %r10d
cmpl $0xf, %r10d
setne %r8b
shll $0x2, %r9d
movb $0x4, %cl
subb %r9b, %cl
movl %edi, %r11d
shrl %cl, %r11d
andl $0xf, %r11d
cmpl %r10d, %r11d
sete %cl
cmpl $0xf, %r11d
setne %r9b
testl %r11d, %r11d
je 0x1936e
andb %r8b, %r9b
andb %cl, %r9b
jne 0x1936e
movq %rbx, %rdi
leaq 0xcf41(%rip), %rsi      # 0x26290
xorl %eax, %eax
callq 0x10fc0
movb (%rbp,%r12), %r14b
movq (%rbx), %rdx
leaq 0x1(%rdx), %rcx
cmpq 0x8(%rbx), %rcx
jae 0x19393
movq 0x10(%rbx), %rax
jmp 0x193df
cmpl $0x0, 0xc(%rsp)
je 0x1938f
testb $0x1, %al
movl $0xf0, %eax
movl $0xf, %ecx
cmovel %ecx, %eax
andl %eax, %edi
movq 0x30(%rsp), %rax
movb %dil, (%rax,%rsi)
incl %edx
jmp 0x19406
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x3610
movq %rax, 0x10(%rbx)
movq (%rbx), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, (%rbx)
movb %r14b, (%rax,%rdx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movl 0x8(%rsp), %eax
incl %eax
movl %eax, 0x8(%rsp)
movl %eax, 0x14(%rsp)
xorl %edx, %edx
movq 0x18(%rsp), %r14
incq %r12
cmpq %r12, %r15
jne 0x192cc
movl 0x10(%rsp), %r12d
addl %r15d, %r12d
addl %r15d, 0x24(%rsp)
jmp 0x19587
movq %rbx, %rdi
leaq 0xce62(%rip), %rsi      # 0x26290
xorl %eax, %eax
callq 0x10fc0
movq (%rbx), %rdx
leaq 0x1(%rdx), %rcx
cmpq 0x8(%rbx), %rcx
movl %r12d, 0x10(%rsp)
jae 0x1944d
movq 0x10(%rbx), %rax
jmp 0x19499
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x3610
movq %rax, 0x10(%rbx)
movq (%rbx), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, (%rbx)
movb $0x5e, (%rax,%rdx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movb $0x0, (%rcx,%rax)
cmpl $0x10, %ebp
jb 0x19569
movslq 0x10(%rsp), %rdi
movl %r15d, %esi
addq 0x38(%rsp), %rdi
xorl %ebp, %ebp
movq %r15, 0x50(%rsp)
movb (%rdi,%rbp), %r12b
testb %r12b, %r12b
je 0x1910e
movq (%rbx), %rdx
leaq 0x1(%rdx), %rcx
cmpq 0x8(%rbx), %rcx
jae 0x194e8
movq 0x10(%rbx), %rax
jmp 0x1954b
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rsi, %r15
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq %r13, %r14
movq %rdi, %r13
movq 0x10(%rbx), %rdi
callq 0x3610
movq %r13, %rdi
movq %r14, %r13
movq %r15, %rsi
movq 0x50(%rsp), %r15
movq %rax, 0x10(%rbx)
movq (%rbx), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, (%rbx)
movb %r12b, (%rax,%rdx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movb $0x0, (%rcx,%rax)
incq %rbp
cmpq %rbp, %rsi
jne 0x194c8
movl 0x10(%rsp), %r12d
addl %r15d, %r12d
movl 0x8(%rsp), %eax
addl %r15d, %eax
movl %eax, 0x8(%rsp)
movl %eax, 0x14(%rsp)
xorl %edx, %edx
movq 0x18(%rsp), %r14
leaq 0xc96e(%rip), %rsi      # 0x25efc
incq %r13
movzwl 0xe(%r14), %eax
cmpq %rax, %r13
jb 0x19256
jmp 0x19110
movq 0x18(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x28(%rsp)
xorl %eax, %eax
leaq 0x15627(%rip), %rdx     # 0x2ebe0
movl $0x0, 0xc(%rsp)
movq 0x48(%rsp), %rsi
movl (%rsi,%rax,4), %r10d
movl %r10d, %r9d
shrl $0x4, %r9d
movl %r10d, %edi
andl $0xf, %edi
je 0x195f6
andl $0xe, %r10d
cmpl $0x2, %r10d
jne 0x196fe
addl 0x28(%rsp), %r9d
movl %r9d, 0x28(%rsp)
jmp 0x19712
cmpl $0x10, %r10d
jb 0x196f2
movslq 0xc(%rsp), %r10
movslq 0x28(%rsp), %r11
addq 0x38(%rsp), %r11
movl %r10d, %r14d
xorl %r15d, %r15d
movsbq (%r11,%r15), %rcx
testq %rcx, %rcx
je 0x1914b
leaq (%r14,%r15), %r12
movl %r12d, %edi
shrl $0x1f, %edi
addl %r12d, %edi
movl %edi, %r8d
andl $-0x2, %r8d
movl %r12d, %esi
subl %r8d, %esi
sarl %edi
movslq %edi, %r13
movq 0x30(%rsp), %rdi
movzbl (%rdi,%r13), %ebp
movzbl (%rdx,%rcx), %r8d
cmpl $0xf, %r8d
setne %dil
shll $0x2, %esi
movb $0x4, %cl
subb %sil, %cl
movl %ebp, %esi
shrl %cl, %esi
andl $0xf, %esi
cmpl %r8d, %esi
sete %cl
cmpl $0xf, %esi
setne %r8b
testl %esi, %esi
je 0x19681
andb %dil, %r8b
andb %cl, %r8b
je 0x196dd
testb $0x1, %r12b
movl $0xf, %ecx
movl $0xf0, %esi
cmovel %esi, %ecx
orl %ecx, %ebp
movq 0x30(%rsp), %rcx
movb %bpl, (%rcx,%r13)
movq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rcx
movq %rcx, %rsi
shrq $0x30, %rsi
shlq $0x2, %rsi
addq 0x30(%rdi), %rsi
shrl $0x18, %ecx
movl 0x10(%rdi), %edi
movl %edi, %r8d
incl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
incl %edi
sarl %edi
movslq %edi, %rdi
addq %rcx, %rdi
addq %r10, %rdi
addq %rsi, %rdi
movb $0x0, (%r15,%rdi)
incq %r15
cmpq %r15, %r9
jne 0x19615
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
addl %r9d, 0x28(%rsp)
addl %r9d, 0xc(%rsp)
jmp 0x19712
cmpl $0x4, %edi
je 0x19708
cmpl $0x1, %edi
jne 0x19712
addl 0xc(%rsp), %r9d
movl %r9d, 0xc(%rsp)
incq %rax
movq %rcx, %rsi
shrq $0x30, %rsi
cmpq %rsi, %rax
jb 0x195c1
jmp 0x1914b
movq 0x10(%rbx), %r8
movq 0x148b3(%rip), %rax     # 0x2dfe8
movq (%rax), %rdi
movq 0x18(%rsp), %r14
movq 0x30(%r14), %rdx
leaq 0xc84a(%rip), %rsi      # 0x25f92
movq %r12, %rcx
xorl %eax, %eax
callq 0x3630
movq %r14, %rdi
movq %r15, %rsi
callq 0x11844
movl (%rbx), %ecx
incl %ecx
movq 0x10(%rbx), %r8
leaq 0xc823(%rip), %rsi      # 0x25f8f
movq %r14, %rdi
movl $0x5a, %edx
callq 0x116a0
movq 0x10(%rbx), %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0x32c0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq