// File: /workspace/github_cpp_makefile_star2_O1/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 $0x2b8, %rsp            # imm = 0x2B8
movq %rsi, 0x98(%rsp)
movq %rdi, 0xb0(%rsp)
movq (%rdi), %rax
movq 0x40(%rax), %rbx
movl 0x18(%rbx), %eax
bswapl %eax
movl %eax, 0x20(%rsp)
testl %eax, %eax
je 0x105a3
leaq 0x10(%rsp), %r14
movq 0x8dc5(%rip), %rax      # 0x18fc8
movq (%rax), %rcx
movq %rcx, 0x90(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x88(%rsp)
xorl %eax, %eax
movl %eax, 0x24(%rsp)
leaq 0x20(%rbx), %r15
movq %r14, (%rsp)
movq %r15, %rdi
callq 0x40f0
leaq (%rbx,%rax), %rdx
addq $0x20, %rdx
movq %rsp, %rdi
movq %r15, 0xa0(%rsp)
movq %r15, %rsi
callq 0x10b94
movl 0x3c(%rbx), %r15d
movb 0x3a(%rbx), %r12b
movb 0x3b(%rbx), %bl
movq 0x8d73(%rip), %rdi      # 0x18fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0xf, 0x10(%rdi,%rax)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x4240
movl $0x9, %edx
movq %rax, %rdi
leaq 0x21c5(%rip), %rsi      # 0x1244f
callq 0x4240
movzbl %r12b, %esi
movq 0x8d36(%rip), %rdi      # 0x18fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rdi,%rax)
callq 0x4150
movl $0x9, %edx
movq %rax, %rdi
leaq 0x219b(%rip), %rsi      # 0x12459
callq 0x4240
movzbl %bl, %esi
movq 0x8d03(%rip), %rdi      # 0x18fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x3, 0x10(%rdi,%rax)
callq 0x4150
movl $0x8, %edx
movq %rax, %rdi
leaq 0x2172(%rip), %rsi      # 0x12463
callq 0x4240
bswapl %r15d
movq 0x8cd0(%rip), %rdi      # 0x18fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%rdi,%rax)
movl %r15d, %esi
movq %rsi, 0xa8(%rsp)
callq 0x4150
movl $0x7, %edx
movq %rax, %rdi
leaq 0x213d(%rip), %rsi      # 0x1246c
callq 0x4240
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
testl %r15d, %r15d
jle 0x10405
movzbl %r12b, %r13d
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movl %r13d, %ecx
shll $0x5, %ecx
movq (%rax,%rcx), %rbp
movq 0x8(%rax,%rcx), %rax
leaq (%rax,%rbp), %rcx
movzbl %bl, %edx
shll $0x9, %edx
addq %rdx, %rbp
cmpq %rax, %rdx
setle %al
leaq 0x200(%rbp), %rdx
cmpq %rcx, %rdx
setbe %r14b
andb %al, %r14b
je 0x103c1
movl $0x1fe, %edx            # imm = 0x1FE
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x4050
addl $0xfffffe02, %r15d      # imm = 0xFFFFFE02
movb 0x1fe(%rbp), %r12b
movb 0x1ff(%rbp), %bl
jmp 0x103fc
movl $0x9, %edx
movq 0x8c03(%rip), %rdi      # 0x18fd0
leaq 0x207b(%rip), %rsi      # 0x1244f
callq 0x4240
movq 0x8bf0(%rip), %rdi      # 0x18fd0
movq %r13, %rsi
callq 0x4150
movl $0x2a, %edx
movq %rax, %rdi
leaq 0x207d(%rip), %rsi      # 0x12474
callq 0x4240
testb %r14b, %r14b
jne 0x1034c
leaq 0x28(%rsp), %rdi
movq 0xa8(%rsp), %rsi
xorl %edx, %edx
callq 0x4090
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
callq 0x4370
leaq 0x58(%rsp), %r15
leaq 0x78(%rsp), %r12
movq %r15, 0x48(%rsp)
movq 0x98(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x10c24
movq %r13, %rdi
leaq 0x203e(%rip), %rsi      # 0x1249f
leaq 0x10(%rsp), %r14
callq 0x43d0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r13, %rdi
callq 0x40a0
movq %r12, 0x68(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x1049f
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x104a7
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
movq %rdx, 0x70(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %rbx, %rdi
leaq 0x68(%rsp), %rsi
movl $0x14, %edx
callq 0x4110
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x104e8
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x104ff
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %rbx, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x42a0
movq 0x90(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq -0x18(%rax), %rax
movq 0x88(%rsp), %rcx
movq %rcx, 0xb8(%rsp,%rax)
leaq 0xc0(%rsp), %rdi
callq 0x41a0
leaq 0x1b0(%rsp), %rdi
callq 0x40b0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x10573
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4210
movq (%rsp), %rdi
cmpq %r14, %rdi
movl 0x24(%rsp), %ebp
je 0x1058d
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x4210
incl %ebp
cmpl 0x20(%rsp), %ebp
movq 0xa0(%rsp), %rbx
movl %ebp, %eax
jne 0x1021c
addq $0x2b8, %rsp            # imm = 0x2B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x10626
jmp 0x10626
movq %rax, %rbx
jmp 0x10608
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
jne 0x105fb
jmp 0x10608
movq %rax, %rbx
jmp 0x105ec
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x105ec
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x10608
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4210
leaq 0x10(%rsp), %r14
jmp 0x10612
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x42d0
jmp 0x1062e
movq %rax, %rbx
jmp 0x1064a
movq %rax, %rbx
leaq 0x10(%rsp), %r14
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x1064a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4210
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x10660
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x4210
movq %rbx, %rdi
callq 0x43a0