// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
callq 0x4840
movl %eax, %ebp
movq (%rbx), %rsi
movl %eax, %edi
callq 0x4910
testl %eax, %eax
je 0xd814
movl %eax, %ebx
movq 0x97f9(%rip), %rdi      # 0x16fd0
leaq 0x1c32(%rip), %rsi      # 0xf410
callq 0x31c0
movq %rax, %rdi
movl %ebx, %esi
callq 0x3370
leaq 0x1cc9(%rip), %rsi      # 0xf4bd
movq %rax, %rdi
callq 0x31c0
movl %ebp, %edi
callq 0x48c1
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
jmp 0xdb6e
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4a86
movl %ebp, %edi
movl $0x1b34, %esi           # imm = 0x1B34
callq 0x4a4e
movl 0x10(%rbx), %eax
movl 0x18(%rbx), %esi
subl 0xc(%rbx), %eax
subl 0x14(%rbx), %esi
incl %eax
incl %esi
imull %eax, %esi
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movq %r15, %rdi
movq %rsi, 0x58(%rsp)
movq %r15, 0x50(%rsp)
callq 0xe1a4
movl %ebp, %ebx
pushq $0x2
popq %rdx
leaq 0x10(%rsp), %r12
leaq 0x60(%rsp), %rax
movl %ebp, 0xc(%rsp)
cmpq 0x58(%rsp), %rdx
jae 0xdb59
movq %rax, %r13
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
andq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movl $0x1, 0x80(%rsp)
movl %edx, %ecx
notl %ecx
movq %rdx, %r14
shrl %edx
andl $0x1, %ecx
leaq 0x80(%rsp), %rdi
movl %ebx, %esi
movl $0x1b34, %r8d           # imm = 0x1B34
callq 0x4b67
movl 0x9c(%rsp), %esi
shrl $0x3, %esi
movq %r12, %rdi
callq 0x3290
movq 0x10(%rsp), %rdi
movq 0x94(%rsp), %rsi
movl 0x9c(%rsp), %edx
shrl $0x3, %edx
callq 0x3160
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0xd78d
movq 0x10(%rsp), %r12
movq %r12, (%rsp)
addq 0x18(%rsp), %r12
movq %rsp, %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0xd70b
cmpl $-0x1, %eax
je 0xd986
movl %eax, %esi
movq %r12, %rax
subq (%rsp), %rax
cqto
pushq $0x2
popq %rcx
idivq %rcx
leal -0x8(%rax), %edx
movq %rbp, %rdi
callq 0xd69d
movq (%rsp), %rax
leaq 0x180c(%rax), %rcx
cmpq %r12, %rcx
jbe 0xd9bd
movq 0x967a(%rip), %rdi      # 0x16fd0
leaq 0x1abf(%rip), %rsi      # 0xf41c
callq 0x31c0
leaq 0x10(%rsp), %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x3120
movq %rax, %rdi
leaq 0x1b41(%rip), %rsi      # 0xf4bd
callq 0x31c0
jmp 0xdb43
movq 0x9643(%rip), %rdi      # 0x16fd0
leaq 0x1a88(%rip), %rsi      # 0xf41c
callq 0x31c0
leaq 0x10(%rsp), %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x3120
movq %rax, %rdi
leaq 0x1a79(%rip), %rsi      # 0xf42c
callq 0x31c0
jmp 0xdb43
movl (%rax), %ecx
movl $0x55555555, %esi       # imm = 0x55555555
andl %esi, %ecx
movl 0x180c(%rax), %edx
movl $0xd5555555, %edi       # imm = 0xD5555555
andl %edi, %edx
leal (%rcx,%rdx,2), %ecx
cmpl $0x5336534f, %ecx       # imm = 0x5336534F
jne 0xda6a
movl 0x4(%rax), %ecx
andl %esi, %ecx
movl 0x1810(%rax), %edx
andl %edi, %edx
leal (%rcx,%rdx,2), %r12d
roll $0x10, %r12d
movl 0x8(%rax), %ecx
andl %esi, %ecx
movl 0x1814(%rax), %edx
andl %edi, %edx
leal (%rcx,%rdx,2), %ecx
roll $0x10, %ecx
leaq 0xc(%rax), %rbx
subl %ecx, %r12d
addl $0xacb0acca, %r12d      # imm = 0xACB0ACCA
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movl $0x5ff, %r15d           # imm = 0x5FF
testl %r15d, %r15d
js 0xdaa1
movl (%rbx), %eax
andl %esi, %eax
movl 0x180c(%rbx), %ecx
andl %edi, %ecx
leal (%rax,%rcx,2), %ebp
movl %ebp, 0x60(%rsp)
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0xe5e0
roll $0x10, %ebp
addq $0x4, %rbx
subl %ebp, %r12d
decl %r15d
movl $0x55555555, %esi       # imm = 0x55555555
movl $0xd5555555, %edi       # imm = 0xD5555555
jmp 0xda2c
movq 0x955f(%rip), %rdi      # 0x16fd0
leaq 0x19a4(%rip), %rsi      # 0xf41c
callq 0x31c0
leaq 0x10(%rsp), %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x3120
movq %rax, %rdi
leaq 0x19a8(%rip), %rsi      # 0xf43f
callq 0x31c0
jmp 0xdb43
movq %rbx, (%rsp)
testl %r12d, %r12d
je 0xdad7
movq 0x951f(%rip), %rdi      # 0x16fd0
leaq 0x196d(%rip), %rsi      # 0xf425
callq 0x31c0
movq %rax, %rdi
movq %r14, %rsi
callq 0x3120
movq %rax, %rdi
leaq 0x1985(%rip), %rsi      # 0xf457
callq 0x31c0
movq 0x30(%rsp), %r12
movq 0x38(%rsp), %rsi
subq %r12, %rsi
movq %r12, %rdi
callq 0xd78d
movq 0x38(%rsp), %rdx
subq 0x30(%rsp), %rdx
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
addq %r12, %rdx
movq %r13, %rbp
movq %r13, %rdi
movq %r12, %rsi
movq 0x50(%rsp), %r15
callq 0xe2a6
movl 0xc(%rsp), %ebx
movq %r14, %rdi
shlq $0x5, %rdi
addq (%r15), %rdi
movq %rbp, %rsi
callq 0x3240
movq %rbp, %rdi
callq 0x33f8
leaq 0x30(%rsp), %rdi
callq 0xe5ac
leaq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x33f8
movq %r14, %rdx
incq %rdx
movq %r13, %rax
jmp 0xd876
movl %ebx, %edi
callq 0x4e66
movl %ebx, %edi
callq 0x4a1b
movl %ebx, %edi
callq 0x48c1
movq %r15, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xdbae
jmp 0xdb97
jmp 0xdb97
jmp 0xdb92
jmp 0xdb92
jmp 0xdb92
movq %rax, %rbx
jmp 0xdba4
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xe5ac
leaq 0x10(%rsp), %rdi
callq 0x33f8
movq 0x50(%rsp), %rdi
callq 0xe1d4
movq %rbx, %rdi
callq 0x3380