// File: karel-brinda[P]ococo/ext/htslib/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", bam_get_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 0x21d8e
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x30, %rax
cmpl $0x1, %eax
jne 0x21c9b
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x21c73
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x21c73
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x21c96
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 0x21d89
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 0x21d4c
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 0x21d08
cmpl $0x2, -0x38(%rbp)
je 0x21d08
cmpl $0x7, -0x38(%rbp)
je 0x21d08
cmpl $0x8, -0x38(%rbp)
jne 0x21d0a
jmp 0x21d4c
cmpl $0x3, -0x38(%rbp)
jne 0x21d1f
movl -0x3c(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
jmp 0x21d3a
cmpl $0x1, -0x38(%rbp)
je 0x21d2b
cmpl $0x4, -0x38(%rbp)
jne 0x21d38
movl -0x3c(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
jmp 0x21d3a
jmp 0x21d3c
jmp 0x21d3e
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x21cbb
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x21d61
jmp 0x21d80
leaq 0x59c7a(%rip), %rdi     # 0x7b9e2
leaq 0x59c31(%rip), %rsi     # 0x7b9a0
movl $0x627, %edx            # imm = 0x627
leaq 0x59c2b(%rip), %rcx     # 0x7b9a6
callq 0x4360
movl -0x34(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0x21fb8
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 0x21fb6
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x21dcd
jmp 0x21dec
leaq 0x59c0b(%rip), %rdi     # 0x7b9df
leaq 0x59bc5(%rip), %rsi     # 0x7b9a0
movl $0x62d, %edx            # imm = 0x62D
leaq 0x59bbf(%rip), %rcx     # 0x7b9a6
callq 0x4360
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 0x21e23
cmpl $0x2, -0x40(%rbp)
je 0x21e23
cmpl $0x3, -0x40(%rbp)
je 0x21e23
cmpl $0x7, -0x40(%rbp)
je 0x21e23
cmpl $0x8, -0x40(%rbp)
jne 0x21e8f
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x21e65
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x21e65
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x21e72
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 0x21f7d
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x21ed1
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x21ed1
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x21ede
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 0x21f74
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 0x21f4a
cmpl $0x2, -0x40(%rbp)
je 0x21f4a
cmpl $0x3, -0x40(%rbp)
je 0x21f4a
cmpl $0x7, -0x40(%rbp)
je 0x21f4a
cmpl $0x8, -0x40(%rbp)
jne 0x21f4c
jmp 0x21f74
cmpl $0x1, -0x40(%rbp)
je 0x21f58
cmpl $0x4, -0x40(%rbp)
jne 0x21f65
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
jmp 0x21f67
jmp 0x21f69
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x21ef7
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 0x21f95
jmp 0x21fb4
leaq 0x59a43(%rip), %rdi     # 0x7b9df
leaq 0x599fd(%rip), %rsi     # 0x7b9a0
movl $0x63d, %edx            # imm = 0x63D
leaq 0x599f7(%rip), %rcx     # 0x7b9a6
callq 0x4360
jmp 0x21fb6
jmp 0x21fb8
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 0x22163
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 0x22163
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 0x22087
xorl %ecx, %ecx
subl -0x54(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0x22161
cmpl $0x1, -0x50(%rbp)
jne 0x2209c
movl -0x54(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0x2215f
cmpl $0x6, -0x50(%rbp)
jne 0x2215d
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 0x2215d
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 0x2214b
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 0x2211c
movl -0x54(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x2213e
cmpl $0x2, -0x50(%rbp)
je 0x2213a
cmpl $0x0, -0x50(%rbp)
je 0x2213a
cmpl $0x3, -0x50(%rbp)
je 0x2213a
cmpl $0x7, -0x50(%rbp)
je 0x2213a
cmpl $0x8, -0x50(%rbp)
jne 0x2213c
jmp 0x2214b
jmp 0x2213e
jmp 0x22140
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x220d6
cmpl $0x0, -0x58(%rbp)
jle 0x2215b
movl -0x58(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0x2215d
jmp 0x2215f
jmp 0x22161
jmp 0x22163
cmpl $0x0, -0x48(%rbp)
je 0x22175
cmpl $0x7, -0x48(%rbp)
je 0x22175
cmpl $0x8, -0x48(%rbp)
jne 0x22191
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 0x221de
cmpl $0x2, -0x48(%rbp)
je 0x2219d
cmpl $0x3, -0x48(%rbp)
jne 0x221dc
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 0x221de
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)