// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/bam_md.c
// Function: bam_fillmd1_core
void bam_fillmd1_core(bam1_t *b, char *ref, int flag, 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;
	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 || op == BAM_CEQUAL || op == BAM_CDIFF) {
			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 (flag&USE_EQUAL) seq[z/2] &= (z&1)? 0xf0 : 0x0f;
					++u;
				} else {
					kputw(u, str); kputc(ref[x+j], str);
					u = 0; ++nm;
				}
			}
			if (j < l) break;
			x += l; y += l;
		} else if (op == BAM_CDEL) {
			kputw(u, str); 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;
		}
	}
	kputw(u, str);
	// 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 || op == BAM_CEQUAL || op == BAM_CDIFF) {
				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
	if (flag & UPDATE_NM) {
		uint8_t *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
	if (flag & UPDATE_MD) {
		uint8_t *old_md = bam_aux_get(b, "MD");
		if (c->flag & BAM_FUNMAP) return;
		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);
			}
		}
	}
	// drop all tags but RG
	if (flag&DROP_TAG) {
		uint8_t *q = bam_aux_get(b, "RG");
		bam_aux_drop_other(b, q);
	}
	// reduce the resolution of base quality
	if (flag&BIN_QUAL) {
		uint8_t *qual = bam1_qual(b);
		for (i = 0; i < b->core.l_qseq; ++i)
			if (qual[i] >= 3) qual[i] = qual[i]/10*10 + 7;
	}
	free(str->s); free(str);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, 0x38(%rsp)
