// File: /workspace/github_cpp_makefile_star2_O3/OpenFodder[P]SOS_Unpacker/SOS_Unpacker.cpp
// Function: GetSOSTracks(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
std::vector<std::string> GetSOSTracks(const std::string& pFilename) {
    CapsImageInfo CapsImageInfo;

    int CapsID = CAPSAddImage();

    // lock this image into the container
    auto ret = CAPSLockImage(CapsID, (PCHAR) pFilename.c_str());
    if (ret != imgeOk) {
		std::cout << "Bad Image: " << ret << "\n";
        CAPSRemImage(CapsID);
        return {};
    }

    // get some information about the image
    CAPSGetImageInfo(&CapsImageInfo, CapsID);
    CAPSLoadImage(CapsID, gCapsFlags);

    auto MaxTracks = (CapsImageInfo.maxcylinder - CapsImageInfo.mincylinder + 1) *  (CapsImageInfo.maxhead - CapsImageInfo.minhead + 1);

    std::vector<std::string> TrackBuffers;
    TrackBuffers.resize(MaxTracks);

    // Track 0 is standard Amiga DOS
    // Track 1 is unformatted
    // Track 2 is the first SOS track
    for (UDWORD Track = 2; Track < MaxTracks; ++Track) {
        std::string FullTrack;
        CapsTrackInfoT1 TrackInfo;
        TrackInfo.type = ctitNoise;

        // SOS has the sides inverted
        auto Data = CAPSLockTrack((PCAPSTRACKINFO)&TrackInfo, CapsID, Track / 2, (Track & 1) ? 0 : 1, gCapsFlags);
        
        FullTrack.resize(TrackInfo.tracklen / 8);
        memcpy((UWORD*)FullTrack.data(), TrackInfo.trackbuf, TrackInfo.tracklen / 8);
        tool_EndianSwap((UBYTE*)FullTrack.data(), FullTrack.size());

        UBYTE* Ptr = (UBYTE*)FullTrack.data();
        UBYTE* PtrEnd = (UBYTE*)FullTrack.data() + FullTrack.size();

        // Find the track start marker
        auto SkipBits = sectorFindSync(&Ptr, Ptr + FullTrack.size());
        if (SkipBits == -1) {
            std::cout << "Skipping Track " << Track << " (SYNC not found)\n";
            continue;
        }
        trackbuffer_shift_bits(&Ptr, SkipBits, (int)((PtrEnd - Ptr) / 2) - 8);

        UBYTE* PtrOdd = Ptr + 0x180c;
        if (PtrOdd > PtrEnd) {
            std::cout << "Skipping Track " << Track << "\n";
            continue;
        }
        SDWORD Checksum = sos_ReadUDWord(Ptr, PtrOdd);

        // Check for Sensible Software marker
        if (Checksum != 'SOS6') {
            std::cout << "Skipping Track " << Track << " (Invalid SOS6 Marker)\n";
            continue;
        }
        Checksum = -Checksum;
        Checksum += sos_ReadUDWord(Ptr, PtrOdd);
        Checksum -= sos_ReadUDWord(Ptr, PtrOdd);

        std::vector<UDWORD> TrackDecodedBuffer;
        for (int count = 0x1800 / 4 - 1; count >= 0; --count) {
            UDWORD NewData = sos_ReadUDWord(Ptr, PtrOdd);
            Checksum -= NewData;
            TrackDecodedBuffer.push_back(swapDWord(NewData));
        }

        if (Checksum) {
            std::cout << "Track " << Track << " has invalid checksum.\n";
        }

        // Track CRC Matches
        UBYTE* TrackData = (UBYTE*)TrackDecodedBuffer.data();
        tool_EndianSwap(TrackData, TrackDecodedBuffer.size() * 4);
        TrackBuffers[Track] = std::string((char*)TrackData, TrackDecodedBuffer.size() * 4);
    }

    CAPSUnlockAllTracks(CapsID);
    CAPSUnlockImage(CapsID);
    CAPSRemImage(CapsID);

    return TrackBuffers;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %rbx
movq %rdi, %r12
callq 0x5b0b
movl %eax, %r14d
movq (%rbx), %rsi
movl %eax, %edi
callq 0x5bfb
testl %eax, %eax
je 0xfcec
movl %eax, %ebx
movl %r14d, %r15d
movq 0x9337(%rip), %r14      # 0x18fd0
leaq 0x287a(%rip), %rsi      # 0x1251a
movl $0xb, %edx
movq %r14, %rdi
callq 0x4240
movq %r14, %rdi
movl %ebx, %esi
callq 0x4380
leaq 0x2909(%rip), %rsi      # 0x125c7
movl $0x1, %edx
movq %rax, %rdi
callq 0x4240
movl %r15d, %edi
callq 0x5ba7
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movq %r12, %rax
jmp 0x1013d
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %esi
callq 0x5d84
movl %r14d, %edi
movl $0x1b34, %esi           # imm = 0x1B34
callq 0x5d4a
movl 0x10(%rbx), %eax
movl 0x18(%rbx), %r13d
subl 0xc(%rbx), %eax
incl %eax
subl 0x14(%rbx), %r13d
incl %r13d
imull %eax, %r13d
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movq %r12, %rdi
movq %r13, %rsi
movq %r12, 0x48(%rsp)
callq 0x10abc
cmpl $0x3, %r13d
jb 0x10120
leaq 0x20(%rsp), %rbx
movl $0x2, %r15d
movl $0xd5555555, %r12d      # imm = 0xD5555555
movl %r14d, 0x54(%rsp)
movq %r13, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movl $0x1, 0x80(%rsp)
movl %r15d, %ecx
notl %ecx
movl %r15d, %edx
shrl %edx
andl $0x1, %ecx
leaq 0x80(%rsp), %rdi
movl %r14d, %esi
movl $0x1b34, %r8d           # imm = 0x1B34
callq 0x5e6f
movl 0x9c(%rsp), %esi
shrl $0x3, %esi
leaq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0x4090
movq 0x10(%rsp), %rdi
movq 0x94(%rsp), %rsi
movl 0x9c(%rsp), %edx
shrl $0x3, %edx
callq 0x4190
movq 0x18(%rsp), %r13
cmpq $0x2, %r13
jae 0xfdec
movq %rsp, %rbx
jmp 0xfe0b
movq 0x10(%rsp), %rax
shrq %r13
xorl %ecx, %ecx
movq %rsp, %rbx
rolw $0x8, (%rax,%rcx,2)
incq %rcx
cmpq %rcx, %r13
jne 0xfdf9
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rax
movq %rax, (%rsp)
addq %rax, %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0xfbb4
cmpl $-0x1, %eax
je 0xfea6
movq %r13, %rcx
subq (%rsp), %rcx
movq %rcx, %rdx
shrq $0x3f, %rdx
addq %rcx, %rdx
shrq %rdx
addl $-0x8, %edx
movq %rbx, %rdi
movl %eax, %esi
callq 0xfb41
movq (%rsp), %rax
leaq 0x180c(%rax), %rcx
cmpq %r13, %rcx
jbe 0xfef0
movl $0xf, %edx
movq 0x9168(%rip), %rdi      # 0x18fd0
leaq 0x26b7(%rip), %rsi      # 0x12526
leaq 0x20(%rsp), %rbx
callq 0x4240
movq 0x8(%rsp), %r13
movq 0x914b(%rip), %rdi      # 0x18fd0
movq %r15, %rsi
callq 0x4150
movl $0x1, %edx
movq %rax, %rdi
leaq 0x272b(%rip), %rsi      # 0x125c7
callq 0x4240
jmp 0x100fd
movl $0xf, %edx
movq 0x911e(%rip), %rdi      # 0x18fd0
leaq 0x266d(%rip), %rsi      # 0x12526
leaq 0x20(%rsp), %rbx
callq 0x4240
movq 0x8(%rsp), %r13
movq 0x9101(%rip), %rdi      # 0x18fd0
movq %r15, %rsi
callq 0x4150
movl $0x12, %edx
movq %rax, %rdi
leaq 0x2650(%rip), %rsi      # 0x12536
callq 0x4240
jmp 0x100fd
movl (%rax), %ecx
movl $0x55555555, %esi       # imm = 0x55555555
andl %esi, %ecx
movl 0x180c(%rax), %edx
andl %r12d, %edx
leal (%rcx,%rdx,2), %ecx
cmpl $0x5336534f, %ecx       # imm = 0x5336534F
jne 0x100b8
movq %r15, 0x58(%rsp)
movl 0x4(%rax), %ecx
andl %esi, %ecx
movl 0x1810(%rax), %edx
andl %r12d, %edx
leal (%rcx,%rdx,2), %r13d
roll $0x10, %r13d
leaq 0xc(%rax), %rbp
movl 0x8(%rax), %ecx
movl $0xd5555555, %edx       # imm = 0xD5555555
movl $0x55555555, %r12d      # imm = 0x55555555
andl %esi, %ecx
movl 0x1814(%rax), %eax
movl $0xd5555555, %r15d      # imm = 0xD5555555
andl %edx, %eax
leal (%rcx,%rax,2), %eax
roll $0x10, %eax
subl %eax, %r13d
addl $0xacb0acca, %r13d      # imm = 0xACB0ACCA
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
xorl %r14d, %r14d
movl (%rbp,%r14), %eax
andl %r12d, %eax
movl 0x180c(%rbp,%r14), %ecx
andl %r15d, %ecx
leal (%rax,%rcx,2), %ebx
movl %ebx, 0x60(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0xffa5
movl %ebx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0xffb4
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x10f3e
roll $0x10, %ebx
subl %ebx, %r13d
addq $0x4, %r14
cmpl $0x1800, %r14d          # imm = 0x1800
jne 0xff72
addq %r14, %rbp
movq %rbp, (%rsp)
testl %r13d, %r13d
movq 0x58(%rsp), %r15
je 0x10013
movl $0x6, %edx
movq 0x8fec(%rip), %rdi      # 0x18fd0
leaq 0x2544(%rip), %rsi      # 0x1252f
callq 0x4240
movq 0x8fd9(%rip), %rdi      # 0x18fd0
movq %r15, %rsi
callq 0x4150
movl $0x17, %edx
movq %rax, %rdi
leaq 0x2553(%rip), %rsi      # 0x12561
callq 0x4240
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
subq %rsi, %rdx
cmpq $0x2, %rdx
movl 0x54(%rsp), %r14d
leaq 0x20(%rsp), %rbx
movq 0x8(%rsp), %r13
jb 0x10051
shrq %rdx
xorl %eax, %eax
rolw $0x8, (%rsi,%rax,2)
incq %rax
cmpq %rax, %rdx
jne 0x1003a
movq 0x38(%rsp), %rdx
subq 0x30(%rsp), %rdx
leaq 0x70(%rsp), %r12
movq %r12, 0x60(%rsp)
addq %rsi, %rdx
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
callq 0x10bd4
movq %r15, %rdi
shlq $0x5, %rdi
movq 0x48(%rsp), %rax
addq (%rax), %rdi
movq %rbp, %rsi
callq 0x4270
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x10099
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
movl $0xd5555555, %r12d      # imm = 0xD5555555
je 0x100fd
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x4210
jmp 0x100fd
movl $0xf, %edx
movq 0x8f0c(%rip), %rdi      # 0x18fd0
leaq 0x245b(%rip), %rsi      # 0x12526
leaq 0x20(%rsp), %rbx
callq 0x4240
movq 0x8(%rsp), %r13
movq 0x8eef(%rip), %rdi      # 0x18fd0
movq %r15, %rsi
callq 0x4150
movl $0x17, %edx
movq %rax, %rdi
leaq 0x2451(%rip), %rsi      # 0x12549
callq 0x4240
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x10114
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4210
incq %r15
cmpq %r13, %r15
jne 0xfd69
movl %r14d, %edi
callq 0x6183
movl %r14d, %edi
callq 0x5d15
movl %r14d, %edi
callq 0x5ba7
movq 0x48(%rsp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1015e
movq %rax, %rbx
jmp 0x10199
jmp 0x1015e
jmp 0x1017a
jmp 0x1017a
jmp 0x1017a
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x1017d
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x4210
jmp 0x1017d
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x10199
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4210
movq 0x48(%rsp), %rdi
callq 0x10aec
movq %rbx, %rdi
callq 0x43a0