// File: /workspace/github_cpp_makefile_star2_O1/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, 0x48(%rsp)
movl %edx, 0x4c(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %r13
movq 0x8(%rdi), %r15
movq %r15, %rax
shrq $0x30, %rax
shrl $0x18, %r15d
addq 0x30(%rdi), %r15
leaq (%r15,%rax,4), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3530
movq %rax, %r14
xorl %edx, %edx
cmpw $0x0, 0xe(%r13)
movq %r13, 0x20(%rsp)
movq %r15, 0x18(%rsp)
je 0x18d8b
movl 0x4(%r13), %edi
leaq 0xe439(%rip), %rsi      # 0x26dfc
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %edx, %edx
movl (%r15,%r8,4), %ebx
movl %ebx, %ecx
andl $0xf, %ecx
cmpl $0x4, %ecx
ja 0x18d30
movl %ebx, %r10d
shrl $0x4, %r10d
movslq (%rsi,%rcx,4), %rax
addq %rsi, %rax
movq %r10, 0x40(%rsp)
jmpq *%rax
addl %r10d, %r9d
movb $0x1, %al
cmpl $0x1, %ecx
jne 0x18d76
addl %r10d, 0x10(%rsp)
jmp 0x18d76
addl %edi, %r10d
movb $0x1, %al
movl %r10d, %edi
jmp 0x18d76
movq %r8, 0x28(%rsp)
cmpl $0x10, %ebx
setae %r15b
movl %edi, 0xc(%rsp)
movl %r9d, 0x14(%rsp)
jb 0x18bb2
movl %r9d, %ebp
movslq %edi, %rbx
movl %r10d, %r11d
addq 0x38(%rsp), %rbx
negq %r11
movl $0x1, %r12d
movq %r11, 0x50(%rsp)
movsbq (%rbx), %r13
testq %r13, %r13
je 0x18b8c
movl %ebp, %eax
shrl $0x1f, %eax
addl %ebp, %eax
sarl %eax
cltq
movq 0x30(%rsp), %rcx
movzbl (%rcx,%rax), %esi
leaq 0x1716a(%rip), %rcx     # 0x2fbe0
movzbl (%rcx,%r13), %r8d
cmpl $0xf, %r8d
setne %dil
movl %ebp, %ecx
shrl $0x1f, %ecx
addl %ebp, %ecx
andl $0x3e, %ecx
movl %ebp, %r9d
subl %ecx, %r9d
shll $0x2, %r9d
movb $0x4, %cl
subb %r9b, %cl
movl %esi, %r9d
shrl %cl, %r9d
andl $0xf, %r9d
cmpl %r8d, %r9d
sete %cl
cmpl $0xf, %r9d
setne %r8b
testl %r9d, %r9d
je 0x18b67
andb %dil, %r8b
andb %cl, %r8b
jne 0x18b67
movq %r14, %rdi
leaq 0xe6bd(%rip), %rsi      # 0x27190
xorl %eax, %eax
callq 0x10a44
movb (%rbx), %sil
movq (%r14), %rax
incq %rax
cmpq 0x8(%r14), %rax
jb 0x18b38
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movb %sil, 0xb(%rsp)
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
callq 0x3610
movb 0xb(%rsp), %sil
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movb %sil, (%rcx,%rax)
movq (%r14), %rax
movq 0x10(%r14), %rcx
movb $0x0, (%rcx,%rax)
incl 0x10(%rsp)
xorl %edx, %edx
movq 0x40(%rsp), %r10
movq 0x50(%rsp), %r11
jmp 0x18b8c
cmpl $0x0, 0x4c(%rsp)
je 0x18b8a
testb $0x1, %bpl
movl $0xf0, %ecx
movl $0xf, %edi
cmovel %edi, %ecx
andl %ecx, %esi
movq 0x30(%rsp), %rcx
movb %sil, (%rcx,%rax)
incl %edx
testb %r13b, %r13b
je 0x18bb2
cmpq %r10, %r12
setb %r15b
incq %rbx
incq %rbp
leaq (%r11,%r12), %rax
incq %rax
incq %r12
cmpq $0x1, %rax
jne 0x18a4e
testb $0x1, %r15b
movq 0x28(%rsp), %r8
je 0x18d34
xorl %eax, %eax
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %r15
leaq 0xe228(%rip), %rsi      # 0x26dfc
movl 0xc(%rsp), %edi
jmp 0x18d71
movl %r9d, 0x14(%rsp)
movq %r8, 0x28(%rsp)
movl %edi, 0xc(%rsp)
movq %r14, %rdi
leaq 0xe59b(%rip), %rsi      # 0x27190
xorl %eax, %eax
callq 0x10a44
movq (%r14), %rax
incq %rax
cmpq 0x8(%r14), %rax
jb 0x18c4d
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
callq 0x3610
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movb $0x5e, (%rcx,%rax)
movq (%r14), %rax
movq 0x10(%r14), %rcx
movb $0x0, (%rcx,%rax)
cmpl $0x10, %ebx
movq 0x40(%rsp), %rsi
jb 0x18d14
movslq 0xc(%rsp), %rbx
movl %esi, %r15d
addq 0x38(%rsp), %rbx
xorl %r12d, %r12d
movb (%rbx,%r12), %bpl
testb %bpl, %bpl
je 0x18d58
movq (%r14), %rax
incq %rax
cmpq 0x8(%r14), %rax
jb 0x18ceb
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
callq 0x3610
movq 0x40(%rsp), %rsi
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movb %bpl, (%rcx,%rax)
movq (%r14), %rax
movq 0x10(%r14), %rcx
movb $0x0, (%rcx,%rax)
incq %r12
cmpq %r12, %r15
jne 0x18c88
addl %esi, 0x10(%rsp)
movl 0xc(%rsp), %edi
addl %esi, %edi
xorl %edx, %edx
movb $0x1, %al
movq 0x18(%rsp), %r15
leaq 0xe0ce(%rip), %rsi      # 0x26dfc
jmp 0x18d6c
movb $0x1, %al
jmp 0x18d76
movl 0xc(%rsp), %edi
addl %r10d, %edi
movl 0x14(%rsp), %r9d
addl %r10d, %r9d
movb $0x1, %al
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %r15
leaq 0xe0a6(%rip), %rsi      # 0x26dfc
jmp 0x18d76
xorl %eax, %eax
xorl %edx, %edx
movq 0x18(%rsp), %r15
leaq 0xe094(%rip), %rsi      # 0x26dfc
movl 0xc(%rsp), %edi
movq 0x28(%rsp), %r8
movl 0x14(%rsp), %r9d
testb %al, %al
je 0x18d8b
incq %r8
movzwl 0xe(%r13), %eax
cmpq %rax, %r8
jb 0x189cb
leaq 0xe3fe(%rip), %rsi      # 0x27190
movq %r14, %rdi
xorl %eax, %eax
callq 0x10a44
movl 0x48(%rsp), %eax
testl %eax, %eax
jle 0x18f3a
cmpl %eax, 0x10(%rsp)
jl 0x18f3a
movabsq $0x1000000000000, %rax  # imm = 0x1000000000000
cmpq %rax, 0x8(%r13)
jb 0x18f3a
movl 0x4(%r13), %esi
xorl %eax, %eax
xorl %r8d, %r8d
movl %r8d, %r8d
movl (%r15,%rax,4), %r10d
movl %r10d, %r9d
shrl $0x4, %r9d
movl %r10d, %ecx
andl $0xf, %ecx
je 0x18e00
andl $0xe, %r10d
cmpl $0x2, %r10d
jne 0x18f01
addl %esi, %r9d
movb $0x1, %cl
movl %r9d, %esi
jmp 0x18f25
cmpl $0x10, %r10d
jb 0x18ef7
movslq %r8d, %r10
movslq %esi, %r11
addq 0x38(%rsp), %r11
xorl %ebx, %ebx
movsbq (%r11,%rbx), %r15
testq %r15, %r15
je 0x18f15
leal (%r8,%rbx), %ecx
movl %ecx, %edi
shrl $0x1f, %edi
addl %ecx, %edi
sarl %edi
movslq %edi, %r12
movq 0x30(%rsp), %rcx
movzbl (%rcx,%r12), %ebp
leaq 0x16d9a(%rip), %rcx     # 0x2fbe0
movzbl (%rcx,%r15), %edi
cmpl $0xf, %edi
setne %r13b
leal (%r8,%rbx), %edx
movl %edx, %ecx
shrl $0x1f, %ecx
addl %edx, %ecx
andl $0x3e, %ecx
subl %ecx, %edx
shll $0x2, %edx
movb $0x4, %cl
subb %dl, %cl
movl %ebp, %edx
shrl %cl, %edx
andl $0xf, %edx
cmpl %edi, %edx
sete %cl
cmpl $0xf, %edx
setne %dil
testl %edx, %edx
je 0x18e88
andb %r13b, %dil
andb %cl, %dil
je 0x18ee6
leaq (%r10,%rbx), %rcx
testb $0x1, %cl
movl $0xf, %ecx
movl $0xf0, %edx
cmovel %edx, %ecx
orl %ecx, %ebp
movq 0x30(%rsp), %rcx
movb %bpl, (%rcx,%r12)
movq 0x20(%rsp), %rdi
movq 0x8(%rdi), %rcx
movq %rcx, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%rdi), %rdx
shrl $0x18, %ecx
movl 0x10(%rdi), %edi
movl %edi, %r12d
incl %r12d
shrl $0x1f, %r12d
addl %r12d, %edi
incl %edi
sarl %edi
movslq %edi, %rdi
addq %rcx, %rdi
addq %r10, %rdi
addq %rdx, %rdi
movb $0x0, (%rbx,%rdi)
testb %r15b, %r15b
je 0x18f15
incq %rbx
cmpq %rbx, %r9
jne 0x18e17
addl %r9d, %esi
addl %r9d, %r8d
movb $0x1, %cl
jmp 0x18f17
cmpl $0x1, %ecx
je 0x18f0b
cmpl $0x4, %ecx
jne 0x18f23
addl %r8d, %r9d
movb $0x1, %cl
movl %r9d, %r8d
jmp 0x18f25
xorl %ecx, %ecx
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %r15
jmp 0x18f25
movb $0x1, %cl
testb %cl, %cl
je 0x18f3a
incq %rax
movzwl 0xe(%r13), %ecx
cmpq %rcx, %rax
jb 0x18dcf
leaq 0xdf17(%rip), %rsi      # 0x26e58
movq %r13, %rdi
callq 0x1110a
testb $0x4, 0xc(%r13)
jne 0x19094
movq %rax, %r15
testq %rax, %rax
je 0x18f66
movq %r15, %rdi
callq 0x1192e
jmp 0x18f6b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %r15, %r15
je 0x18fa3
movl 0x10(%rsp), %r8d
cmpl %eax, %r8d
je 0x18fc1
movq 0x16067(%rip), %rcx     # 0x2efe8
movq (%rcx), %rdi
movq 0x30(%r13), %rdx
leaq 0xdecc(%rip), %rsi      # 0x26e5b
movl %eax, %ecx
xorl %eax, %eax
callq 0x3630
movq %r13, %rdi
movq %r15, %rsi
callq 0x11205
leaq 0xdeae(%rip), %rsi      # 0x26e58
leaq 0x10(%rsp), %r8
movq %r13, %rdi
movl $0x69, %edx
movl $0x4, %ecx
callq 0x11050
leaq 0xdec7(%rip), %rsi      # 0x26e8f
movq %r13, %rdi
callq 0x1110a
testq %rax, %rax
je 0x19066
movq %rax, %r15
movq %rax, %r12
incq %r12
movq %r12, %rdi
callq 0x32d0
movq (%r14), %rbx
cmpq %rbx, %rax
jne 0x19033
movq %rbx, %r13
negq %r13
movq $-0x1, %rbp
movq %r13, %rax
addq %rbp, %rax
cmpq $-0x1, %rax
je 0x19083
callq 0x3280
movq (%rax), %rax
movzbl 0x2(%r15,%rbp), %ecx
movl (%rax,%rcx,4), %ecx
movq 0x10(%r14), %rdx
movsbq 0x1(%rdx,%rbp), %rdx
incq %rbp
cmpl (%rax,%rdx,4), %ecx
je 0x18fff
cmpq %rbp, %rbx
jbe 0x19083
movq 0x15fae(%rip), %rax     # 0x2efe8
movq (%rax), %rdi
movq 0x20(%rsp), %r13
movq 0x30(%r13), %rdx
movq 0x10(%r14), %r8
leaq 0xde41(%rip), %rsi      # 0x26e92
movq %r12, %rcx
xorl %eax, %eax
callq 0x3630
movq %r13, %rdi
movq %r15, %rsi
callq 0x11205
movl (%r14), %ecx
incl %ecx
movq 0x10(%r14), %r8
leaq 0xde19(%rip), %rsi      # 0x26e8f
movq %r13, %rdi
movl $0x5a, %edx
callq 0x11050
movq 0x10(%r14), %rdi
callq 0x32c0
movq %r14, %rdi
callq 0x32c0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq