// File: /workspace/github_cpp_makefile_star2_O3/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 $0x168, %rsp            # imm = 0x168
callq 0xbac64
movq 0x47c15(%rip), %rbx     # 0x101fe8
leaq 0x25747(%rip), %rsi     # 0xdfb21
movl $0x52, %edx
movq %rbx, %rdi
callq 0x84e0
movq 0x49cf2(%rip), %rsi     # 0x1040e0
movq 0x49cf3(%rip), %rdx     # 0x1040e8
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 0x25746(%rip), %rsi     # 0xdfb74
movl $0x24, %edx
movq %rbx, %rdi
callq 0x84e0
movq 0x49cbe(%rip), %rsi     # 0x104100
movq 0x49cbf(%rip), %rdx     # 0x104108
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 0x49c5e(%rip), %rsi     # 0x1040e0
leaq 0xb8(%rsp), %rdi
leaq 0xe8(%rsp), %rdx
callq 0x287de
leaq 0xb8(%rsp), %rdi
movl $0x8, %esi
callq 0x500af
movq %rax, %rbx
leaq 0xc8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xba4cd
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x49c2c(%rip), %rsi     # 0x104100
leaq 0xb8(%rsp), %rdi
callq 0x5072f
leaq 0xe8(%rsp), %rdi
movq %rbx, %rsi
callq 0xba240
leaq 0x120(%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 0xf0(%rsp), %rax
movq 0x10(%rax), %rcx
cmpq %rax, %rcx
je 0xba958
leaq 0x38(%rsp), %r13
leaq 0x48(%rsp), %r14
leaq 0x148(%rsp), %r15
movq 0x47aa9(%rip), %r12     # 0x101fe8
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rcx), %rsi
movq 0x28(%rcx), %rdx
addq %rsi, %rdx
movq %rcx, %rbx
leaq 0x8(%rsp), %rdi
callq 0x2ba06
movq %rbx, 0xa0(%rsp)
leaq 0x40(%rbx), %rsi
movq %r14, %rdi
callq 0x30dd2
leaq 0x118(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3b46e
movq 0x8(%rax), %rdx
movq %rax, %rdi
xorl %esi, %esi
leaq 0x13434(%rip), %rcx     # 0xcd9cb
xorl %r8d, %r8d
callq 0x86c0
leaq 0x118(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3b46e
movl $0x2faf080, %esi        # imm = 0x2FAF080
movq %rax, %rdi
callq 0x86f0
movq 0x48(%rsp), %rax
cmpq %rax, 0x50(%rsp)
je 0xba906
xorl %ebp, %ebp
movq %r13, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
leaq (,%rbp,2), %r14
addq %rbp, %r14
movq (%rax,%r14,8), %rdi
movl $0xc0, %eax
addq %rax, %rdi
leaq 0x1ab83(%rip), %rsi     # 0xd5184
callq 0x8160
testl %eax, %eax
je 0xba882
movq %rbp, 0xb0(%rsp)
movq %r15, %r13
movq 0x48(%rsp), %rax
movq (%rax,%r14,8), %rax
leaq 0x70(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movq 0xa0(%rax), %rsi
movq 0xa8(%rax), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %r15
movq %r15, %rdi
callq 0x2ba06
movq 0x48(%rsp), %rax
movq (%rax,%r14,8), %rbx
movq 0xc0(%rbx), %rdi
callq 0x86e0
movl %eax, %ebp
movq 0xe0(%rbx), %rdi
callq 0x86e0
movl %eax, %ebx
leaq 0xb8(%rsp), %rdi
movq %r15, %rsi
callq 0xbaee8
movq %r13, %r15
movl %ebx, %ecx
subl %ebp, %ecx
incl %ecx
leal -0x1(%rbp), %edx
movslq %edx, %rdx
movslq %ecx, %rcx
movq %r13, %rdi
movq %rax, %rsi
callq 0x84a0
movq %rbp, 0xa8(%rsp)
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x8540
movq 0x148(%rsp), %rdi
leaq 0x158(%rsp), %rax
cmpq %rax, %rdi
je 0xba6d6
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x48(%rsp), %rax
movq (%rax,%r14,8), %rdi
movl $0x100, %eax            # imm = 0x100
addq %rax, %rdi
leaq 0x129ca(%rip), %rsi     # 0xcd0b8
callq 0x8160
testl %eax, %eax
leaq 0x38(%rsp), %r13
movq 0xb0(%rsp), %rbp
jne 0xba7cc
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rdx
testq %rdx, %rdx
setne %sil
leaq (%rax,%rdx), %rcx
decq %rcx
cmpq %rax, %rcx
seta %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0xba753
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 0xba733
movq 0x30(%rsp), %rdx
testq %rdx, %rdx
je 0xba7cc
xorl %eax, %eax
movq 0x28(%rsp), %rcx
movzbl (%rcx,%rax), %edx
cmpl $0x60, %edx
jg 0xba77f
cmpl $0x46, %edx
jg 0xba793
movb $0x54, %sil
cmpl $0x41, %edx
je 0xba7be
cmpl $0x43, %edx
jne 0xba7c2
movb $0x47, %sil
jmp 0xba7be
cmpl $0x66, %edx
jg 0xba7a2
cmpl $0x61, %edx
je 0xba7b1
cmpl $0x63, %edx
jne 0xba7c2
movb $0x67, %sil
jmp 0xba7be
cmpl $0x47, %edx
je 0xba7b6
cmpl $0x54, %edx
jne 0xba7c2
movb $0x41, %sil
jmp 0xba7be
cmpl $0x67, %edx
je 0xba7bb
cmpl $0x74, %edx
jne 0xba7c2
movb $0x61, %sil
jmp 0xba7be
movb $0x74, %sil
jmp 0xba7be
movb $0x43, %sil
jmp 0xba7be
movb $0x63, %sil
movb %sil, (%rcx,%rax)
incq %rax
cmpq 0x30(%rsp), %rax
jne 0xba75a
movl $0x6, %edx
movq %r12, %rdi
leaq 0x25423(%rip), %rsi     # 0xdfbfe
callq 0x84e0
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq %r12, %rdi
callq 0x84e0
movq %rax, %r14
movl $0x9, %edx
movq %rax, %rdi
leaq 0x25401(%rip), %rsi     # 0xdfc05
callq 0x84e0
movq %r14, %rdi
movq 0xa8(%rsp), %rsi
callq 0x8810
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x26b84(%rip), %rsi     # 0xe13af
callq 0x84e0
movq %r14, %rdi
movl %ebx, %esi
callq 0x8810
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 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xba8a0
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x8480
jmp 0xba8a0
movq 0x30(%rsp), %rdx
movl $0x64, %r8d
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
leaq 0x252fe(%rip), %rcx     # 0xdfb99
callq 0x86c0
leaq 0x118(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3b46e
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %rax, %rdi
callq 0x8180
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0xba8db
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x8480
incq %rbp
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rcx, %rbp
jne 0xba5d0
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x2efec
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xba92f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0xa0(%rsp), %rdi
callq 0x8310
movq %rax, %rcx
leaq 0xf0(%rsp), %rax
cmpq %rax, %rcx
jne 0xba53f
movq 0x100(%rsp), %rcx
cmpq %rax, %rcx
je 0xbab08
leaq 0x8(%rsp), %rbx
leaq 0x118(%rsp), %r13
movq 0x47643(%rip), %rbp     # 0x101fb8
leaq 0x80(%rsp), %r12
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rcx), %rsi
movq %rcx, %r14
movq 0x28(%rcx), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x2ba06
movl $0xd, %edx
movq 0x4763f(%rip), %rdi     # 0x101fe8
leaq 0x2525f(%rip), %rsi     # 0xdfc0f
callq 0x84e0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x47622(%rip), %rdi     # 0x101fe8
callq 0x84e0
movq %rax, %r15
movl $0x9, %edx
movq %rax, %rdi
leaq 0x1a3ad(%rip), %rsi     # 0xd4d8a
callq 0x84e0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3b46e
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 %rbp, %rdi
leaq 0x151a8(%rip), %rsi     # 0xcfbda
callq 0x84e0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbp, %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 %r13, %rdi
movq %rbx, %rsi
callq 0x3b46e
leaq 0x90(%rsp), %rcx
movq %rcx, 0x80(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x2ba06
movq %rbp, %rdi
movq %r12, %rsi
callq 0x501cd
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0xbaad0
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xbaaec
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x8480
movq %r14, %rdi
callq 0x8310
movq %rax, %rcx
leaq 0xf0(%rsp), %rax
cmpq %rax, %rcx
jne 0xba97d
leaq 0x118(%rsp), %rdi
callq 0x3c6d4
leaq 0xe8(%rsp), %rdi
callq 0x30870
leaq 0xb8(%rsp), %rdi
callq 0x3c6d4
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xbac4f
movq %rax, %rbx
leaq 0xc8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xbac5c
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x8480
jmp 0xbac5c
movq %rax, %rbx
jmp 0xbac5c
jmp 0xbab84
jmp 0xbabc5
movq %rax, %rbx
jmp 0xbac35
movq %rax, %rbx
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0xbac19
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x8480
jmp 0xbac19
movq %rax, %rbx
jmp 0xbac0f
movq %rax, %rbx
leaq 0x38(%rsp), %r13
jmp 0xbabf8
movq %rax, %rbx
jmp 0xbac19
movq %rax, %rbx
leaq 0x38(%rsp), %r13
jmp 0xbabdc
movq %rax, %rbx
jmp 0xbabf8
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xbabf8
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0xbac0f
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x48(%rsp), %rdi
callq 0x2efec
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xbac35
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x118(%rsp), %rdi
callq 0x3c6d4
leaq 0xe8(%rsp), %rdi
callq 0x30870
leaq 0xb8(%rsp), %rdi
callq 0x3c6d4
movq %rbx, %rdi
callq 0x8840