// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq 0x8(%rdi), %rcx
xorl %ebp, %ebp
btq $0x22, %rcx
jb 0xa499
movq %rdi, %r12
movq 0x30(%rdi), %rax
movl %ecx, %edi
shrl $0x18, %edi
addq %rax, %rdi
xorl %ebp, %ebp
shrq $0x30, %rcx
movl $0x0, %edx
je 0x9f36
xorl %edx, %edx
movl (%rdi,%rdx,4), %esi
andl $0xf, %esi
cmpl $0x9, %esi
je 0x9f36
incq %rdx
cmpq %rdx, %rcx
jne 0x9f21
movl %ecx, %edx
cmpl %ecx, %edx
je 0xa499
movl (%rdi), %edx
andl $0xf, %edx
cmpl $0x9, %edx
jne 0x9f61
movabsq $0x400000000, %rax      # imm = 0x400000000
orq %rax, 0x8(%r12)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xa499
movl 0x24(%r12), %edx
leal (%rdx,%rcx,4), %esi
addl $0x4, %esi
cmpl 0x28(%r12), %esi
jle 0x9fc0
leal (,%rcx,4), %ecx
addl %edx, %ecx
decl %ecx
movl %ecx, %edx
sarl %edx
orl %ecx, %edx
movl %edx, %ecx
sarl $0x2, %ecx
orl %edx, %ecx
movl %ecx, %edx
sarl $0x4, %edx
orl %ecx, %edx
movl %edx, %ecx
sarl $0x8, %ecx
orl %edx, %ecx
movl %ecx, %edx
sarl $0x10, %edx
orl %ecx, %edx
incl %edx
movl %edx, 0x28(%r12)
movslq %edx, %rsi
movq %rax, %rdi
callq 0x3430
movq %rax, 0x30(%r12)
movzbl 0xb(%r12), %edi
addq %rax, %rdi
movq 0x30(%r12), %rdx
movl 0x28(%r12), %esi
movq 0x8(%r12), %rax
movq %rax, %rcx
shrq $0x2e, %rcx
andl $-0x4, %ecx
subl %ecx, %esi
movslq %esi, %r13
addq %rdx, %r13
movl %eax, %r8d
shrl $0x18, %r8d
addq %rdx, %r8
movq %r8, 0x48(%rsp)
leaq (%r8,%rcx), %r9
movl 0x10(%r12), %edx
incl %edx
sarl %edx
movslq %edx, %rdx
movq %rdx, 0x20(%rsp)
movb (%rdx,%r9), %dl
movb %dl, 0x7(%rsp)
xorl %esi, %esi
movl $0x0, %r14d
movabsq $0x1000000000000, %rdx  # imm = 0x1000000000000
cmpq %rdx, %rax
movq %r9, 0x10(%rsp)
jb 0xa354
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
shrq $0x30, %rax
addq 0x20(%rsp), %rcx
addq %rcx, 0x48(%rsp)
movl $0xffffffff, 0x1c(%rsp) # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movq $0x0, 0x28(%rsp)
xorl %ecx, %ecx
xorl %esi, %esi
movq %rdi, 0x50(%rsp)
movq %r13, 0x40(%rsp)
movq %r12, 0x58(%rsp)
movq %rcx, 0x8(%rsp)
movl (%rdi,%r10,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
movl %ecx, %r11d
shrl $0x4, %r11d
cmpl $0x9, %edx
jne 0xa0b0
decl %eax
movl $0x6, %ebx
cmpq %rax, %r10
je 0xa0c8
movl $0x5, %ebx
movq 0x8(%rsp), %rax
movl %eax, %r14d
subl %r11d, %r14d
jge 0xa275
movl %eax, %r14d
jmp 0xa324
movslq %esi, %rax
incl %esi
movl %ecx, (%r13,%rax,4)
addl %edx, %edx
xorl %ebx, %ebx
movl $0x14105, %eax          # imm = 0x14105
btl %edx, %eax
jb 0xa0d2
movq 0x8(%rsp), %r14
jmp 0xa324
movq %rsi, 0x60(%rsp)
movq %r10, 0x30(%rsp)
movq 0x8(%rsp), %rax
cmpl %eax, 0x28(%rsp)
je 0xa236
cmpl $0x10, %ecx
jb 0xa236
movq 0x28(%rsp), %rcx
movslq %ecx, %r15
movq 0x8(%rsp), %rdx
movslq %edx, %r8
movslq 0x1c(%rsp), %rsi
leal (,%rdx,4), %r13d
movq 0x48(%rsp), %rax
leaq (%rax,%r8), %rbx
movl %edx, %edx
movq %rdx, 0x70(%rsp)
leal (,%rcx,4), %r12d
addq %rax, %r15
movl %ecx, %eax
movq %rax, 0x68(%rsp)
xorl %r10d, %r10d
movq %rsi, 0x38(%rsp)
movq 0x68(%rsp), %rax
addl %r10d, %eax
sarl %eax
cltq
movzbl (%r9,%rax), %edi
movl %r12d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edi
andl $0xf, %edi
leaq (%r8,%r10), %rcx
movq 0x70(%rsp), %rax
leal (%rax,%r10), %edx
sarl %edx
movl %r13d, %eax
andl $0x4, %eax
movslq %edx, %rbp
movzbl (%r9,%rbp), %r14d
cmpq %rsi, %rcx
jge 0xa1b5
movq %r8, %rsi
movq %r11, %r8
movl %eax, %edx
xorl $0x4, %edx
movl %r14d, %r9d
movl %edx, %ecx
shrl %cl, %r9d
andl $0xf, %r9d
movb (%rbx,%r10), %cl
movq %r15, %r11
movb (%r15,%r10), %r15b
cmpl %r9d, %edi
jne 0xa1d7
movzbl %cl, %eax
movzbl %r15b, %edx
cmpb %r15b, %cl
cmoval %eax, %edx
movb %dl, (%rbx,%r10)
jmp 0xa20f
movl $0xf, %edx
movl %eax, %ecx
shll %cl, %edx
andl %r14d, %edx
xorb $0x4, %al
movl %eax, %ecx
shll %cl, %edi
orl %edx, %edi
movb %dil, (%r9,%rbp)
movb (%r15,%r10), %al
movb %al, (%rbx,%r10)
jmp 0xa222
subb %r15b, %cl
jae 0xa20b
movl $0xf, %r9d
movl %eax, %ecx
shll %cl, %r9d
movl %edx, %ecx
shll %cl, %edi
andl %r14d, %r9d
orl %r9d, %edi
movq 0x10(%rsp), %r9
movb %dil, (%r9,%rbp)
movq %r11, %r15
movb (%r11,%r10), %al
subb (%rbx,%r10), %al
movb %al, (%rbx,%r10)
jmp 0xa217
movb %cl, (%rbx,%r10)
movq 0x10(%rsp), %r9
movq %r11, %r15
movq %r8, %r11
movq %rsi, %r8
movq 0x38(%rsp), %rsi
incq %r10
addl $0x4, %r13d
addl $0x4, %r12d
cmpq %r10, %r11
jne 0xa13b
movq 0x28(%rsp), %rax
addl %r11d, %eax
movq %rax, 0x28(%rsp)
movq 0x8(%rsp), %r14
addl %r11d, %r14d
movq 0x58(%rsp), %r12
movq 0x50(%rsp), %rdi
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
movq 0x30(%rsp), %r10
xorl %ebx, %ebx
movq 0x40(%rsp), %r13
movq 0x60(%rsp), %rsi
jmp 0xa324
movq %r10, 0x30(%rsp)
movl %esi, %ecx
movl %esi, %eax
sarl $0x1f, %eax
andl %esi, %eax
movq %rcx, %r10
shlq $0x20, %r10
addq %rbp, %r10
decq %rcx
xorl %edi, %edi
movq %r10, %rdx
testl %esi, %esi
jle 0xa300
movl %ecx, %r10d
movl (%r13,%r10,4), %r15d
leal (%r15,%r15), %ebx
movb $0x1, %r9b
movl $0x14105, %r13d         # imm = 0x14105
btl %ebx, %r13d
jb 0xa2bd
movl %edi, %ebx
movq 0x40(%rsp), %r13
jmp 0xa2e6
movl %r15d, %ebx
shrl $0x4, %ebx
addl %edi, %ebx
cmpl %r11d, %ebx
movq 0x40(%rsp), %r13
jl 0xa2e6
movl %edi, %r9d
subl %r11d, %r9d
shll $0x4, %r9d
addl %r9d, %r15d
movl %r15d, (%r13,%r10,4)
xorl %r9d, %r9d
movl %edi, %ebx
decl %esi
leaq (%rdx,%rbp), %r10
decq %rcx
movl %ebx, %edi
testb %r9b, %r9b
movq 0x10(%rsp), %r9
jne 0xa292
leal 0x1(%rsi), %eax
jmp 0xa302
decl %esi
sarq $0x1e, %rdx
cmpl $0x10, (%r13,%rdx)
cmovael %eax, %esi
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movl %eax, 0x1c(%rsp)
movq 0x50(%rsp), %rdi
movq 0x30(%rsp), %r10
testl %ebx, %ebx
jne 0xa342
incq %r10
movq 0x8(%r12), %rax
shrq $0x30, %rax
movl %r14d, %ecx
cmpq %rax, %r10
jb 0xa06c
jmp 0xa354
cmpl $0x5, %ebx
je 0x9f48
cmpl $0x6, %ebx
jne 0xa4aa
movq 0x20(%rsp), %r11
addq %r9, %r11
cmpb $-0x1, 0x7(%rsp)
jne 0xa367
movb $-0x1, (%r11)
movl %esi, %eax
cmpl $0x2, %esi
jl 0xa3a8
movl $0x1, %ecx
movl -0x4(%r13,%rcx,4), %edx
movl (%r13,%rcx,4), %r10d
movl %edx, %r9d
xorl %r10d, %r9d
testb $0xf, %r9b
jne 0xa3a0
movl %edx, %r9d
andl $-0x10, %r9d
addl %r10d, %r9d
movl %r9d, (%r13,%rcx,4)
andl $0xf, %edx
movl %edx, -0x4(%r13,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0xa373
movq %r11, 0x20(%rsp)
xorl %ebp, %ebp
movl $0x0, %ebx
testl %esi, %esi
jle 0xa3d8
xorl %ecx, %ecx
xorl %ebx, %ebx
movl (%r13,%rcx,4), %edx
cmpl $0x10, %edx
jb 0xa3d0
movslq %ebx, %r8
incl %ebx
movl %edx, (%r13,%r8,4)
incq %rcx
cmpq %rcx, %rax
jne 0xa3bc
leal (,%rbx,4), %eax
movq %r13, %rsi
movslq %eax, %r13
movq %r13, %rdx
callq 0x3370
movq %r12, %r15
movzbl 0xb(%r12), %r12d
addq 0x30(%r15), %r12
addq %r13, %r12
leal 0x1(%r14), %eax
sarl %eax
movslq %eax, %r13
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x3470
addq %r13, %r12
movslq %r14d, %r14
movq %r12, %rdi
movq 0x20(%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)
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa499