// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2898, %rsp           # imm = 0x2898
callq 0x2c4ae
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
leaq 0x160(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x140(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
movq 0xb128b(%rip), %rdi     # 0xdcfe8
leaq 0x6a8cc(%rip), %rsi     # 0x96630
callq 0x8480
movq %rax, %rdi
callq 0x8230
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %r14
movq 0xb1b63(%rip), %rsi     # 0xdd8e8
pushq $0x8
popq %rdx
movq %rax, %rdi
callq 0x8890
movq 0xb1251(%rip), %rdi     # 0xdcfe8
leaq 0x6a8b2(%rip), %rsi     # 0x96650
callq 0x8480
leaq 0xb1b3e(%rip), %rsi     # 0xdd8e8
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0x100(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xf0(%rsp), %rbx
leaq 0x40(%rsp), %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2be01
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2b0a4
jmp 0x2bddb
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x60(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x80(%rsp), %rax
andq $0x0, 0x10(%rax)
movapd %xmm0, (%rax)
xorl %ebx, %ebx
leaq 0x188(%rsp), %r12
movq 0x40(%rsp), %r13
movq 0x48(%rsp), %rax
subq %r13, %rax
sarq $0x5, %rax
cmpq %rax, %rbx
je 0x2c039
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %rbp
movq %rbx, %r15
shlq $0x5, %r15
movq (%r13,%r15), %rsi
movq %rax, %rdi
pushq $0x8
popq %rdx
callq 0x8890
leaq 0x198(%rsp), %rax
movq %rax, 0x188(%rsp)
andq $0x0, 0x190(%rsp)
movb $0x0, 0x198(%rsp)
movq $0x0, 0x10(%rsp)
pushq $0x1
popq %r13
movq %rbp, %rdi
movq %r12, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2bf87
movl %r13d, %ecx
andl $0x80000001, %ecx       # imm = 0x80000001
movq 0x188(%rsp), %rax
movb (%rax), %al
cmpl $0x1, %ecx
jne 0x2beea
cmpb $0x3e, %al
jne 0x2c355
incl %r13d
jmp 0x2bef1
incl %r13d
cmpb $0x3e, %al
jne 0x2bf05
testq %rbx, %rbx
jne 0x2bea8
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x2b0a4
jmp 0x2bea8
testq %rbx, %rbx
je 0x2bf25
movq 0x10(%rsp), %r14
movq %r14, %rdi
shlq $0x5, %rdi
addq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x8920
jmp 0x2bf4d
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x2b0a4
movq 0x10(%rsp), %r14
movq %r14, %rdi
shlq $0x5, %rdi
addq 0x20(%rsp), %rdi
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x8700
testq %r14, %r14
jne 0x2bf7a
movl 0x190(%rsp), %eax
movl %eax, 0x1510(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x1510(%rsp), %rsi
callq 0x2c80c
movq 0x10(%rsp), %r14
incq %r14
movq %r14, 0x10(%rsp)
jmp 0x2bea8
movq %rbp, %rdi
callq 0x80e0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %r13
leaq 0x170(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x2c6d4
movq %r13, %xmm0
punpckldq 0x66286(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x6628e(%rip), %xmm0    # 0x92250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
leaq 0x170(%rsp), %r13
movq %r13, %rdi
callq 0x2c6ac
ucomisd 0x10(%rsp), %xmm0
jne 0x2c372
jp 0x2c372
movq %r13, %rdi
callq 0x2aef2
movq 0xb0fe3(%rip), %rdi     # 0xdcfe8
leaq 0x6a6cc(%rip), %rsi     # 0x966d8
callq 0x8480
addq 0x40(%rsp), %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq %r12, %rdi
callq 0x8978
incq %rbx
jmp 0x2be3c
movq 0xb0fa8(%rip), %rdi     # 0xdcfe8
leaq 0x6a6a3(%rip), %rsi     # 0x966ea
callq 0x8480
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x8320
leaq 0x6a69a(%rip), %rsi     # 0x966fe
movq %rax, %rdi
callq 0x8480
movq %rax, %rdi
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
pushq $0x3
popq %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
callq 0x8320
leaq 0x664f0(%rip), %rsi     # 0x9257f
movq %rax, %rdi
callq 0x8480
movq %rax, %rdi
callq 0x8230
movq 0x20(%rsp), %rax
movl 0x8(%rax), %eax
pushq $0x3
popq %rcx
cltd
idivl %ecx
movl %eax, %ebx
leaq 0x188(%rsp), %rdi
callq 0x2c722
leaq 0x188(%rsp), %rdi
callq 0x8610
movl %eax, %esi
leaq 0x1510(%rsp), %rdi
callq 0x2c946
decl %ebx
leaq 0xc8(%rsp), %rax
andl $0x0, (%rax)
movl %ebx, 0x4(%rax)
xorl %ebx, %ebx
leaq 0xd0(%rsp), %r14
leaq 0x110(%rsp), %r13
pushq $0x3
popq %r15
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %rax, %rbx
je 0x2c2e2
movq %rbx, %rax
shlq $0x5, %rax
addq %rax, %rsi
movq %r14, %rdi
leaq 0x6a5dc(%rip), %rdx     # 0x96708
callq 0x28806
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %rbp
movq 0xd0(%rsp), %rsi
movq %rax, %rdi
pushq $0x10
popq %rdx
callq 0x84e0
movq 0x80(%rsp), %rax
movq %rbx, 0xa0(%rsp)
movl (%rax,%rbx,4), %eax
cltd
pushq $0x3
popq %rcx
idivl %ecx
movl %eax, 0x10(%rsp)
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %r14
movq %r13, %rdi
leaq 0x6dc9d(%rip), %rsi     # 0x99e1f
leaq 0xf(%rsp), %rdx
callq 0x21904
subq %rbx, %r14
sarq $0x5, %r14
leaq 0xb0(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
leaq 0xe(%rsp), %rcx
callq 0x2c778
movq %r13, %rdi
callq 0x8978
xorl %r12d, %r12d
movl 0x10(%rsp), %eax
testl %eax, %eax
cmovlel %r12d, %eax
movl %eax, 0x10(%rsp)
movq %rbp, 0xa8(%rsp)
cmpl 0x10(%rsp), %r12d
je 0x2c24a
leaq 0xc8(%rsp), %rdi
leaq 0x1510(%rsp), %rsi
callq 0x2c7ce
leal (%rax,%rax,2), %eax
movslq %eax, %r14
xorl %ebp, %ebp
xorl %ebx, %ebx
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %rbx, %rax
jbe 0x2c23d
addq %rbp, %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x84b0
movq 0xb0(%rsp), %rdi
addq %rbp, %rdi
movq %r13, %rsi
callq 0x8920
movq %r13, %rdi
callq 0x8978
incq %rbx
addq $0x20, %rbp
jmp 0x2c1f2
incl %r12d
movq 0xa8(%rsp), %rbp
jmp 0x2c1cc
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0x2c2b5
movq 0x60(%rsp), %rsi
addq %rbx, %rsi
movq %rbp, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq 0xb0(%rsp), %rsi
addq %rbx, %rsi
movq %rbp, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq 0xb0(%rsp), %rdi
addq %rbx, %rdi
leaq 0x6db78(%rip), %rsi     # 0x99e1f
callq 0x88f0
incq %r14
addq $0x20, %rbx
jmp 0x2c24f
leaq 0xb0(%rsp), %rdi
callq 0x28a08
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
callq 0x8978
movq 0xa0(%rsp), %rbx
incq %rbx
jmp 0x2c0fe
leaq 0x188(%rsp), %rdi
callq 0x2c7d6
leaq 0x80(%rsp), %rdi
callq 0x2aef2
leaq 0x60(%rsp), %rdi
callq 0x28a08
leaq 0x20(%rsp), %rdi
callq 0x28a08
leaq 0xf0(%rsp), %rdi
callq 0x8978
leaq 0x130(%rsp), %rdi
callq 0x8978
leaq 0x150(%rsp), %rdi
callq 0x8978
leaq 0x40(%rsp), %rdi
callq 0x28a08
xorl %eax, %eax
addq $0x2898, %rsp           # imm = 0x2898
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6a307(%rip), %rdi     # 0x96663
leaq 0x6a30f(%rip), %rsi     # 0x96672
leaq 0x6a31f(%rip), %rcx     # 0x96689
pushq $0x42
popq %rdx
callq 0x82f0
leaq 0x6a330(%rip), %rdi     # 0x966a9
leaq 0x6a2f2(%rip), %rsi     # 0x96672
leaq 0x6a302(%rip), %rcx     # 0x96689
pushq $0x54
jmp 0x2c36c
jmp 0x2c3e4
movq %rax, %rbx
movl $0x208, %esi            # imm = 0x208
movq %r14, %rdi
callq 0x84a0
jmp 0x2c482
jmp 0x2c3c6
jmp 0x2c3a6
movq %rax, %rbx
jmp 0x2c482
jmp 0x2c404
jmp 0x2c3e4
jmp 0x2c404
movq %rax, %rbx
movl $0x200, %esi            # imm = 0x200
movq %rbp, %rdi
callq 0x84a0
jmp 0x2c43a
movq %rax, %rbx
jmp 0x2c447
jmp 0x2c3cd
movq %rax, %rbx
jmp 0x2c43a
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x8978
jmp 0x2c43a
movq %rax, %rbx
jmp 0x2c454
movq %rax, %rbx
movl $0x208, %esi            # imm = 0x208
movq %rbp, %rdi
callq 0x84a0
jmp 0x2c454
movq %rax, %rbx
jmp 0x2c475
jmp 0x2c404
jmp 0x2c42a
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
callq 0x8978
jmp 0x2c454
jmp 0x2c42a
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x8978
jmp 0x2c42d
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x28a08
leaq 0xd0(%rsp), %rdi
callq 0x8978
leaq 0x188(%rsp), %rdi
callq 0x2c7d6
leaq 0x80(%rsp), %rdi
callq 0x2aef2
leaq 0x60(%rsp), %rdi
callq 0x28a08
leaq 0x20(%rsp), %rdi
callq 0x28a08
leaq 0xf0(%rsp), %rdi
callq 0x8978
leaq 0x130(%rsp), %rdi
callq 0x8978
leaq 0x150(%rsp), %rdi
callq 0x8978
leaq 0x40(%rsp), %rdi
callq 0x28a08
movq %rbx, %rdi
callq 0x8870