// File: /workspace/github_cpp_makefile_star2_O1/millanek[P]evo/evo_permute_codons.cpp
// Function: permuteCodons(int, char**)
int permuteCodons(int argc, char** argv) {
    parsePermuteCodonsOptions(argc, argv);
    std::vector<string> allAligmentFiles; string statsFileName; string pcaVectorsFileName;
    
    std::cerr << "Permuting gene coding sequences" << std::endl;
    
    
    std::ifstream* alignmentList = new std::ifstream(opt::alignmentListFile.c_str());
    std::cerr << "for the genes in: " << opt::alignmentListFile << std::endl;
    std::string line;
    while (getline(*alignmentList, line)) {
        allAligmentFiles.push_back(line);
    }
    
        
    // Loop over the mutiple alignment files for the first time to load the codons:
    std::vector<string> allSeqs; std::vector<string> sampleNames; std::vector<int> geneLengths;
    for (std::vector<std::string>::size_type i = 0; i != allAligmentFiles.size(); i++) {
        std::ifstream* alignment = new std::ifstream(allAligmentFiles[i].c_str());
        std::string line; int lineNum = 1; int j = 0;
        while (getline(*alignment, line)) {
            if (lineNum % 2 == 1) assert(line[0] == '>');
            lineNum++;
            if (line[0] == '>') {
                if (i == 0)
                    sampleNames.push_back(line);
                continue;
            }
            if (i == 0) {
                allSeqs.push_back(line);
                allSeqs[j].reserve(1000000);
            } else {
                allSeqs[j] += line;
            }
            if (j == 0)
                geneLengths.push_back((int)line.length());
            j++;
        } alignment->close();
        
        assert(allSeqs[0].length() == vector_sum(geneLengths));
        std::cerr << "loaded seqs for: " << allAligmentFiles[i] << std::endl;
    }
    std::cerr << "allSeqs[0].size(): " << allSeqs[0].length() << "; equals " << allSeqs[0].length()/3 << "AA" << std::endl;
    
    // Loop over the mutiple alignment files for the second time to print out the permuted sequences:
    int totalAA = (int)allSeqs[0].length()/3;
    std::random_device rd; // obtain a random number from hardware
    std::mt19937 eng(rd()); // seed the generator
    std::uniform_int_distribution<> randomAApos(0, totalAA-1); // define the range
    for (std::vector<std::string>::size_type i = 0; i != allAligmentFiles.size(); i++) {
        std::string permutedFileName = allAligmentFiles[i] + "_permuted.txt";
        std::ofstream* permutedFile = new std::ofstream(permutedFileName.c_str());
        int thisLengthAA = geneLengths[i]/3;
        std::vector<string> thisSeqs(sampleNames.size(),"");
        for (int j = 0; j < thisLengthAA; j++) {
            int pos = randomAApos(eng);
            for (int k = 0; k < allSeqs.size(); k++) {
                thisSeqs[k] += allSeqs[k].substr(pos*3,3);
            }
        }
        for (int k = 0; k < allSeqs.size(); k++) {
            *permutedFile << sampleNames[k] << std::endl;
            *permutedFile << thisSeqs[k] << std::endl;
            thisSeqs[k] = "";
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2888, %rsp           # imm = 0x2888
callq 0x356d3
xorl %eax, %eax
movq %rax, 0x40(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
leaq 0x150(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x130(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq 0xd643a(%rip), %rbx     # 0x10afe8
leaq 0x9990b(%rip), %rsi     # 0xce4c0
movl $0x1f, %edx
movq %rbx, %rdi
callq 0x84e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq 0xd6408(%rip), %rdi     # 0x10afe8
callq 0x8040
movq %rax, %rdi
callq 0x8300
movl $0x208, %edi            # imm = 0x208
callq 0x8470
movq %rax, %rbx
movq 0xd6caf(%rip), %rsi     # 0x10b8b0
movq %rax, %rdi
movl $0x8, %edx
callq 0x8860
movq 0xd63d3(%rip), %rdi     # 0x10afe8
leaq 0x998c4(%rip), %rsi     # 0xce4e0
movl $0x12, %edx
callq 0x84e0
movq 0xd6c83(%rip), %rsi     # 0x10b8b0
movq 0xd6c84(%rip), %rdx     # 0x10b8b8
movq 0xd63ad(%rip), %rdi     # 0x10afe8
callq 0x84e0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r14, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
leaq 0x110(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x100(%rsp), %r14
leaq 0x30(%rsp), %r15
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8870
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x34ccb
movq %r15, %rdi
movq %r14, %rsi
callq 0x33db0
jmp 0x34c8e
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movq %rax, 0xd0(%rsp)
movapd %xmm0, 0xc0(%rsp)
movq %rax, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movq 0x30(%rsp), %r14
cmpq %r14, 0x38(%rsp)
je 0x34fb8
xorl %r12d, %r12d
leaq 0x178(%rsp), %rbx
movl $0x208, %edi            # imm = 0x208
callq 0x8470
movq %rax, %rbp
movq %r12, %r15
shlq $0x5, %r15
movq (%r14,%r15), %rsi
movq %rax, %rdi
movl $0x8, %edx
callq 0x8860
leaq 0x188(%rsp), %rax
movq %rax, 0x178(%rsp)
movq $0x0, 0x180(%rsp)
movb $0x0, 0x188(%rsp)
movl $0x1, %r13d
xorl %r14d, %r14d
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %edx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x8870
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x34e71
movl %r13d, %eax
andl $0x80000001, %eax       # imm = 0x80000001
cmpl $0x1, %eax
jne 0x34dbb
movq 0x178(%rsp), %rax
cmpb $0x3e, (%rax)
jne 0x354b1
movq 0x178(%rsp), %rsi
cmpb $0x3e, (%rsi)
jne 0x34de2
testq %r12, %r12
jne 0x34ddd
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0x33db0
incl %r13d
jmp 0x34d68
testq %r12, %r12
je 0x34e02
movq %r14, %rdi
shlq $0x5, %rdi
addq 0x10(%rsp), %rdi
movq 0x180(%rsp), %rdx
callq 0x8180
jmp 0x34e25
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x33db0
movq %r14, %rdi
shlq $0x5, %rdi
addq 0x10(%rsp), %rdi
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x86f0
testq %r14, %r14
jne 0x34e66
movl 0x180(%rsp), %eax
movl %eax, 0x1500(%rsp)
movq 0x78(%rsp), %rsi
cmpq 0x80(%rsp), %rsi
je 0x34e54
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x78(%rsp)
jmp 0x34e66
leaq 0x70(%rsp), %rdi
leaq 0x1500(%rsp), %rdx
callq 0x35b1e
incl %r13d
incq %r14
jmp 0x34d68
movq %rbp, %rdi
callq 0x80f0
movq 0xd6168(%rip), %r13     # 0x10afe8
movq 0x10(%rsp), %rax
movq 0x8(%rax), %r14
leaq 0x160(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x35990
movq %r14, %xmm0
punpckldq 0x952f8(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x95300(%rip), %xmm0    # 0xca1b0
movq 0x160(%rsp), %rdi
movq 0x168(%rsp), %rax
subq %rdi, %rax
je 0x34eed
sarq $0x2, %rax
cmpq $0x1, %rax
adcq $0x0, %rax
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
xorps %xmm2, %xmm2
cvtsi2sdl (%rdi,%rcx,4), %xmm2
addsd %xmm2, %xmm1
incq %rcx
cmpq %rcx, %rax
jne 0x34ed7
jmp 0x34ef1
xorpd %xmm1, %xmm1
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jne 0x354d0
jp 0x354d0
testq %rdi, %rdi
je 0x34f22
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movl $0x11, %edx
movq %r13, %rdi
leaq 0x99637(%rip), %rsi     # 0xce568
callq 0x84e0
movq 0x30(%rsp), %rax
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
movq %r13, %rdi
callq 0x84e0
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %rbp, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq 0x178(%rsp), %rdi
leaq 0x188(%rsp), %rax
cmpq %rax, %rdi
je 0x34f9b
movq 0x188(%rsp), %rsi
incq %rsi
callq 0x8480
incq %r12
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rax
subq %r14, %rax
sarq $0x5, %rax
cmpq %rax, %r12
jne 0x34d16
movq 0xd6029(%rip), %rdi     # 0x10afe8
leaq 0x995b4(%rip), %rsi     # 0xce57a
movl $0x13, %edx
callq 0x84e0
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0xd6008(%rip), %rdi     # 0x10afe8
callq 0x82f0
movq %rax, %rbx
leaq 0x9959f(%rip), %rsi     # 0xce58e
movl $0x9, %edx
movq %rax, %rdi
callq 0x84e0
movq 0x10(%rsp), %rcx
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
mulq 0x8(%rcx)
shrq %rdx
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x82f0
movq %rax, %rbx
leaq 0x96288(%rip), %rsi     # 0xcb2af
movl $0x2, %edx
movq %rax, %rdi
callq 0x84e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq 0x10(%rsp), %rax
movslq 0x8(%rax), %rbx
leaq 0x178(%rsp), %rdi
callq 0x35a10
leaq 0x178(%rsp), %rdi
callq 0x85e0
imulq $0x55555556, %rbx, %rcx # imm = 0x55555556
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
movl %eax, %eax
movq %rax, 0x1500(%rsp)
movl $0x1, %edx
movq %rax, %rsi
shrq $0x1e, %rsi
xorl %eax, %esi
imull $0x6c078965, %esi, %eax # imm = 0x6C078965
addl %edx, %eax
movq %rax, 0x1500(%rsp,%rdx,8)
incq %rdx
cmpq $0x270, %rdx            # imm = 0x270
jne 0x350a1
movq $0x270, 0x2880(%rsp) # imm = 0x270
decl %ecx
movl $0x0, 0xb8(%rsp)
movl %ecx, 0xbc(%rsp)
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x353e6
leaq 0x60(%rsp), %rbx
leaq 0xa8(%rsp), %r14
xorl %r15d, %r15d
leaq 0x50(%rsp), %r13
leaq 0x98(%rsp), %r12
movq %r15, %rcx
shlq $0x5, %rcx
movq %rbx, 0x50(%rsp)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x2e45c
movq %r13, %rdi
leaq 0x9945b(%rip), %rsi     # 0xce598
callq 0x88c0
movl $0x200, %edi            # imm = 0x200
callq 0x8470
movq 0x50(%rsp), %rsi
movq %rax, 0x90(%rsp)
movq %rax, %rdi
movl $0x10, %edx
callq 0x84c0
movq 0x70(%rsp), %rax
movq %r15, 0xd8(%rsp)
movslq (%rax,%r15,4), %rbx
movq 0xc0(%rsp), %r15
movq 0xc8(%rsp), %r13
movq %r14, 0x98(%rsp)
movq %r12, %rdi
leaq 0x9cae6(%rip), %rdx     # 0xd1c7f
movq %rdx, %rsi
callq 0x28952
subq %r15, %r13
sarq $0x5, %r13
leaq 0xe8(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
leaq 0xf(%rsp), %rcx
callq 0x35a8a
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x351dd
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x8480
cmpl $0x3, %ebx
jl 0x352af
imulq $0x55555556, %rbx, %rcx # imm = 0x55555556
movq %rcx, %rax
shrq $0x3f, %rax
shrq $0x20, %rcx
addl %eax, %ecx
movq %rcx, 0xe0(%rsp)
xorl %ebp, %ebp
leaq 0xb8(%rsp), %rdi
leaq 0x1500(%rsp), %rsi
movq %rdi, %rdx
callq 0x35cbe
movq 0x10(%rsp), %rsi
cmpq %rsi, 0x18(%rsp)
je 0x352a0
leal (%rax,%rax,2), %eax
movslq %eax, %r13
xorl %ebx, %ebx
xorl %r15d, %r15d
addq %rbx, %rsi
movl $0x3, %ecx
movq %r12, %rdi
movq %r13, %rdx
callq 0x84a0
movq 0xe8(%rsp), %rdi
addq %rbx, %rdi
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
callq 0x8180
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x35283
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x8480
incq %r15
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
addq $0x20, %rbx
cmpq %r15, %rax
ja 0x35233
incl %ebp
cmpl 0xe0(%rsp), %ebp
jne 0x35204
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
leaq 0x60(%rsp), %rbx
je 0x35398
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0xc0(%rsp), %rax
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
movq 0x90(%rsp), %rdi
callq 0x84e0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r13, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq 0xe8(%rsp), %rax
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
movq 0x90(%rsp), %rdi
callq 0x84e0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r13, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq 0xe8(%rsp), %rdi
movq 0x8(%rdi,%r15), %rdx
addq %r15, %rdi
xorl %esi, %esi
leaq 0x9c90d(%rip), %rcx     # 0xd1c7f
xorl %r8d, %r8d
callq 0x86c0
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
incq %rbp
sarq $0x5, %rax
addq $0x20, %r15
cmpq %rbp, %rax
ja 0x352c9
leaq 0xe8(%rsp), %rdi
callq 0x3123c
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x353bc
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0xd8(%rsp), %r15
incq %r15
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
cmpq %rcx, %r15
leaq 0x50(%rsp), %r13
jne 0x35113
leaq 0x178(%rsp), %rdi
callq 0x8430
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x3540d
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
leaq 0xc0(%rsp), %rdi
callq 0x3123c
leaq 0x10(%rsp), %rdi
callq 0x3123c
movq 0x100(%rsp), %rdi
leaq 0x110(%rsp), %rax
cmpq %rax, %rdi
je 0x35449
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x120(%rsp), %rdi
leaq 0x130(%rsp), %rax
cmpq %rax, %rdi
je 0x3546e
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x140(%rsp), %rdi
leaq 0x150(%rsp), %rax
cmpq %rax, %rdi
je 0x35493
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x30(%rsp), %rdi
callq 0x3123c
xorl %eax, %eax
addq $0x2888, %rsp           # imm = 0x2888
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9903b(%rip), %rdi     # 0xce4f3
leaq 0x99043(%rip), %rsi     # 0xce502
leaq 0x99053(%rip), %rcx     # 0xce519
movl $0x42, %edx
callq 0x82c0
leaq 0x99062(%rip), %rdi     # 0xce539
leaq 0x99024(%rip), %rsi     # 0xce502
leaq 0x99034(%rip), %rcx     # 0xce519
movl $0x54, %edx
callq 0x82c0
jmp 0x356cb
jmp 0x35519
jmp 0x3556c
movq %rax, %r12
movl $0x208, %esi            # imm = 0x208
movq %rbx, %rdi
jmp 0x3566a
jmp 0x3558f
jmp 0x3550f
movq %rax, %r12
jmp 0x3566f
jmp 0x3556c
movq %rax, %r12
jmp 0x3560c
jmp 0x35523
movq %rax, %r12
jmp 0x355f5
movq %rax, %r12
movl $0x200, %esi            # imm = 0x200
movq 0x90(%rsp), %rdi
jmp 0x3555d
movq %rax, %r12
jmp 0x35562
movq %rax, %r12
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x35562
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x60(%rsp), %rbx
jmp 0x355f5
movq %rax, %r12
jmp 0x35619
movq %rax, %r12
movl $0x208, %esi            # imm = 0x208
movq %rbp, %rdi
jmp 0x355b2
jmp 0x3558f
movq %rax, %r12
jmp 0x3564a
jmp 0x3558f
jmp 0x355be
movq %rax, %r12
movq 0x178(%rsp), %rdi
leaq 0x188(%rsp), %rax
cmpq %rax, %rdi
je 0x35619
movq 0x188(%rsp), %rsi
incq %rsi
callq 0x8480
jmp 0x35619
movq %rax, %r12
jmp 0x355e8
movq %rax, %r12
jmp 0x355e3
movq %rax, %r12
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x355e3
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x60(%rsp), %rbx
leaq 0xe8(%rsp), %rdi
callq 0x3123c
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3560c
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x178(%rsp), %rdi
callq 0x8430
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x35633
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
leaq 0xc0(%rsp), %rdi
callq 0x3123c
leaq 0x10(%rsp), %rdi
callq 0x3123c
movq 0x100(%rsp), %rdi
leaq 0x110(%rsp), %rax
cmpq %rax, %rdi
je 0x3566f
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x120(%rsp), %rdi
leaq 0x130(%rsp), %rax
cmpq %rax, %rdi
je 0x35694
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x140(%rsp), %rdi
leaq 0x150(%rsp), %rax
cmpq %rax, %rdi
je 0x356b9
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x30(%rsp), %rdi
callq 0x3123c
movq %r12, %rdi
callq 0x8840
movq %rax, %rdi
callq 0x287e1