// File: /workspace/github_cpp_makefile_star2_O1/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, -0x60(%rbp)
movq %rsi, -0x78(%rbp)
cmpq $0x0, 0x8(%r8)
je 0x4fa2
movq (%r8), %r12
cmpb $0x4d, (%r12)
jne 0x5215
cmpb $0x44, 0x1(%r12)
jne 0x5215
cmpb $0x3a, 0x2(%r12)
jne 0x5215
cmpb $0x5a, 0x3(%r12)
jne 0x5215
cmpb $0x3a, 0x4(%r12)
jne 0x5215
movq %rdi, %r13
movb 0x5(%r12), %al
addq $0x5, %r12
leal -0x30(%rax), %ecx
movq $-0x1, %rbx
cmpb $0x9, %cl
ja 0x4c4c
xorl %ebx, %ebx
leaq (%rbx,%rbx,4), %rcx
movzbl %al, %eax
leaq (%rax,%rcx,2), %rbx
addq $-0x30, %rbx
movb 0x1(%r12), %al
incq %r12
leal -0x30(%rax), %ecx
cmpb $0xa, %cl
jb 0x4c2a
decq %rbx
movq (%r8), %rax
addq 0x8(%r8), %rax
cmpq %rax, %r12
movq %r8, -0x80(%rbp)
je 0x4e41
movq %rdx, -0x68(%rbp)
leaq 0xd0(%r13), %rax
movq %rax, -0x58(%rbp)
movq -0x70(%rbp), %rax
incq %rax
movq %rax, -0x88(%rbp)
xorl %r15d, %r15d
movb (%r12), %al
cmpb $0x5e, %al
jne 0x4d3a
movb 0x1(%r12), %al
incq %r12
addb $-0x3a, %al
cmpb $-0xb, %al
ja 0x4d32
movq -0x88(%rbp), %rax
leaq (%rax,%rbx), %r14
movq (%r8), %rax
addq 0x8(%r8), %rax
cmpq %rax, %r12
je 0x4d32
leaq (%r14,%r15), %rax
movb (%r12), %cl
movl $0x2, -0x50(%rbp)
movq %rax, -0x48(%rbp)
movq %rbx, -0x40(%rbp)
movb %cl, -0x38(%rbp)
movb $0x2d, -0x37(%rbp)
movq $0x28, -0x30(%rbp)
movq 0xd8(%r13), %rsi
cmpq 0xe0(%r13), %rsi
je 0x4d0c
movq -0x30(%rbp), %rax
movq %rax, 0x20(%rsi)
movups -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x28, 0xd8(%r13)
jmp 0x4d1d
movq -0x58(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x64d6
movq -0x80(%rbp), %r8
movb 0x1(%r12), %al
incq %r12
addb $-0x3a, %al
incq %r15
cmpb $-0xa, %al
jb 0x4ca9
movq %rbx, %r14
jmp 0x4deb
addb $-0x5b, %al
cmpb $-0x1b, %al
jbe 0x50ed
leaq 0x1(%rbx), %r14
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rdx
cmpq %r14, %rdx
jbe 0x5048
movq -0x68(%rbp), %rsi
movq 0x8(%rsi), %rdx
cmpq %r14, %rdx
jbe 0x5048
movq -0x70(%rbp), %rax
addq %r15, %rax
leaq 0x1(%rbx,%rax), %rax
movb (%r12), %cl
movq -0x78(%rbp), %rdx
movq (%rdx), %rdx
movb (%rdx,%r14), %dl
movq (%rsi), %rsi
movsbq (%rsi,%r14), %rsi
movl $0x4, -0x50(%rbp)
movq %rax, -0x48(%rbp)
movq %r14, -0x40(%rbp)
movb %cl, -0x38(%rbp)
movb %dl, -0x37(%rbp)
movq %rsi, -0x30(%rbp)
movq 0xd8(%r13), %rsi
cmpq 0xe0(%r13), %rsi
je 0x4dd7
movq -0x30(%rbp), %rax
movq %rax, 0x20(%rsi)
movups -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x28, 0xd8(%r13)
jmp 0x4de8
movq -0x58(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x64d6
movq -0x80(%rbp), %r8
incq %r12
movb (%r12), %al
addb $-0x3a, %al
cmpb $-0xa, %al
jb 0x4fb4
movb (%r12), %al
leal -0x30(%rax), %ecx
cmpb $0x9, %cl
ja 0x4e27
xorl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
movzbl %al, %eax
leal (%rax,%rcx,2), %ecx
addl $-0x30, %ecx
movb 0x1(%r12), %al
incq %r12
leal -0x30(%rax), %edx
cmpb $0xa, %dl
jb 0x4e07
movl %ecx, %eax
jmp 0x4e29
xorl %eax, %eax
addq %rax, %r14
movq (%r8), %rax
addq 0x8(%r8), %rax
movq %r14, %rbx
cmpq %rax, %r12
jne 0x4c80
jmp 0x4e44
movq %rbx, %r14
movq -0x60(%rbp), %rdx
movq (%rdx), %r15
movq 0x8(%rdx), %rax
movzbl -0x1(%r15,%rax), %eax
addl $-0x44, %eax
cmpl $0x9, %eax
ja 0x5366
movl $0x221, %ecx            # imm = 0x221
btl %eax, %ecx
jae 0x5366
movq (%rdx), %rax
addq 0x8(%rdx), %rax
xorl %r9d, %r9d
cmpq %rax, %r15
movq -0x70(%rbp), %r12
je 0x4f85
leaq 0xd0(%r13), %rax
movq %rax, -0x68(%rbp)
movb (%r15), %al
leal -0x30(%rax), %ecx
xorl %ebx, %ebx
cmpb $0x9, %cl
ja 0x4ebc
leaq (%rbx,%rbx,4), %rcx
movzbl %al, %eax
leaq (%rax,%rcx,2), %rbx
addq $-0x30, %rbx
movb 0x1(%r15), %al
incq %r15
leal -0x30(%rax), %ecx
cmpb $0xa, %cl
jb 0x4e9e
testq %rbx, %rbx
je 0x5059
cmpb $0x44, %al
je 0x4edd
movzbl %al, %eax
cmpl $0x49, %eax
je 0x4ee5
cmpl $0x4d, %eax
jne 0x5181
addl %ebx, %r9d
addq %rbx, %r12
jmp 0x4f6f
movq 0xd0(%r13), %rsi
movq 0xd8(%r13), %rax
movslq %r9d, %rcx
cmpq %rax, %rsi
je 0x4f1d
movq %rsi, %rdx
movq 0x10(%rdx), %rdi
cmpq %rcx, %rdi
jb 0x4f0e
addq %rbx, %rdi
movq %rdi, 0x10(%rdx)
addq $0x28, %rdx
cmpq %rax, %rdx
jne 0x4efe
jmp 0x4f1d
addq $0x28, %rsi
cmpq %rax, %rsi
je 0x4f28
cmpq %rcx, 0x10(%rsi)
jb 0x4f19
addq %rbx, %r14
leaq -0x1(%r12), %rax
leal -0x1(%r9), %ecx
movslq %ecx, %rcx
movl $0x1, -0x50(%rbp)
movq %rax, -0x48(%rbp)
movq %rcx, -0x40(%rbp)
movw $0x2d2d, -0x38(%rbp)    # imm = 0x2D2D
movq $0x2d, -0x30(%rbp)
movq -0x68(%rbp), %rdi
leaq -0x50(%rbp), %rdx
movq %r9, -0x58(%rbp)
callq 0x6688
addl -0x58(%rbp), %ebx
movl %ebx, %r9d
movq -0x60(%rbp), %rdx
incq %r15
movq (%rdx), %rax
addq 0x8(%rdx), %rax
cmpq %rax, %r15
jne 0x4e91
movslq %r9d, %r9
incq %r14
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
cmpq %rax, %r14
jne 0x52a9
cmpq %r9, %rax
jne 0x52a9
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
movq %r8, %r14
callq 0x4150
movq %rax, %rbx
leaq -0x1b8(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x1c8(%rbp), %rdi
callq 0x63a2
leaq -0x198(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xc146(%rip), %rsi      # 0x1113d
leaq 0xc198(%rip), %rdx      # 0x11196
leaq -0x1a8(%rbp), %rdi
callq 0x643c
movb $0x1, %r12b
leaq 0xc06b(%rip), %rcx      # 0x1107f
leaq -0x1c8(%rbp), %rsi
leaq -0x1a8(%rbp), %rdx
movq %rbx, %rdi
callq 0x5ae8
xorl %r12d, %r12d
leaq 0x12854(%rip), %rsi     # 0x17888
leaq 0x1195(%rip), %rdx      # 0x61d0
movq %rbx, %rdi
callq 0x44d0
jmp 0x53f5
leaq 0xc248(%rip), %rdi      # 0x11297
movq %r14, %rsi
xorl %eax, %eax
callq 0x4420
movl $0x28, %edi
movq %rdx, %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 -0x118(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xc120(%rip), %rsi      # 0x111bc
leaq 0xc13d(%rip), %rdx      # 0x111e0
leaq -0x128(%rbp), %rdi
callq 0x643c
movb $0x1, %r12b
leaq 0xbfc6(%rip), %rcx      # 0x1107f
leaq -0x148(%rbp), %rsi
leaq -0x128(%rbp), %rdx
movq %rbx, %rdi
callq 0x5ae8
xorl %r12d, %r12d
leaq 0x127af(%rip), %rsi     # 0x17888
leaq 0x10f0(%rip), %rdx      # 0x61d0
movq %rbx, %rdi
callq 0x44d0
jmp 0x53f5
movl $0x28, %edi
movq %r8, %r14
callq 0x4150
movq %rax, %rbx
leaq -0x1f8(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x208(%rbp), %rdi
callq 0x63a2
leaq -0x1d8(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xbfb0(%rip), %rsi      # 0x110e0
leaq 0xc005(%rip), %rdx      # 0x1113c
leaq -0x1e8(%rbp), %rdi
callq 0x643c
movb $0x1, %r12b
leaq 0xbf32(%rip), %rcx      # 0x1107f
leaq -0x208(%rbp), %rsi
leaq -0x1e8(%rbp), %rdx
movq %rbx, %rdi
callq 0x5ae8
xorl %r12d, %r12d
leaq 0x1271b(%rip), %rsi     # 0x17888
leaq 0x105c(%rip), %rdx      # 0x61d0
movq %rbx, %rdi
callq 0x44d0
jmp 0x53f5
movl $0x28, %edi
movq %rdx, %r14
callq 0x4150
movq %rax, %rbx
leaq -0xf8(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x108(%rbp), %rdi
callq 0x63a2
leaq -0xd8(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xc01d(%rip), %rsi      # 0x111e1
leaq 0xc03d(%rip), %rdx      # 0x11208
leaq -0xe8(%rbp), %rdi
callq 0x643c
movb $0x1, %r12b
leaq 0xbe9e(%rip), %rcx      # 0x1107f
leaq -0x108(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
movq %rbx, %rdi
callq 0x5ae8
xorl %r12d, %r12d
leaq 0x12687(%rip), %rsi     # 0x17888
leaq 0xfc8(%rip), %rdx       # 0x61d0
movq %rbx, %rdi
callq 0x44d0
jmp 0x53f5
movl $0x28, %edi
movq %r8, %r14
callq 0x4150
movq %rax, %rbx
leaq -0x238(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x248(%rbp), %rdi
callq 0x63a2
leaq -0x218(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xbdb4(%rip), %rsi      # 0x1100c
leaq 0xbe1f(%rip), %rdx      # 0x1107e
leaq -0x228(%rbp), %rdi
callq 0x643c
movb $0x1, %r12b
leaq 0xbe0a(%rip), %rcx      # 0x1107f
leaq -0x248(%rbp), %rsi
leaq -0x228(%rbp), %rdx
movq %rbx, %rdi
callq 0x5ae8
xorl %r12d, %r12d
leaq 0x125f3(%rip), %rsi     # 0x17888
leaq 0xf34(%rip), %rdx       # 0x61d0
movq %rbx, %rdi
callq 0x44d0
jmp 0x53f5
movl $0x28, %edi
callq 0x4150
movq %rax, %rbx
leaq -0xb8(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0xbf41(%rip), %rsi      # 0x11209
leaq 0xbf3c(%rip), %rdx      # 0x1120b
leaq -0xc8(%rbp), %rdi
callq 0x643c
leaq -0x50(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
movq -0x60(%rbp), %rsi
callq 0x6267
leaq -0x268(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq -0x80(%rbp), %rdx
callq 0x6203
leaq -0x98(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xbef7(%rip), %rsi      # 0x1120c
leaq 0xbf23(%rip), %rdx      # 0x1123f
leaq -0xa8(%rbp), %rdi
callq 0x643c
movb $0x1, %r12b
leaq 0xbd4d(%rip), %rcx      # 0x1107f
leaq -0x268(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
movq %rbx, %rdi
callq 0x5ae8
xorl %r12d, %r12d
leaq 0x12536(%rip), %rsi     # 0x17888
leaq 0xe77(%rip), %rdx       # 0x61d0
movq %rbx, %rdi
callq 0x44d0
jmp 0x53f5
movl $0x28, %edi
movq %rdx, %r14
callq 0x4150
movq %rax, %rbx
leaq -0x178(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x188(%rbp), %rdi
callq 0x63a2
leaq -0x158(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0xbdee(%rip), %rsi      # 0x11197
leaq 0xbe0b(%rip), %rdx      # 0x111bb
leaq -0x168(%rbp), %rdi
callq 0x643c
movb $0x1, %r12b
leaq 0xbcb9(%rip), %rcx      # 0x1107f
leaq -0x188(%rbp), %rsi
leaq -0x168(%rbp), %rdx
movq %rbx, %rdi
callq 0x5ae8
xorl %r12d, %r12d
leaq 0x124a2(%rip), %rsi     # 0x17888
leaq 0xde3(%rip), %rdx       # 0x61d0
movq %rbx, %rdi
callq 0x44d0
movq %rax, %r14
movq -0xa8(%rbp), %rdi
cmpq %r13, %rdi
je 0x5411
callq 0x4280
jmp 0x5411
movq %rax, %r14
movb $0x1, %r12b
leaq -0x258(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5456
callq 0x4280
jmp 0x5456
movq %rax, %r14
movq -0x168(%rbp), %rdi
cmpq %r13, %rdi
je 0x5444
callq 0x4280
jmp 0x5444
movq %rax, %r14
movb $0x1, %r12b
movq -0x188(%rbp), %rdi
jmp 0x5544
movq %rax, %r14
movb $0x1, %r12b
leaq -0x40(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5475
callq 0x4280
jmp 0x5475
jmp 0x5554
movq %rax, %r14
movb $0x1, %r12b
movq -0xc8(%rbp), %rdi
jmp 0x5544
jmp 0x5554
movq %rax, %r14
movq -0x228(%rbp), %rdi
cmpq %r13, %rdi
je 0x54a2
callq 0x4280
jmp 0x54a2
movq %rax, %r14
movb $0x1, %r12b
movq -0x248(%rbp), %rdi
jmp 0x5544
movq %rax, %r14
movq -0xe8(%rbp), %rdi
cmpq %r13, %rdi
je 0x54ca
callq 0x4280
jmp 0x54ca
movq %rax, %r14
movb $0x1, %r12b
movq -0x108(%rbp), %rdi
jmp 0x5544
movq %rax, %r14
movq -0x1e8(%rbp), %rdi
cmpq %r13, %rdi
je 0x54ef
callq 0x4280
jmp 0x54ef
movq %rax, %r14
movb $0x1, %r12b
movq -0x208(%rbp), %rdi
jmp 0x5544
jmp 0x5554
jmp 0x5554
movq %rax, %r14
movq -0x128(%rbp), %rdi
cmpq %r13, %rdi
je 0x5518
callq 0x4280
jmp 0x5518
movq %rax, %r14
movb $0x1, %r12b
movq -0x148(%rbp), %rdi
jmp 0x5544
movq %rax, %r14
movq -0x1a8(%rbp), %rdi
cmpq %r13, %rdi
je 0x553d
callq 0x4280
jmp 0x553d
movq %rax, %r14
movb $0x1, %r12b
movq -0x1c8(%rbp), %rdi
cmpq %r15, %rdi
je 0x555a
callq 0x4280
jmp 0x555a
jmp 0x5554
jmp 0x5554
movq %rax, %r14
movb $0x1, %r12b
testb %r12b, %r12b
je 0x5567
movq %rbx, %rdi
callq 0x41f0
movq %r14, %rdi
callq 0x4500
nop