// File: /workspace/github_c_makefile_star2_O0/DReichLab[P]adna/sam.c
// Function: resolve_cigar2
static inline int resolve_cigar2(bam_pileup1_t *p, int32_t pos, cstate_t *s)
{
#define _cop(c) ((c)&BAM_CIGAR_MASK)
#define _cln(c) ((c)>>BAM_CIGAR_SHIFT)

	bam1_t *b = p->b;
	bam1_core_t *c = &b->core;
	uint32_t *cigar = bam_get_cigar(b);
	int k;
	// determine the current CIGAR operation
//	fprintf(stderr, "%s\tpos=%d\tend=%d\t(%d,%d,%d)\n", bam1_qname(b), pos, s->end, s->k, s->x, s->y);
	if (s->k == -1) { // never processed
		if (c->n_cigar == 1) { // just one operation, save a loop
		  if (_cop(cigar[0]) == BAM_CMATCH || _cop(cigar[0]) == BAM_CEQUAL || _cop(cigar[0]) == BAM_CDIFF) s->k = 0, s->x = c->pos, s->y = 0;
		} else { // find the first match or deletion
			for (k = 0, s->x = c->pos, s->y = 0; k < c->n_cigar; ++k) {
				int op = _cop(cigar[k]);
				int l = _cln(cigar[k]);
				if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CEQUAL || op == BAM_CDIFF) break;
				else if (op == BAM_CREF_SKIP) s->x += l;
				else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) s->y += l;
			}
			assert(k < c->n_cigar);
			s->k = k;
		}
	} else { // the read has been processed before
		int op, l = _cln(cigar[s->k]);
		if (pos - s->x >= l) { // jump to the next operation
			assert(s->k < c->n_cigar); // otherwise a bug: this function should not be called in this case
			op = _cop(cigar[s->k+1]);
			if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP || op == BAM_CEQUAL || op == BAM_CDIFF) { // jump to the next without a loop
			  if (_cop(cigar[s->k]) == BAM_CMATCH|| _cop(cigar[s->k]) == BAM_CEQUAL || _cop(cigar[s->k]) == BAM_CDIFF) s->y += l;
				s->x += l;
				++s->k;
			} else { // find the next M/D/N/=/X
			  if (_cop(cigar[s->k]) == BAM_CMATCH|| _cop(cigar[s->k]) == BAM_CEQUAL || _cop(cigar[s->k]) == BAM_CDIFF) s->y += l;
				s->x += l;
				for (k = s->k + 1; k < c->n_cigar; ++k) {
					op = _cop(cigar[k]), l = _cln(cigar[k]);
					if (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CREF_SKIP || op == BAM_CEQUAL || op == BAM_CDIFF) break;
					else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) s->y += l;
				}
				s->k = k;
			}
			assert(s->k < c->n_cigar); // otherwise a bug
		} // else, do nothing
	}
	{ // collect pileup information
		int op, l;
		op = _cop(cigar[s->k]); l = _cln(cigar[s->k]);
		p->is_del = p->indel = p->is_refskip = 0;
		if (s->x + l - 1 == pos && s->k + 1 < c->n_cigar) { // peek the next operation
			int op2 = _cop(cigar[s->k+1]);
			int l2 = _cln(cigar[s->k+1]);
			if (op2 == BAM_CDEL) p->indel = -(int)l2;
			else if (op2 == BAM_CINS) p->indel = l2;
			else if (op2 == BAM_CPAD && s->k + 2 < c->n_cigar) { // no working for adjacent padding
				int l3 = 0;
				for (k = s->k + 2; k < c->n_cigar; ++k) {
					op2 = _cop(cigar[k]); l2 = _cln(cigar[k]);
					if (op2 == BAM_CINS) l3 += l2;
					else if (op2 == BAM_CDEL || op2 == BAM_CMATCH || op2 == BAM_CREF_SKIP || op2 == BAM_CEQUAL || op2 == BAM_CDIFF) break;
				}
				if (l3 > 0) p->indel = l3;
			}
		}
		if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
			p->qpos = s->y + (pos - s->x);
		} else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
			p->is_del = 1; p->qpos = s->y; // FIXME: distinguish D and N!!!!!
			p->is_refskip = (op == BAM_CREF_SKIP);
		} // cannot be other operations; otherwise a bug
		p->is_head = (pos == c->pos); p->is_tail = (pos == s->end);
	}
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x18, %rcx
andq $0xff, %rcx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
cmpl $-0x1, (%rax)
jne 0x11b7e
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x30, %rax
cmpl $0x1, %eax
jne 0x11a8b
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x11a63
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x11a63
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x11a86
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0x11b79
movl $0x0, -0x34(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x8(%rax)
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x11b3c
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x11af8
cmpl $0x2, -0x38(%rbp)
je 0x11af8
cmpl $0x7, -0x38(%rbp)
je 0x11af8
cmpl $0x8, -0x38(%rbp)
jne 0x11afa
jmp 0x11b3c
cmpl $0x3, -0x38(%rbp)
jne 0x11b0f
movl -0x3c(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
jmp 0x11b2a
cmpl $0x1, -0x38(%rbp)
je 0x11b1b
cmpl $0x4, -0x38(%rbp)
jne 0x11b28
movl -0x3c(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
jmp 0x11b2a
jmp 0x11b2c
jmp 0x11b2e
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x11aab
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x11b51
jmp 0x11b70
leaq 0x1cf9(%rip), %rdi      # 0x13851
leaq 0x1cb0(%rip), %rsi      # 0x1380f
movl $0x38c, %edx            # imm = 0x38C
leaq 0x1caa(%rip), %rcx      # 0x13815
callq 0x2180
movl -0x34(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0x11da8
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, -0x44(%rbp)
movl -0xc(%rbp), %eax
movq -0x18(%rbp), %rcx
subl 0x4(%rcx), %eax
cmpl -0x44(%rbp), %eax
jl 0x11da6
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x11bbd
jmp 0x11bdc
leaq 0x1c8a(%rip), %rdi      # 0x1384e
leaq 0x1c44(%rip), %rsi      # 0x1380f
movl $0x392, %edx            # imm = 0x392
leaq 0x1c3e(%rip), %rcx      # 0x13815
callq 0x2180
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x11c13
cmpl $0x2, -0x40(%rbp)
je 0x11c13
cmpl $0x3, -0x40(%rbp)
je 0x11c13
cmpl $0x7, -0x40(%rbp)
je 0x11c13
cmpl $0x8, -0x40(%rbp)
jne 0x11c7f
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x11c55
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x11c55
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x11c62
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x11d6d
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x11cc1
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x11cc1
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x11cce
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x11d64
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x11d3a
cmpl $0x2, -0x40(%rbp)
je 0x11d3a
cmpl $0x3, -0x40(%rbp)
je 0x11d3a
cmpl $0x7, -0x40(%rbp)
je 0x11d3a
cmpl $0x8, -0x40(%rbp)
jne 0x11d3c
jmp 0x11d64
cmpl $0x1, -0x40(%rbp)
je 0x11d48
cmpl $0x4, -0x40(%rbp)
jne 0x11d55
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
jmp 0x11d57
jmp 0x11d59
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x11ce7
movl -0x34(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x11d85
jmp 0x11da4
leaq 0x1ac2(%rip), %rdi      # 0x1384e
leaq 0x1a7c(%rip), %rsi      # 0x1380f
movl $0x3a2, %edx            # imm = 0x3A2
leaq 0x1a76(%rip), %rcx      # 0x13815
callq 0x2180
jmp 0x11da6
jmp 0x11da8
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %ecx
andl $-0x9, %ecx
orl $0x0, %ecx
movl %ecx, 0x14(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %ecx
andl $-0x2, %ecx
orl $0x0, %ecx
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
addl -0x4c(%rbp), %eax
subl $0x1, %eax
cmpl -0xc(%rbp), %eax
jne 0x11f53
movq -0x18(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x11f53
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, -0x54(%rbp)
cmpl $0x2, -0x50(%rbp)
jne 0x11e77
xorl %ecx, %ecx
subl -0x54(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0x11f51
cmpl $0x1, -0x50(%rbp)
jne 0x11e8c
movl -0x54(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0x11f4f
cmpl $0x6, -0x50(%rbp)
jne 0x11f4d
movq -0x18(%rbp), %rax
movl (%rax), %eax
addl $0x2, %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x11f4d
movl $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x11f3b
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, -0x54(%rbp)
cmpl $0x1, -0x50(%rbp)
jne 0x11f0c
movl -0x54(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x11f2e
cmpl $0x2, -0x50(%rbp)
je 0x11f2a
cmpl $0x0, -0x50(%rbp)
je 0x11f2a
cmpl $0x3, -0x50(%rbp)
je 0x11f2a
cmpl $0x7, -0x50(%rbp)
je 0x11f2a
cmpl $0x8, -0x50(%rbp)
jne 0x11f2c
jmp 0x11f3b
jmp 0x11f2e
jmp 0x11f30
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x11ec6
cmpl $0x0, -0x58(%rbp)
jle 0x11f4b
movl -0x58(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0x11f4d
jmp 0x11f4f
jmp 0x11f51
jmp 0x11f53
cmpl $0x0, -0x48(%rbp)
je 0x11f65
cmpl $0x7, -0x48(%rbp)
je 0x11f65
cmpl $0x8, -0x48(%rbp)
jne 0x11f81
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movl -0xc(%rbp), %eax
movq -0x18(%rbp), %rdx
subl 0x4(%rdx), %eax
addl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0x11fce
cmpl $0x2, -0x48(%rbp)
je 0x11f8d
cmpl $0x3, -0x48(%rbp)
jne 0x11fcc
movq -0x8(%rbp), %rax
movl 0x14(%rax), %ecx
andl $-0x2, %ecx
orl $0x1, %ecx
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x8(%rax)
cmpl $0x3, -0x48(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %edx
movq -0x8(%rbp), %rax
movl 0x14(%rax), %ecx
andl $0x1, %edx
shll $0x3, %edx
andl $-0x9, %ecx
orl %edx, %ecx
movl %ecx, 0x14(%rax)
jmp 0x11fce
movl -0xc(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x4(%rcx), %eax
sete %al
andb $0x1, %al
movzbl %al, %edx
movq -0x8(%rbp), %rax
movl 0x14(%rax), %ecx
andl $0x1, %edx
shll %edx
andl $-0x3, %ecx
orl %edx, %ecx
movl %ecx, 0x14(%rax)
movl -0xc(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0xc(%rcx), %eax
sete %al
andb $0x1, %al
movzbl %al, %edx
movq -0x8(%rbp), %rax
movl 0x14(%rax), %ecx
andl $0x1, %edx
shll $0x2, %edx
andl $-0x5, %ecx
orl %edx, %ecx
movl %ecx, 0x14(%rax)
movl $0x1, %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)