// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movl %ecx, 0x5c(%rsp)
movl %edx, 0x4c(%rsp)
movq 0x8(%rdi), %rbx
movl %ebx, %eax
shrl $0x18, %eax
movq %rdi, 0x8(%rsp)
addq 0x30(%rdi), %rax
movq %rsi, 0x50(%rsp)
shrq $0x30, %rbx
movq %rax, 0x60(%rsp)
leaq (%rax,%rbx,4), %rax
movq %rax, 0x38(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3610
movq %rax, %r9
xorl %r10d, %r10d
movl $0x0, %edi
testq %rbx, %rbx
movq %rax, 0x18(%rsp)
jne 0x2d67b
movq %r10, 0x20(%rsp)
movq %r9, %rsi
callq 0x2dc80
movl 0x5c(%rsp), %eax
testl %eax, %eax
jle 0x2d52a
cmpl %eax, 0x20(%rsp)
jl 0x2d52a
movabsq $0x1000000000000, %rax  # imm = 0x1000000000000
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
cmpq %rax, %rcx
jae 0x2da85
movl 0x4c(%rsp), %ebp
testb $0x8, %bpl
je 0x2d5bf
leaq 0x17271(%rip), %rsi     # 0x447b0
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x22374
testb $0x4, 0xc(%rbx)
jne 0x2da76
movq %rax, %r15
testq %rax, %rax
je 0x2d59f
movq %r15, %rdi
callq 0x233b4
cmpl %eax, 0x20(%rsp)
je 0x2d5bf
movq 0x22a75(%rip), %rcx     # 0x4ffe8
movq (%rcx), %rdi
movq 0x8(%rsp), %rbx
movq 0x30(%rbx), %rdx
leaq 0x1722d(%rip), %rsi     # 0x447b3
movl %eax, %ecx
movq 0x20(%rsp), %r8
xorl %eax, %eax
callq 0x3750
movq %rbx, %rdi
movq %r15, %rsi
callq 0x224e7
leaq 0x1720a(%rip), %rsi     # 0x447b0
leaq 0x14(%rsp), %r8
movq 0x8(%rsp), %rdi
movl $0x69, %edx
movl $0x4, %ecx
callq 0x222b8
testb $0x10, %bpl
jne 0x2d9f5
testb $0x2, %bpl
je 0x2d5ee
leaq 0x147bb(%rip), %rsi     # 0x41d91
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x22374
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2267b
testb $0x4, %bpl
movq 0x18(%rsp), %rbx
je 0x2d665
movq 0x8(%rsp), %rax
movl 0x10(%rax), %eax
testl %eax, %eax
jle 0x2d665
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rdx
movq %rdx, %rsi
shrq $0x30, %rsi
shlq $0x2, %rsi
addq 0x30(%rcx), %rsi
shrl $0x18, %edx
leal 0x1(%rax), %ecx
sarl %ecx
movslq %ecx, %rcx
addq %rdx, %rcx
addq %rsi, %rcx
xorl %edx, %edx
movb (%rcx,%rdx), %sil
cmpb $0x3, %sil
jb 0x2d65a
movzbl %sil, %eax
imull $0xcd, %eax, %eax
shrl $0xa, %eax
andl $-0x2, %eax
leal (%rax,%rax,4), %eax
addb $0x7, %al
movb %al, (%rcx,%rdx)
movq 0x8(%rsp), %rax
movl 0x10(%rax), %eax
incq %rdx
movslq %eax, %rsi
cmpq %rsi, %rdx
jl 0x2d630
movq 0x10(%rbx), %rdi
callq 0x3350
movq %rbx, %rdi
callq 0x3350
jmp 0x2da76
movq 0x8(%rsp), %rax
movl 0x4(%rax), %r13d
xorl %r10d, %r10d
leaq 0x23752(%rip), %r11     # 0x50de0
xorl %ecx, %ecx
movq $0x0, 0x40(%rsp)
xorl %edi, %edi
movq 0x60(%rsp), %rax
movq %rcx, 0x30(%rsp)
movl (%rax,%rcx,4), %ebp
movl %ebp, %ebx
shrl $0x4, %ebx
movl %ebp, %eax
andl $0xf, %eax
cmpl $0x7, %eax
setne %cl
testb $0x7, %bpl
setne %dl
testb %cl, %dl
movq %rbx, 0x20(%rsp)
jne 0x2d84b
cmpl $0x10, %ebp
movl %r13d, 0x2c(%rsp)
jb 0x2d832
movslq %r13d, %r14
addq 0x50(%rsp), %r14
movq 0x40(%rsp), %rax
movl %eax, %r15d
leal (,%rax,4), %ebp
xorl %r13d, %r13d
movq %r14, 0x70(%rsp)
movq %r15, 0x68(%rsp)
movsbq (%r14,%r13), %rax
testq %rax, %rax
je 0x2d4f3
movzbl (%r11,%rax), %esi
cmpl $0xf, %esi
setne %dl
leaq (%r15,%r13), %rax
movl %eax, %ecx
sarl %ecx
movslq %ecx, %rcx
movq 0x38(%rsp), %r8
movzbl (%r8,%rcx), %r8d
movl %ebp, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %r8d
andl $0xf, %r8d
cmpl %esi, %r8d
sete %cl
cmpl $0xf, %r8d
setne %sil
testl %r8d, %r8d
je 0x2d77c
andb %dl, %sil
andb %cl, %sil
jne 0x2d77c
movq %r10, %r15
movq %r9, %rsi
callq 0x2dc80
movq 0x18(%rsp), %r9
movb (%r14,%r13), %r14b
movq (%r9), %rbx
leaq 0x1(%rbx), %r12
cmpq 0x8(%r9), %r12
jae 0x2d7aa
movq 0x10(%r9), %rax
jmp 0x2d7f4
testb $0x1, 0x4c(%rsp)
je 0x2d7a6
testb $0x1, %al
movl $0xf0, %ecx
movl $0xf, %edx
cmovel %edx, %ecx
movl %eax, %edx
shrl $0x1f, %edx
addl %eax, %edx
sarl %edx
movslq %edx, %rax
movq 0x38(%rsp), %rdx
andb %cl, (%rdx,%rax)
incl %edi
jmp 0x2d823
movq %r12, %rax
shrq %rax
orq %r12, %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(%r9)
movq 0x10(%r9), %rdi
callq 0x3720
movq 0x18(%rsp), %r9
movq %rax, 0x10(%r9)
movq %r15, %r10
movq %r12, (%r9)
movb %r14b, (%rax,%rbx)
movb $0x0, 0x1(%rax,%rbx)
incl %r10d
movl %r10d, 0x14(%rsp)
xorl %edi, %edi
leaq 0x235cc(%rip), %r11     # 0x50de0
movq 0x20(%rsp), %rbx
movq 0x70(%rsp), %r14
movq 0x68(%rsp), %r15
incq %r13
addl $0x4, %ebp
cmpq %r13, %rbx
jne 0x2d6fe
movl 0x2c(%rsp), %r13d
addl %ebx, %r13d
movq 0x40(%rsp), %rax
addl %ebx, %eax
movq %rax, 0x40(%rsp)
jmp 0x2d9d6
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
ja 0x2d9d6
leaq 0x16ec6(%rip), %rdx     # 0x44724
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq 0x40(%rsp), %rcx
addl %ebx, %ecx
movq %rcx, 0x40(%rsp)
cmpl $0x1, %eax
jne 0x2d9d6
addl %ebx, %r10d
movl %r10d, 0x14(%rsp)
jmp 0x2d9d6
movq %r10, %r14
movq %r9, %rsi
callq 0x2dc80
movq 0x18(%rsp), %r9
movq (%r9), %r12
movq 0x8(%r9), %r15
leaq 0x1(%r12), %rbx
cmpq %r15, %rbx
jae 0x2d8bb
movq 0x10(%r9), %rax
jmp 0x2d908
addl %r13d, %ebx
movl %ebx, %r13d
jmp 0x2d9d6
movq %rbx, %rax
shrq %rax
orq %rbx, %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, %r15
shrq $0x10, %r15
orq %rcx, %r15
incq %r15
movq %r15, 0x8(%r9)
movq 0x10(%r9), %rdi
movq %r15, %rsi
callq 0x3720
movq 0x18(%rsp), %r9
movq %rax, 0x10(%r9)
movq %r14, %r10
movq %rbx, (%r9)
movw $0x5e, (%rax,%r12)
cmpl $0x10, %ebp
movq 0x20(%rsp), %rdx
jb 0x2d9c2
movl %r13d, 0x2c(%rsp)
movslq %r13d, %rbx
movl %edx, %r14d
addq 0x50(%rsp), %rbx
addq $0x2, %r12
xorl %ebp, %ebp
movb (%rbx,%rbp), %r13b
testb %r13b, %r13b
je 0x2dbfa
cmpq %r15, %r12
jb 0x2d997
movq %r12, %rcx
shrq %rcx
orq %r12, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x4, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x8, %rdx
orq %rcx, %rdx
movq %rdx, %r15
shrq $0x10, %r15
orq %rdx, %r15
incq %r15
movq %r15, 0x8(%r9)
movq %rax, %rdi
movq %r15, %rsi
callq 0x3720
movq 0x18(%rsp), %r9
movq %rax, 0x10(%r9)
movq %r12, (%r9)
movb %r13b, -0x1(%rax,%r12)
movq 0x10(%r9), %rax
movb $0x0, (%rax,%r12)
incq %rbp
incq %r12
cmpq %rbp, %r14
jne 0x2d939
movl 0x14(%rsp), %r10d
movl 0x2c(%rsp), %r13d
movq 0x20(%rsp), %rdx
addl %edx, %r13d
addl %edx, %r10d
movl %r10d, 0x14(%rsp)
xorl %edi, %edi
leaq 0x2340a(%rip), %r11     # 0x50de0
movq 0x30(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %rax
movzwl 0xe(%rax), %eax
cmpq %rax, %rcx
jb 0x2d69b
jmp 0x2d4f3
leaq 0x16deb(%rip), %rsi     # 0x447e7
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x22374
testb $0x4, 0xc(%rbx)
jne 0x2da76
movq %rax, %r15
testq %rax, %rax
je 0x2dc06
movq %r15, %rdi
incq %rdi
movq %rdi, 0x38(%rsp)
callq 0x3360
movq %rax, %r13
movq 0x18(%rsp), %rax
movq (%rax), %r14
cmpq %r14, %r13
jne 0x2dc2e
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x2d5c9
callq 0x3300
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 0x2da41
jmp 0x2dc32
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movl 0x4(%rax), %esi
xorl %eax, %eax
leaq 0x2334a(%rip), %rdx     # 0x50de0
xorl %r8d, %r8d
movq 0x60(%rsp), %rdi
movl (%rdi,%rax,4), %r11d
movl %r11d, %r9d
shrl $0x4, %r9d
movl %r11d, %r10d
andl $0xf, %r10d
cmpl $0x7, %r10d
setne %bl
testb $0x7, %r11b
setne %bpl
testb %bl, %bpl
jne 0x2dbbe
cmpl $0x10, %r11d
jb 0x2dbb6
movslq %r8d, %r10
movslq %esi, %r11
addq 0x50(%rsp), %r11
movl %r10d, %ebx
leal (,%r10,4), %ebp
xorl %r15d, %r15d
movsbq (%r11,%r15), %rcx
testq %rcx, %rcx
je 0x2d52a
movzbl (%rdx,%rcx), %r13d
cmpl $0xf, %r13d
setne %r12b
leaq (%rbx,%r15), %r14
movl %r14d, %ecx
sarl %ecx
movslq %ecx, %rcx
movq 0x38(%rsp), %rdi
movzbl (%rdi,%rcx), %edi
movl %ebp, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edi
andl $0xf, %edi
cmpl %r13d, %edi
sete %cl
cmpl $0xf, %edi
setne %r13b
testl %edi, %edi
je 0x2db40
andb %r12b, %r13b
andb %cl, %r13b
je 0x2db9e
testb $0x1, %r14b
movl $0xf, %ecx
movl $0xf0, %edi
cmovel %edi, %ecx
movl %r14d, %edi
shrl $0x1f, %edi
addl %r14d, %edi
sarl %edi
movslq %edi, %rdi
movq 0x38(%rsp), %r14
orb %cl, (%r14,%rdi)
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rcx
movq %rcx, %rdi
shrq $0x30, %rdi
shlq $0x2, %rdi
addq 0x30(%r14), %rdi
shrl $0x18, %ecx
movl 0x10(%r14), %r14d
incl %r14d
sarl %r14d
movslq %r14d, %r14
addq %rcx, %r14
addq %r10, %r14
addq %rdi, %r14
movb $0x0, (%r15,%r14)
incq %r15
addl $0x4, %ebp
cmpq %r15, %r9
jne 0x2daeb
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
addl %r9d, %esi
addl %r9d, %r8d
jmp 0x2dbe2
andl $0xe, %r11d
cmpl $0x2, %r11d
jne 0x2dbd0
addl %esi, %r9d
movl %r9d, %esi
jmp 0x2dbe2
cmpl $0x4, %r10d
je 0x2dbdc
cmpl $0x1, %r10d
jne 0x2dbe2
addl %r8d, %r9d
movl %r9d, %r8d
incq %rax
movq %rcx, %rdi
shrq $0x30, %rdi
cmpq %rdi, %rax
jb 0x2da99
jmp 0x2d52a
xorl %edi, %edi
movl 0x14(%rsp), %r10d
jmp 0x2d4f3
movq 0x18(%rsp), %rax
movl (%rax), %ecx
incl %ecx
movq 0x10(%rax), %r8
leaq 0x16bcd(%rip), %rsi     # 0x447e7
movq 0x8(%rsp), %rdi
movl $0x5a, %edx
callq 0x222b8
jmp 0x2d5c9
movq 0x10(%rax), %r12
movq 0x223af(%rip), %rax     # 0x4ffe8
movq (%rax), %rdi
movq 0x8(%rsp), %rbx
movq 0x30(%rbx), %rdx
leaq 0x16b9e(%rip), %rsi     # 0x447ea
movq 0x38(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x3750
movq %rbx, %rdi
movq %r15, %rsi
callq 0x224e7
incl %r14d
leaq 0x16b77(%rip), %rsi     # 0x447e7
movq %rbx, %rdi
movl $0x5a, %edx
movl %r14d, %ecx
movq %r12, %r8
jmp 0x2dc24