// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/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 $0x68, %rsp
movl %ecx, 0x4c(%rsp)
movl %edx, 0xc(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %r14
movq 0x8(%rdi), %rcx
movq %rcx, %rax
shrq $0x30, %rax
shrl $0x18, %ecx
addq 0x30(%rdi), %rcx
movq %rcx, 0x50(%rsp)
leaq (%rcx,%rax,4), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3610
movq %rax, %r8
xorl %edi, %edi
cmpw $0x0, 0xe(%r14)
movq %r14, 0x10(%rsp)
movq %rax, 0x18(%rsp)
je 0x2ceb2
movl 0x4(%r14), %eax
movl %eax, 0x4(%rsp)
xorl %esi, %esi
xorl %r9d, %r9d
xorl %edi, %edi
movq 0x50(%rsp), %rax
movl (%rax,%rsi,4), %ebx
movl %ebx, %r10d
shrl $0x4, %r10d
movl %ebx, %ecx
andl $0xf, %ecx
cmpl $0x7, %ecx
setne %al
testb $0x7, %bl
setne %dl
testb %al, %dl
movq %r10, 0x40(%rsp)
jne 0x2ccf7
movq %rsi, 0x28(%rsp)
cmpl $0x10, %ebx
setae %r11b
movq %r9, 0x20(%rsp)
jb 0x2ccea
movl %r9d, %ebx
movslq 0x4(%rsp), %r12
movl %r10d, %r13d
addq 0x38(%rsp), %r12
leal (,%r9,4), %r15d
negq %r13
movl $0x1, %ebp
movq %r13, 0x60(%rsp)
movsbq (%r12), %r14
testq %r14, %r14
je 0x2ccbc
leaq 0x2323b(%rip), %rax     # 0x4fde0
movzbl (%rax,%r14), %edx
cmpl $0xf, %edx
setne %al
movl %ebx, %ecx
sarl %ecx
movslq %ecx, %rcx
movq 0x30(%rsp), %rsi
movzbl (%rsi,%rcx), %esi
movl %r15d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %esi
andl $0xf, %esi
cmpl %edx, %esi
sete %cl
cmpl $0xf, %esi
setne %dl
testl %esi, %esi
je 0x2cc8f
andb %al, %dl
andb %cl, %dl
jne 0x2cc8f
movb %r11b, 0x3(%rsp)
movq %r8, %rsi
callq 0x2d2d0
movq 0x18(%rsp), %r8
movb (%r12), %cl
movq (%r8), %rdx
leaq 0x1(%rdx), %r13
cmpq 0x8(%r8), %r13
jb 0x2cc69
movq %r13, %rax
shrq %rax
orq %r13, %rax
movb %cl, 0x2(%rsp)
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(%r8)
movq 0x10(%r8), %rdi
movq %rdx, 0x58(%rsp)
callq 0x3720
movq 0x58(%rsp), %rdx
movb 0x2(%rsp), %cl
movq 0x18(%rsp), %r8
movq %rax, 0x10(%r8)
movq 0x10(%r8), %rax
movq %r13, (%r8)
movb %cl, (%rax,%rdx)
movb $0x0, 0x1(%rax,%rdx)
incl 0x8(%rsp)
xorl %edi, %edi
movq 0x40(%rsp), %r10
movb 0x3(%rsp), %r11b
movq 0x60(%rsp), %r13
jmp 0x2ccbc
testb $0x1, 0xc(%rsp)
je 0x2ccba
testb $0x1, %bl
movl $0xf0, %eax
movl $0xf, %ecx
cmovel %ecx, %eax
movl %ebx, %ecx
shrl $0x1f, %ecx
addl %ebx, %ecx
sarl %ecx
movslq %ecx, %rcx
movq 0x30(%rsp), %rdx
andb %al, (%rdx,%rcx)
incl %edi
testb %r14b, %r14b
je 0x2ccea
cmpq %r10, %rbp
setb %r11b
incq %r12
incq %rbx
addl $0x4, %r15d
leaq 0x1(,%rbp), %rax
addq %r13, %rax
incq %rbp
cmpq $0x1, %rax
jne 0x2cb90
testb $0x1, %r11b
je 0x2cd2b
xorl %eax, %eax
jmp 0x2ce8e
leal -0x1(%rcx), %eax
cmpl $0x3, %eax
ja 0x2ce86
leaq 0x1692e(%rip), %rdx     # 0x43638
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
addl %r10d, %r9d
movb $0x1, %al
cmpl $0x1, %ecx
jne 0x2ce9d
addl %r10d, 0x8(%rsp)
jmp 0x2ce9d
addl %r10d, 0x4(%rsp)
movq 0x20(%rsp), %r9
addl %r10d, %r9d
movb $0x1, %al
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %rsi
jmp 0x2ce9d
movq %r9, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
movq %r8, %rsi
callq 0x2d2d0
movq 0x18(%rsp), %r8
movq (%r8), %r14
leaq 0x1(%r14), %r15
cmpq 0x8(%r8), %r15
jb 0x2cdb7
movq %r15, %rax
shrq %rax
orq %r15, %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(%r8)
movq 0x10(%r8), %rdi
callq 0x3720
movq 0x18(%rsp), %r8
movq %rax, 0x10(%r8)
movq 0x10(%r8), %rax
movq %r15, (%r8)
movw $0x5e, (%rax,%r14)
cmpl $0x10, %ebx
movq 0x40(%rsp), %rcx
jb 0x2ce6a
movslq 0x4(%rsp), %rbx
movl %ecx, %r14d
addq 0x38(%rsp), %rbx
xorl %r15d, %r15d
movb (%rbx,%r15), %bpl
testb %bpl, %bpl
je 0x2ce8a
movq (%r8), %r12
leaq 0x1(%r12), %r13
cmpq 0x8(%r8), %r13
jb 0x2ce4d
movq %r13, %rax
shrq %rax
orq %r13, %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(%r8)
movq 0x10(%r8), %rdi
callq 0x3720
movq 0x40(%rsp), %rcx
movq 0x18(%rsp), %r8
movq %rax, 0x10(%r8)
movq 0x10(%r8), %rax
movq %r13, (%r8)
movb %bpl, (%rax,%r12)
movb $0x0, 0x1(%rax,%r12)
incq %r15
cmpq %r15, %r14
jne 0x2cde3
addl %ecx, 0x8(%rsp)
addl %ecx, 0x4(%rsp)
xorl %edi, %edi
movb $0x1, %al
jmp 0x2ce8e
addl 0x4(%rsp), %r10d
movb $0x1, %al
movl %r10d, 0x4(%rsp)
jmp 0x2ce9d
movb $0x1, %al
jmp 0x2ce9d
xorl %eax, %eax
xorl %edi, %edi
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %r9
testb %al, %al
je 0x2ceb2
incq %rsi
movzwl 0xe(%r14), %eax
cmpq %rax, %rsi
jb 0x2cb27
movq %r8, %rsi
callq 0x2d2d0
movl 0x4c(%rsp), %eax
testl %eax, %eax
jle 0x2ced8
cmpl %eax, 0x8(%rsp)
jl 0x2ced8
movabsq $0x1000000000000, %rax  # imm = 0x1000000000000
cmpq %rax, 0x8(%r14)
jae 0x2cf18
movl 0xc(%rsp), %ecx
testb $0x8, %cl
je 0x2d108
leaq 0x167dc(%rip), %rsi     # 0x436c8
movq %r14, %rdi
callq 0x21c82
testb $0x4, 0xc(%r14)
jne 0x2d29b
movq %rax, %r15
testq %rax, %rax
je 0x2d0a5
movq %r15, %rdi
callq 0x22a3f
jmp 0x2d0aa
movl 0x4(%r14), %edi
xorl %eax, %eax
leaq 0x22ebb(%rip), %rdx     # 0x4fde0
movl $0xf0, %esi
xorl %r8d, %r8d
movl %r8d, %r8d
movq 0x50(%rsp), %rcx
movl (%rcx,%rax,4), %r10d
movl %r10d, %r9d
shrl $0x4, %r9d
movl %r10d, %ecx
andl $0xf, %ecx
cmpl $0x7, %ecx
setne %r11b
testb $0x7, %r10b
setne %bl
testb %r11b, %bl
jne 0x2d054
cmpl $0x10, %r10d
jb 0x2d04a
movslq %r8d, %r10
movslq %edi, %r11
addq 0x38(%rsp), %r11
leal (,%r10,4), %ebx
xorl %r14d, %r14d
movsbq (%r11,%r14), %r15
testq %r15, %r15
je 0x2d07c
movzbl (%rdx,%r15), %r12d
cmpl $0xf, %r12d
setne %bpl
leal (%r8,%r14), %ecx
sarl %ecx
movslq %ecx, %rcx
movq 0x30(%rsp), %r13
movzbl (%r13,%rcx), %r13d
movl %ebx, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %r13d
andl $0xf, %r13d
cmpl %r12d, %r13d
sete %cl
cmpl $0xf, %r13d
setne %r12b
testl %r13d, %r13d
je 0x2cfd5
andb %bpl, %r12b
andb %cl, %r12b
je 0x2d036
leaq (%r10,%r14), %rcx
testb $0x1, %cl
movl $0xf, %ecx
cmovel %esi, %ecx
leal (%r8,%r14), %ebp
movl %ebp, %r12d
shrl $0x1f, %r12d
addl %ebp, %r12d
sarl %r12d
movslq %r12d, %r12
movq 0x30(%rsp), %r13
orb %cl, (%r13,%r12)
movq 0x10(%rsp), %r13
movq 0x8(%r13), %rcx
movq %rcx, %r12
shrq $0x30, %r12
shlq $0x2, %r12
addq 0x30(%r13), %r12
shrl $0x18, %ecx
movl 0x10(%r13), %ebp
incl %ebp
sarl %ebp
movslq %ebp, %r13
addq %rcx, %r13
addq %r10, %r13
addq %r12, %r13
movb $0x0, (%r14,%r13)
testb %r15b, %r15b
je 0x2d07c
incq %r14
addl $0x4, %ebx
cmpq %r14, %r9
jne 0x2cf7d
addl %r9d, %edi
addl %r9d, %r8d
movb $0x1, %cl
jmp 0x2d07e
andl $0xe, %r10d
cmpl $0x2, %r10d
jne 0x2d068
addl %edi, %r9d
movb $0x1, %cl
movl %r9d, %edi
jmp 0x2d083
cmpl $0x1, %ecx
je 0x2d072
cmpl $0x4, %ecx
jne 0x2d0a1
addl %r8d, %r9d
movb $0x1, %cl
movl %r9d, %r8d
jmp 0x2d083
xorl %ecx, %ecx
movq 0x10(%rsp), %r14
testb %cl, %cl
je 0x2ced8
incq %rax
movzwl 0xe(%r14), %ecx
cmpq %rcx, %rax
jb 0x2cf2d
jmp 0x2ced8
movb $0x1, %cl
jmp 0x2d083
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %r15, %r15
je 0x2d0e6
movl 0x8(%rsp), %r8d
cmpl %eax, %r8d
movl 0xc(%rsp), %ecx
je 0x2d108
movq 0x21f24(%rip), %rcx     # 0x4efe8
movq (%rcx), %rdi
movq 0x30(%r14), %rdx
leaq 0x165f9(%rip), %rsi     # 0x436cb
movl %eax, %ecx
xorl %eax, %eax
callq 0x3750
movq %r14, %rdi
movq %r15, %rsi
callq 0x21e01
leaq 0x165db(%rip), %rsi     # 0x436c8
leaq 0x8(%rsp), %r8
movq %r14, %rdi
movl $0x69, %edx
movl $0x4, %ecx
callq 0x21bc8
movl 0xc(%rsp), %ecx
testb $0x10, %cl
jne 0x2d1b5
movl 0xc(%rsp), %ebp
testb $0x2, %bpl
movq 0x10(%rsp), %rbx
je 0x2d13a
leaq 0x13c22(%rip), %rsi     # 0x40d49
movq %rbx, %rdi
callq 0x21c82
movq %rbx, %rdi
movq %rax, %rsi
callq 0x21f85
testb $0x4, %bpl
movq 0x18(%rsp), %rsi
je 0x2d19c
movl 0x10(%rbx), %eax
testl %eax, %eax
jle 0x2d19c
movq 0x8(%rbx), %rcx
movq %rcx, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%rbx), %rdx
shrl $0x18, %ecx
incl %eax
sarl %eax
cltq
addq %rcx, %rax
addq %rdx, %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
cmpb $0x3, %dl
jb 0x2d190
movzbl %dl, %edx
imull $0xcd, %edx, %edx
shrl $0xa, %edx
andl $-0x2, %edx
leal (%rdx,%rdx,4), %edx
addb $0x7, %dl
movb %dl, (%rax,%rcx)
incq %rcx
movslq 0x10(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x2d170
movq 0x10(%rsi), %rdi
movq %rsi, %rbx
callq 0x3350
movq %rbx, %rdi
callq 0x3350
jmp 0x2d29b
leaq 0x16543(%rip), %rsi     # 0x436ff
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x21c82
testb $0x4, 0xc(%rbx)
jne 0x2d29b
movq %rax, %r15
testq %rax, %rax
je 0x2d2aa
movq %r15, %r13
incq %r13
movq %r13, %rdi
callq 0x3360
movq 0x18(%rsp), %rcx
movq (%rcx), %r12
cmpq %r12, %rax
jne 0x2d246
movq %r12, %rbx
negq %rbx
movq $-0x1, %r14
leaq (%rbx,%r14), %rax
cmpq $-0x1, %rax
je 0x2d111
callq 0x3300
movq (%rax), %rax
movzbl 0x2(%r15,%r14), %ecx
movl (%rax,%rcx,4), %ecx
movq 0x18(%rsp), %rdx
movq 0x10(%rdx), %rdx
movsbq 0x1(%rdx,%r14), %rdx
incq %r14
cmpl (%rax,%rdx,4), %ecx
je 0x2d207
cmpq %r14, %r12
jbe 0x2d111
movq 0x21d9b(%rip), %rax     # 0x4efe8
movq (%rax), %rdi
movq 0x10(%rsp), %rbx
movq 0x30(%rbx), %rdx
movq 0x18(%rsp), %rax
movq 0x10(%rax), %r14
leaq 0x16499(%rip), %rsi     # 0x43702
movq %r13, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x3750
movq %rbx, %rdi
movq %r15, %rsi
callq 0x21e01
incl %r12d
leaq 0x16474(%rip), %rsi     # 0x436ff
movq %rbx, %rdi
movl $0x5a, %edx
movl %r12d, %ecx
movq %r14, %r8
jmp 0x2d2c6
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rax
movl (%rax), %ecx
incl %ecx
movq 0x10(%rax), %r8
leaq 0x16441(%rip), %rsi     # 0x436ff
movq %rbx, %rdi
movl $0x5a, %edx
callq 0x21bc8
jmp 0x2d111