// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam.c
// Function: bam_remove_B
int bam_remove_B(bam1_t *b)
{
	int i, j, end_j, k, l, no_qual;
	uint32_t *cigar, *new_cigar;
	uint8_t *seq, *qual, *p;
	// test if removal is necessary
	if (b->core.flag & BAM_FUNMAP) return 0; // unmapped; do nothing
	cigar = bam1_cigar(b);
	for (k = 0; k < b->core.n_cigar; ++k)
		if (bam_cigar_op(cigar[k]) == BAM_CBACK) break;
	if (k == b->core.n_cigar) return 0; // no 'B'
	if (bam_cigar_op(cigar[0]) == BAM_CBACK) goto rmB_err; // cannot be removed
	// allocate memory for the new CIGAR
	if (b->data_len + (b->core.n_cigar + 1) * 4 > b->m_data) { // not enough memory
		b->m_data = b->data_len + b->core.n_cigar * 4;
		kroundup32(b->m_data);
		b->data = (uint8_t*)realloc(b->data, b->m_data);
		cigar = bam1_cigar(b); // after realloc, cigar may be changed
	}
	new_cigar = (uint32_t*)(b->data + (b->m_data - b->core.n_cigar * 4)); // from the end of b->data
	// the core loop
	seq = bam1_seq(b); qual = bam1_qual(b);
	no_qual = (qual[0] == 0xff); // test whether base quality is available
	i = j = 0; end_j = -1;
	for (k = l = 0; k < b->core.n_cigar; ++k) {
		int op  = bam_cigar_op(cigar[k]);
		int len = bam_cigar_oplen(cigar[k]);
		if (op == BAM_CBACK) { // the backward operation
			int t, u;
			if (k == b->core.n_cigar - 1) break; // ignore 'B' at the end of CIGAR
			if (len > j) goto rmB_err; // an excessively long backward
			for (t = l - 1, u = 0; t >= 0; --t) { // look back
				int op1  = bam_cigar_op(new_cigar[t]);
				int len1 = bam_cigar_oplen(new_cigar[t]);
				if (bam_cigar_type(op1)&1) { // consume the query
					if (u + len1 >= len) { // stop
						new_cigar[t] -= (len - u) << BAM_CIGAR_SHIFT;
						break;
					} else u += len1;
				}
			}
			if (bam_cigar_oplen(new_cigar[t]) == 0) --t; // squeeze out the zero-length operation
			l = t + 1;
			end_j = j; j -= len;
		} else { // other CIGAR operations
			new_cigar[l++] = cigar[k];
			if (bam_cigar_type(op)&1) { // consume the query
				if (i != j) { // no need to copy if i == j
					int u, c, c0;
					for (u = 0; u < len; ++u) { // construct the consensus
						c = bam1_seqi(seq, i+u);
						if (j + u < end_j) { // in an overlap
							c0 = bam1_seqi(seq, j+u);
							if (c != c0) { // a mismatch; choose the better base
								if (qual[j+u] < qual[i+u]) { // the base in the 2nd segment is better
									bam1_seq_seti(seq, j+u, c);
									qual[j+u] = qual[i+u] - qual[j+u];
								} else qual[j+u] -= qual[i+u]; // the 1st is better; reduce base quality
							} else qual[j+u] = qual[j+u] > qual[i+u]? qual[j+u] : qual[i+u];
						} else { // not in an overlap; copy over
							bam1_seq_seti(seq, j+u, c);
							qual[j+u] = qual[i+u];
						}
					}
				}
				i += len, j += len;
			}
		}
	}
	if (no_qual) qual[0] = 0xff; // in very rare cases, this may be modified
	// merge adjacent operations if possible
	for (k = 1; k < l; ++k)
		if (bam_cigar_op(new_cigar[k]) == bam_cigar_op(new_cigar[k-1]))
			new_cigar[k] += new_cigar[k-1] >> BAM_CIGAR_SHIFT << BAM_CIGAR_SHIFT, new_cigar[k-1] &= 0xf;
	// kill zero length operations
	for (k = i = 0; k < l; ++k)
		if (new_cigar[k] >> BAM_CIGAR_SHIFT)
			new_cigar[i++] = new_cigar[k];
	l = i;
	// update b
	memcpy(cigar, new_cigar, l * 4); // set CIGAR
	p = b->data + b->core.l_qname + l * 4;
	memmove(p, seq, (j+1)>>1); p += (j+1)>>1; // set SEQ
	memmove(p, qual, j); p += j; // set QUAL
	memmove(p, bam1_aux(b), b->l_aux); p += b->l_aux; // set optional fields
	b->core.n_cigar = l, b->core.l_qseq = j; // update CIGAR length and query length
	b->data_len = p - b->data; // update record length
	return 0;

rmB_err:
	b->core.flag |= BAM_FUNMAP;
	return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq 0x8(%rdi), %rcx
btq $0x22, %rcx
jb 0xa344
movq %rdi, %r15
movq 0x30(%rdi), %rax
movl %ecx, %r11d
shrl $0x18, %r11d
leaq (%rax,%r11), %rdi
xorl %ebp, %ebp
movq %rcx, %rdx
shrq $0x30, %rdx
movl $0x0, %esi
jne 0xa32c
cmpl %edx, %esi
je 0xa346
movl (%rdi), %esi
andl $0xf, %esi
cmpl $0x9, %esi
jne 0xa35a
movabsq $0x400000000, %rax      # imm = 0x400000000
orq %rcx, %rax
movq %rax, 0x8(%r15)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xa346
xorl %esi, %esi
movl (%rdi,%rsi,4), %r8d
andl $0xf, %r8d
cmpl $0x9, %r8d
je 0xa306
incq %rsi
cmpq %rsi, %rdx
jne 0xa32e
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x24(%r15), %r9d
movl 0x28(%r15), %esi
leal (%r9,%rdx,4), %r8d
addl $0x4, %r8d
cmpl %esi, %r8d
jle 0xa3c8
leal (%r9,%rdx,4), %edx
decl %edx
movl %edx, %ecx
sarl %ecx
orl %edx, %ecx
movl %ecx, %edx
sarl $0x2, %edx
orl %ecx, %edx
movl %edx, %ecx
sarl $0x4, %ecx
orl %edx, %ecx
movl %ecx, %edx
sarl $0x8, %edx
orl %ecx, %edx
movl %edx, %ecx
sarl $0x10, %ecx
orl %edx, %ecx
incl %ecx
movl %ecx, 0x28(%r15)
movslq %ecx, %rsi
movq %rax, %rdi
callq 0x3430
movq %rax, 0x30(%r15)
movq 0x8(%r15), %rcx
movl %ecx, %r11d
shrl $0x18, %r11d
movq %rax, %rdi
addq %r11, %rdi
movl 0x28(%r15), %esi
movq %rcx, %rdx
shrq $0x30, %rdx
movq %rcx, %r10
shrq $0x2e, %r10
andl $-0x4, %r10d
subl %r10d, %esi
movslq %esi, %rsi
addq %rax, %rsi
movq %rax, %rbx
leaq (%rax,%r11), %r9
addq %r10, %r9
movl 0x10(%r15), %r8d
incl %r8d
sarl %r8d
movslq %r8d, %rax
movq %rax, 0x18(%rsp)
movq %r9, 0x8(%rsp)
movb (%rax,%r9), %al
movb %al, 0x7(%rsp)
xorl %r9d, %r9d
movl $0x0, %r13d
movabsq $0x1000000000000, %r8   # imm = 0x1000000000000
cmpq %r8, %rcx
jb 0xa6e9
addq %r10, %r11
addq %r11, %rbx
addq 0x18(%rsp), %rbx
movq %rbx, 0x48(%rsp)
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movl $0x14105, %r11d         # imm = 0x14105
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r9d, %r9d
movq %r15, 0x60(%rsp)
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movl (%rdi,%r10,4), %eax
movl %eax, %r8d
andl $0xf, %r8d
movl %eax, %r14d
shrl $0x4, %r14d
cmpl $0x9, %r8d
jne 0xa4dd
decl %edx
cmpq %rdx, %r10
je 0xa6e6
movl %r12d, %r13d
subl %r14d, %r13d
jl 0xa314
leal -0x1(%r9), %eax
testl %r9d, %r9d
jle 0xa6b8
movl %eax, %eax
xorl %ecx, %ecx
movl (%rsi,%rax,4), %edx
leal (%rdx,%rdx), %r8d
btl %r8d, %r11d
jb 0xa4ab
movl %ecx, %r8d
jmp 0xa4be
movl %edx, %r8d
shrl $0x4, %r8d
addl %ecx, %r8d
cmpl %r14d, %r8d
jge 0xa6ad
leaq -0x1(%rax), %rdx
decl %r9d
movl %r8d, %ecx
testq %rax, %rax
movq %rdx, %rax
jg 0xa499
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
jmp 0xa6b8
movslq %r9d, %rcx
incl %r9d
movl %eax, (%rsi,%rcx,4)
addl %r8d, %r8d
btl %r8d, %r11d
jb 0xa4f7
movl %r12d, %r13d
jmp 0xa6ca
movq %r10, 0x68(%rsp)
movq %rbx, 0x38(%rsp)
movq %r12, 0x30(%rsp)
cmpl %r12d, %ebx
movq 0x8(%rsp), %r10
je 0xa681
cmpl $0x10, %eax
jb 0xa681
movq 0x38(%rsp), %rdx
movslq %edx, %r8
movq 0x30(%rsp), %rdi
movslq %edi, %r15
movslq 0x14(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x48(%rsp), %rcx
leaq (%rcx,%r15), %rbp
leal (,%rdi,4), %eax
movl %edi, %edi
movq %rdi, 0x78(%rsp)
addq %rcx, %r8
movq %r8, 0x40(%rsp)
leal (,%rdx,4), %edi
movl %edx, %ecx
movq %rcx, 0x70(%rsp)
xorl %r11d, %r11d
movq %r14, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movq 0x70(%rsp), %rcx
addl %r11d, %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%r10,%rcx), %ebx
movl %edi, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %ebx
andl $0xf, %ebx
leaq (%r15,%r11), %rcx
movq 0x78(%rsp), %rdx
leal (%rdx,%r11), %r8d
sarl %r8d
movl %eax, %edx
andl $0x4, %edx
movslq %r8d, %r12
movzbl (%r10,%r12), %r13d
movq 0x80(%rsp), %rsi
cmpq %rsi, %rcx
jge 0xa5fb
movl %edx, %r8d
xorl $0x4, %r8d
movl %r13d, %r10d
movl %r8d, %ecx
shrl %cl, %r10d
andl $0xf, %r10d
movb (%rbp,%r11), %cl
movq 0x40(%rsp), %r15
movq %r15, %r14
movb (%r15,%r11), %r15b
cmpl %r10d, %ebx
jne 0xa627
movzbl %cl, %edx
movzbl %r15b, %r8d
cmpb %r15b, %cl
cmoval %edx, %r8d
movb %r8b, (%rbp,%r11)
jmp 0xa660
movl $0xf, %r8d
movl %edx, %ecx
shll %cl, %r8d
andl %r13d, %r8d
xorb $0x4, %dl
movl %edx, %ecx
shll %cl, %ebx
orl %r8d, %ebx
movb %bl, (%r10,%r12)
movq 0x40(%rsp), %rcx
movb (%rcx,%r11), %cl
movb %cl, (%rbp,%r11)
jmp 0xa66f
subb %r15b, %cl
jae 0xa65b
movl $0xf, %r10d
movl %edx, %ecx
shll %cl, %r10d
movl %r8d, %ecx
shll %cl, %ebx
andl %r13d, %r10d
orl %r10d, %ebx
movq 0x8(%rsp), %r10
movb %bl, (%r10,%r12)
movb (%r14,%r11), %cl
subb (%rbp,%r11), %cl
movb %cl, (%rbp,%r11)
jmp 0xa665
movb %cl, (%rbp,%r11)
movq 0x8(%rsp), %r10
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %r14
incq %r11
addl $0x4, %eax
addl $0x4, %edi
cmpq %r11, %r14
jne 0xa574
movq 0x38(%rsp), %rbx
addl %r14d, %ebx
movq 0x30(%rsp), %r13
addl %r14d, %r13d
movq 0x60(%rsp), %r15
movq 0x58(%rsp), %rdi
movq 0x68(%rsp), %r10
movl $0x14105, %r11d         # imm = 0x14105
movq 0x50(%rsp), %rsi
jmp 0xa6ca
subl %r14d, %ecx
shll $0x4, %ecx
addl %ecx, %edx
movl %edx, (%rsi,%rax,4)
cltq
cmpl $0x10, (%rsi,%rax,4)
cmovael %r9d, %eax
movl %eax, %r9d
movl %r12d, 0x14(%rsp)
incq %r10
movq 0x8(%r15), %rcx
movq %rcx, %rdx
shrq $0x30, %rdx
movl %r13d, %r12d
cmpq %rdx, %r10
jb 0xa459
jmp 0xa6e9
movl %r12d, %r13d
movq 0x8(%rsp), %rax
addq %rax, 0x18(%rsp)
cmpb $-0x1, 0x7(%rsp)
jne 0xa702
movq 0x18(%rsp), %rax
movb $-0x1, (%rax)
movl %r9d, %eax
cmpl $0x2, %r9d
jl 0xa741
movl $0x1, %ecx
movl -0x4(%rsi,%rcx,4), %edx
movl (%rsi,%rcx,4), %r10d
movl %edx, %r8d
xorl %r10d, %r8d
testb $0xf, %r8b
jne 0xa739
movl %edx, %r8d
andl $-0x10, %r8d
addl %r10d, %r8d
movl %r8d, (%rsi,%rcx,4)
andl $0xf, %edx
movl %edx, -0x4(%rsi,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0xa710
xorl %ebp, %ebp
movl $0x0, %ebx
testl %r9d, %r9d
jle 0xa76a
xorl %ecx, %ecx
xorl %ebx, %ebx
movl (%rsi,%rcx,4), %edx
cmpl $0x10, %edx
jb 0xa762
movslq %ebx, %r8
incl %ebx
movl %edx, (%rsi,%r8,4)
incq %rcx
cmpq %rcx, %rax
jne 0xa751
leal (,%rbx,4), %eax
movslq %eax, %r14
movq %r14, %rdx
callq 0x3370
movzbl 0xb(%r15), %r12d
addq 0x30(%r15), %r12
addq %r14, %r12
leal 0x1(%r13), %eax
sarl %eax
movslq %eax, %r14
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x3470
addq %r14, %r12
movslq %r13d, %r14
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x3470
movq 0x8(%r15), %rax
movq %rax, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x30(%r15), %rcx
addq %r14, %r12
shrl $0x18, %eax
movslq 0x10(%r15), %rdx
addq %rdx, %rax
leal 0x1(%rdx), %esi
shrl $0x1f, %esi
addl %esi, %edx
incl %edx
sarl %edx
movslq %edx, %rsi
addq %rax, %rsi
addq %rcx, %rsi
movslq 0x20(%r15), %rdx
movq %r12, %rdi
callq 0x3470
addl 0x20(%r15), %r12d
shlq $0x30, %rbx
movabsq $0x1000000000000, %rax  # imm = 0x1000000000000
decq %rax
andq 0x8(%r15), %rax
orq %rbx, %rax
movq %rax, 0x8(%r15)
movl %r14d, 0x10(%r15)
subl 0x30(%r15), %r12d
movl %r12d, 0x24(%r15)
jmp 0xa346