// File: drarwood[P]gags/GA.cpp
// Function: Mutate(std::vector<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, std::allocator<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>>&, std::vector<int, std::allocator<int>>&, double, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, double, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, double>>>&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, int>>>&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, int>>>&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, int>>>&)
void Mutate(vector<vector<string> >& chrXY, vector<int>& n, double m, vector<string>& s, map<string,double>& p, map<string, int>& chrXAlleles, map<string, int>& chrYAlleles, map<string, int>& e) {
    
    double min = 1/(double)chrXY[0].size();    
    if (min <= m) {
        
        int                      mutationIndex1;
        string                   mutationIndex1Allele;        
        int                      mutationIndex2;
        string                   mutationIndex2Allele;
        string                   newAllele;
        map<string,int>::iterator it;

        double random = min + ((double)rand() / RAND_MAX) * (m - min);        
        int mutations = round((double)chrXY[0].size() * random);
        
        // for each mutation to be performed // mutate each chromosome in turn
        for (int j = 0; j < mutations; ++j) {
            
            // CHROMOSOME X (0)
            // pick indicies at random in which alleles are to be swapper
            mutationIndex1       = rand() % chrXY[0].size();
            mutationIndex1Allele = chrXY[0][mutationIndex1];
            // pick and allele at random to place into this from the full set of IDs. This will help get round local maxima
            do {
                mutationIndex2       = rand() % s.size();
                mutationIndex2Allele = s[mutationIndex2];
            } while (e.find(mutationIndex2Allele) != e.end());

            // Determine if this is a new allele in the chromosome or whether this will need to be a swap if in chromosome
            it = chrXAlleles.find(mutationIndex2Allele);
            // if not found then insert new allele into preselected  mutation site - this should be the default first check
            if (it == chrXAlleles.end()) {
                chrXY[0][mutationIndex1] = mutationIndex2Allele;
                chrXAlleles.erase(mutationIndex1Allele);
                chrXAlleles[mutationIndex2Allele] = 0;
                
            }
            // else mutate by performing simple swap but dont put contraint on index range to chose from as below
            else {
                mutationIndex2       = rand() % chrXY[0].size();
                mutationIndex2Allele = chrXY[0][mutationIndex2];
                chrXY[0][mutationIndex1] = mutationIndex2Allele;
                chrXY[0][mutationIndex2] = mutationIndex1Allele;                
            }
            
            // CHROMOSOME Y (1)
            // pick indicies at random in which alleles are to be swapper
            mutationIndex1       = rand() % chrXY[1].size();
            mutationIndex1Allele = chrXY[1][mutationIndex1];
            // pick and allele at random to place into this from the full set of IDs. This will help get round local maxima
            do {
                mutationIndex2       = rand() % s.size();
                mutationIndex2Allele = s[mutationIndex2];
            } while (e.find(mutationIndex2Allele) != e.end());
            
            // Determine if this is a new allele in the chromosome or whether this will need to be a swap if in chromosome
            it = chrYAlleles.find(mutationIndex2Allele);
            // if not found then insert new allele into preselected  mutation site
            if (it == chrYAlleles.end()) {
                chrXY[1][mutationIndex1] = mutationIndex2Allele;
                chrYAlleles.erase(mutationIndex1Allele);
                chrYAlleles[mutationIndex2Allele] = 0;
                
            }
            // else mutate similar to below performing simple swap but dont put contraint on index range to chose from as below
            else {
                mutationIndex2       = rand() % chrXY[1].size();
                mutationIndex2Allele = chrXY[1][mutationIndex2];
                chrXY[1][mutationIndex1] = mutationIndex2Allele;
                chrXY[1][mutationIndex2] = mutationIndex1Allele;                
            }
        }
    }
    else {
        cout << "Cannot mutate at user rate - not enough samples in chromosome" << endl;
    }
}

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movq 0x1b0(%rsp), %rax
movq %rdi, 0x1a0(%rsp)
movq %rsi, 0x198(%rsp)
movsd %xmm0, 0x190(%rsp)
movq %rdx, 0x188(%rsp)
movq %rcx, 0x180(%rsp)
movq %r8, 0x178(%rsp)
movq %r9, 0x170(%rsp)
movq 0x1a0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5f00
movq %rax, %rdi
callq 0xd220
movq %rax, %xmm1
movaps 0x8c73(%rip), %xmm0     # 0x1a2b0
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x8c77(%rip), %xmm0     # 0x1a2c0
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movsd 0x8cf0(%rip), %xmm0     # 0x1a350
divsd %xmm1, %xmm0
movsd %xmm0, 0x168(%rsp)
movsd 0x168(%rsp), %xmm1
movsd 0x190(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x11de5
leaq 0x140(%rsp), %rdi
callq 0x3410
leaq 0x118(%rsp), %rdi
callq 0x3410
leaq 0xf8(%rsp), %rdi
callq 0x3410
leaq 0xf0(%rsp), %rdi
callq 0x12370
movsd 0x168(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
callq 0x30d0
movsd 0x88(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
movsd 0x8c6f(%rip), %xmm2     # 0x1a358
divsd %xmm2, %xmm0
movsd 0x190(%rsp), %xmm2
movsd 0x168(%rsp), %xmm3
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xe8(%rsp)
movq 0x1a0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5f00
movq %rax, %rdi
callq 0xd220
movq %rax, %xmm0
movaps 0x8b77(%rip), %xmm1     # 0x1a2b0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x8b7b(%rip), %xmm1     # 0x1a2c0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
mulsd 0xe8(%rsp), %xmm0
callq 0x3130
cvttsd2si %xmm0, %eax
movl %eax, 0xe4(%rsp)
movl $0x0, 0xe0(%rsp)
movl 0xe0(%rsp), %eax
cmpl 0xe4(%rsp), %eax
jge 0x11dbc
callq 0x30d0
cltq
movq %rax, 0x80(%rsp)
movq 0x1a0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x78(%rsp)
callq 0x5f00
movq %rax, %rdi
callq 0xd220
movq 0x78(%rsp), %rsi
movq %rax, %rcx
movq 0x80(%rsp), %rax
movq %rsi, %rdx
divq %rcx
movl %edx, %eax
movl %eax, 0x164(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x5f00
movq %rax, %rdi
movslq 0x164(%rsp), %rsi
callq 0xeca0
movq %rax, %rsi
leaq 0x140(%rsp), %rdi
callq 0x3040
jmp 0x11807
jmp 0x11809
callq 0x30d0
cltq
movq %rax, 0x70(%rsp)
movq 0x188(%rsp), %rdi
callq 0xd220
movq %rax, %rcx
movq 0x70(%rsp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x13c(%rsp)
movq 0x188(%rsp), %rdi
movslq 0x13c(%rsp), %rsi
callq 0xeca0
movq %rax, %rsi
leaq 0x118(%rsp), %rdi
callq 0x3040
jmp 0x1185f
jmp 0x11861
movq 0x1b0(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x12040
movq %rax, 0x68(%rsp)
jmp 0x1187d
movq 0x68(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0xbf10
movq %rax, 0xc0(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x12390
testb $0x1, %al
jne 0x11809
movq 0x178(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x12040
movq %rax, 0x60(%rsp)
jmp 0x118d8
movq 0x60(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0x178(%rsp), %rdi
callq 0xbf10
movq %rax, 0xb0(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xbef0
testb $0x1, %al
jne 0x11928
jmp 0x119de
movq 0x1a0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5f00
movq %rax, %rdi
movslq 0x164(%rsp), %rsi
callq 0xeca0
movq %rax, %rdi
leaq 0x118(%rsp), %rsi
callq 0x3040
jmp 0x1195b
movq 0x178(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x123b0
jmp 0x11972
movq 0x178(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x5fc0
movq %rax, 0x58(%rsp)
jmp 0x1198e
movq 0x58(%rsp), %rax
movl $0x0, (%rax)
jmp 0x11abb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x31c0
leaq 0x118(%rsp), %rdi
callq 0x31c0
leaq 0x140(%rsp), %rdi
callq 0x31c0
jmp 0x11e0f
callq 0x30d0
cltq
movq %rax, 0x50(%rsp)
movq 0x1a0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x48(%rsp)
callq 0x5f00
movq %rax, %rdi
callq 0xd220
movq 0x48(%rsp), %rsi
movq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rsi, %rdx
divq %rcx
movl %edx, %eax
movl %eax, 0x13c(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x5f00
movq %rax, %rdi
movslq 0x13c(%rsp), %rsi
callq 0xeca0
movq %rax, %rsi
leaq 0x118(%rsp), %rdi
callq 0x3040
jmp 0x11a53
movq 0x1a0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5f00
movq %rax, %rdi
movslq 0x164(%rsp), %rsi
callq 0xeca0
movq %rax, %rdi
leaq 0x118(%rsp), %rsi
callq 0x3040
jmp 0x11a86
movq 0x1a0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5f00
movq %rax, %rdi
movslq 0x13c(%rsp), %rsi
callq 0xeca0
movq %rax, %rdi
leaq 0x140(%rsp), %rsi
callq 0x3040
jmp 0x11ab9
jmp 0x11abb
callq 0x30d0
cltq
movq %rax, 0x40(%rsp)
movq 0x1a0(%rsp), %rdi
movl $0x1, %esi
movq %rsi, 0x38(%rsp)
callq 0x5f00
movq %rax, %rdi
callq 0xd220
movq 0x38(%rsp), %rsi
movq %rax, %rcx
movq 0x40(%rsp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x164(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x5f00
movq %rax, %rdi
movslq 0x164(%rsp), %rsi
callq 0xeca0
movq %rax, %rsi
leaq 0x140(%rsp), %rdi
callq 0x3040
jmp 0x11b30
jmp 0x11b32
callq 0x30d0
cltq
movq %rax, 0x30(%rsp)
movq 0x188(%rsp), %rdi
callq 0xd220
movq %rax, %rcx
movq 0x30(%rsp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x13c(%rsp)
movq 0x188(%rsp), %rdi
movslq 0x13c(%rsp), %rsi
callq 0xeca0
movq %rax, %rsi
leaq 0x118(%rsp), %rdi
callq 0x3040
jmp 0x11b88
jmp 0x11b8a
movq 0x1b0(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x12040
movq %rax, 0x28(%rsp)
jmp 0x11ba6
movq 0x28(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0xbf10
movq %rax, 0xa0(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x12390
testb $0x1, %al
jne 0x11b32
movq 0x170(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x12040
movq %rax, 0x20(%rsp)
jmp 0x11c01
movq 0x20(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0x170(%rsp), %rdi
callq 0xbf10
movq %rax, 0x90(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xbef0
testb $0x1, %al
jne 0x11c4e
jmp 0x11cc5
movq 0x1a0(%rsp), %rdi
movl $0x1, %esi
callq 0x5f00
movq %rax, %rdi
movslq 0x164(%rsp), %rsi
callq 0xeca0
movq %rax, %rdi
leaq 0x118(%rsp), %rsi
callq 0x3040
jmp 0x11c82
movq 0x170(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x123b0
jmp 0x11c99
movq 0x170(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x5fc0
movq %rax, 0x18(%rsp)
jmp 0x11cb5
movq 0x18(%rsp), %rax
movl $0x0, (%rax)
jmp 0x11da4
callq 0x30d0
cltq
movq %rax, 0x10(%rsp)
movq 0x1a0(%rsp), %rdi
movl $0x1, %esi
movq %rsi, 0x8(%rsp)
callq 0x5f00
movq %rax, %rdi
callq 0xd220
movq 0x8(%rsp), %rsi
movq %rax, %rcx
movq 0x10(%rsp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x13c(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x5f00
movq %rax, %rdi
movslq 0x13c(%rsp), %rsi
callq 0xeca0
movq %rax, %rsi
leaq 0x118(%rsp), %rdi
callq 0x3040
jmp 0x11d3a
movq 0x1a0(%rsp), %rdi
movl $0x1, %esi
callq 0x5f00
movq %rax, %rdi
movslq 0x164(%rsp), %rsi
callq 0xeca0
movq %rax, %rdi
leaq 0x118(%rsp), %rsi
callq 0x3040
jmp 0x11d6e
movq 0x1a0(%rsp), %rdi
movl $0x1, %esi
callq 0x5f00
movq %rax, %rdi
movslq 0x13c(%rsp), %rsi
callq 0xeca0
movq %rax, %rdi
leaq 0x140(%rsp), %rsi
callq 0x3040
jmp 0x11da2
jmp 0x11da4
jmp 0x11da6
movl 0xe0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe0(%rsp)
jmp 0x11778
leaq 0xf8(%rsp), %rdi
callq 0x31c0
leaq 0x118(%rsp), %rdi
callq 0x31c0
leaq 0x140(%rsp), %rdi
callq 0x31c0
jmp 0x11e07
movq 0x111ec(%rip), %rdi     # 0x22fd8
leaq 0x8585(%rip), %rsi      # 0x1a378
callq 0x32c0
movq %rax, %rdi
movq 0x111c6(%rip), %rsi     # 0x22fc8
callq 0x3330
addq $0x1a8, %rsp            # imm = 0x1A8
retq
movq 0xd8(%rsp), %rdi
callq 0x3510
nopl (%rax)