// File: /workspace/github_cpp_makefile_star2_O2/yhwu[P]sam2psl/sam2psl.cpp
// Function: resolve_cigar_pos(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, POSCIGAR_st&)
void resolve_cigar_pos(int POS, string& CIGAR, POSCIGAR_st& m)  
{  
  if ( CIGAR=="*" ) { m.pos=-1; return; }
  
  char numchar[]="99999999999999";
  const string ops=BAM_CIGAR_STR;
  
  m.base=0;
  m.pos=POS;
  m.tStart=POS;
  m.anchor=-1;
  m.iclip=-1;
  m.op.resize(0);
  m.nop.resize(0);
  m.l_qseq=0;
  
  for(int i=0, k=0; i<(int)CIGAR.size(); ++i) {
    if ( CIGAR[i]>='0' && CIGAR[i]<='9'  ) {
      numchar[k]=CIGAR[i];
      ++k;
      if ( k>9 ) { cerr << "CIGAR error:" << CIGAR << endl; exit(1); }
    }
    else {
      size_t cigar_num=ops.find(CIGAR[i]);
      if ( cigar_num==string::npos) { 
	cerr << "Unknown CIGAR:\t" << CIGAR << endl; 
	exit(1); 
      }
      numchar[k]=0;
      m.op.push_back(cigar_num);
      m.nop.push_back(atoi(numchar));
      k=0;
    }
  }
  
  m.qop.resize(m.op.size());
  m.top.resize(m.op.size());
  
  int ns=0;
  int end=0; //position on seq 0-based
  for (int k = 0; k < (int)m.op.size(); ++k) {
    
    int op=m.op[k];
    int l=m.nop[k];
    m.qop[k]=end;
    
    if ( op == BAM_CMATCH || 
	 op == BAM_CINS || 
	 op == BAM_CSOFT_CLIP || 
	 op == BAM_CEQUAL || 
	 op == BAM_CDIFF ) end+=l;
    
    if (  op == BAM_CSOFT_CLIP && l > ns) { ns=l; m.iclip=k; }
    
    if ( m.op[k] == BAM_CMATCH || 
	 m.op[k] == BAM_CDEL || 
	 m.op[k] == BAM_CEQUAL || 
	 m.op[k] == BAM_CDIFF) { if ( m.anchor<0 ) m.anchor=k; }
    
  }
  if ( m.anchor<0 ) { m.pos=0; return; }
  
  // position on reference 0-based
  // BAM_CSOFT_CLIP is added because we need positions of S 
  end = m.pos;  //position on reference
  for (int k = m.anchor; k < (int)m.op.size(); ++k) {
    m.top[k]=end;
    if (  m.op[k] == BAM_CMATCH || 
	  m.op[k] == BAM_CDEL || 
	  m.op[k] == BAM_CREF_SKIP || 
	  m.op[k] == BAM_CSOFT_CLIP ) end += m.nop[k] ;
  }
  end = m.pos;
  for (int k = m.anchor-1; k >= 0; --k) {
    if (  m.op[k] == BAM_CMATCH || 
	  m.op[k] == BAM_CDEL || 
	  m.op[k] == BAM_CREF_SKIP || 
	  m.op[k] == BAM_CSOFT_CLIP )  end -= m.nop[k] ;
    m.top[k]=end;
  }
  
  // get qStart, tStart, qEnd, qEnd
  m.qStart=m.qop[ m.anchor ];
  m.tStart=m.top[ m.anchor ];
  for (int k = (int)m.op.size()-1; k>=0; --k) {
    if ( m.op[k] == BAM_CSOFT_CLIP ||
	 m.op[k] == BAM_CHARD_CLIP ) continue;
    m.qEnd=m.qop[k]+m.nop[k]-1;
    m.tEnd=m.top[k]+m.nop[k]-1;
  }
  if( m.qEnd<=m.qStart || m.tStart!=m.pos ||
      m.tEnd<=m.tStart ) {
    cerr << "Error resolving CIGAR:\t" << CIGAR << "\n"
	 << "tStart=" << m.tStart << "\n"
	 << "qStart=" << m.qStart << "\n"
	 << "qEnd=" << m.qEnd << endl;
    exit(1);
  }
  
  m.l_qseq=m.qop.back()+m.nop.back()*(m.op.back()!=BAM_CHARD_CLIP);
  return;  
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movl %edi, %ebp
leaq 0x2be7(%rip), %rsi      # 0x5020
movq %r14, %rdi
callq 0x3bed
testb %al, %al
je 0x244e
orl $-0x1, 0x8(%rbx)
jmp 0x273a
movq %r14, 0x8(%rsp)
movabsq $0x39393939393939, %rax # imm = 0x39393939393939
movq %rax, 0x17(%rsp)
movabsq $0x3939393939393939, %rax # imm = 0x3939393939393939
movq %rax, 0x10(%rsp)
leaq 0x2bb9(%rip), %rsi      # 0x5031
leaq 0x28(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x3bfa
andl $0x0, 0x4(%rbx)
movl %ebp, 0x8(%rbx)
movl %ebp, 0x10(%rbx)
orq $-0x1, 0x20(%rbx)
leaq 0x30(%rbx), %rdi
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
callq 0x3c36
leaq 0x48(%rbx), %r12
xorl %r15d, %r15d
movq %r12, %rdi
xorl %esi, %esi
callq 0x3c36
andl $0x0, 0x28(%rbx)
leaq 0x4(%rsp), %rbp
xorl %r13d, %r13d
movq 0x8(%rsp), %rcx
movslq 0x8(%rcx), %rax
cmpq %rax, %r13
jge 0x254b
movq (%rcx), %rax
movb (%rax,%r13), %al
leal -0x30(%rax), %ecx
movslq %r15d, %r14
cmpb $0x9, %cl
ja 0x24f7
movb %al, 0x10(%rsp,%r14)
cmpl $0x9, %r15d
jge 0x2749
incl %r15d
jmp 0x2543
movsbl %al, %esi
leaq 0x28(%rsp), %rdi
xorl %edx, %edx
callq 0x2210
cmpq $-0x1, %rax
je 0x2773
movb $0x0, 0x10(%rsp,%r14)
movl %eax, 0x4(%rsp)
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x4180
leaq 0x10(%rsp), %rdi
callq 0x2250
movl %eax, 0x4(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x4180
xorl %r15d, %r15d
incq %r13
jmp 0x24c3
leaq 0x60(%rbx), %rdi
movq 0x38(%rbx), %rsi
subq 0x30(%rbx), %rsi
sarq $0x2, %rsi
callq 0x3c36
leaq 0x78(%rbx), %rdi
movq 0x38(%rbx), %rsi
subq 0x30(%rbx), %rsi
sarq $0x2, %rsi
callq 0x3c36
movq 0x30(%rbx), %rcx
movq 0x38(%rbx), %rax
movq %rax, %rdx
subq %rcx, %rdx
shrq $0x2, %rdx
movq 0x48(%rbx), %rsi
movq 0x60(%rbx), %rdi
xorl %r8d, %r8d
testl %edx, %edx
movl $0x0, %r9d
cmovgl %edx, %r9d
movl $0x85, %r10d
movl $0x193, %r11d           # imm = 0x193
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq %r8, %r9
je 0x2615
movl (%rcx,%r8,4), %r14d
movl (%rsi,%r8,4), %r12d
movl %ebp, (%rdi,%r8,4)
cmpl $0x8, %r14d
ja 0x25cf
btl %r14d, %r11d
jae 0x25cf
addl %r12d, %ebp
cmpl $0x4, %r14d
sete %r14b
cmpl %r15d, %r12d
setg %r13b
andb %r14b, %r13b
cmpb $0x1, %r13b
jne 0x25ee
movl %r8d, 0x24(%rbx)
movl %r12d, %r15d
movl (%rcx,%r8,4), %r14d
cmpl $0x7, %r14d
ja 0x260d
btl %r14d, %r10d
jae 0x260d
cmpl $0x0, 0x20(%rbx)
jns 0x2608
movl %r8d, 0x20(%rbx)
incq %r8
jmp 0x25af
cmpl $0x8, %r14d
je 0x25fe
jmp 0x2608
movslq 0x20(%rbx), %rsi
testq %rsi, %rsi
js 0x272c
movl 0x8(%rbx), %edi
leaq (,%rsi,4), %r8
movq 0x78(%rbx), %r9
addq %r8, %r9
leaq (%rcx,%rsi,4), %r10
addq 0x48(%rbx), %r8
xorl %r11d, %r11d
leal (%rsi,%r11), %ebp
cmpl %edx, %ebp
jge 0x2662
movl %edi, (%r9,%r11,4)
movl (%r10,%r11,4), %ebp
cmpl $0x4, %ebp
ja 0x265d
cmpl $0x1, %ebp
je 0x265d
addl (%r8,%r11,4), %edi
incq %r11
jmp 0x263f
movl 0x8(%rbx), %esi
movl 0x20(%rbx), %edi
movq 0x48(%rbx), %r8
movq 0x78(%rbx), %r9
testl %edi, %edi
jle 0x2694
movl -0x4(%rcx,%rdi,4), %r10d
cmpl $0x4, %r10d
ja 0x268a
cmpl $0x1, %r10d
je 0x268a
subl -0x4(%r8,%rdi,4), %esi
movl %esi, -0x4(%r9,%rdi,4)
decq %rdi
jmp 0x2670
movslq 0x20(%rbx), %rsi
movq 0x48(%rbx), %rdi
movq 0x60(%rbx), %r9
movl (%r9,%rsi,4), %r8d
movl %r8d, 0xc(%rbx)
movq 0x78(%rbx), %r10
movl (%r10,%rsi,4), %esi
movl %esi, 0x10(%rbx)
movl %edx, %edx
testl %edx, %edx
jle 0x26f5
movl -0x4(%rcx,%rdx,4), %r11d
andl $-0x2, %r11d
cmpl $0x4, %r11d
je 0x26f0
movl -0x4(%r9,%rdx,4), %r11d
movl -0x4(%rdi,%rdx,4), %r14d
addl %r14d, %r11d
decl %r11d
movl %r11d, 0x14(%rbx)
movl -0x4(%r10,%rdx,4), %r11d
movl -0x4(%rdi,%rdx,4), %r14d
addl %r14d, %r11d
decl %r11d
movl %r11d, 0x18(%rbx)
decq %rdx
jmp 0x26b5
cmpl %r8d, 0x14(%rbx)
jle 0x27a3
cmpl 0x8(%rbx), %esi
jne 0x27a3
cmpl %esi, 0x18(%rbx)
jle 0x27a3
movq 0x68(%rbx), %rcx
xorl %edx, %edx
cmpl $0x5, -0x4(%rax)
je 0x2724
movq 0x50(%rbx), %rax
movl -0x4(%rax), %edx
addl -0x4(%rcx), %edx
movl %edx, 0x28(%rbx)
jmp 0x2730
andl $0x0, 0x8(%rbx)
leaq 0x28(%rsp), %rdi
callq 0x20b0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x58a0(%rip), %rdi      # 0x7ff0
leaq 0x28e5(%rip), %rsi      # 0x503c
callq 0x2170
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x2160
movq %rax, %rdi
callq 0x20d0
jmp 0x279b
movq 0x5876(%rip), %rdi      # 0x7ff0
leaq 0x28c8(%rip), %rsi      # 0x5049
callq 0x2170
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x2160
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rdi
callq 0x21d0
movq 0x5846(%rip), %rdi      # 0x7ff0
leaq 0x28a8(%rip), %rsi      # 0x5059
callq 0x2170
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x2160
leaq 0x28a7(%rip), %rsi      # 0x5071
movq %rax, %rdi
callq 0x2170
leaq 0x289a(%rip), %rsi      # 0x5073
movq %rax, %rdi
callq 0x2170
movl 0x10(%rbx), %esi
movq %rax, %rdi
callq 0x2290
leaq 0x287e(%rip), %rsi      # 0x5071
movq %rax, %rdi
callq 0x2170
leaq 0x2879(%rip), %rsi      # 0x507b
movq %rax, %rdi
callq 0x2170
movl 0xc(%rbx), %esi
movq %rax, %rdi
callq 0x2290
leaq 0x2855(%rip), %rsi      # 0x5071
movq %rax, %rdi
callq 0x2170
leaq 0x2858(%rip), %rsi      # 0x5083
movq %rax, %rdi
callq 0x2170
movl 0x14(%rbx), %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
callq 0x20d0
jmp 0x279b
jmp 0x2855
jmp 0x2855
jmp 0x2855
jmp 0x2855
jmp 0x2855
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x22a0