// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movl %edi, %ebp
leaq 0x3bc7(%rip), %rsi      # 0x6020
movq %r14, %rdi
callq 0x2070
testl %eax, %eax
je 0x26aa
movabsq $0x39393939393939, %rax # imm = 0x39393939393939
movq %rax, 0x27(%rsp)
movabsq $0x3939393939393939, %rax # imm = 0x3939393939393939
movq %rax, 0x20(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x3b9a(%rip), %rsi      # 0x6031
leaq 0x3b9d(%rip), %rdx      # 0x603b
leaq 0x30(%rsp), %rdi
callq 0x48d2
movl $0x0, 0x4(%rbx)
movl %ebp, 0x8(%rbx)
movl %ebp, 0x10(%rbx)
movq $-0x1, 0x20(%rbx)
leaq 0x30(%rbx), %rdi
movq %rdi, 0x10(%rsp)
xorl %esi, %esi
callq 0x44c4
leaq 0x48(%rbx), %rdi
movq %rdi, 0x18(%rsp)
xorl %esi, %esi
callq 0x44c4
movl $0x0, 0x28(%rbx)
movq 0x8(%r14), %rax
testl %eax, %eax
jle 0x25b7
xorl %r15d, %r15d
leaq 0x30(%rsp), %r13
leaq 0x20(%rsp), %rbp
xorl %r12d, %r12d
movq (%r14), %rcx
movb (%rcx,%r15), %cl
leal -0x30(%rcx), %edx
cmpb $0x9, %dl
ja 0x2528
movslq %r12d, %rdx
movb %cl, 0x20(%rsp,%rdx)
cmpl $0x9, %r12d
jge 0x2816
incl %r12d
jmp 0x25a8
movsbl %cl, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x2200
cmpq $-0x1, %rax
je 0x284b
movslq %r12d, %rcx
movb $0x0, 0x20(%rsp,%rcx)
movl %eax, 0xc(%rsp)
movq 0x38(%rbx), %rsi
cmpq 0x40(%rbx), %rsi
je 0x2561
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rbx)
jmp 0x2570
movq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x4ad6
movq %rbp, %rdi
callq 0x2250
movl %eax, 0xc(%rsp)
movq 0x50(%rbx), %rsi
cmpq 0x58(%rbx), %rsi
je 0x2592
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x50(%rbx)
jmp 0x25a1
movq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x4ad6
movq 0x8(%r14), %rax
xorl %r12d, %r12d
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x2500
leaq 0x60(%rbx), %r13
movq 0x38(%rbx), %rsi
subq 0x30(%rbx), %rsi
sarq $0x2, %rsi
movq %r13, %rdi
callq 0x44c4
leaq 0x78(%rbx), %r15
movq 0x38(%rbx), %rsi
subq 0x30(%rbx), %rsi
sarq $0x2, %rsi
movq %r15, %rdi
callq 0x44c4
movq %r15, 0x10(%rsp)
movq 0x30(%rbx), %rcx
movq 0x38(%rbx), %rdx
movq %rdx, 0x50(%rsp)
subq %rcx, %rdx
shrq $0x2, %rdx
testl %edx, %edx
jle 0x26b6
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq (%r13), %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), %eax
movl (%rsi,%r9,4), %r13d
movl %ebp, (%rdi,%r9,4)
cmpl $0x8, %eax
ja 0x264c
btl %eax, %r11d
jae 0x264c
addl %r13d, %ebp
cmpl $0x4, %eax
sete %al
cmpl %r15d, %r13d
setg %r12b
andb %al, %r12b
cmpb $0x1, %r12b
jne 0x2669
movl %r9d, 0x24(%rbx)
movl %r13d, %r15d
movl (%rcx,%r9,4), %eax
cmpl $0x7, %eax
ja 0x2692
btl %eax, %r10d
jae 0x2692
movl 0x20(%rbx), %r13d
testl %r13d, %r13d
jns 0x2688
movl %r9d, 0x20(%rbx)
movl %r9d, %r13d
incq %r9
cmpq %r9, %r8
jne 0x2632
jmp 0x26ba
cmpl $0x8, %eax
sete %al
movl 0x20(%rbx), %r13d
testl %r13d, %r13d
sets %r12b
testb %r12b, %al
jne 0x2681
jmp 0x2688
movl $0xffffffff, 0x8(%rbx)  # imm = 0xFFFFFFFF
jmp 0x2807
movl 0x20(%rbx), %r13d
testl %r13d, %r13d
js 0x27e4
movq 0x10(%rsp), %rax
movq (%rax), %rsi
cmpl %edx, %r13d
jge 0x2712
movl 0x8(%rbx), %edi
movl %r13d, %r8d
leaq (,%r8,4), %r9
leaq (%rsi,%r8,4), %r10
leaq (%rcx,%r8,4), %r11
addq 0x48(%rbx), %r9
xorl %r15d, %r15d
movl %edi, (%r10,%r15,4)
movl (%r11,%r15,4), %eax
cmpl $0x4, %eax
ja 0x2703
cmpl $0x1, %eax
je 0x2703
addl (%r9,%r15,4), %edi
incq %r15
leal (%r8,%r15), %eax
cmpl %edx, %eax
jl 0x26ed
movl 0x20(%rbx), %r13d
testl %r13d, %r13d
jle 0x274a
movl 0x8(%rbx), %edi
movq 0x48(%rbx), %r8
movl %r13d, %r9d
incq %r9
movl -0x8(%rcx,%r9,4), %eax
cmpl $0x4, %eax
ja 0x2738
cmpl $0x1, %eax
je 0x2738
subl -0x8(%r8,%r9,4), %edi
movl %edi, -0x8(%rsi,%r9,4)
decq %r9
cmpq $0x1, %r9
ja 0x2724
movl 0x20(%rbx), %r13d
movslq %r13d, %rax
movq 0x60(%rbx), %r9
movl (%r9,%rax,4), %r8d
movl %r8d, 0xc(%rbx)
movl (%rsi,%rax,4), %edi
movl %edi, 0x10(%rbx)
testl %edx, %edx
jle 0x27a8
movq 0x48(%rbx), %r10
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
incq %rdx
movl -0x8(%rcx,%rdx,4), %eax
andl $-0x2, %eax
cmpl $0x4, %eax
je 0x279f
movl -0x8(%r9,%rdx,4), %eax
movl -0x8(%r10,%rdx,4), %r11d
addl %r11d, %eax
decl %eax
movl %eax, 0x14(%rbx)
movl -0x8(%rsi,%rdx,4), %eax
movl -0x8(%r10,%rdx,4), %r11d
addl %r11d, %eax
decl %eax
movl %eax, 0x18(%rbx)
decq %rdx
cmpq $0x1, %rdx
ja 0x2770
cmpl %r8d, 0x14(%rbx)
jle 0x2888
cmpl 0x8(%rbx), %edi
jne 0x2888
cmpl %edi, 0x18(%rbx)
jle 0x2888
movq 0x68(%rbx), %rax
xorl %ecx, %ecx
movq 0x50(%rsp), %rdx
cmpl $0x5, -0x4(%rdx)
je 0x27dc
movq 0x50(%rbx), %rcx
movl -0x4(%rcx), %ecx
addl -0x4(%rax), %ecx
movl %ecx, 0x28(%rbx)
jmp 0x27eb
movl $0x0, 0x8(%rbx)
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2807
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x57d3(%rip), %rdi      # 0x7ff0
leaq 0x3818(%rip), %rsi      # 0x603c
movl $0xc, %edx
callq 0x21c0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x57b4(%rip), %rdi      # 0x7ff0
callq 0x21c0
movq %rax, %rdi
callq 0x20c0
jmp 0x287e
movq 0x579e(%rip), %rdi      # 0x7ff0
leaq 0x37f0(%rip), %rsi      # 0x6049
movl $0xf, %edx
callq 0x21c0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x577f(%rip), %rdi      # 0x7ff0
callq 0x21c0
movq %rax, %rdi
callq 0x20c0
movl $0x1, %edi
callq 0x21d0
movq 0x5761(%rip), %rdi      # 0x7ff0
leaq 0x37c3(%rip), %rsi      # 0x6059
movl $0x17, %edx
callq 0x21c0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x5742(%rip), %rdi      # 0x7ff0
callq 0x21c0
movq %rax, %r14
leaq 0x37b4(%rip), %rsi      # 0x6071
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
leaq 0x37a2(%rip), %rsi      # 0x6073
movl $0x7, %edx
movq %r14, %rdi
callq 0x21c0
movl 0x10(%rbx), %esi
movq %r14, %rdi
callq 0x2290
movq %rax, %r14
leaq 0x377e(%rip), %rsi      # 0x6071
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
leaq 0x3774(%rip), %rsi      # 0x607b
movl $0x7, %edx
movq %r14, %rdi
callq 0x21c0
movl 0xc(%rbx), %esi
movq %r14, %rdi
callq 0x2290
movq %rax, %r14
leaq 0x3748(%rip), %rsi      # 0x6071
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
leaq 0x3746(%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 0x287e
jmp 0x296c
jmp 0x296c
jmp 0x296c
jmp 0x296c
jmp 0x296c
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x298b
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x22b0