// File: /workspace/github_cpp_makefile_star2_O2/jackz314[P]bars-to-bwav/./bars-to-bwav.cpp
// Function: main
int main(int argc, char const *argv[])
{
    if(argc < 2){
        cout << "Missing file arguments!" << endl;
        displayHelp();
        return -1;
    }

    if(argc == 2 && string(argv[1]) == "-h"){
        displayHelp();
        return 0;
    }

    vector<string> barsFiles;
    if(filesystem::is_directory(argv[1])){// process a directory of bars files
        filesystem::directory_iterator end_itr;
        filesystem::path p(argv[1]);
        cout << "BARS file to be processed in directory " << argv[1] << ':' << endl;
        for(filesystem::directory_iterator itr(p); itr != end_itr; itr++){
            //only deal with bars files
            if (is_regular_file(itr->path()) && itr->path().extension().generic_string() == ".bars") {
                string fileName = itr->path().string();
                barsFiles.push_back(fileName);
                cout << fileName << endl;
            }
        }

    }else{// process single file
        barsFiles.push_back(argv[1]);
        cout << "BARS file to be processed: " << argv[1] << endl;
    }

    string baseOutputDir;
    if(argc > 2){// have output folder or no overwrite
        if(argc == 4 && string(argv[3]) == "--no-overwrite"){ // have no overwrite and output folder
            baseOutputDir = argv[2];
            OVERWRITE = false;
        }
        if(argc == 3 && string(argv[2]) == "--no-overwrite"){ // only no overwrite
            baseOutputDir = "BWAV-Output/";
            OVERWRITE = false;
        }else{ // only out dir
            cout << '['<<argv[2]<<']' << endl;
            baseOutputDir = argv[2];
        }
        if(baseOutputDir.back() != '/' || baseOutputDir.back() != '\\'){
            baseOutputDir += '/';
        }
    }else{
        baseOutputDir = "BWAV-Output/";
    }

    string spliter = "----------------------------------------";
    
    int totalBWAVCount = 0;

    int countB = 0;
    for(string barsFile : barsFiles){
        ifstream inFile(barsFile, ios::binary);
        if(!inFile){
            cout << "Failed to open BARS file " << barsFile << endl;
            return -2;
        }
        inFile.seekg(0, ios::end);
        streamsize barsSize = inFile.tellg();
        inFile.seekg(0,ios::beg);
        
        cout << spliter << '(' << dec << ++countB << '/' << barsFiles.size() << ')' << spliter << endl;
        cout << "Processing BARS file " << barsFile << " size: " << barsSize << endl;

        vector<char> fBuf(barsSize);
        if(!inFile.read(fBuf.data(), barsSize)){
            //read filed
            cout << "Read file failed." << endl;
        }

        vector<string> audioNames;
        vector<streamoff> startOffsets;

        for (streamoff i = 0; i < barsSize - 4; i+=4){
            //Hex representation of BWAV's header start (BWAV: 0x42 0x57 0x41 0x56)
            if(fBuf[i] == 0x42 && fBuf[i+1] == 0x57 && fBuf[i+2] == 0x41 && fBuf[i+3] == 0x56){
                //matched BWAV, send to start offsets
                startOffsets.push_back(i);
               if(VERBOSE) cout << "Found BWAV at offset 0x" << hex << i <<endl;
            }

            //Hex representation of BARS's AMTA header start (AMTA: 0x41 0x4D 0x54 0x41)
            if(fBuf[i] == 0x41 && fBuf[i+1] == 0x4D && fBuf[i+2] == 0x54 && fBuf[i+3] == 0x41){
                //matched AMTA, send to file names
                streamsize nameLen = 0;
                streamoff nameStart = i+0x48;//start of name string offset +0x48 after start of AMTA tag (possibly after a padding)
                if(fBuf[nameStart] == 0x01){
                    //start is 0x01, skip padding (or something else) of 0x8 to the actual file name
                    nameStart += 0x8;
                    /* for(; nameStart < barsSize; nameStart++){
                        if(fBuf[nameStart] != '\0') break; // start of name, not null
                    } */
                }
                //go to file name end
                for(streamoff j = nameStart; j < barsSize; j++, nameLen++){
                    // cout << "CHAR:[" << fBuf[j] << "]. Int: " << int(fBuf[j]) << endl;
                    if(fBuf[j] == -0x3e){ // some files needs to move start idx forward if -c2 is present, inconsistent
                        // cout << "FOUND -3e" << endl; // c2 in signed byte is -3e
                        nameStart = j + 1;
                        nameLen = -1; // so it's 0 when the next iteration begins
                    }
                    if(fBuf[j] == '\0') break; // end of name, null
                }
                string name = string(fBuf.begin() + nameStart, fBuf.begin() + nameStart + nameLen);
                if(find(audioNames.begin(),audioNames.end(), name) != audioNames.end()){//already exists
                    //deal with duplicates with repeat counter
                    int repeatCounter = 1;
                    while(find(audioNames.begin(),audioNames.end(), name + '-' + to_string(repeatCounter)) != audioNames.end()){
                        repeatCounter++;
                    }
                    name += '-' + to_string(repeatCounter);
                }
                audioNames.push_back(name);
                if(VERBOSE) cout << "Found AMTA tag at offset 0x" << hex << i << " Name: " << name << endl;
            }
        }

        cout << endl << "Found all BWAV files. Total count: " << dec << startOffsets.size() << ". Writing bwav files to " << baseOutputDir << endl;
        
        if(!createDirIfNotExist(baseOutputDir)) return -3;

        if(audioNames.size() < startOffsets.size()){
            cout << "BWAV names count is not the same as BWAV counts!" << endl;
            for(size_t i = 1; i < startOffsets.size() - audioNames.size() + 1; i++){
                audioNames.push_back("extra_" + to_string(i));
            }
        }

        //write bwav files
        string onlyBarsFileName = filesystem::path(barsFile).filename().string();
        string outputDir = onlyBarsFileName.substr(0, onlyBarsFileName.size() - 5) + '/';//remove the extension and add directory slash
        cout << "Output subdirectory: " << outputDir << endl;
        if(!createDirIfNotExist(baseOutputDir + outputDir)) return -3;
        int illegal_counter = 0;
        for(size_t i = 0; i < startOffsets.size(); i++){
            streamoff offset = startOffsets[i];
            streamoff nextOffset;
            if(i+1 == startOffsets.size()){
                nextOffset = barsSize;
            }else{
                nextOffset = startOffsets[i+1];            
            }
            streamsize writeSize = nextOffset - offset;
            string fName = audioNames[i] + ".bwav";
            if(VERBOSE) cout << dec << '(' << i+1 << '/' << startOffsets.size() << ") Writing " << fName << " from offset 0x" << hex << offset << endl;
            string oFilePath = baseOutputDir + outputDir + fName;

            //deal with duplicate file names, already dealt with before, here to deal with pre-existing ones
            if(!OVERWRITE && filesystem::exists(oFilePath)){//don't overwrite
                int repeatCounter = 1;
                string newOFilePath = oFilePath.substr(0,oFilePath.size()-5) + '-';
                while(filesystem::exists(newOFilePath + to_string(repeatCounter) + ".bwav")){//already exists, use another name
                    repeatCounter++;
                }
                oFilePath = newOFilePath + to_string(repeatCounter) + ".bwav";
            }

            fstream oFile(oFilePath, ios::out | ios::binary);
            if(!oFile.write(fBuf.data()+offset, writeSize)){// if failed, probably due to illegal name
                ++illegal_counter;
                oFilePath = baseOutputDir + outputDir + "illegal_name_" + to_string(illegal_counter) + ".bwav";
                fstream oFileRetry(oFilePath, ios::out | ios::binary); // try again
                if(!oFileRetry.write(fBuf.data()+offset, writeSize)){ // still failed
                    cout << "Write bwav failed. Path: [" << oFilePath << "] Count: " << dec << i << " Offset: 0x" << hex << offset << endl;
                }else{
                    ++totalBWAVCount;
                }
                oFileRetry.close();
            }else{
                ++totalBWAVCount;
            }
            oFile.close();
        }
    }

    cout << spliter << spliter << endl;
    cout << "Done! Processed " << dec << barsFiles.size() << " BARS files and generated " << totalBWAVCount << " BWAV files in total." << endl;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x888, %rsp            # imm = 0x888
cmpl $0x1, %edi
jg 0x18863
leaq 0xf6c9e(%rip), %rdi     # 0x10f4e0
leaq 0xb68bb(%rip), %rsi     # 0xcf104
callq 0x84350
movq %rax, %rdi
callq 0x83c30
callq 0x187ca
pushq $-0x1
popq %rax
jmp 0x199b5
movq %rsi, %r15
movl %edi, %r12d
cmpl $0x2, %edi
jne 0x188b3
movq 0x8(%r15), %rsi
leaq -0x460(%rbp), %rbx
leaq -0x670(%rbp), %rdx
movq %rbx, %rdi
callq 0x19e2a
leaq 0xb688d(%rip), %rsi     # 0xcf11c
movq %rbx, %rdi
callq 0x19e1a
movl %eax, %r14d
movq %rbx, %rdi
callq 0x8a720
testb %r14b, %r14b
je 0x188b3
callq 0x187ca
xorl %eax, %eax
jmp 0x199b5
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
andq $0x0, -0xc0(%rbp)
leaq 0x8(%r15), %rbx
leaq -0x460(%rbp), %rdi
pushq $0x2
popq %rdx
movq %rbx, %rsi
callq 0x19e76
leaq -0x460(%rbp), %rdi
callq 0x19e65
movl %eax, %r14d
leaq -0x460(%rbp), %rdi
callq 0x19dfa
testb %r14b, %r14b
je 0x18a4d
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
leaq -0x460(%rbp), %rdi
pushq $0x2
popq %rdx
movq %rbx, %rsi
callq 0x19e76
leaq 0xf6bbe(%rip), %rdi     # 0x10f4e0
leaq 0xb67f6(%rip), %rsi     # 0xcf11f
callq 0x84350
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x84350
pushq $0x3a
popq %rsi
movq %rax, %rdi
callq 0x84280
movq %rax, %rdi
callq 0x83c30
movl %r12d, -0x40(%rbp)
movq %r15, -0x38(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x460(%rbp), %rsi
callq 0x19ee4
leaq -0x128(%rbp), %r13
leaq -0x670(%rbp), %r15
leaq -0x8a8(%rbp), %r12
leaq -0x6a0(%rbp), %r14
movq -0x120(%rbp), %rax
cmpq -0xa8(%rbp), %rax
je 0x18aaa
movq %r13, %rdi
callq 0xa59b0
movq %rax, %rdi
callq 0x19ef2
testb %al, %al
je 0x18a33
movq %r13, %rdi
callq 0xa59b0
movq %r15, %rdi
movq %rax, %rsi
callq 0x19f02
movq %r12, %rdi
movq %r15, %rsi
callq 0x19f82
movq %r12, %rdi
leaq 0xb6770(%rip), %rsi     # 0xcf147
callq 0x19e1a
movl %eax, %ebx
movq %r12, %rdi
callq 0x8a720
movq %r15, %rdi
callq 0x19dfa
testb %bl, %bl
je 0x18a33
movq %r13, %rdi
callq 0xa59b0
movq %r15, %rdi
movq %rax, %rsi
callq 0x19f9e
leaq -0xd0(%rbp), %rdi
movq %r15, %rsi
callq 0x19fba
leaq 0xf6ac5(%rip), %rdi     # 0x10f4e0
movq %r15, %rsi
callq 0x8d0e0
movq %rax, %rdi
callq 0x83c30
movq %r15, %rdi
callq 0x8a720
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x19ff6
movq %r14, %rdi
callq 0x19dfa
jmp 0x18983
movq (%rbx), %rsi
leaq -0x460(%rbp), %rdi
leaq -0x670(%rbp), %rdx
callq 0x19e2a
leaq -0xd0(%rbp), %rdi
leaq -0x460(%rbp), %rsi
callq 0x1ac92
leaq -0x460(%rbp), %rdi
callq 0x8a720
leaq 0xf6a57(%rip), %rdi     # 0x10f4e0
leaq 0xb66bd(%rip), %rsi     # 0xcf14d
callq 0x84350
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x84350
movq %rax, %rdi
callq 0x83c30
jmp 0x18ad6
leaq -0x120(%rbp), %rdi
callq 0x1a818
leaq -0x460(%rbp), %rdi
callq 0x19dfa
leaq -0xa8(%rbp), %rdi
callq 0x1a818
movq -0x38(%rbp), %r15
movl -0x40(%rbp), %r12d
leaq -0xa0(%rbp), %rax
andq $0x0, -0x8(%rax)
movq %rax, -0x10(%rax)
movb $0x0, (%rax)
cmpl $0x2, %r12d
jne 0x18b07
leaq 0xb6682(%rip), %rsi     # 0xcf178
leaq -0xb0(%rbp), %rdi
callq 0x8b6d0
jmp 0x18c13
cmpl $0x3, %r12d
je 0x18b6d
cmpl $0x4, %r12d
jne 0x18bc6
movq 0x18(%r15), %rsi
leaq -0x460(%rbp), %rdi
leaq -0x670(%rbp), %rdx
callq 0x19e2a
leaq 0xb6634(%rip), %rsi     # 0xcf169
leaq -0x460(%rbp), %rbx
movq %rbx, %rdi
callq 0x19e1a
movl %eax, %r14d
movq %rbx, %rdi
callq 0x8a720
testb %r14b, %r14b
je 0x18bc6
movq 0x10(%r15), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x8b6d0
movb $0x0, 0xf58e5(%rip)     # 0x10e450
jmp 0x18bc6
movq 0x10(%r15), %rsi
leaq -0x460(%rbp), %rdi
leaq -0x670(%rbp), %rdx
callq 0x19e2a
leaq 0xb65de(%rip), %rsi     # 0xcf169
leaq -0x460(%rbp), %rbx
movq %rbx, %rdi
callq 0x19e1a
movl %eax, %r14d
movq %rbx, %rdi
callq 0x8a720
testb %r14b, %r14b
je 0x18bc6
leaq 0xb65c7(%rip), %rsi     # 0xcf178
leaq -0xb0(%rbp), %rdi
callq 0x8b6d0
movb $0x0, 0xf588c(%rip)     # 0x10e450
jmp 0x18c04
leaq 0xf6913(%rip), %rdi     # 0x10f4e0
pushq $0x5b
popq %rsi
callq 0x84280
movq 0x10(%r15), %rsi
movq %rax, %rdi
callq 0x84350
pushq $0x5d
popq %rsi
movq %rax, %rdi
callq 0x84280
movq %rax, %rdi
callq 0x83c30
movq 0x10(%r15), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x8b6d0
leaq -0xb0(%rbp), %rdi
pushq $0x2f
popq %rsi
callq 0x8adb0
leaq 0xb656b(%rip), %rsi     # 0xcf185
leaq -0x128(%rbp), %rdi
leaq -0x460(%rbp), %rdx
callq 0x19e2a
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq $0x0, -0x48(%rbp)
leaq -0x230(%rbp), %r15
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x2c(%rbp)
cmpq -0x1c8(%rbp), %rsi
je 0x198e9
movq %r15, %rdi
movq %rsi, -0x1d0(%rbp)
callq 0x8ceb0
leaq -0x8a8(%rbp), %rdi
movq %r15, %rsi
pushq $0x4
popq %rdx
callq 0x60810
movq -0x8a8(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x888(%rbp,%rax)
je 0x18cd6
leaq 0xf6837(%rip), %rdi     # 0x10f4e0
leaq 0xb64fe(%rip), %rsi     # 0xcf1ae
callq 0x84350
movq %rax, %rdi
movq %r15, %rsi
callq 0x8d0e0
movq %rax, %rdi
callq 0x83c30
xorl %r14d, %r14d
pushq $-0x2
popq %rax
movl %eax, -0x2c(%rbp)
jmp 0x198bc
leaq -0x8a8(%rbp), %rdi
xorl %esi, %esi
pushq $0x2
popq %rdx
callq 0x69c40
leaq -0x8a8(%rbp), %rbx
movq %rbx, %rdi
callq 0x699d0
movq %rax, -0x38(%rbp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x69c40
leaq 0xf67d3(%rip), %rdi     # 0x10f4e0
leaq -0x128(%rbp), %rsi
callq 0x8d0e0
movq %rax, %rdi
pushq $0x28
popq %rsi
callq 0x84280
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movl -0x4c(%rbp), %esi
incl %esi
movq %rax, %rdi
movl %esi, -0x4c(%rbp)
callq 0x846f0
movq %rax, %rdi
pushq $0x2f
popq %rsi
callq 0x84280
movq -0xc8(%rbp), %rsi
subq -0xd0(%rbp), %rsi
sarq $0x5, %rsi
movq %rax, %rdi
callq 0x84720
movq %rax, %rdi
pushq $0x29
popq %rsi
callq 0x84280
movq %rax, %rdi
leaq -0x128(%rbp), %rsi
callq 0x8d0e0
movq %rax, %rdi
callq 0x83c30
leaq 0xf6749(%rip), %rdi     # 0x10f4e0
leaq 0xb642a(%rip), %rsi     # 0xcf1c8
callq 0x84350
movq %rax, %rdi
movq %r15, %rsi
callq 0x8d0e0
movq %rax, %rdi
leaq 0xb6426(%rip), %rsi     # 0xcf1de
callq 0x84350
movq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x84470
movq %rax, %rdi
callq 0x83c30
leaq -0x180(%rbp), %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
leaq -0x460(%rbp), %rdx
callq 0x1a03a
movq -0x180(%rbp), %rsi
leaq -0x8a8(%rbp), %rdi
movq %rbx, %rdx
callq 0x69380
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x18e2a
leaq 0xf66ca(%rip), %rdi     # 0x10f4e0
leaq 0xb63c9(%rip), %rsi     # 0xcf1e6
callq 0x84350
movq %rax, %rdi
callq 0x83c30
andq $0x0, -0x60(%rbp)
andq $0x0, -0x80(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x90(%rbp)
movq -0x38(%rbp), %rax
leaq -0x4(%rax), %r14
xorl %eax, %eax
movq %r14, -0x40(%rbp)
movq %rax, -0x148(%rbp)
cmpq %r14, %rax
jge 0x19105
movq -0x180(%rbp), %rcx
cmpb $0x42, (%rcx,%rax)
jne 0x18edd
cmpb $0x57, 0x1(%rcx,%rax)
jne 0x18edd
cmpb $0x41, 0x2(%rcx,%rax)
jne 0x18edd
cmpb $0x56, 0x3(%rcx,%rax)
jne 0x18edd
leaq -0x90(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x1a08c
cmpb $0x1, 0xf5726(%rip)     # 0x10e5c2
jne 0x18edd
leaq 0xf663b(%rip), %rdi     # 0x10f4e0
leaq 0xb634c(%rip), %rsi     # 0xcf1f8
callq 0x84350
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq -0x148(%rbp), %rsi
movq %rax, %rdi
callq 0x84470
movq %rax, %rdi
callq 0x83c30
movq -0x148(%rbp), %rax
movq -0x180(%rbp), %rsi
cmpb $0x41, (%rsi,%rax)
jne 0x190fc
cmpb $0x4d, 0x1(%rsi,%rax)
jne 0x190fc
cmpb $0x54, 0x2(%rsi,%rax)
jne 0x190fc
cmpb $0x41, 0x3(%rsi,%rax)
jne 0x190fc
leaq 0x48(%rax), %rcx
leaq 0x50(%rax), %rdx
cmpb $0x1, 0x48(%rsi,%rax)
cmoveq %rdx, %rcx
xorl %edx, %edx
movq %rcx, %rax
cmpq -0x38(%rbp), %rax
jge 0x18f53
movb (%rsi,%rax), %dil
incq %rax
cmpb $-0x3e, %dil
pushq $-0x1
popq %r8
cmoveq %r8, %rdx
cmoveq %rax, %rcx
testb %dil, %dil
je 0x18f53
incq %rdx
jmp 0x18f2c
addq %rcx, %rsi
addq %rsi, %rdx
leaq -0x450(%rbp), %rax
movq %rax, -0x460(%rbp)
leaq -0x460(%rbp), %rbx
movq %rbx, %rdi
callq 0x1b016
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq %rbx, %rdx
callq 0x1b08d
movq -0x68(%rbp), %r14
cmpq %r14, %rax
je 0x1906f
pushq $0x1
popq %rbx
movq -0x70(%rbp), %r15
leaq -0x168(%rbp), %rdi
leaq -0x460(%rbp), %rsi
pushq $0x2d
popq %rdx
callq 0x1a124
leaq -0x108(%rbp), %r12
movq %r12, %rdi
movl %ebx, %esi
callq 0x1a15f
leaq -0x670(%rbp), %r13
movq %r13, %rdi
leaq -0x168(%rbp), %rsi
movq %r12, %rdx
callq 0x1a0b3
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1b08d
movq %rax, %r14
movq -0x68(%rbp), %r15
movq %r13, %rdi
callq 0x8a720
leaq -0x108(%rbp), %rdi
callq 0x8a720
leaq -0x168(%rbp), %rdi
callq 0x8a720
cmpq %r15, %r14
je 0x1901f
incl %ebx
movq -0x68(%rbp), %r14
jmp 0x18f96
leaq -0x168(%rbp), %r14
movq %r14, %rdi
movl %ebx, %esi
callq 0x1a15f
leaq -0x230(%rbp), %r15
leaq -0x670(%rbp), %rbx
movq %rbx, %rdi
pushq $0x2d
popq %rsi
movq %r14, %rdx
callq 0x1a1bf
leaq -0x460(%rbp), %rdi
movq %rbx, %rsi
callq 0x8be30
movq %rbx, %rdi
callq 0x8a720
leaq -0x168(%rbp), %rdi
callq 0x8a720
leaq -0x70(%rbp), %rdi
leaq -0x460(%rbp), %rsi
callq 0x19fba
cmpb $0x1, 0xf553c(%rip)     # 0x10e5c2
jne 0x190e5
leaq 0xf6451(%rip), %rdi     # 0x10f4e0
leaq 0xb617a(%rip), %rsi     # 0xcf210
callq 0x84350
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq -0x148(%rbp), %rsi
movq %rax, %rdi
callq 0x84470
movq %rax, %rdi
leaq 0xb6163(%rip), %rsi     # 0xcf22c
callq 0x84350
movq %rax, %rdi
leaq -0x460(%rbp), %rsi
callq 0x8d0e0
movq %rax, %rdi
callq 0x83c30
leaq -0x460(%rbp), %rdi
callq 0x8a720
movq -0x148(%rbp), %rax
movq -0x40(%rbp), %r14
addq $0x4, %rax
jmp 0x18e50
leaq 0xf63d4(%rip), %rdi     # 0x10f4e0
callq 0x83c30
movq %rax, %rdi
leaq 0xb6119(%rip), %rsi     # 0xcf234
callq 0x84350
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq -0x88(%rbp), %rsi
subq -0x90(%rbp), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x84720
movq %rax, %rdi
leaq 0xb60ff(%rip), %rsi     # 0xcf258
callq 0x84350
movq %rax, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x8d0e0
movq %rax, %rdi
callq 0x83c30
leaq -0xb0(%rbp), %rdi
callq 0x1873c
testb %al, %al
je 0x19247
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
sarq $0x5, %rax
movq -0x88(%rbp), %rcx
subq -0x90(%rbp), %rcx
sarq $0x3, %rcx
cmpq %rcx, %rax
jae 0x19255
leaq 0xf6329(%rip), %rdi     # 0x10f4e0
leaq 0xb60b3(%rip), %rsi     # 0xcf271
callq 0x84350
movq %rax, %rdi
callq 0x83c30
pushq $0x1
popq %rbx
movq -0x88(%rbp), %rax
subq -0x90(%rbp), %rax
sarq $0x3, %rax
movq -0x68(%rbp), %rcx
subq -0x70(%rbp), %rcx
sarq $0x5, %rcx
subq %rcx, %rax
incq %rax
cmpq %rax, %rbx
jae 0x19255
leaq -0x670(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1a21d
leaq -0x460(%rbp), %r12
movq %r12, %rdi
leaq 0xb6088(%rip), %rsi     # 0xcf2a2
movq %r14, %rdx
callq 0x1a1ef
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x1ac92
movq %r12, %rdi
callq 0x8a720
leaq -0x670(%rbp), %rdi
callq 0x8a720
incq %rbx
jmp 0x191ce
xorl %r14d, %r14d
pushq $-0x3
popq %rax
movl %eax, -0x2c(%rbp)
jmp 0x1989b
leaq -0x670(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x2
popq %rdx
callq 0x19d72
leaq -0x460(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1a260
leaq -0x168(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x19f9e
movq %r14, %rdi
callq 0x19dfa
leaq -0x670(%rbp), %rdi
callq 0x19dfa
movq -0x160(%rbp), %rcx
addq $-0x5, %rcx
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x8d060
leaq -0x108(%rbp), %rdi
movq %r14, %rsi
pushq $0x2f
popq %rdx
callq 0x1a2d8
movq %r14, %rdi
callq 0x8a720
leaq 0xf6205(%rip), %rdi     # 0x10f4e0
leaq 0xb5fc7(%rip), %rsi     # 0xcf2a9
callq 0x84350
movq %rax, %rdi
leaq -0x108(%rbp), %rsi
callq 0x8d0e0
movq %rax, %rdi
callq 0x83c30
leaq -0x460(%rbp), %rbx
movq %rbx, %rdi
leaq -0xb0(%rbp), %rsi
leaq -0x108(%rbp), %rdx
callq 0x1a304
movq %rbx, %rdi
callq 0x1873c
movl %eax, %r14d
movq %rbx, %rdi
callq 0x8a720
testb %r14b, %r14b
je 0x1987d
movq $0x0, -0xe0(%rbp)
xorl %edx, %edx
leaq -0x108(%rbp), %rdi
movl %r14d, -0xd4(%rbp)
movq -0x90(%rbp), %rax
movq -0x88(%rbp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %rdx
jae 0x1988a
movq %rdx, -0xe8(%rbp)
incq %rdx
movq -0x38(%rbp), %rbx
cmpq %rcx, %rdx
je 0x19387
movq (%rax,%rdx,8), %rbx
movq %rdx, -0x40(%rbp)
movq -0xe8(%rbp), %rsi
movq (%rax,%rsi,8), %r15
shlq $0x5, %rsi
addq -0x70(%rbp), %rsi
leaq -0x148(%rbp), %rdi
leaq 0xb5f13(%rip), %rdx     # 0xcf2bf
callq 0x1a33c
cmpb $0x1, 0xf520a(%rip)     # 0x10e5c2
jne 0x19468
leaq 0xf611b(%rip), %rdi     # 0x10f4e0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movl 0x18(%rdi,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%rdi,%rax)
pushq $0x28
popq %rsi
callq 0x84280
movq %rax, %rdi
movq -0x40(%rbp), %rsi
callq 0x84720
movq %rax, %rdi
pushq $0x2f
popq %rsi
callq 0x84280
movq -0x88(%rbp), %rsi
subq -0x90(%rbp), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x84720
movq %rax, %rdi
leaq 0xb5ea8(%rip), %rsi     # 0xcf2c5
callq 0x84350
movq %rax, %rdi
leaq -0x148(%rbp), %rsi
callq 0x8d0e0
movq %rax, %rdi
leaq 0xb5e95(%rip), %rsi     # 0xcf2d0
callq 0x84350
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movq %r15, %rsi
callq 0x84470
movq %rax, %rdi
callq 0x83c30
leaq -0x460(%rbp), %r12
movq %r12, %rdi
leaq -0xb0(%rbp), %rsi
leaq -0x108(%rbp), %rdx
callq 0x1a304
leaq -0x1c0(%rbp), %rdi
movq %r12, %rsi
leaq -0x148(%rbp), %rdx
callq 0x1a374
movq %r12, %rdi
callq 0x8a720
cmpb $0x0, 0xf4fa6(%rip)     # 0x10e450
jne 0x19630
movq %r12, %rdi
leaq -0x1c0(%rbp), %rsi
pushq $0x2
popq %rdx
callq 0x19d72
movq %r12, %rdi
callq 0x19ddc
movl %eax, %r14d
movq %r12, %rdi
callq 0x19dfa
testb %r14b, %r14b
je 0x19630
movq -0x1b8(%rbp), %rcx
addq $-0x5, %rcx
movq %r12, %rdi
leaq -0x1c0(%rbp), %rsi
xorl %edx, %edx
callq 0x8d060
leaq -0x670(%rbp), %rdi
movq %r12, %rsi
pushq $0x2d
popq %rdx
callq 0x1a2d8
movq %r12, %rdi
callq 0x8a720
pushq $0x1
popq %r14
leaq -0x210(%rbp), %r12
movq %r12, %rdi
movl %r14d, %esi
callq 0x1a15f
leaq -0x1a0(%rbp), %r13
movq %r13, %rdi
leaq -0x670(%rbp), %rsi
movq %r12, %rdx
callq 0x1a3c6
leaq -0x1f0(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
leaq 0xb5d68(%rip), %rdx     # 0xcf2bf
callq 0x1a39d
leaq -0x460(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
pushq $0x2
popq %rdx
callq 0x1a3f4
movq %r13, %rdi
callq 0x19ddc
movl %eax, %r12d
movq %r13, %rdi
callq 0x19dfa
leaq -0x1f0(%rbp), %rdi
callq 0x8a720
leaq -0x1a0(%rbp), %rdi
callq 0x8a720
leaq -0x210(%rbp), %rdi
callq 0x8a720
testb %r12b, %r12b
je 0x195b5
incl %r14d
jmp 0x19518
leaq -0x1a0(%rbp), %r12
movq %r12, %rdi
movl %r14d, %esi
callq 0x1a15f
leaq -0x1f0(%rbp), %r14
movq %r14, %rdi
leaq -0x670(%rbp), %rsi
movq %r12, %rdx
callq 0x1a3c6
leaq -0x460(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
leaq 0xb5ccb(%rip), %rdx     # 0xcf2bf
callq 0x1a39d
leaq -0x1c0(%rbp), %rdi
movq %r12, %rsi
callq 0x8a770
movq %r12, %rdi
callq 0x8a720
movq %r14, %rdi
callq 0x8a720
leaq -0x1a0(%rbp), %rdi
callq 0x8a720
leaq -0x670(%rbp), %rdi
callq 0x8a720
movq %r12, %rdi
leaq -0x1c0(%rbp), %rsi
pushq $0x14
popq %rdx
callq 0x60210
subq %r15, %rbx
movq -0x180(%rbp), %rsi
addq %r15, %rsi
leaq -0x450(%rbp), %rdi
movq %rbx, %rdx
callq 0x83aa0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x19801
leaq -0x210(%rbp), %r14
movq %r14, %rdi
leaq -0xb0(%rbp), %rsi
leaq -0x108(%rbp), %rdx
callq 0x1a304
leaq -0x1a0(%rbp), %rdi
movq %r14, %rsi
leaq 0xb5c42(%rip), %rdx     # 0xcf2e0
callq 0x1a39d
movq -0xe0(%rbp), %rsi
incl %esi
leaq -0x250(%rbp), %r14
movq %r14, %rdi
movq %rsi, -0xe0(%rbp)
callq 0x1a15f
leaq -0x1f0(%rbp), %r13
movq %r13, %rdi
leaq -0x1a0(%rbp), %rsi
movq %r14, %rdx
callq 0x1a0b3
leaq -0x670(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
leaq 0xb5bd0(%rip), %rdx     # 0xcf2bf
callq 0x1a39d
leaq -0x1c0(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x8a770
movq %r14, %rdi
callq 0x8a720
movq %r13, %rdi
callq 0x8a720
leaq -0x250(%rbp), %rdi
callq 0x8a720
leaq -0x1a0(%rbp), %rdi
callq 0x8a720
leaq -0x210(%rbp), %rdi
callq 0x8a720
movq %r14, %rdi
movq %r12, %rsi
pushq $0x14
popq %rdx
callq 0x60210
movq -0x180(%rbp), %rsi
addq %r15, %rsi
leaq -0x660(%rbp), %rdi
movq %rbx, %rdx
callq 0x83aa0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x1980d
leaq 0xf5d66(%rip), %rdi     # 0x10f4e0
leaq 0xb5b6d(%rip), %rsi     # 0xcf2ee
callq 0x84350
movq %rax, %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x8d0e0
movq %rax, %rdi
leaq 0xb5b6a(%rip), %rsi     # 0xcf309
callq 0x84350
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movq -0xe8(%rbp), %rsi
callq 0x84720
movq %rax, %rdi
leaq 0xb5b41(%rip), %rsi     # 0xcf313
callq 0x84350
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movq %r15, %rsi
callq 0x84470
movq %rax, %rdi
callq 0x83c30
jmp 0x19817
movq -0x48(%rbp), %rax
incl %eax
movq %rax, -0x48(%rbp)
jmp 0x1982f
movq -0x48(%rbp), %rax
incl %eax
movq %rax, -0x48(%rbp)
leaq -0x670(%rbp), %rdi
callq 0x61210
leaq -0x670(%rbp), %rdi
callq 0x619e0
leaq -0x460(%rbp), %rdi
callq 0x61210
leaq -0x460(%rbp), %rdi
callq 0x619e0
leaq -0x1c0(%rbp), %rdi
callq 0x8a720
leaq -0x148(%rbp), %rdi
callq 0x8a720
movq -0x40(%rbp), %rdx
leaq -0x230(%rbp), %r15
leaq -0x108(%rbp), %rdi
movl -0xd4(%rbp), %r14d
jmp 0x19352
pushq $-0x3
popq %rax
movl %eax, -0x2c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x8a720
leaq -0x168(%rbp), %rdi
callq 0x8a720
leaq -0x90(%rbp), %rdi
callq 0x1ae7c
leaq -0x70(%rbp), %rdi
callq 0x1a446
leaq -0x180(%rbp), %rdi
callq 0x1adea
leaq -0x8a8(%rbp), %rdi
callq 0x61540
movq %r15, %rdi
callq 0x8a720
movq -0x1d0(%rbp), %rsi
addq $0x20, %rsi
testb %r14b, %r14b
jne 0x18c5f
jmp 0x1998e
leaq 0xf5bf0(%rip), %rdi     # 0x10f4e0
leaq -0x128(%rbp), %rsi
callq 0x8d0e0
leaq -0x128(%rbp), %rsi
movq %rax, %rdi
callq 0x8d0e0
movq %rax, %rdi
callq 0x83c30
leaq 0xf5bc6(%rip), %rdi     # 0x10f4e0
leaq 0xb59fe(%rip), %rsi     # 0xcf31f
callq 0x84350
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq -0xc8(%rbp), %rsi
subq -0xd0(%rbp), %rsi
sarq $0x5, %rsi
movq %rax, %rdi
callq 0x84720
leaq 0xb59d4(%rip), %rsi     # 0xcf330
movq %rax, %rdi
callq 0x84350
movq %rax, %rdi
movq -0x48(%rbp), %rsi
callq 0x846f0
leaq 0xb59d4(%rip), %rsi     # 0xcf34b
movq %rax, %rdi
callq 0x84350
movl $0x0, -0x2c(%rbp)
movq %rax, %rdi
callq 0x83c30
leaq -0x128(%rbp), %rdi
callq 0x8a720
leaq -0xb0(%rbp), %rdi
callq 0x8a720
leaq -0xd0(%rbp), %rdi
callq 0x1a446
movl -0x2c(%rbp), %eax
addq $0x888, %rsp            # imm = 0x888
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x19a69
jmp 0x19a69
jmp 0x19a0c
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
jmp 0x19d58
jmp 0x19a04
movq %rax, %rbx
jmp 0x19bb2
jmp 0x19a69
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
callq 0x19dfa
jmp 0x19d5d
jmp 0x19a04
movq %rax, %rbx
jmp 0x19d5d
movq %rax, %rbx
jmp 0x19ba6
movq %rax, %rbx
jmp 0x19a58
jmp 0x19c0d
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
jmp 0x19cef
jmp 0x19be5
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
jmp 0x19cfb
movq %rax, %rbx
jmp 0x19d00
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
callq 0x19dfa
leaq -0x670(%rbp), %rdi
callq 0x19dfa
jmp 0x19d0c
movq %rax, %rbx
jmp 0x19d51
jmp 0x19b97
jmp 0x19b23
jmp 0x19c15
jmp 0x19c15
movq %rax, %rbx
leaq -0x670(%rbp), %rdi
jmp 0x19c65
jmp 0x19c6c
jmp 0x19c7d
jmp 0x19b97
movq %rax, %rbx
leaq -0x670(%rbp), %rdi
callq 0x19dfa
jmp 0x19b9a
jmp 0x19be5
movq %rax, %rbx
leaq -0x1f0(%rbp), %rdi
callq 0x8a720
jmp 0x19ad0
movq %rax, %rbx
leaq -0x1a0(%rbp), %rdi
jmp 0x19cc6
jmp 0x19ccd
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
jmp 0x19cd7
jmp 0x19bcc
jmp 0x19c7d
jmp 0x19c0d
movq %rax, %rbx
leaq -0x670(%rbp), %rdi
callq 0x8a720
jmp 0x19b9a
movq %rax, %rbx
jmp 0x19d21
movq %rax, %rbx
jmp 0x19d39
movq %rax, %rbx
jmp 0x19d45
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
callq 0x19dfa
jmp 0x19cdc
jmp 0x19bcc
movq %rax, %rbx
leaq -0x1f0(%rbp), %rdi
callq 0x8a720
jmp 0x19b67
movq %rax, %rbx
jmp 0x19b73
jmp 0x19c1d
jmp 0x19c1d
movq %rax, %rbx
leaq -0x250(%rbp), %rdi
callq 0x8a720
leaq -0x1a0(%rbp), %rdi
callq 0x8a720
jmp 0x19b84
movq %rax, %rbx
leaq -0x210(%rbp), %rdi
callq 0x8a720
jmp 0x19c31
jmp 0x19c0d
movq %rax, %rbx
leaq -0x120(%rbp), %rdi
callq 0x1a818
leaq -0x460(%rbp), %rdi
callq 0x19dfa
leaq -0xa8(%rbp), %rdi
callq 0x1a818
jmp 0x19d5d
jmp 0x19c15
jmp 0x19c7d
jmp 0x19c0d
movq %rax, %rbx
jmp 0x19cdc
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
jmp 0x19ce3
jmp 0x19c42
movq %rax, %rbx
jmp 0x19cf4
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
callq 0x8a720
jmp 0x19c01
movq %rax, %rbx
leaq -0x670(%rbp), %rdi
jmp 0x19d07
movq %rax, %rbx
jmp 0x19d0c
movq %rax, %rbx
jmp 0x19d2d
movq %rax, %rbx
jmp 0x19c31
movq %rax, %rbx
leaq -0x670(%rbp), %rdi
callq 0x619e0
leaq -0x460(%rbp), %rdi
callq 0x619e0
jmp 0x19cdc
movq %rax, %rbx
jmp 0x19ce8
movq %rax, %rbx
leaq -0x670(%rbp), %rdi
callq 0x8a720
jmp 0x19c5e
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
callq 0x8a720
jmp 0x19c6f
movq %rax, %rbx
leaq -0x168(%rbp), %rdi
callq 0x8a720
jmp 0x19c80
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
jmp 0x19d07
movq %rax, %rbx
jmp 0x19ca2
movq %rax, %rbx
jmp 0x19cae
movq %rax, %rbx
leaq -0x460(%rbp), %rdi
callq 0x19dfa
leaq -0x1f0(%rbp), %rdi
callq 0x8a720
leaq -0x1a0(%rbp), %rdi
callq 0x8a720
jmp 0x19cbf
movq %rax, %rbx
leaq -0x210(%rbp), %rdi
callq 0x8a720
jmp 0x19cd0
movq %rax, %rbx
leaq -0x670(%rbp), %rdi
callq 0x8a720
leaq -0x1c0(%rbp), %rdi
callq 0x8a720
leaq -0x148(%rbp), %rdi
callq 0x8a720
leaq -0x108(%rbp), %rdi
callq 0x8a720
leaq -0x168(%rbp), %rdi
callq 0x8a720
leaq -0x90(%rbp), %rdi
callq 0x1ae7c
leaq -0x70(%rbp), %rdi
callq 0x1a446
leaq -0x180(%rbp), %rdi
callq 0x1adea
leaq -0x8a8(%rbp), %rdi
callq 0x61540
leaq -0x230(%rbp), %rdi
callq 0x8a720
leaq -0x128(%rbp), %rdi
callq 0x8a720
leaq -0xb0(%rbp), %rdi
callq 0x8a720
leaq -0xd0(%rbp), %rdi
callq 0x1a446
movq %rbx, %rdi
callq 0x137b0
nop