// File: /workspace/github_cpp_makefile_star2_O2/CK7[P]abawaca/src/ReadMapping.cpp
// Function: Bio::ReadMapping::determine_snps(Bio::DNAString const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long)
void ReadMapping::determine_snps(const DNAString& dna_str, const string& quality_str, string CIGAR_indels, string CIGAR_mismatches, size_t ref_pos)
{
	if(CIGAR_mismatches.size() == 0)
		return;

	// This regexp was taken from the SAM manual
	// /^MD:Z:([0-9]+)(([ACGTN]|\^[ACGTN]+)[0-9]+)*$/)
	string::const_iterator p = CIGAR_mismatches.begin();
	if((*p!='M') || (*(p+1)!='D') || (*(p+2)!=':') || (*(p+3)!='Z') || (*(p+4)!=':')) {
		throw Illegal_mapping(CIGAR_mismatches, "Illegal SNPs description, expected MD:Z: at the beginning of CIGAR_mismatches format but found something different", __PRETTY_FUNCTION__);
	}
	p += 5;

	// Read the offset to the first SNP
	size_t	read_pos = 0, ref_deleted = 0;
	while((*p>='0') && (*p<='9')) {
		read_pos *= 10;
		read_pos += (*(p++)-'0');
	}
	// Set the offset to 0 
	read_pos--;
	// Keep on reading the next snps until we reach the end
	while(p != CIGAR_mismatches.end()) {
		// First we expect the ref's char
		if((*p!='^') && ((*p<'A') || (*p>'Z')))
			throw Illegal_mapping(CIGAR_mismatches, string("Illegal SNPs description (2), expected CIGAR_mismatches format but found something different"), __PRETTY_FUNCTION__);

		if(*p=='^') {
			p++;
			while(((*p<'0') || (*p>'9')) && (p != CIGAR_mismatches.end())) {
				ref_deleted++;
				m_snps.push_back(SNP(SNP::DELETION, ref_pos+read_pos+ref_deleted, read_pos, *p, '-', 40));
				p++;
			}
		}
		else {
			read_pos++;
			m_snps.push_back(SNP(SNP::MISMATCH, ref_pos+read_pos+ref_deleted, read_pos, *p, dna_str.get(read_pos), quality_str.at(read_pos)));
			p++;
		}

		// There must be at least one more number
		if((*p < '0') || (*p > '9'))
			throw Illegal_mapping(CIGAR_mismatches, "Illegal SNPs description, expeected CIGAR_mismatches format but found something different", __PRETTY_FUNCTION__);
		unsigned int o = 0;
		while((*p>='0') && (*p<='9')) {
			o *= 10;
			o += (*(p++)-'0');
		}
		read_pos += o;
	}

	// Now we have to go through the insertion/deletion CIGAR string and correct mismatches accordingly
	// Examples: 6M1D9M1I132M, 31M1I106M	
	p = CIGAR_indels.begin();
	// Make sure that the end of the string is legal (so we don't have to check it all the time in the loop below)
	char ch = *(CIGAR_indels.rbegin());
	if((ch!='I') && (ch!='D') && (ch!='M')) {
		throw Illegal_mapping(CIGAR_indels, string("Illegal CIGAR-indels description (3)"), __PRETTY_FUNCTION__);
	}

	int read_pos2 = 0;
	size_t ref_pos2 = ref_pos;
	while(p!=CIGAR_indels.end()) {
		size_t o = 0;
		while((*p >= '0') && (*p <= '9')) {
			o *= 10;
			o += (*(p++)-'0');
		}
		if(o == 0) {
			throw Illegal_mapping(CIGAR_indels, string("Illegal CIGAR-indels description (4)"), __PRETTY_FUNCTION__);
		}
		vector<SNP>::iterator ps;
		size_t pos;
		switch (*(p++)) {
			case 'M':
				read_pos2 += o;
				ref_pos2 += o;
				break;
			case 'D':
				// Nothig to do here - this case was already taken care of in CIGAR_mismatches
				ref_pos2 += o;
				break;
			case 'I':
				for(ps=m_snps.begin(); ps!=m_snps.end(); ps++) {
					if(ps->read_pos >= read_pos2) {
						ps->read_pos += o;
					}
				}
				read_pos += o;

				for(ps=m_snps.begin(); (ps!=m_snps.end()) && (ps->read_pos < read_pos2); ps++)
					;
				// At this point we don't provide any information about the insetion except that it exists
				m_snps.insert(ps, SNP(SNP::INSERTION, ref_pos2-1, read_pos2-1, '-', '-', '-'));
				read_pos2 += o;

				break;
			default:
				throw Illegal_mapping(CIGAR_indels, string("Unexpected CIGAR-indels operation found"), __PRETTY_FUNCTION__);
		}
	}

	// Sanity check
	if(((read_pos+1) != dna_str.size()) || (read_pos2 != dna_str.size()))
		throw Illegal_mapping(CIGAR_indels+string(", ")+CIGAR_mismatches, "snps and offsets do not sum to expected read length", __PRETTY_FUNCTION__);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rcx, -0x48(%rbp)
movq %rdx, -0xa0(%rbp)
movq %rsi, -0x88(%rbp)
movq %r8, -0x38(%rbp)
movq 0x8(%r8), %rax
testq %rax, %rax
je 0x4c94
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
cmpb $0x4d, (%rcx)
jne 0x4e66
cmpb $0x44, 0x1(%rcx)
jne 0x4e66
cmpb $0x3a, 0x2(%rcx)
jne 0x4e66
cmpb $0x5a, 0x3(%rcx)
jne 0x4e66
cmpb $0x3a, 0x4(%rcx)
jne 0x4e66
movq %r9, %r14
leaq 0x5(%rcx), %r12
xorl %r13d, %r13d
movb (%r12), %bl
leal -0x30(%rbx), %edx
cmpb $0x9, %dl
ja 0x49f5
movzbl %bl, %edx
imulq $0xa, %r13, %rsi
incq %r12
leaq (%rsi,%rdx), %r13
addq $-0x30, %r13
jmp 0x49d5
decq %r13
movq %rdi, -0x90(%rbp)
leaq 0xd0(%rdi), %rdx
movq %rdx, -0x80(%rbp)
leaq 0x1(%r14), %rdx
movq %rdx, -0x98(%rbp)
xorl %r15d, %r15d
movq %r14, -0x78(%rbp)
addq %rcx, %rax
cmpq %rax, %r12
je 0x4b53
cmpb $0x5e, %bl
jne 0x4a95
incq %r12
movq -0x98(%rbp), %rax
leaq (%rax,%r13), %r14
movb (%r12), %bl
leal -0x3a(%rbx), %eax
cmpb $-0xb, %al
ja 0x4b0a
movq -0x38(%rbp), %rcx
movq (%rcx), %rax
addq 0x8(%rcx), %rax
cmpq %rax, %r12
je 0x4b10
leaq (%r14,%r15), %rax
incq %r15
movl $0x2, -0x70(%rbp)
movq %rax, -0x68(%rbp)
movq %r13, -0x60(%rbp)
movb %bl, -0x58(%rbp)
movb $0x2d, -0x57(%rbp)
movq $0x28, -0x50(%rbp)
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x5a20
incq %r12
jmp 0x4a3b
leal -0x5b(%rbx), %eax
cmpb $-0x1b, %al
jbe 0x4df6
leaq (%r15,%r14), %rax
leaq (%rax,%r13), %r14
incq %r14
incq %r13
movq -0x88(%rbp), %rdi
movq %r13, %rsi
callq 0x5850
movb %al, -0x29(%rbp)
movq -0xa0(%rbp), %rdi
movq %r13, %rsi
callq 0x4320
movsbq (%rax), %rax
movl $0x4, -0x70(%rbp)
movq %r14, -0x68(%rbp)
movq -0x78(%rbp), %r14
movq %r13, -0x60(%rbp)
movb %bl, -0x58(%rbp)
movb -0x29(%rbp), %cl
movb %cl, -0x57(%rbp)
movq %rax, -0x50(%rbp)
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x5a20
movb 0x1(%r12), %bl
incq %r12
jmp 0x4b14
movq -0x78(%rbp), %r14
jmp 0x4b1f
movq -0x78(%rbp), %r14
leal -0x3a(%rbx), %eax
cmpb $-0xa, %al
jb 0x4d86
xorl %eax, %eax
leal -0x30(%rbx), %ecx
cmpb $0x9, %cl
ja 0x4b3e
imull $0xa, %eax, %eax
movzbl %bl, %ecx
addl %ecx, %eax
addl $-0x30, %eax
movb 0x1(%r12), %bl
incq %r12
jmp 0x4b21
movl %eax, %eax
addq %rax, %r13
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
jmp 0x4a1c
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movzbl -0x1(%rcx,%rax), %edx
addl $-0x44, %edx
cmpl $0x9, %edx
ja 0x4f72
movl $0x221, %esi            # imm = 0x221
btl %edx, %esi
jae 0x4f72
xorl %r12d, %r12d
leaq -0x70(%rbp), %rdx
movq %rcx, %rbx
addq %rcx, %rax
movslq %r12d, %rcx
cmpq %rax, %rbx
je 0x4c74
incq %rbx
xorl %r15d, %r15d
movzbl -0x1(%rbx), %edi
leal -0x30(%rdi), %esi
cmpb $0x9, %sil
ja 0x4bba
imulq $0xa, %r15, %rsi
leaq (%rsi,%rdi), %r15
addq $-0x30, %r15
incq %rbx
jmp 0x4b9c
testq %r15, %r15
je 0x4ca6
cmpq $0x44, %rdi
jne 0x4bce
addq %r15, %r14
jmp 0x4b8a
cmpl $0x49, %edi
je 0x4be1
cmpl $0x4d, %edi
jne 0x4d16
addl %r15d, %r12d
jmp 0x4bc9
movq -0x90(%rbp), %rax
movq 0xd0(%rax), %rsi
movq 0xd8(%rax), %rax
movq %rsi, %r8
cmpq %rax, %r8
je 0x4c14
movq 0x10(%r8), %rdi
cmpq %rcx, %rdi
jb 0x4c0e
addq %r15, %rdi
movq %rdi, 0x10(%r8)
addq $0x28, %r8
jmp 0x4bf9
addq %r15, %r13
cmpq %rax, %rsi
je 0x4c28
cmpq %rcx, 0x10(%rsi)
jae 0x4c28
addq $0x28, %rsi
jmp 0x4c17
leaq -0x1(%r14), %rax
leal -0x1(%r12), %ecx
movslq %ecx, %rcx
movl $0x1, -0x70(%rbp)
movq %rax, -0x68(%rbp)
movq %rcx, -0x60(%rbp)
movw $0x2d2d, -0x58(%rbp)    # imm = 0x2D2D
movq $0x2d, -0x50(%rbp)
movq -0x80(%rbp), %rdi
callq 0x5bf2
leaq -0x70(%rbp), %rdx
addl %r12d, %r15d
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movl %r15d, %r12d
jmp 0x4b87
incq %r13
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rax
cmpq %rax, %r13
jne 0x4ed6
cmpq %rcx, %rax
jne 0x4ed6
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x28
popq %rdi
callq 0x4150
movq %rax, %rbx
leaq -0x180(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x43a0
leaq 0x94f2(%rip), %rsi      # 0xe1ba
leaq -0x160(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x40f0
movb $0x1, %r15b
leaq 0x939b(%rip), %rcx      # 0xe07d
leaq -0x180(%rbp), %rsi
leaq -0x160(%rbp), %rdx
movq %rbx, %rdi
callq 0x5564
xorl %r15d, %r15d
leaq 0xfbc6(%rip), %rsi      # 0x148c8
leaq 0xb1d(%rip), %rdx       # 0x5826
movq %rbx, %rdi
callq 0x4490
jmp 0x4fdd
pushq $0x28
popq %rdi
callq 0x4150
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x43a0
leaq 0x94a7(%rip), %rsi      # 0xe1df
leaq -0x120(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x40f0
movb $0x1, %r15b
leaq 0x932b(%rip), %rcx      # 0xe07d
leaq -0x140(%rbp), %rsi
leaq -0x120(%rbp), %rdx
movq %rbx, %rdi
callq 0x5564
xorl %r15d, %r15d
leaq 0xfb56(%rip), %rsi      # 0x148c8
leaq 0xaad(%rip), %rdx       # 0x5826
movq %rbx, %rdi
callq 0x4490
jmp 0x4fdd
pushq $0x28
popq %rdi
callq 0x4150
movq %rax, %rbx
leaq -0x200(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x43a0
leaq 0x9393(%rip), %rsi      # 0xe13b
leaq -0x1e0(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x40f0
movb $0x1, %r15b
leaq 0x92bb(%rip), %rcx      # 0xe07d
leaq -0x200(%rbp), %rsi
leaq -0x1e0(%rbp), %rdx
movq %rbx, %rdi
callq 0x5564
xorl %r15d, %r15d
leaq 0xfae6(%rip), %rsi      # 0x148c8
leaq 0xa3d(%rip), %rdx       # 0x5826
movq %rbx, %rdi
callq 0x4490
jmp 0x4fdd
pushq $0x28
popq %rdi
callq 0x4150
movq %rax, %rbx
leaq -0x240(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x43a0
leaq 0x92c6(%rip), %rsi      # 0xe0de
leaq -0x220(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x40f0
movb $0x1, %r15b
leaq 0x924b(%rip), %rcx      # 0xe07d
leaq -0x240(%rbp), %rsi
leaq -0x220(%rbp), %rdx
movq %rbx, %rdi
callq 0x5564
xorl %r15d, %r15d
leaq 0xfa76(%rip), %rsi      # 0x148c8
leaq 0x9cd(%rip), %rdx       # 0x5826
movq %rbx, %rdi
callq 0x4490
jmp 0x4fdd
pushq $0x28
popq %rdi
callq 0x4150
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x43a0
leaq 0x9182(%rip), %rsi      # 0xe00a
leaq -0x260(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x40f0
movb $0x1, %r15b
leaq 0x91db(%rip), %rcx      # 0xe07d
leaq -0x280(%rbp), %rsi
leaq -0x260(%rbp), %rdx
movq %rbx, %rdi
callq 0x5564
xorl %r15d, %r15d
leaq 0xfa06(%rip), %rsi      # 0x148c8
leaq 0x95d(%rip), %rdx       # 0x5826
movq %rbx, %rdi
callq 0x4490
jmp 0x4fdd
pushq $0x28
popq %rdi
callq 0x4150
movq %rax, %rbx
leaq 0x931f(%rip), %rsi      # 0xe207
leaq -0xe0(%rbp), %rdi
leaq -0x3a(%rbp), %rdx
callq 0x40f0
leaq -0x70(%rbp), %rdi
leaq -0xe0(%rbp), %rdx
movq -0x48(%rbp), %rsi
callq 0x5886
leaq -0x100(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x585d
leaq 0x92e3(%rip), %rsi      # 0xe20a
leaq -0xc0(%rbp), %rdi
leaq -0x39(%rbp), %rdx
callq 0x40f0
movb $0x1, %r15b
leaq 0x913c(%rip), %rcx      # 0xe07d
leaq -0x100(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
movq %rbx, %rdi
callq 0x5564
xorl %r15d, %r15d
leaq 0xf967(%rip), %rsi      # 0x148c8
leaq 0x8be(%rip), %rdx       # 0x5826
movq %rbx, %rdi
callq 0x4490
jmp 0x4fdd
pushq $0x28
popq %rdi
callq 0x4150
movq %rax, %rbx
leaq -0x1c0(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x43a0
leaq 0x9201(%rip), %rsi      # 0xe195
leaq -0x1a0(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x40f0
movb $0x1, %r15b
leaq 0x90cf(%rip), %rcx      # 0xe07d
leaq -0x1c0(%rbp), %rsi
leaq -0x1a0(%rbp), %rdx
movq %rbx, %rdi
callq 0x5564
xorl %r15d, %r15d
leaq 0xf8fa(%rip), %rsi      # 0x148c8
leaq 0x851(%rip), %rdx       # 0x5826
movq %rbx, %rdi
callq 0x4490
movq %rax, %r14
leaq -0xc0(%rbp), %rdi
callq 0x4130
jmp 0x4ff4
movq %rax, %r14
movb $0x1, %r15b
leaq -0x100(%rbp), %rdi
callq 0x4130
jmp 0x502b
movq %rax, %r14
leaq -0x1a0(%rbp), %rdi
callq 0x4130
jmp 0x5019
movq %rax, %r14
movb $0x1, %r15b
leaq -0x1c0(%rbp), %rdi
jmp 0x50f8
movq %rax, %r14
movb $0x1, %r15b
leaq -0x70(%rbp), %rdi
callq 0x4130
jmp 0x5041
jmp 0x510a
movq %rax, %r14
movb $0x1, %r15b
leaq -0xe0(%rbp), %rdi
jmp 0x50f8
jmp 0x510a
movq %rax, %r14
leaq -0x260(%rbp), %rdi
callq 0x4130
jmp 0x5069
movq %rax, %r14
movb $0x1, %r15b
leaq -0x280(%rbp), %rdi
jmp 0x50f8
jmp 0x510a
movq %rax, %r14
leaq -0x220(%rbp), %rdi
callq 0x4130
jmp 0x5091
movq %rax, %r14
movb $0x1, %r15b
leaq -0x240(%rbp), %rdi
jmp 0x50f8
movq %rax, %r14
leaq -0x1e0(%rbp), %rdi
callq 0x4130
jmp 0x50b1
movq %rax, %r14
movb $0x1, %r15b
leaq -0x200(%rbp), %rdi
jmp 0x50f8
movq %rax, %r14
leaq -0x120(%rbp), %rdi
callq 0x4130
jmp 0x50d1
movq %rax, %r14
movb $0x1, %r15b
leaq -0x140(%rbp), %rdi
jmp 0x50f8
movq %rax, %r14
leaq -0x160(%rbp), %rdi
callq 0x4130
jmp 0x50f1
movq %rax, %r14
movb $0x1, %r15b
leaq -0x180(%rbp), %rdi
callq 0x4130
testb %r15b, %r15b
jne 0x510d
jmp 0x5115
jmp 0x510a
jmp 0x510a
jmp 0x510a
movq %rax, %r14
movq %rbx, %rdi
callq 0x41d0
movq %r14, %rdi
callq 0x44b0
nop