// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam_color.c
// Function: bam_aux_getCEi
char bam_aux_getCEi(bam1_t *b, int i)
{
	int cs_i;
	uint8_t *c = bam_aux_get(b, "CS");
	char *cs = NULL;
	char prev_b, cur_b;
	char cur_color, cor_color;

	// return the base if the tag was not found
	if(0 == c) return 0;
	
	cs = bam_aux2Z(c);

	// adjust for strandedness and leading adaptor
	if(bam1_strand(b)) { //reverse strand
		cs_i = strlen(cs) - 1 - i;
		// adjust for leading hard clip
		uint32_t cigar = bam1_cigar(b)[0];
		if((cigar & BAM_CIGAR_MASK) == BAM_CHARD_CLIP) {
			cs_i -= cigar >> BAM_CIGAR_SHIFT;
		}
		// get current color
		cur_color = cs[cs_i];
		// get previous base.  Note: must rc adaptor
		prev_b = (cs_i == 1) ? "TGCAN"[(int)bam_aux_nt2int(cs[0])] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i+1)];
		// get current base
		cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)]; 
	}
	else {
		cs_i=i+1;
		// get current color
		cur_color = cs[cs_i];
		// get previous base
		prev_b = (0 == i) ? cs[0] : bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i-1)];
		// get current base
		cur_b = bam_nt16_rev_table[bam1_seqi(bam1_seq(b), i)];
	}

	// corrected color
	cor_color = bam_aux_ntnt2cs(prev_b, cur_b);

	if(cur_color == cor_color) { 
		return '-';
	}
	else {
		return cur_color;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebx
movq %rdi, %r15
leaq 0x328d3(%rip), %rsi     # 0x4152b
callq 0x22374
testq %rax, %rax
je 0xeccc
movq %rax, %rdi
callq 0x23423
movq %rax, %r14
movq 0x8(%r15), %r12
btq $0x24, %r12
jb 0xecd3
movslq %ebx, %rdi
movq %r12, %rsi
shrq $0x2e, %rsi
andl $-0x4, %esi
leaq 0x42454(%rip), %rax     # 0x510e0
movq (%rax), %rax
movq 0x30(%r15), %r8
testl %ebx, %ebx
je 0xed8d
leaq (%r8,%rsi), %rcx
shrl $0x18, %r12d
addq %r12, %rcx
leal -0x1(%rbx), %edx
sarl %edx
movslq %edx, %rdx
movzbl (%rdx,%rcx), %edx
leal (,%rbx,4), %ecx
andl $0x4, %ecx
shrl %cl, %edx
andl $0xf, %edx
addq %rax, %rdx
xorl $0x4, %ecx
jmp 0xed99
xorl %eax, %eax
jmp 0xedce
movq %r14, %rdi
callq 0x3360
movl %ebx, %ecx
notl %ecx
addl %ecx, %eax
movq 0x30(%r15), %r15
movl %r12d, %r13d
shrl $0x18, %r13d
movl (%r15,%r13), %ecx
movl %ecx, %edx
andl $0xf, %edx
shrl $0x4, %ecx
xorl %esi, %esi
cmpl $0x5, %edx
cmovel %ecx, %esi
subl %esi, %eax
cltq
movb (%r14,%rax), %bpl
cmpl $0x1, %eax
jne 0xed41
movsbl (%r14), %edi
callq 0xebc7
movzbl %al, %eax
leaq 0x32816(%rip), %rdx     # 0x41536
addq %rax, %rdx
leaq 0x423b6(%rip), %rax     # 0x510e0
movq (%rax), %rax
shrq $0x2e, %r12
andl $-0x4, %r12d
leal (,%rbx,4), %ecx
andl $0x4, %ecx
jmp 0xed78
leaq 0x42398(%rip), %rax     # 0x510e0
shrq $0x30, %r12
leaq (%r15,%r12,4), %rcx
addq %r13, %rcx
leal 0x1(%rbx), %edx
sarl %edx
movslq %edx, %rdx
movzbl (%rdx,%rcx), %edx
leal (,%rbx,4), %ecx
andl $0x4, %ecx
shrl %cl, %edx
movq (%rax), %rax
shlq $0x2, %r12
andl $0xf, %edx
addq %rax, %rdx
addq %r13, %r15
addq %r12, %r15
sarl %ebx
movslq %ebx, %rsi
movzbl (%rsi,%r15), %esi
xorb $0x4, %cl
jmp 0xedae
shrl $0x18, %r12d
movl $0x4, %ecx
movq %r14, %rdx
movb 0x1(%r14,%rdi), %bpl
addq %rsi, %r8
addq %r12, %r8
sarl %ebx
movslq %ebx, %rsi
movzbl (%rsi,%r8), %esi
shrl %cl, %esi
andl $0xf, %esi
movsbl (%rax,%rsi), %esi
movsbl (%rdx), %edi
callq 0xec05
cmpb %al, %bpl
movzbl %bpl, %ecx
movl $0x2d, %eax
cmovnel %ecx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)