movl %edx, 0x3c(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %r14
movq 0x8(%rdi), %rbx
movq %rbx, %rax
shrq $0x30, %rax
movl %ebx, %ecx
shrl $0x18, %ecx
addq 0x30(%rdi), %rcx
movq %rcx, 0x50(%rsp)
leaq (%rcx,%rax,4), %r13
andl $0x0, 0x24(%rsp)
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
callq 0x31f40
movq %rax, 0x18(%rsp)
movq %r14, 0x10(%rsp)
movl 0x4(%r14), %esi
xorl %r15d, %r15d
xorl %r8d, %r8d
xorl %edi, %edi
xorl %r9d, %r9d
shrq $0x30, %rbx
cmpq %rbx, %r8
jae 0x29c8d4
movq 0x50(%rsp), %rax
movl (%rax,%r8,4), %ecx
movl %ecx, %r12d
shrl $0x4, %r12d
movl %ecx, %eax
andl $0xf, %eax
cmpl $0x7, %eax
setne %dl
testb $0x7, %cl
setne %cl
movslq %esi, %rbp
testb %dl, %cl
movq %r12, 0x30(%rsp)
jne 0x29c7ff
movq %r8, 0x28(%rsp)
movl %esi, 0x4c(%rsp)
addq 0x40(%rsp), %rbp
movl %r9d, 0xc(%rsp)
movl %r9d, %r14d
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x29c834
leaq (%r14,%rbx), %rsi
movl %esi, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movsbq (%rbp,%rbx), %rcx
testq %rcx, %rcx
je 0x29c8d4
cltq
movzbl (%r13,%rax), %r8d
leaq 0x1748f7(%rip), %r9     # 0x411060
movzbl (%r9,%rcx), %r10d
cmpl $0xf, %r10d
setne %r9b
shll $0x2, %edx
movb $0x4, %cl
subb %dl, %cl
movl %r8d, %r11d
shrl %cl, %r11d
andl $0xf, %r11d
cmpl %r10d, %r11d
sete %cl
cmpl $0xf, %r11d
setne %dl
testl %r11d, %r11d
je 0x29c7d7
andb %r9b, %dl
andb %cl, %dl
jne 0x29c7d7
movq %r13, %r12
movq %r15, %r13
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x29ccb0
movsbl (%rbp,%rbx), %edi
movq %r15, %rsi
movq %r13, %r15
movq %r12, %r13
movq 0x30(%rsp), %r12
callq 0x29cd8b
incl %r15d
movl %r15d, 0x24(%rsp)
xorl %edi, %edi
jmp 0x29c7f7
testb $0x1, 0x3c(%rsp)
je 0x29c7f5
testb $0x1, %sil
movl $0xf0, %ecx
pushq $0xf
popq %rdx
cmovel %edx, %ecx
andl %ecx, %r8d
movb %r8b, (%r13,%rax)
incl %edi
incq %rbx
jmp 0x29c736
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
ja 0x29c8c1
leaq 0xbeede(%rip), %rdx     # 0x35b6f0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
addl %r12d, %r9d
cmpl $0x1, %eax
jne 0x29c8c1
addl %r12d, %r15d
movl %r15d, 0x24(%rsp)
jmp 0x29c8c1
movl 0x4c(%rsp), %esi
addl %r12d, %esi
movl 0xc(%rsp), %r9d
addl %r12d, %r9d
movq 0x28(%rsp), %r8
jmp 0x29c8c1
movl %r9d, 0xc(%rsp)
movq %r8, 0x28(%rsp)
movq %r12, %r14
movl %esi, %r12d
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x29ccb0
pushq $0x5e
popq %rdi
movq %rbx, %rsi
callq 0x29cd8b
movl %r14d, %ebx
addq 0x40(%rsp), %rbp
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x29c8a3
movsbl (%rbp,%r14), %edi
testl %edi, %edi
je 0x29c8d2
movq 0x18(%rsp), %rsi
callq 0x29cd8b
incq %r14
jmp 0x29c87d
addl %esi, %r12d
movl %r12d, %esi
jmp 0x29c8c1
movl %r12d, %esi
movq 0x30(%rsp), %rax
addl %eax, %esi
addl %eax, %r15d
movl %r15d, 0x24(%rsp)
xorl %edi, %edi
movq 0x28(%rsp), %r8
movl 0xc(%rsp), %r9d
incq %r8
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rbx
jmp 0x29c6e0
xorl %edi, %edi
movl 0x38(%rsp), %ebx
movq 0x18(%rsp), %rsi
callq 0x29ccb0
testl %ebx, %ebx
jle 0x29ca5e
cmpl %ebx, %r15d
jl 0x29ca5e
movq 0x10(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x28(%rsp)
xorl %edi, %edi
movl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rax
movzwl 0xe(%rax), %eax
movq %rdi, 0x30(%rsp)
cmpq %rax, %rdi
jae 0x29ca5e
movq 0x50(%rsp), %rax
movq 0x30(%rsp), %rdi
movl (%rax,%rdi,4), %ecx
movl %ecx, %r11d
shrl $0x4, %r11d
movl %ecx, %eax
andl $0xf, %eax
cmpl $0x7, %eax
setne %dl
testb $0x7, %cl
setne %sil
testb %dl, %sil
jne 0x29ca1d
movslq 0x28(%rsp), %rbx
movslq 0xc(%rsp), %r14
addq 0x40(%rsp), %rbx
movl %r14d, %r12d
xorl %ebp, %ebp
cmpq %rbp, %r11
je 0x29ca31
leaq (%r12,%rbp), %rsi
movl %esi, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movsbq (%rbx,%rbp), %rcx
testq %rcx, %rcx
je 0x29ca5e
cltq
movzbl (%r13,%rax), %edi
leaq 0x1746cb(%rip), %r8     # 0x411060
movzbl (%r8,%rcx), %r9d
cmpl $0xf, %r9d
setne %r8b
shll $0x2, %edx
movb $0x4, %cl
subb %dl, %cl
movl %edi, %r10d
shrl %cl, %r10d
andl $0xf, %r10d
cmpl %r9d, %r10d
sete %cl
cmpl $0xf, %r10d
setne %dl
testl %r10d, %r10d
je 0x29c9cc
andb %r8b, %dl
andb %cl, %dl
je 0x29ca15
testb $0x1, %sil
pushq $0xf
popq %rcx
movl $0xf0, %edx
cmovel %edx, %ecx
orl %ecx, %edi
movb %dil, (%r13,%rax)
movq 0x10(%rsp), %rdx
movq 0x8(%rdx), %rax
movq %rax, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x30(%rdx), %rcx
shrl $0x18, %eax
movl 0x10(%rdx), %edx
incl %edx
sarl %edx
movslq %edx, %rdx
addq %rax, %rdx
addq %r14, %rdx
addq %rcx, %rdx
movb $0x0, (%rbp,%rdx)
incq %rbp
jmp 0x29c963
andl $0xe, %ecx
cmpl $0x2, %ecx
jne 0x29ca42
addl 0x28(%rsp), %r11d
movl %r11d, 0x28(%rsp)
jmp 0x29ca56
addl %r11d, 0x28(%rsp)
addl %r11d, 0xc(%rsp)
movq 0x30(%rsp), %rdi
jmp 0x29ca56
cmpl $0x4, %eax
je 0x29ca4c
cmpl $0x1, %eax
jne 0x29ca56
addl 0xc(%rsp), %r11d
movl %r11d, 0xc(%rsp)
incq %rdi
jmp 0x29c909
movl 0x3c(%rsp), %ebp
testb $0x8, %bpl
je 0x29caec
leaq 0xbb48b(%rip), %rsi     # 0x357efe
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x293fec
testb $0x4, 0xc(%rbx)
jne 0x29cbbd
movq %rax, %r12
testq %rax, %rax
je 0x29cad0
movq %r12, %rdi
callq 0x294b0f
cmpl %eax, %r15d
je 0x29caec
movq 0x16e50a(%rip), %rcx    # 0x40afb0
movq (%rcx), %rdi
movq 0x10(%rsp), %rbx
movq 0x30(%rbx), %rdx
leaq 0xbeccf(%rip), %rsi     # 0x35b788
movl %eax, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x32960
movq %rbx, %rdi
movq %r12, %rsi
callq 0x29410b
leaq 0xbb427(%rip), %rsi     # 0x357efe
pushq $0x69
popq %rdx
pushq $0x4
popq %rcx
leaq 0x24(%rsp), %r8
movq 0x10(%rsp), %rdi
callq 0x293f30
testb $0x10, %bpl
jne 0x29cb3c
testb $0x2, %bpl
je 0x29cb17
leaq 0xbb459(%rip), %rsi     # 0x357f58
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x293fec
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2941f6
testb $0x4, %bpl
jne 0x29cbcc
movq 0x18(%rsp), %rbx
movq 0x10(%rbx), %rdi
callq 0x33058
movq %rbx, %rdi
callq 0x33058
jmp 0x29cbbd
leaq 0xbec79(%rip), %rsi     # 0x35b7bc
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x293fec
testb $0x4, 0xc(%rbx)
jne 0x29cbbd
movq %rax, %r15
testq %rax, %rax
je 0x29cc35
movq %r15, %rdi
incq %rdi
movq %rdi, 0x30(%rsp)
callq 0x2fe60
movq %rax, %r13
movq 0x18(%rsp), %rax
movq (%rax), %r14
cmpq %r14, %r13
jne 0x29cc5b
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x29caf2
callq 0x2fc70
movq (%rax), %rax
movzbl 0x1(%r15,%rbx), %ecx
movq 0x18(%rsp), %rdx
movq 0x10(%rdx), %r12
movsbq (%r12,%rbx), %rdx
incq %rbx
movl (%rax,%rcx,4), %ecx
cmpl (%rax,%rdx,4), %ecx
je 0x29cb88
jmp 0x29cc64
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdx
movq %rdx, %rsi
shrq $0x30, %rsi
shlq $0x2, %rsi
addq 0x30(%rax), %rsi
shrl $0x18, %edx
movl 0x10(%rax), %eax
leal 0x1(%rax), %ecx
sarl %ecx
movslq %ecx, %rcx
addq %rdx, %rcx
addq %rsi, %rcx
xorl %edx, %edx
movb $0xa, %sil
movslq %eax, %rdi
cmpq %rdi, %rdx
jge 0x29cb21
movb (%rcx,%rdx), %dil
cmpb $0x3, %dil
jb 0x29cc30
movzbl %dil, %eax
divb %sil
movzbl %ah, %eax
subb %al, %dil
addb $0x7, %dil
movb %dil, (%rcx,%rdx)
movq 0x10(%rsp), %rax
movl 0x10(%rax), %eax
incq %rdx
jmp 0x29cbfd
movq 0x18(%rsp), %rax
movl (%rax), %ecx
incl %ecx
movq 0x10(%rax), %r8
leaq 0xbeb73(%rip), %rsi     # 0x35b7bc
pushq $0x5a
popq %rdx
movq 0x10(%rsp), %rdi
callq 0x293f30
jmp 0x29caf2
movq 0x18(%rsp), %rax
movq 0x10(%rax), %r12
movq 0x16e345(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
movq 0x10(%rsp), %rbx
movq 0x30(%rbx), %rdx
leaq 0xbeb41(%rip), %rsi     # 0x35b7bf
movq 0x30(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x32960
movq %rbx, %rdi
movq %r15, %rsi
callq 0x29410b
incl %r14d
leaq 0xbeb1a(%rip), %rsi     # 0x35b7bc
pushq $0x5a
popq %rdx
movq %rbx, %rdi
movl %r14d, %ecx
movq %r12, %r8
jmp 0x29cc51