// File: /workspace/github_c_makefile_star2_O1/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 0x31bab(%rip), %rsi     # 0x404eb
callq 0x21c82
testq %rax, %rax
je 0xe9c6
movq %rax, %rdi
callq 0x22aae
movq %rax, %r14
movq 0x8(%r15), %r12
btq $0x24, %r12
jb 0xe9cd
movslq %ebx, %rdx
movq %r14, %rax
testl %ebx, %ebx
je 0xe9a6
movq %r12, %rax
shrq $0x30, %rax
shlq $0x2, %rax
addq 0x30(%r15), %rax
movl %r12d, %ecx
shrl $0x18, %ecx
addq %rax, %rcx
leal -0x1(%rbx), %eax
sarl %eax
cltq
movzbl (%rax,%rcx), %eax
leal (,%rbx,4), %ecx
andb $0x4, %cl
shrl %cl, %eax
leaq 0x41740(%rip), %rcx     # 0x500e0
andl $0xf, %eax
addq (%rcx), %rax
movb 0x1(%r14,%rdx), %bpl
movq %r12, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x30(%r15), %rcx
shrl $0x18, %r12d
addq %rcx, %r12
jmp 0xea5d
xorl %eax, %eax
jmp 0xea9d
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 0xea1f
movsbl (%r14), %edi
callq 0xe8af
movzbl %al, %ecx
leaq 0x31adc(%rip), %rax     # 0x404f6
addq %rcx, %rax
jmp 0xea52
movq %r12, %rax
shrq $0x30, %rax
leaq (%r15,%rax,4), %rax
addq %r13, %rax
leal 0x1(%rbx), %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%rcx,%rax), %eax
leal (,%rbx,4), %ecx
andb $0x4, %cl
shrl %cl, %eax
leaq 0x41694(%rip), %rcx     # 0x500e0
andl $0xf, %eax
addq (%rcx), %rax
shrq $0x30, %r12
leaq (%r15,%r12,4), %r12
addq %r13, %r12
leaq 0x4167c(%rip), %rdx     # 0x500e0
movl %ebx, %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%r12,%rcx), %esi
shll $0x2, %ebx
notb %bl
andb $0x4, %bl
movl %ebx, %ecx
shrl %cl, %esi
movq (%rdx), %rcx
andl $0xf, %esi
movsbl (%rcx,%rsi), %esi
movsbl (%rax), %edi
callq 0xe8ed
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