// File: /workspace/github_cpp_makefile_star2_O3/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 $0x2c8, %rsp            # imm = 0x2C8
movq %rsi, 0xb0(%rsp)
movq (%rdi), %rax
movq 0x40(%rax), %rcx
movl 0x18(%rcx), %eax
bswapl %eax
movl %eax, 0x28(%rsp)
testl %eax, %eax
je 0x105e0
leaq 0x18(%rsp), %r14
movq 0x8de0(%rip), %rax      # 0x18fc8
movq (%rax), %rdx
movq %rdx, 0xa0(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x98(%rsp)
xorl %eax, %eax
movq %rdi, 0xa8(%rsp)
movl %eax, 0x2c(%rsp)
movq %rcx, %rbx
leaq 0x20(%rcx), %r15
movq %r14, 0x8(%rsp)
movq %r15, %rdi
callq 0x40f0
leaq (%rbx,%rax), %rdx
addq $0x20, %rdx
leaq 0x8(%rsp), %rdi
movq %r15, 0xb8(%rsp)
movq %r15, %rsi
callq 0x10bd4
movzbl 0x3c(%rbx), %r12d
movzbl 0x3d(%rbx), %r15d
movzbl 0x3e(%rbx), %r14d
movzbl 0x3f(%rbx), %eax
movq %rax, 0x50(%rsp)
movb 0x3a(%rbx), %r13b
movb 0x3b(%rbx), %bpl
movq 0x8d6b(%rip), %rdi      # 0x18fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xf, 0x10(%rdi,%rax)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x4240
movl $0x9, %edx
movq %rax, %rdi
leaq 0x22e6(%rip), %rsi      # 0x12579
callq 0x4240
movzbl %r13b, %esi
movq 0x8d2d(%rip), %rdi      # 0x18fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rdi,%rax)
callq 0x4150
movl $0x9, %edx
movq %rax, %rdi
leaq 0x22bc(%rip), %rsi      # 0x12583
callq 0x4240
movzbl %bpl, %esi
movq 0x8cf9(%rip), %rdi      # 0x18fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x3, 0x10(%rdi,%rax)
callq 0x4150
movl $0x8, %edx
movq %rax, %rdi
leaq 0x2292(%rip), %rsi      # 0x1258d
callq 0x4240
shll $0x18, %r12d
shll $0x10, %r15d
leal (%r15,%r12), %ebx
shll $0x8, %r14d
orl %r14d, %ebx
addl 0x50(%rsp), %ebx
movq 0x8cb2(%rip), %rdi      # 0x18fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%rdi,%rax)
movl %ebx, %esi
movq %rsi, 0xc0(%rsp)
callq 0x4150
movl $0x7, %edx
movq %rax, %rdi
leaq 0x224a(%rip), %rsi      # 0x12596
callq 0x4240
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movb $0x0, 0x40(%rsp)
testl %ebx, %ebx
movq 0xa8(%rsp), %rbx
jle 0x10434
addl %r15d, %r12d
addl %r14d, %r12d
movq 0x50(%rsp), %rax
leal (%rax,%r12), %r14d
addl $0x1fe, %r14d           # imm = 0x1FE
movzbl %r13b, %r13d
movq (%rbx), %rdx
movl %r13d, %esi
shll $0x5, %esi
movq 0x8(%rdx,%rsi), %rcx
movzbl %bpl, %eax
shll $0x9, %eax
cmpq %rcx, %rax
jg 0x103f9
movq (%rdx,%rsi), %rbp
addq %rbp, %rcx
leaq (%rax,%rbp), %rdx
addq $0x200, %rdx            # imm = 0x200
cmpq %rcx, %rdx
ja 0x103f9
addq %rax, %rbp
movl $0x1fe, %edx            # imm = 0x1FE
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x4050
movb 0x1fe(%rbp), %r13b
movb 0x1ff(%rbp), %bpl
addl $0xfffffe02, %r14d      # imm = 0xFFFFFE02
cmpl $0x1fe, %r14d           # imm = 0x1FE
jg 0x1038f
jmp 0x10434
movl $0x9, %edx
movq 0x8bcb(%rip), %rdi      # 0x18fd0
leaq 0x216d(%rip), %rsi      # 0x12579
callq 0x4240
movq 0x8bb8(%rip), %rdi      # 0x18fd0
movq %r13, %rsi
callq 0x4150
movl $0x2a, %edx
movq %rax, %rdi
leaq 0x216f(%rip), %rsi      # 0x1259e
callq 0x4240
leaq 0x30(%rsp), %rdi
movq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x4090
leaq 0xc8(%rsp), %r13
movq %r13, %rdi
callq 0x4370
leaq 0x68(%rsp), %r15
leaq 0x88(%rsp), %r12
leaq 0x58(%rsp), %rbp
movq %r15, 0x58(%rsp)
movq 0xb0(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x10c6a
movq %rbp, %rdi
leaq 0x2136(%rip), %rsi      # 0x125c9
leaq 0x18(%rsp), %r14
callq 0x43d0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbp, %rdi
callq 0x40a0
movq %r12, 0x78(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x104d5
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
jmp 0x104dd
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
movq %rdx, 0x80(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r13, %rdi
leaq 0x78(%rsp), %rsi
movl $0x14, %edx
callq 0x4110
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x10524
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x1053b
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r13, %rdi
callq 0x4290
movq %r13, %rdi
callq 0x42a0
movq 0xa0(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq -0x18(%rax), %rax
movq 0x98(%rsp), %rcx
movq %rcx, 0xc8(%rsp,%rax)
leaq 0xd0(%rsp), %rdi
callq 0x41a0
leaq 0x1c0(%rsp), %rdi
callq 0x40b0
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x105af
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
movl 0x2c(%rsp), %ebp
je 0x105ca
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x4210
incl %ebp
cmpl 0x28(%rsp), %ebp
movq 0xb8(%rsp), %rcx
movl %ebp, %eax
jne 0x10209
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1066d
jmp 0x1066d
jmp 0x1066d
movq %rax, %rbx
jmp 0x1064a
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
jne 0x1063d
jmp 0x1064a
movq %rax, %rbx
jmp 0x1062e
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x1062e
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x1064a
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4210
leaq 0x18(%rsp), %r14
jmp 0x10654
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x42d0
jmp 0x10675
movq %rax, %rbx
leaq 0x18(%rsp), %r14
jmp 0x10691
movq %rax, %rbx
leaq 0x18(%rsp), %r14
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x10691
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x106a8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x4210
movq %rbx, %rdi
callq 0x43a0