// File: medvedevgroup[P]varmatch/src/wholegenome.cpp
// Function: WholeGenome::NormalizeVariantSequence(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, int)
int WholeGenome::NormalizeVariantSequence(int pos, string & parsimonious_ref, string & parsimonious_alt0, string & parsimonious_alt1, int chr_id) {

	int left_index = pos;
	if (genome_sequences[chr_id].size() == 0) return -1;
	if (parsimonious_ref.size() == 1 && parsimonious_alt0.size() == 1 && parsimonious_alt1.size() == 1) return true;

	bool change_in_allels = true;
	while (change_in_allels) {
		change_in_allels = false;
		if (parsimonious_ref.back() == parsimonious_alt0.back() && parsimonious_ref.back() == parsimonious_alt1.back() ) {
			if ((parsimonious_ref.size() > 1 && parsimonious_alt0.size() > 1 && parsimonious_alt1.size() > 1) || left_index > 0) { // when left_index == 0, can not make further changes
				parsimonious_ref.pop_back();
				parsimonious_alt0.pop_back();
				parsimonious_alt1.pop_back();
				change_in_allels = true;
			}
            // else do not make further changes
		}
		if (parsimonious_ref.length() == 0 || parsimonious_alt0.length() == 0 || parsimonious_alt1.length() == 0) {
			left_index--;
			char left_char = toupper(genome_sequences[chr_id][left_index]);
			parsimonious_ref = left_char + parsimonious_ref;
			parsimonious_alt0 = left_char + parsimonious_alt0;
			parsimonious_alt1 = left_char + parsimonious_alt1;
		}
	}
	while (parsimonious_ref[0] == parsimonious_alt0[0] &&
            parsimonious_ref[0] == parsimonious_alt1[0] &&
            parsimonious_ref.size() > 1 &&
            parsimonious_alt0.size() > 1 &&
            parsimonious_alt1.size() > 1)
    {
		parsimonious_ref.erase(0, 1);
		parsimonious_alt0.erase(0, 1);
		parsimonious_alt1.erase(0, 1);
        left_index ++; // left_index indicates variant position, if truncate the leftmost, then
	}
	return left_index;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x38(%rbp)
addq $0x118, %rdi            # imm = 0x118
leaq -0x34(%rbp), %rsi
callq 0x32770
movq %rax, %rdi
callq 0x6320
cmpq $0x0, %rax
jne 0x3093d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x30c39
movq -0x20(%rbp), %rdi
callq 0x6320
cmpq $0x1, %rax
jne 0x30976
movq -0x28(%rbp), %rdi
callq 0x6320
cmpq $0x1, %rax
jne 0x30976
movq -0x30(%rbp), %rdi
callq 0x6320
cmpq $0x1, %rax
jne 0x30976
movl $0x1, -0x4(%rbp)
jmp 0x30c39
movb $0x1, -0x39(%rbp)
testb $0x1, -0x39(%rbp)
je 0x30b23
movb $0x0, -0x39(%rbp)
movq -0x20(%rbp), %rdi
callq 0x6340
movsbl (%rax), %eax
movl %eax, -0xac(%rbp)
movq -0x28(%rbp), %rdi
callq 0x6340
movq %rax, %rcx
movl -0xac(%rbp), %eax
movsbl (%rcx), %ecx
cmpl %ecx, %eax
jne 0x30a32
movq -0x20(%rbp), %rdi
callq 0x6340
movsbl (%rax), %eax
movl %eax, -0xb0(%rbp)
movq -0x30(%rbp), %rdi
callq 0x6340
movq %rax, %rcx
movl -0xb0(%rbp), %eax
movsbl (%rcx), %ecx
cmpl %ecx, %eax
jne 0x30a32
movq -0x20(%rbp), %rdi
callq 0x6320
cmpq $0x1, %rax
jbe 0x30a0b
movq -0x28(%rbp), %rdi
callq 0x6320
cmpq $0x1, %rax
jbe 0x30a0b
movq -0x30(%rbp), %rdi
callq 0x6320
cmpq $0x1, %rax
ja 0x30a11
cmpl $0x0, -0x38(%rbp)
jle 0x30a30
movq -0x20(%rbp), %rdi
callq 0x6520
movq -0x28(%rbp), %rdi
callq 0x6520
movq -0x30(%rbp), %rdi
callq 0x6520
movb $0x1, -0x39(%rbp)
jmp 0x30a32
movq -0x20(%rbp), %rdi
callq 0x6630
cmpq $0x0, %rax
je 0x30a63
movq -0x28(%rbp), %rdi
callq 0x6630
cmpq $0x0, %rax
je 0x30a63
movq -0x30(%rbp), %rdi
callq 0x6630
cmpq $0x0, %rax
jne 0x30b1e
movq -0xa8(%rbp), %rdi
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
addq $0x118, %rdi            # imm = 0x118
leaq -0x34(%rbp), %rsi
callq 0x32770
movq %rax, %rdi
movslq -0x38(%rbp), %rsi
callq 0x6810
movsbl (%rax), %edi
callq 0x6830
movb %al, -0x3a(%rbp)
movb -0x3a(%rbp), %al
movq -0x20(%rbp), %rdx
leaq -0x60(%rbp), %rdi
movsbl %al, %esi
callq 0x362a0
movq -0x20(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x6530
leaq -0x60(%rbp), %rdi
callq 0x6828
movb -0x3a(%rbp), %al
movq -0x28(%rbp), %rdx
leaq -0x80(%rbp), %rdi
movsbl %al, %esi
callq 0x362a0
movq -0x28(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x6530
leaq -0x80(%rbp), %rdi
callq 0x6828
movb -0x3a(%rbp), %al
movq -0x30(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
movsbl %al, %esi
callq 0x362a0
movq -0x30(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x6530
leaq -0xa0(%rbp), %rdi
callq 0x6828
jmp 0x3097a
jmp 0x30b25
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6810
movsbl (%rax), %eax
movl %eax, -0xb8(%rbp)
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6810
movl -0xb8(%rbp), %ecx
movsbl (%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0xb1(%rbp)
jne 0x30be3
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6810
movsbl (%rax), %eax
movl %eax, -0xbc(%rbp)
movq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6810
movl -0xbc(%rbp), %ecx
movsbl (%rax), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0xb1(%rbp)
jne 0x30be3
movq -0x20(%rbp), %rdi
callq 0x6320
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x1, %rcx
movb %al, -0xb1(%rbp)
jbe 0x30be3
movq -0x28(%rbp), %rdi
callq 0x6320
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x1, %rcx
movb %al, -0xb1(%rbp)
jbe 0x30be3
movq -0x30(%rbp), %rdi
callq 0x6320
cmpq $0x1, %rax
seta %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x30bef
jmp 0x30c33
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x60f0
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x60f0
movq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x60f0
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x30b25
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)