// File: /workspace/github_cpp_makefile_star2_O3/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 $0x248, %rsp            # imm = 0x248
movq %r9, -0x70(%rbp)
movq %rcx, -0x30(%rbp)
movq %rsi, -0x78(%rbp)
movq 0x8(%r8), %rcx
testq %rcx, %rcx
je 0x4f6d
movq %rdi, %r13
movq (%r8), %rdi
cmpb $0x4d, (%rdi)
jne 0x51e2
cmpb $0x44, 0x1(%rdi)
jne 0x51e2
cmpb $0x3a, 0x2(%rdi)
jne 0x51e2
cmpb $0x5a, 0x3(%rdi)
jne 0x51e2
cmpb $0x3a, 0x4(%rdi)
jne 0x51e2
leaq 0x5(%rdi), %rbx
movb 0x5(%rdi), %al
leal -0x30(%rax), %esi
movq $-0x1, %r12
cmpb $0x9, %sil
ja 0x4c33
xorl %r12d, %r12d
leaq (%r12,%r12,4), %rsi
movzbl %al, %eax
leaq (%rax,%rsi,2), %r12
addq $-0x30, %r12
movb 0x1(%rbx), %al
incq %rbx
leal -0x30(%rax), %esi
cmpb $0xa, %sil
jb 0x4c12
decq %r12
addq %rcx, %rdi
cmpq %rdi, %rbx
movq %r8, -0x80(%rbp)
je 0x4e09
movq %rdx, -0x68(%rbp)
leaq 0xd0(%r13), %rcx
movq %rcx, -0x60(%rbp)
movq -0x70(%rbp), %rcx
incq %rcx
movq %rcx, -0x88(%rbp)
xorl %r14d, %r14d
cmpb $0x5e, %al
jne 0x4d16
movb 0x1(%rbx), %al
incq %rbx
leal -0x3a(%rax), %ecx
cmpb $-0xb, %cl
ja 0x4dd8
movq -0x88(%rbp), %rcx
leaq (%rcx,%r12), %r15
movq (%r8), %rcx
addq 0x8(%r8), %rcx
cmpq %rcx, %rbx
je 0x4dcc
leaq (%r15,%r14), %rcx
movl $0x2, -0x58(%rbp)
movq %rcx, -0x50(%rbp)
movq %r12, -0x48(%rbp)
movb %al, -0x40(%rbp)
movb $0x2d, -0x3f(%rbp)
movq $0x28, -0x38(%rbp)
movq 0xd8(%r13), %rsi
cmpq 0xe0(%r13), %rsi
je 0x4ceb
movq -0x38(%rbp), %rax
movq %rax, 0x20(%rsi)
movups -0x58(%rbp), %xmm0
movups -0x48(%rbp), %xmm1
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x28, 0xd8(%r13)
jmp 0x4cfc
movq -0x60(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x64de
movq -0x80(%rbp), %r8
movb 0x1(%rbx), %al
incq %rbx
leal -0x3a(%rax), %ecx
incq %r14
cmpb $-0xa, %cl
jb 0x4c88
jmp 0x4dd8
leal -0x5b(%rax), %ecx
cmpb $-0x1b, %cl
jbe 0x50b9
leaq 0x1(%r12), %r15
movq -0x78(%rbp), %rcx
movq 0x8(%rcx), %rdx
cmpq %r15, %rdx
jbe 0x5014
movq -0x68(%rbp), %rsi
movq 0x8(%rsi), %rdx
cmpq %r15, %rdx
jbe 0x5014
movq -0x70(%rbp), %rcx
addq %r14, %rcx
leaq 0x1(%r12,%rcx), %rcx
movq -0x78(%rbp), %rdx
movq (%rdx), %rdx
movb (%rdx,%r15), %dl
movq (%rsi), %rsi
movsbq (%rsi,%r15), %rsi
movl $0x4, -0x58(%rbp)
movq %rcx, -0x50(%rbp)
movq %r15, -0x48(%rbp)
movb %al, -0x40(%rbp)
movb %dl, -0x3f(%rbp)
movq %rsi, -0x38(%rbp)
movq 0xd8(%r13), %rsi
cmpq 0xe0(%r13), %rsi
je 0x4db2
movq -0x38(%rbp), %rax
movq %rax, 0x20(%rsi)
movups -0x58(%rbp), %xmm0
movups -0x48(%rbp), %xmm1
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x28, 0xd8(%r13)
jmp 0x4dc3
movq -0x60(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x64de
movq -0x80(%rbp), %r8
movb 0x1(%rbx), %al
incq %rbx
movq %r15, %r12
leal -0x3a(%rax), %ecx
cmpb $-0xa, %cl
jb 0x5025
xorl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
movzbl %al, %eax
leal (%rax,%rcx,2), %ecx
addl $-0x30, %ecx
movb 0x1(%rbx), %al
incq %rbx
leal -0x30(%rax), %edx
cmpb $0xa, %dl
jb 0x4dda
movl %ecx, %ecx
addq %rcx, %r12
movq (%r8), %rcx
addq 0x8(%r8), %rcx
cmpq %rcx, %rbx
jne 0x4c63
movq -0x30(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movzbl -0x1(%rax,%rcx), %edx
addl $-0x44, %edx
cmpl $0x9, %edx
ja 0x5333
movl $0x221, %esi            # imm = 0x221
btl %edx, %esi
jae 0x5333
xorl %r9d, %r9d
testq %rcx, %rcx
movq -0x70(%rbp), %rbx
je 0x4f50
leaq 0xd0(%r13), %rdx
movq %rdx, -0x68(%rbp)
movq %rax, %r15
movb (%r15), %dl
leal -0x30(%rdx), %esi
cmpb $0x9, %sil
ja 0x4f7f
incq %r15
xorl %r14d, %r14d
leaq (%r14,%r14,4), %rsi
movzbl %dl, %edx
leaq (%rdx,%rsi,2), %r14
addq $-0x30, %r14
movb (%r15), %dl
leal -0x30(%rdx), %esi
incq %r15
cmpb $0xa, %sil
jb 0x4e67
testq %r14, %r14
je 0x4f7f
movzbl %dl, %edx
cmpl $0x44, %edx
je 0x4ea7
cmpl $0x49, %edx
je 0x4eaf
cmpl $0x4d, %edx
jne 0x514d
addl %r14d, %r9d
addq %r14, %rbx
jmp 0x4f40
movq 0xd0(%r13), %rsi
movq 0xd8(%r13), %rax
cmpq %rax, %rsi
je 0x4ef2
movslq %r9d, %rcx
movq %rsi, %rdx
movq 0x10(%rdx), %rdi
cmpq %rcx, %rdi
jb 0x4ed8
addq %r14, %rdi
movq %rdi, 0x10(%rdx)
addq $0x28, %rdx
cmpq %rax, %rdx
jne 0x4ec8
jmp 0x4ee7
addq $0x28, %rsi
cmpq %rax, %rsi
je 0x4ef2
cmpq %rcx, 0x10(%rsi)
jb 0x4ee3
addq %r14, %r12
leaq -0x1(%rbx), %rax
leal -0x1(%r9), %ecx
movslq %ecx, %rcx
movl $0x1, -0x58(%rbp)
movq %rax, -0x50(%rbp)
movq %rcx, -0x48(%rbp)
movw $0x2d2d, -0x40(%rbp)    # imm = 0x2D2D
movq $0x2d, -0x38(%rbp)
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rdx
movq %r9, -0x60(%rbp)
callq 0x6690
addl -0x60(%rbp), %r14d
movq -0x30(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movl %r14d, %r9d
leaq (%rax,%rcx), %rdx
cmpq %rdx, %r15
jne 0x4e51
movslq %r9d, %r9
incq %r12
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
cmpq %rax, %r12
jne 0x5276
cmpq %r9, %rax
jne 0x5276
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
callq 0x4150
movq %rax, %rbx
leaq -0xd8(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0xe8(%rbp), %rdi
callq 0x63a2
leaq -0x1b8(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xc1f7(%rip), %rsi      # 0x111ba
leaq 0xc214(%rip), %rdx      # 0x111de
leaq -0x1c8(%rbp), %rdi
callq 0x6440
movb $0x1, %r12b
leaq 0xc09d(%rip), %rcx      # 0x1107d
leaq -0xe8(%rbp), %rsi
leaq -0x1c8(%rbp), %rdx
movq %rbx, %rdi
callq 0x5af8
xorl %r12d, %r12d
leaq 0x128c0(%rip), %rsi     # 0x178c0
leaq 0x11c9(%rip), %rdx      # 0x61d0
movq %rbx, %rdi
callq 0x44c0
jmp 0x53c3
leaq 0xc27a(%rip), %rdi      # 0x11295
movq %r15, %rsi
xorl %eax, %eax
callq 0x4410
movl $0x28, %edi
movq %r8, %r14
callq 0x4150
movq %rax, %rbx
leaq -0x118(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x128(%rbp), %rdi
callq 0x63a2
leaq -0x1f8(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xc0d3(%rip), %rsi      # 0x1113b
leaq 0xc125(%rip), %rdx      # 0x11194
leaq -0x208(%rbp), %rdi
callq 0x6440
movb $0x1, %r12b
leaq 0xbff8(%rip), %rcx      # 0x1107d
leaq -0x128(%rbp), %rsi
leaq -0x208(%rbp), %rdx
movq %rbx, %rdi
callq 0x5af8
xorl %r12d, %r12d
leaq 0x1281b(%rip), %rsi     # 0x178c0
leaq 0x1124(%rip), %rdx      # 0x61d0
movq %rbx, %rdi
callq 0x44c0
jmp 0x53c3
movl $0x28, %edi
movq %r8, %r14
callq 0x4150
movq %rax, %rbx
leaq -0x138(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x148(%rbp), %rdi
callq 0x63a2
leaq -0x218(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xbfe2(%rip), %rsi      # 0x110de
leaq 0xc037(%rip), %rdx      # 0x1113a
leaq -0x228(%rbp), %rdi
callq 0x6440
movb $0x1, %r12b
leaq 0xbf64(%rip), %rcx      # 0x1107d
leaq -0x148(%rbp), %rsi
leaq -0x228(%rbp), %rdx
movq %rbx, %rdi
callq 0x5af8
xorl %r12d, %r12d
leaq 0x12787(%rip), %rsi     # 0x178c0
leaq 0x1090(%rip), %rdx      # 0x61d0
movq %rbx, %rdi
callq 0x44c0
jmp 0x53c3
movl $0x28, %edi
callq 0x4150
movq %rax, %rbx
leaq -0xb8(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0xc8(%rbp), %rdi
callq 0x63a2
leaq -0x198(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xc04e(%rip), %rsi      # 0x111df
leaq 0xc06e(%rip), %rdx      # 0x11206
leaq -0x1a8(%rbp), %rdi
callq 0x6440
movb $0x1, %r12b
leaq 0xbecf(%rip), %rcx      # 0x1107d
leaq -0xc8(%rbp), %rsi
leaq -0x1a8(%rbp), %rdx
movq %rbx, %rdi
callq 0x5af8
xorl %r12d, %r12d
leaq 0x126f2(%rip), %rsi     # 0x178c0
leaq 0xffb(%rip), %rdx       # 0x61d0
movq %rbx, %rdi
callq 0x44c0
jmp 0x53c3
movl $0x28, %edi
movq %r8, %r14
callq 0x4150
movq %rax, %rbx
leaq -0x158(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x168(%rbp), %rdi
callq 0x63a2
leaq -0x238(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xbde5(%rip), %rsi      # 0x1100a
leaq 0xbe50(%rip), %rdx      # 0x1107c
leaq -0x248(%rbp), %rdi
callq 0x6440
movb $0x1, %r12b
leaq 0xbe3b(%rip), %rcx      # 0x1107d
leaq -0x168(%rbp), %rsi
leaq -0x248(%rbp), %rdx
movq %rbx, %rdi
callq 0x5af8
xorl %r12d, %r12d
leaq 0x1265e(%rip), %rsi     # 0x178c0
leaq 0xf67(%rip), %rdx       # 0x61d0
movq %rbx, %rdi
callq 0x44c0
jmp 0x53c3
movl $0x28, %edi
callq 0x4150
movq %rax, %rbx
leaq -0x98(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0xbf72(%rip), %rsi      # 0x11207
leaq 0xbf6d(%rip), %rdx      # 0x11209
leaq -0xa8(%rbp), %rdi
callq 0x6440
leaq -0x58(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
movq -0x30(%rbp), %rsi
callq 0x6267
leaq -0x268(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movq -0x80(%rbp), %rdx
callq 0x6203
leaq -0x178(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xbf28(%rip), %rsi      # 0x1120a
leaq 0xbf54(%rip), %rdx      # 0x1123d
leaq -0x188(%rbp), %rdi
callq 0x6440
movb $0x1, %r12b
leaq 0xbd7e(%rip), %rcx      # 0x1107d
leaq -0x268(%rbp), %rsi
leaq -0x188(%rbp), %rdx
movq %rbx, %rdi
callq 0x5af8
xorl %r12d, %r12d
leaq 0x125a1(%rip), %rsi     # 0x178c0
leaq 0xeaa(%rip), %rdx       # 0x61d0
movq %rbx, %rdi
callq 0x44c0
jmp 0x53c3
movl $0x28, %edi
callq 0x4150
movq %rax, %rbx
leaq -0xf8(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x108(%rbp), %rdi
callq 0x63a2
leaq -0x1d8(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xbe1e(%rip), %rsi      # 0x11195
leaq 0xbe3b(%rip), %rdx      # 0x111b9
leaq -0x1e8(%rbp), %rdi
callq 0x6440
movb $0x1, %r12b
leaq 0xbce9(%rip), %rcx      # 0x1107d
leaq -0x108(%rbp), %rsi
leaq -0x1e8(%rbp), %rdx
movq %rbx, %rdi
callq 0x5af8
xorl %r12d, %r12d
leaq 0x1250c(%rip), %rsi     # 0x178c0
leaq 0xe15(%rip), %rdx       # 0x61d0
movq %rbx, %rdi
callq 0x44c0
movq %rax, %r14
movq -0x188(%rbp), %rdi
cmpq %r13, %rdi
je 0x53df
callq 0x4280
jmp 0x53df
movq %rax, %r14
movb $0x1, %r12b
leaq -0x258(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x542b
callq 0x4280
jmp 0x542b
movq %rax, %r14
movq -0x1e8(%rbp), %rdi
cmpq %r13, %rdi
je 0x540a
callq 0x4280
movq -0x108(%rbp), %rdi
jmp 0x5535
movq %rax, %r14
movq -0x108(%rbp), %rdi
jmp 0x5550
movq %rax, %r14
movb $0x1, %r12b
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x543d
callq 0x4280
movq -0xa8(%rbp), %rdi
jmp 0x5535
jmp 0x555c
movq %rax, %r14
movq -0xa8(%rbp), %rdi
jmp 0x5550
jmp 0x555c
movq %rax, %r14
movq -0x248(%rbp), %rdi
cmpq %r13, %rdi
je 0x5476
callq 0x4280
movq -0x168(%rbp), %rdi
jmp 0x5535
movq %rax, %r14
movq -0x168(%rbp), %rdi
jmp 0x5550
movq %rax, %r14
movq -0x1a8(%rbp), %rdi
cmpq %r13, %rdi
je 0x54a5
callq 0x4280
movq -0xc8(%rbp), %rdi
jmp 0x5535
movq %rax, %r14
movq -0xc8(%rbp), %rdi
jmp 0x5550
movq %rax, %r14
movq -0x228(%rbp), %rdi
cmpq %r13, %rdi
je 0x54d4
callq 0x4280
movq -0x148(%rbp), %rdi
jmp 0x5535
movq %rax, %r14
movq -0x148(%rbp), %rdi
jmp 0x5550
jmp 0x555c
movq %rax, %r14
movq -0x208(%rbp), %rdi
cmpq %r13, %rdi
je 0x54ff
callq 0x4280
movq -0x128(%rbp), %rdi
jmp 0x5535
movq %rax, %r14
movq -0x128(%rbp), %rdi
jmp 0x5550
jmp 0x555c
jmp 0x555c
jmp 0x555c
movq %rax, %r14
movq -0x1c8(%rbp), %rdi
cmpq %r13, %rdi
je 0x552e
callq 0x4280
movq -0xe8(%rbp), %rdi
cmpq %r15, %rdi
je 0x553f
callq 0x4280
testb %r12b, %r12b
jne 0x555f
jmp 0x5567
movq %rax, %r14
movq -0xe8(%rbp), %rdi
cmpq %r15, %rdi
je 0x555f
callq 0x4280
jmp 0x555f
movq %rax, %r14
movq %rbx, %rdi
callq 0x41f0
movq %r14, %rdi
callq 0x44f0
nop