// File: /workspace/github_cpp_makefile_star2_O1/millanek[P]evo/evo_agpToNewFasta.cpp
// Function: agpFastaMain(int, char**)
int agpFastaMain(int argc, char** argv) {
    
    parseAgpFastaOptions(argc, argv);
    
    std::cerr << "Generating a new genome sequence with rearrangements as indicated in an AGP file: " << opt::agpFile << std::endl;
    std::cerr << "from the original reference genome: " << opt::genomeFile << std::endl;
    
    // Read from the agp and reference genome files
    std::istream* agpFile = createReader(opt::agpFile.c_str());
    std::map<string, string> fastaSeqs = readMultiFastaToMap(opt::genomeFile);
    std::map<string, std::vector<std::vector<string> > > newAgpStructure = readAGPstructureToMap(agpFile);
    std::map<string, string> outputSeqs;
    
    for(std::map<string,std::vector<std::vector<string> > >::iterator it = newAgpStructure.begin(); it != newAgpStructure.end(); ++it) {
        string processedScaffold = it->first;
        std::vector<std::vector<string> > processedScaffoldStructure = it->second;
        outputSeqs[processedScaffold] = ""; outputSeqs[processedScaffold].reserve(50000000);
        
        for (std::vector<std::string>::size_type i = 0; i != processedScaffoldStructure.size(); i++) {
            string toAdd;
            if (processedScaffoldStructure[i][6] == "scaffold") {
                toAdd = HUNDRED_Ns;
            } else {
                string originScaffold = processedScaffoldStructure[i][5];
                int originStart = atoi(processedScaffoldStructure[i][6].c_str()); int originEnd = atoi(processedScaffoldStructure[i][7].c_str());
                int originLength = (originEnd - originStart) + 1;
                toAdd = fastaSeqs.at(originScaffold).substr(originStart-1,originLength);
                // If the sequence should be inverted, get its reverse complement
                if (processedScaffoldStructure[i][8] == "-") {
                    std::reverse(toAdd.begin(), toAdd.end());
                    // complement
                    for (std::string::size_type j = 0; j != toAdd.length(); j++) {
                        switch (toAdd[j]) {
                            case 'A': toAdd[j] = 'T'; break;
                            case 'T': toAdd[j] = 'A'; break;
                            case 'C': toAdd[j] = 'G'; break;
                            case 'G': toAdd[j] = 'C'; break;
                            case 'a': toAdd[j] = 't'; break;
                            case 't': toAdd[j] = 'a'; break;
                            case 'c': toAdd[j] = 'g'; break;
                            case 'g': toAdd[j] = 'c'; break;
                            default:
                                break;
                        }
                    }
                }
                std::cerr << "Added " << originScaffold << " coords: " << originStart << " " << originEnd << std::endl;
            }
            outputSeqs[processedScaffold].append(toAdd);
        }
    }
    
    for(std::map<string,std::vector<std::vector<string> > >::iterator it = newAgpStructure.begin(); it != newAgpStructure.end(); ++it) {
        string processedScaffold = it->first;
        std::cerr << "New scaffold " << processedScaffold << " length: " << outputSeqs[processedScaffold].length() << std::endl;
        std::cout << ">" << processedScaffold << std::endl;
        print80bpPerLineStdOut(std::cout, outputSeqs[processedScaffold]);
    }
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
callq 0xbbc57
movq 0x4fbdd(%rip), %rbx     # 0x10afe8
leaq 0x2d697(%rip), %rsi     # 0xe8aa9
movl $0x52, %edx
movq %rbx, %rdi
callq 0x84e0
movq 0x51caa(%rip), %rsi     # 0x10d0d0
movq 0x51cab(%rip), %rdx     # 0x10d0d8
movq %rbx, %rdi
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 0x2d696(%rip), %rsi     # 0xe8afc
movl $0x24, %edx
movq %rbx, %rdi
callq 0x84e0
movq 0x51c76(%rip), %rsi     # 0x10d0f0
movq 0x51c77(%rip), %rdx     # 0x10d0f8
movq %rbx, %rdi
callq 0x84e0
movq %rax, %rbx
movq (%rax), %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 0x51c16(%rip), %rsi     # 0x10d0d0
leaq 0xa8(%rsp), %rdi
leaq 0xd8(%rsp), %rdx
callq 0x286de
leaq 0xa8(%rsp), %rdi
movl $0x8, %esi
callq 0x53224
movq %rax, %rbx
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xbb505
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x51be4(%rip), %rsi     # 0x10d0f0
leaq 0xa8(%rsp), %rdi
callq 0x538d6
leaq 0xd8(%rsp), %rdi
movq %rbx, %rsi
callq 0xbb278
leaq 0x130(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0xe0(%rsp), %rax
movq 0x10(%rax), %rbx
cmpq %rax, %rbx
je 0xbb959
leaq 0x30(%rsp), %r13
movq 0x4fa7e(%rip), %r12     # 0x10afe8
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x2e45c
leaq 0x40(%rbx), %rsi
leaq 0x40(%rsp), %rdi
callq 0x3386e
movq %rbx, 0x98(%rsp)
leaq 0x128(%rsp), %rdi
movq %rsp, %rsi
callq 0x3e394
movq 0x8(%rax), %rdx
movq %rax, %rdi
xorl %esi, %esi
leaq 0x166c3(%rip), %rcx     # 0xd1c7f
xorl %r8d, %r8d
callq 0x86c0
leaq 0x128(%rsp), %rdi
movq %rsp, %rsi
callq 0x3e394
movl $0x2faf080, %esi        # imm = 0x2FAF080
movq %rax, %rdi
callq 0x86f0
movq 0x40(%rsp), %rax
cmpq %rax, 0x48(%rsp)
je 0xbb913
xorl %ebx, %ebx
movq %r13, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
leaq (%rbx,%rbx,2), %r14
movq (%rax,%r14,8), %rdi
movl $0xc0, %eax
addq %rax, %rdi
leaq 0x20027(%rip), %rsi     # 0xdb644
callq 0x8160
testl %eax, %eax
je 0xbb891
movq 0x40(%rsp), %rax
movq (%rax,%r14,8), %rax
leaq 0x68(%rsp), %rcx
movq %rcx, 0x58(%rsp)
movq 0xa0(%rax), %rsi
movq 0xa8(%rax), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r13
movq %r13, %rdi
callq 0x2e45c
movq 0x40(%rsp), %rax
movq (%rax,%r14,8), %r15
movq 0xc0(%r15), %rdi
callq 0x86e0
movl %eax, %ebp
movq 0xe0(%r15), %rdi
callq 0x86e0
movl %eax, %r15d
leaq 0xa8(%rsp), %rdi
movq %r13, %rsi
callq 0xbbede
movl %r15d, %ecx
subl %ebp, %ecx
incl %ecx
leal -0x1(%rbp), %edx
movslq %edx, %rdx
movslq %ecx, %rcx
leaq 0x108(%rsp), %rdi
movq %rax, %rsi
callq 0x84a0
movq %rbp, 0xa0(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x8540
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0xbb6f0
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x40(%rsp), %rax
movq (%rax,%r14,8), %rdi
movl $0x100, %eax            # imm = 0x100
addq %rax, %rdi
leaq 0x151a8(%rip), %rsi     # 0xd08b0
callq 0x8160
testl %eax, %eax
leaq 0x30(%rsp), %r13
jne 0xbb7da
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
setne %dl
addq %rax, %rcx
decq %rcx
cmpq %rax, %rcx
seta %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xbb75e
incq %rax
movb -0x1(%rax), %dl
movb (%rcx), %sil
movb %sil, -0x1(%rax)
movb %dl, (%rcx)
decq %rcx
leaq 0x1(%rax), %rdx
cmpq %rcx, %rax
movq %rdx, %rax
jb 0xbb743
cmpq $0x0, 0x28(%rsp)
je 0xbb7da
xorl %eax, %eax
movq 0x20(%rsp), %rcx
movzbl (%rcx,%rax), %edx
cmpl $0x60, %edx
jg 0xbb78d
cmpl $0x46, %edx
jg 0xbb7a1
movb $0x54, %sil
cmpl $0x41, %edx
je 0xbb7cc
cmpl $0x43, %edx
jne 0xbb7d0
movb $0x47, %sil
jmp 0xbb7cc
cmpl $0x66, %edx
jg 0xbb7b0
cmpl $0x61, %edx
je 0xbb7bf
cmpl $0x63, %edx
jne 0xbb7d0
movb $0x67, %sil
jmp 0xbb7cc
cmpl $0x47, %edx
je 0xbb7c4
cmpl $0x54, %edx
jne 0xbb7d0
movb $0x41, %sil
jmp 0xbb7cc
cmpl $0x67, %edx
je 0xbb7c9
cmpl $0x74, %edx
jne 0xbb7d0
movb $0x61, %sil
jmp 0xbb7cc
movb $0x74, %sil
jmp 0xbb7cc
movb $0x43, %sil
jmp 0xbb7cc
movb $0x63, %sil
movb %sil, (%rcx,%rax)
incq %rax
cmpq 0x28(%rsp), %rax
jne 0xbb768
movl $0x6, %edx
movq %r12, %rdi
leaq 0x2d39d(%rip), %rsi     # 0xe8b86
callq 0x84e0
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq %r12, %rdi
callq 0x84e0
movq %rax, %r14
movl $0x9, %edx
movq %rax, %rdi
leaq 0x2d37b(%rip), %rsi     # 0xe8b8d
callq 0x84e0
movq %r14, %rdi
movq 0xa0(%rsp), %rsi
callq 0x8810
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2eafe(%rip), %rsi     # 0xea337
callq 0x84e0
movq %r14, %rdi
movl %r15d, %esi
callq 0x8810
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r15, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0xbb8af
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x8480
jmp 0xbb8af
movq 0x28(%rsp), %rdx
movl $0x64, %r8d
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
leaq 0x2d277(%rip), %rcx     # 0xe8b21
callq 0x86c0
leaq 0x128(%rsp), %rdi
movq %rsp, %rsi
callq 0x3e394
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rax, %rdi
callq 0x8180
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0xbb8e8
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x8480
incq %rbx
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rcx, %rbx
jne 0xbb5f3
leaq 0x40(%rsp), %rdi
callq 0x31aa2
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xbb938
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x98(%rsp), %rdi
callq 0x8310
movq %rax, %rbx
leaq 0xe0(%rsp), %rax
cmpq %rax, %rbx
jne 0xbb56a
movq 0xf0(%rsp), %rbx
cmpq %rax, %rbx
je 0xbbaff
movq %rsp, %r14
leaq 0x128(%rsp), %rbp
movq 0x4f63c(%rip), %r12     # 0x10afb8
leaq 0x78(%rsp), %r13
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x2e45c
movl $0xd, %edx
movq 0x4f63f(%rip), %rdi     # 0x10afe8
leaq 0x2d1e7(%rip), %rsi     # 0xe8b97
callq 0x84e0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x4f623(%rip), %rdi     # 0x10afe8
callq 0x84e0
movq %rax, %r15
movl $0x9, %edx
movq %rax, %rdi
leaq 0x1f86e(%rip), %rsi     # 0xdb24a
callq 0x84e0
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3e394
movq 0x8(%rax), %rsi
movq %r15, %rdi
callq 0x82f0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r15, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movl $0x1, %edx
movq %r12, %rdi
leaq 0x1af41(%rip), %rsi     # 0xd6972
callq 0x84e0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r12, %rdi
callq 0x84e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r15, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3e394
leaq 0x88(%rsp), %rcx
movq %rcx, 0x78(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x2e45c
movq %r12, %rdi
movq %r13, %rsi
callq 0x53342
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0xbbac8
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x8480
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xbbae3
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x8480
movq %rbx, %rdi
callq 0x8310
movq %rax, %rbx
leaq 0xe0(%rsp), %rax
cmpq %rax, %rbx
jne 0xbb981
leaq 0x128(%rsp), %rdi
callq 0x3f5f4
leaq 0xd8(%rsp), %rdi
callq 0x33302
leaq 0xa8(%rsp), %rdi
callq 0x3f5f4
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0xbbc42
movq %rax, %r14
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xbbc4f
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x8480
jmp 0xbbc4f
movq %rax, %r14
jmp 0xbbc4f
jmp 0xbbb7b
jmp 0xbbbb9
movq %rax, %r14
jmp 0xbbc28
movq %rax, %r14
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0xbbc0d
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x8480
jmp 0xbbc0d
movq %rax, %r14
jmp 0xbbc03
movq %rax, %r14
leaq 0x30(%rsp), %r13
jmp 0xbbbec
movq %rax, %r14
jmp 0xbbc0d
movq %rax, %r14
leaq 0x30(%rsp), %r13
jmp 0xbbbd0
movq %rax, %r14
jmp 0xbbbec
movq %rax, %r14
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0xbbbec
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0xbbc03
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x40(%rsp), %rdi
callq 0x31aa2
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0xbbc28
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x128(%rsp), %rdi
callq 0x3f5f4
leaq 0xd8(%rsp), %rdi
callq 0x33302
leaq 0xa8(%rsp), %rdi
callq 0x3f5f4
movq %r14, %rdi
callq 0x8840