// File: /workspace/github_c_makefile_star2_O2/skoren[P]bacValidation/bamcat.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:
movq 0x8(%rdi), %rsi
xorl %ecx, %ecx
btq $0x22, %rsi
jb 0x601e
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r14
movabsq $0x400000000, %r11      # imm = 0x400000000
movq 0x30(%rdi), %rax
movl %esi, %r10d
shrl $0x18, %r10d
leaq (%rax,%r10), %rdi
movl 0x10(%r14), %edx
xorl %ecx, %ecx
xorl %r9d, %r9d
cmpq %r9, %rdx
je 0x600d
movl (%rdi,%r9,4), %r8d
andl $0xf, %r8d
cmpl $0x9, %r8d
je 0x5b72
incq %r9
jmp 0x5b56
cmpl %r9d, %edx
je 0x600d
movl (%rdi), %ecx
andl $0xf, %ecx
cmpl $0x9, %ecx
jne 0x5b94
orq %r11, %rsi
movq %rsi, 0x8(%r14)
pushq $-0x1
popq %rcx
jmp 0x600d
movl 0x28(%r14), %ecx
movl 0x2c(%r14), %r13d
leal (%rcx,%rdx,4), %r8d
addl $0x4, %r8d
cmpl %r13d, %r8d
jbe 0x5bf8
leal (%rcx,%rdx,4), %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, 0x2c(%r14)
movslq %edx, %rsi
movq %rax, %rdi
callq 0x3590
movq %rax, 0x30(%r14)
movzbl 0xb(%r14), %r10d
movq %rax, %rdi
addq %r10, %rdi
movl 0x10(%r14), %edx
movl 0x2c(%r14), %r13d
leal (,%rdx,4), %ecx
pushq $-0x1
popq %r8
movl %r8d, -0x4c(%rbp)
subl %ecx, %r13d
addq %rax, %r13
movl %ecx, %ecx
leaq (%rax,%r10), %r15
addq %rcx, %r15
movl 0x14(%r14), %r8d
incl %r8d
sarl %r8d
movslq %r8d, %r8
leaq (%r15,%r8), %r9
movq %r9, -0x80(%rbp)
movb (%r8,%r15), %r9b
movb %r9b, -0x29(%rbp)
addq %rcx, %r10
addq %rax, %r10
addq %r8, %r10
movq %r10, -0x90(%rbp)
xorl %r8d, %r8d
movq $0x0, -0x40(%rbp)
xorl %r12d, %r12d
movq $0x0, -0x58(%rbp)
movq %r15, -0x38(%rbp)
movq %r14, -0xa0(%rbp)
movq %rdi, -0x98(%rbp)
movq %r13, -0x60(%rbp)
movl %edx, %ecx
movslq -0x40(%rbp), %rax
movslq %r12d, %rsi
movq %rsi, -0x78(%rbp)
cmpq %rcx, %r8
jae 0x5efb
movl (%rdi,%r8,4), %r9d
movl %r9d, %ecx
andl $0xf, %ecx
movl %r9d, %esi
shrl $0x4, %esi
movq %rsi, -0x48(%rbp)
cmpl $0x9, %ecx
movq %r8, -0xa8(%rbp)
jne 0x5d4d
decl %edx
cmpq %rdx, %r8
je 0x5efb
movl %r12d, %eax
subl -0x48(%rbp), %eax
jl 0x6021
movq %r12, %r8
movl -0x40(%rbp), %ecx
xorl %r11d, %r11d
movl %r11d, %edx
movq %rcx, %r11
shlq $0x20, %r11
movabsq $-0x100000000, %r9      # imm = 0xFFFFFFFF00000000
addq %r9, %r11
movq %r11, %r9
movl %ecx, %r15d
testl %ecx, %ecx
jle 0x5d2d
movl -0x4(%r13,%rcx,4), %r10d
leal (%r10,%r10), %ebx
decq %rcx
movabsq $-0x100000000, %r11     # imm = 0xFFFFFFFF00000000
addq %r9, %r11
movl $0x14105, %r12d         # imm = 0x14105
btl %ebx, %r12d
jae 0x5ce0
movl %r10d, %r11d
shrl $0x4, %r11d
addl %edx, %r11d
cmpl -0x48(%rbp), %r11d
jb 0x5cc9
subl -0x48(%rbp), %edx
shll $0x4, %edx
addl %edx, %r10d
movl %r10d, (%r13,%rcx,4)
sarq $0x1e, %r9
cmpl $0x10, (%r13,%r9)
leal -0x1(%r15), %ecx
cmovael %r15d, %ecx
movq %rcx, -0x40(%rbp)
movl %r8d, -0x4c(%rbp)
movq -0x38(%rbp), %r15
jmp 0x5d9c
leal 0x1(%rax), %edx
movq %rdx, -0x40(%rbp)
movl %r9d, (%r13,%rax,4)
addl %ecx, %ecx
movl $0x14105, %eax          # imm = 0x14105
btl %ecx, %eax
jb 0x5d6a
movl %r12d, %eax
jmp 0x5d9c
movq %r12, -0x88(%rbp)
cmpl %r12d, -0x58(%rbp)
jne 0x5db2
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
addl %ecx, %eax
movq %rax, -0x58(%rbp)
movq -0x88(%rbp), %rax
addl %ecx, %eax
movq -0xa0(%rbp), %r14
movq -0x98(%rbp), %rdi
movq -0xa8(%rbp), %r8
incq %r8
movl 0x10(%r14), %edx
movl %eax, %r12d
jmp 0x5c6f
movslq -0x4c(%rbp), %r11
movq -0x58(%rbp), %rcx
movslq %ecx, %rdx
movq -0x90(%rbp), %rax
addq %rax, %rdx
movq %rdx, -0x70(%rbp)
leal (,%rcx,4), %esi
movl %ecx, %ecx
movq %rcx, -0xb8(%rbp)
movq -0x78(%rbp), %rcx
leaq (%rax,%rcx), %r9
movq -0x88(%rbp), %rax
leal (,%rax,4), %edi
movl %eax, %eax
movq %rax, -0xb0(%rbp)
xorl %r12d, %r12d
movq %r11, -0x68(%rbp)
cmpq %r12, -0x48(%rbp)
je 0x5d77
movq -0xb8(%rbp), %rax
addl %r12d, %eax
sarl %eax
cltq
movzbl (%r15,%rax), %ebx
movl %esi, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %ebx
andl $0xf, %ebx
movq -0x78(%rbp), %rax
leaq (%rax,%r12), %rcx
movq -0xb0(%rbp), %rax
leal (%rax,%r12), %edx
sarl %edx
movl %edi, %eax
andl $0x4, %eax
movslq %edx, %r14
movzbl (%r15,%r14), %r10d
cmpq %r11, %rcx
jge 0x5e87
movl %eax, %edx
xorl $0x4, %edx
movl %r10d, %r11d
movl %edx, %ecx
shrl %cl, %r11d
andl $0xf, %r11d
movb (%r9,%r12), %cl
movq -0x70(%rbp), %r13
movb (%r13,%r12), %r13b
cmpl %r11d, %ebx
jne 0x5eab
movzbl %cl, %eax
movzbl %r13b, %edx
cmpb %r13b, %cl
cmoval %eax, %edx
movb %dl, (%r9,%r12)
jmp 0x5ee1
pushq $0xf
popq %rdx
movl %eax, %ecx
shll %cl, %edx
andl %r10d, %edx
xorb $0x4, %al
movl %eax, %ecx
shll %cl, %ebx
orl %edx, %ebx
movb %bl, (%r15,%r14)
movq -0x70(%rbp), %rax
movb (%rax,%r12), %al
movb %al, (%r9,%r12)
jmp 0x5eed
subb %r13b, %cl
jae 0x5edd
pushq $0xf
popq %r11
movl %eax, %ecx
shll %cl, %r11d
movl %edx, %ecx
shll %cl, %ebx
andl %r10d, %r11d
orl %r11d, %ebx
movq -0x38(%rbp), %r15
movb %bl, (%r15,%r14)
movq -0x70(%rbp), %rax
movb (%rax,%r12), %al
subb (%r9,%r12), %al
movb %al, (%r9,%r12)
jmp 0x5ee5
movb %cl, (%r9,%r12)
movq -0x38(%rbp), %r15
movq -0x68(%rbp), %r11
movq -0x60(%rbp), %r13
incq %r12
addl $0x4, %esi
addl $0x4, %edi
jmp 0x5e01
cmpb $-0x1, -0x29(%rbp)
jne 0x5f08
movq -0x80(%rbp), %rcx
movb $-0x1, (%rcx)
pushq $0x1
popq %rcx
movq -0x40(%rbp), %r15
cmpq %rax, %rcx
jge 0x5f46
movl -0x4(%r13,%rcx,4), %edx
movl (%r13,%rcx,4), %r9d
movl %edx, %r8d
xorl %r9d, %r8d
testb $0xf, %r8b
jne 0x5f41
movl %edx, %r8d
andl $-0x10, %r8d
addl %r9d, %r8d
movl %r8d, (%r13,%rcx,4)
andl $0xf, %edx
movl %edx, -0x4(%r13,%rcx,4)
incq %rcx
jmp 0x5f0f
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
xorl %ebx, %ebx
cmpq %rax, %r15
je 0x5f6f
movl (%r13,%rax,4), %ecx
cmpl $0x10, %ecx
jb 0x5f6a
movslq %ebx, %rdx
incl %ebx
movl %ecx, (%r13,%rdx,4)
incq %rax
jmp 0x5f51
leal (,%rbx,4), %eax
movslq %eax, %rdx
movq %r13, %rsi
movq %rdx, %r15
callq 0x3260
movzbl 0xb(%r14), %r13d
addq 0x30(%r14), %r13
addq %r15, %r13
leal 0x1(%r12), %eax
sarl %eax
movslq %eax, %r15
movq %r13, %rdi
movq -0x38(%rbp), %rsi
movq %r15, %rdx
callq 0x3570
addq %r15, %r13
movq %r13, %rdi
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %r15
movq %r15, %rdx
callq 0x3570
addq %r15, %r13
movl 0x10(%r14), %eax
shll $0x2, %eax
addq 0x30(%r14), %rax
movzbl 0xb(%r14), %ecx
movslq 0x14(%r14), %rdx
addq %rdx, %rcx
addq %rax, %rcx
leal 0x1(%rdx), %eax
pushq $0x2
popq %rsi
cltd
idivl %esi
movslq %eax, %rsi
addq %rcx, %rsi
movslq 0x24(%r14), %rdx
movq %r13, %rdi
callq 0x3570
addl 0x24(%r14), %r13d
movl %ebx, 0x10(%r14)
movl %r12d, 0x14(%r14)
subl 0x30(%r14), %r13d
movl %r13d, 0x28(%r14)
xorl %ecx, %ecx
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movl %ecx, %eax
retq
movq 0x8(%r14), %rsi
movabsq $0x400000000, %r11      # imm = 0x400000000
jmp 0x5b85