// File: /workspace/github_cpp_makefile_star2_O2/OpenFodder[P]SOS_Unpacker/SOS_Unpacker.cpp
// Function: ExtractFiles(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void ExtractFiles(const std::vector<std::string> pTrackBuffers, const std::string& pTargetFolder) {

    // Track3 is the directory listing
    UBYTE* TrackPtr = (UBYTE*)pTrackBuffers[2].data();
    UDWORD FileCount = readBEDWord(TrackPtr + 0x18);

    // Move to first file entry
    TrackPtr += 0x20;
    for (UDWORD Count = 0; Count < FileCount; ++Count) {
        std::string Filename((char*)TrackPtr);
        UDWORD Filesize = readBEDWord(TrackPtr + 0x1C);
        UBYTE FileTrack = *(TrackPtr + 0x1A);
        UBYTE FileBlock = *(TrackPtr + 0x1B);

        std::cout << std::setw(15) << Filename << "  Track: ";
        std::cout << std::setw(4) << ((size_t)FileTrack) << "  Block: ";
        std::cout << std::setw(3) << ((size_t)FileBlock) << "  Size: ";
        std::cout << std::setw(6) << ((size_t)Filesize) << " bytes\n";

        std::string FileData;
        // Read each block
        SDWORD FileRemaining = Filesize;
        while (FileRemaining > 0) {
            UBYTE* FileTrackPtr = (UBYTE*)pTrackBuffers[FileTrack].data();
            UBYTE *FileTrackPtrEnd = (UBYTE*)pTrackBuffers[FileTrack].data() + pTrackBuffers[FileTrack].size();
            FileTrackPtr += (FileBlock * 512);

            if (FileTrackPtr > FileTrackPtrEnd || (FileTrackPtr + 512) > FileTrackPtrEnd) {
                std::cout << "  Track: " << ((size_t)FileTrack) << ": Read past end of available track data.\n\n";
                break;
            }
            FileData.append((char*)FileTrackPtr, 510);
            FileRemaining -= 510;
            FileTrackPtr += 510;

            FileTrack = *FileTrackPtr++;
            FileBlock = *FileTrackPtr++;
        }

        // Truncate to actual file size
        FileData.resize(Filesize);

        std::ofstream outfile;
        outfile.open(pTargetFolder + "//" + Filename, std::ios_base::out | std::ios::binary);
        outfile.write((char*)FileData.data(), FileData.size());
        outfile.close();

        // Next File
        TrackPtr += 0x20;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
movq (%rdi), %rax
movq 0x40(%rax), %r12
leaq 0x18(%r12), %rdi
callq 0xe1f5
movl %eax, (%rsp)
xorl %eax, %eax
leaq 0x58(%rsp), %r15
leaq 0x98(%rsp), %r13
leaq 0x18(%rsp), %rbx
cmpl (%rsp), %eax
je 0xde4e
movl %eax, 0x4(%rsp)
leaq 0x20(%r12), %rsi
movq %r15, %rdi
movq %rsi, 0x10(%rsp)
movq %r13, %rdx
callq 0x30a0
leaq 0x3c(%r12), %rdi
callq 0xe1f5
movq %r15, %rsi
movl %eax, %r15d
movb 0x3a(%r12), %bpl
movb 0x3b(%r12), %r13b
movq 0x938c(%rip), %rdi      # 0x16fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xf, 0x10(%rdi,%rax)
callq 0x3190
movq %rax, %rdi
leaq 0x180c(%rip), %rsi      # 0xf46f
callq 0x31c0
movzbl %bpl, %esi
movq 0x935d(%rip), %rdi      # 0x16fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rdi,%rax)
callq 0x3120
movq %rax, %rdi
leaq 0x17e7(%rip), %rsi      # 0xf479
callq 0x31c0
movzbl %r13b, %esi
movq 0x932e(%rip), %rdi      # 0x16fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x3, 0x10(%rdi,%rax)
callq 0x3120
movq %rax, %rdi
leaq 0x17c2(%rip), %rsi      # 0xf483
callq 0x31c0
movq 0x9303(%rip), %rdi      # 0x16fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%rdi,%rax)
movl %r15d, %r12d
movq %r12, %rsi
callq 0x3120
movq %rax, %rdi
leaq 0x179a(%rip), %rsi      # 0xf48c
callq 0x31c0
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
andq $0x0, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
testl %r15d, %r15d
jle 0xdda0
movzbl %bpl, %ebp
movq (%r14), %rdx
movl %ebp, %esi
shll $0x5, %esi
movq 0x8(%rdx,%rsi), %rcx
movzbl %r13b, %eax
shll $0x9, %eax
cmpq %rcx, %rax
jg 0xdd73
movq (%rdx,%rsi), %r13
addq %r13, %rcx
leaq (%rax,%r13), %rdx
addq $0x200, %rdx            # imm = 0x200
cmpq %rcx, %rdx
ja 0xdd73
addq %rax, %r13
movl $0x1fe, %edx            # imm = 0x1FE
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3050
addl $0xfffffe02, %r15d      # imm = 0xFFFFFE02
movb 0x1fe(%r13), %bpl
movb 0x1ff(%r13), %r13b
jmp 0xdd0c
movq 0x9256(%rip), %rdi      # 0x16fd0
leaq 0x16ee(%rip), %rsi      # 0xf46f
callq 0x31c0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3120
movq %rax, %rdi
leaq 0x16f9(%rip), %rsi      # 0xf494
callq 0x31c0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3290
leaq 0x98(%rsp), %r13
movq %r13, %rdi
callq 0x3360
leaq 0x58(%rsp), %r15
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movq 0x8(%rsp), %rsi
leaq 0x16eb(%rip), %rdx      # 0xf4bf
callq 0xe219
leaq 0x78(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xe1fa
movq %r13, %rdi
movq %rbp, %rsi
pushq $0x14
popq %rdx
callq 0x30e0
movq %rbp, %rdi
callq 0x33f8
leaq 0x38(%rsp), %rdi
callq 0x33f8
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r13, %rdi
callq 0x3270
movq %r13, %rdi
callq 0x32a0
movq %r13, %rdi
callq 0x32d0
movq %rbx, %rdi
callq 0x33f8
movq %r15, %rdi
callq 0x33f8
movl 0x4(%rsp), %eax
incl %eax
movq 0x10(%rsp), %r12
jmp 0xdc01
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xde72
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x33f8
leaq 0x38(%rsp), %rdi
callq 0x33f8
jmp 0xde89
jmp 0xde9d
jmp 0xde9d
jmp 0xde86
jmp 0xde9d
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x32d0
jmp 0xdea0
movq %rax, %rbx
jmp 0xdeaa
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x33f8
leaq 0x58(%rsp), %rdi
callq 0x33f8
movq %rbx, %rdi
callq 0x3380