// File: /workspace/github_cpp_makefile_star2_O1/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 0x3bc7(%rip), %rsi      # 0x6020
movq %r14, %rdi
callq 0x2070
testl %eax, %eax
je 0x27ae
movabsq $0x39393939393939, %rax # imm = 0x39393939393939
movq %rax, 0x1f(%rsp)
movabsq $0x3939393939393939, %rax # imm = 0x3939393939393939
movq %rax, 0x18(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x3b9a(%rip), %rsi      # 0x6031
leaq 0x3b9d(%rip), %rdx      # 0x603b
leaq 0x28(%rsp), %rdi
callq 0x4804
movl $0x0, 0x4(%rbx)
movl %ebp, 0x8(%rbx)
movl %ebp, 0x10(%rbx)
movq $-0x1, 0x20(%rbx)
leaq 0x30(%rbx), %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
callq 0x43fa
leaq 0x48(%rbx), %rdi
movq %rdi, 0x10(%rsp)
xorl %esi, %esi
callq 0x43fa
movl $0x0, 0x28(%rbx)
cmpl $0x0, 0x8(%r14)
jle 0x25b0
xorl %r15d, %r15d
leaq 0x28(%rsp), %r13
leaq 0x18(%rsp), %rbp
xorl %r12d, %r12d
movq (%r14), %rax
movb (%rax,%r15), %al
leal -0x30(%rax), %ecx
cmpb $0x9, %cl
ja 0x2524
movslq %r12d, %rcx
movb %al, 0x18(%rsp,%rcx)
cmpl $0x9, %r12d
jge 0x27e9
incl %r12d
jmp 0x25a0
movsbl %al, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x2200
cmpq $-0x1, %rax
je 0x281e
movslq %r12d, %rcx
movb $0x0, 0x18(%rsp,%rcx)
movl %eax, 0x4(%rsp)
movq 0x38(%rbx), %rsi
cmpq 0x40(%rbx), %rsi
je 0x255d
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rbx)
jmp 0x256c
movq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x49fc
movq %rbp, %rdi
callq 0x2250
movl %eax, 0x4(%rsp)
movq 0x50(%rbx), %rsi
cmpq 0x58(%rbx), %rsi
je 0x258e
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x50(%rbx)
jmp 0x259d
movq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x49fc
xorl %r12d, %r12d
incq %r15
movslq 0x8(%r14), %rax
cmpq %rax, %r15
jl 0x24ff
leaq 0x60(%rbx), %r12
movq 0x38(%rbx), %rsi
subq 0x30(%rbx), %rsi
sarq $0x2, %rsi
movq %r12, %rdi
callq 0x43fa
leaq 0x78(%rbx), %rdi
movq 0x38(%rbx), %rsi
subq 0x30(%rbx), %rsi
sarq $0x2, %rsi
callq 0x43fa
movq 0x30(%rbx), %rcx
movq 0x38(%rbx), %rdx
movq %rdx, 0x8(%rsp)
subq %rcx, %rdx
shrq $0x2, %rdx
testl %edx, %edx
jle 0x2686
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq (%r12), %rdi
movl %edx, %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %r9d, %r9d
movl $0x85, %r10d
movl $0x193, %r11d           # imm = 0x193
xorl %ebp, %ebp
xorl %r15d, %r15d
movl (%rcx,%r9,4), %r13d
movl (%rsi,%r9,4), %r12d
movl %ebp, (%rdi,%r9,4)
cmpl $0x8, %r13d
ja 0x263e
btl %r13d, %r11d
jae 0x263e
addl %r12d, %ebp
cmpl $0x4, %r13d
sete %r13b
cmpl %r15d, %r12d
setg %al
andb %r13b, %al
cmpb $0x1, %al
jne 0x265a
movl %r9d, 0x24(%rbx)
movl %r12d, %r15d
movl (%rcx,%r9,4), %r12d
cmpl $0x7, %r12d
ja 0x267e
btl %r12d, %r10d
jae 0x267e
cmpl $0x0, 0x20(%rbx)
jns 0x2674
movl %r9d, 0x20(%rbx)
incq %r9
cmpq %r9, %r8
jne 0x2623
jmp 0x2686
cmpl $0x8, %r12d
je 0x266a
jmp 0x2674
movl 0x20(%rbx), %esi
testl %esi, %esi
js 0x27b7
cmpl %edx, %esi
jge 0x26d3
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
movl %edi, (%r9,%r11,4)
movl (%r10,%r11,4), %ebp
cmpl $0x4, %ebp
ja 0x26c8
cmpl $0x1, %ebp
je 0x26c8
addl (%r8,%r11,4), %edi
incq %r11
leal (%rsi,%r11), %eax
cmpl %edx, %eax
jl 0x26b2
movslq 0x20(%rbx), %rsi
testq %rsi, %rsi
jle 0x270e
movl 0x8(%rbx), %edi
movq 0x48(%rbx), %r8
movq 0x78(%rbx), %r9
incq %rsi
movl -0x8(%rcx,%rsi,4), %r10d
cmpl $0x4, %r10d
ja 0x2700
cmpl $0x1, %r10d
je 0x2700
subl -0x8(%r8,%rsi,4), %edi
movl %edi, -0x8(%r9,%rsi,4)
decq %rsi
cmpq $0x1, %rsi
jg 0x26ea
movslq 0x20(%rbx), %rax
movq 0x60(%rbx), %rdi
movq 0x78(%rbx), %r9
movl (%rdi,%rax,4), %r8d
movl %r8d, 0xc(%rbx)
movl (%r9,%rax,4), %esi
movl %esi, 0x10(%rbx)
testl %edx, %edx
jle 0x2772
movq 0x48(%rbx), %r10
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
incq %rdx
movl -0x8(%rcx,%rdx,4), %eax
andl $-0x2, %eax
cmpl $0x4, %eax
je 0x2769
movl -0x8(%rdi,%rdx,4), %eax
movl -0x8(%r10,%rdx,4), %r11d
addl %r11d, %eax
decl %eax
movl %eax, 0x14(%rbx)
movl -0x8(%r9,%rdx,4), %eax
movl -0x8(%r10,%rdx,4), %r11d
addl %r11d, %eax
decl %eax
movl %eax, 0x18(%rbx)
decq %rdx
cmpq $0x1, %rdx
jg 0x273a
cmpl %r8d, 0x14(%rbx)
jle 0x285b
cmpl 0x8(%rbx), %esi
jne 0x285b
cmpl %esi, 0x18(%rbx)
jle 0x285b
movq 0x68(%rbx), %rcx
xorl %edx, %edx
movq 0x8(%rsp), %rax
cmpl $0x5, -0x4(%rax)
je 0x27a6
movq 0x50(%rbx), %rax
movl -0x4(%rax), %edx
addl -0x4(%rcx), %edx
movl %edx, 0x28(%rbx)
jmp 0x27be
movl $0xffffffff, 0x8(%rbx)  # imm = 0xFFFFFFFF
jmp 0x27da
movl $0x0, 0x8(%rbx)
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x27da
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2190
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5800(%rip), %rdi      # 0x7ff0
leaq 0x3845(%rip), %rsi      # 0x603c
movl $0xc, %edx
callq 0x21c0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x57e1(%rip), %rdi      # 0x7ff0
callq 0x21c0
movq %rax, %rdi
callq 0x20c0
jmp 0x2851
movq 0x57cb(%rip), %rdi      # 0x7ff0
leaq 0x381d(%rip), %rsi      # 0x6049
movl $0xf, %edx
callq 0x21c0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x57ac(%rip), %rdi      # 0x7ff0
callq 0x21c0
movq %rax, %rdi
callq 0x20c0
movl $0x1, %edi
callq 0x21d0
movq 0x578e(%rip), %rdi      # 0x7ff0
leaq 0x37f0(%rip), %rsi      # 0x6059
movl $0x17, %edx
callq 0x21c0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x576f(%rip), %rdi      # 0x7ff0
callq 0x21c0
movq %rax, %r14
leaq 0x37e1(%rip), %rsi      # 0x6071
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
leaq 0x37cf(%rip), %rsi      # 0x6073
movl $0x7, %edx
movq %r14, %rdi
callq 0x21c0
movl 0x10(%rbx), %esi
movq %r14, %rdi
callq 0x2290
movq %rax, %r14
leaq 0x37ab(%rip), %rsi      # 0x6071
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
leaq 0x37a1(%rip), %rsi      # 0x607b
movl $0x7, %edx
movq %r14, %rdi
callq 0x21c0
movl 0xc(%rbx), %esi
movq %r14, %rdi
callq 0x2290
movq %rax, %r14
leaq 0x3775(%rip), %rsi      # 0x6071
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
leaq 0x3773(%rip), %rsi      # 0x6083
movl $0x5, %edx
movq %r14, %rdi
callq 0x21c0
movl 0x14(%rbx), %esi
movq %r14, %rdi
callq 0x2290
movq %rax, %rdi
callq 0x20c0
jmp 0x2851
jmp 0x293f
jmp 0x293f
jmp 0x293f
jmp 0x293f
jmp 0x293f
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x295e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x22b0