// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/libsquish/squish.cpp
// Function: squish::ComputeMSE(unsigned char const*, int, int, int, unsigned char const*, int, double&, double&)
void ComputeMSE( u8 const *rgba, int width, int height, int pitch, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE )
{
    // fix any bad flags
    flags = FixFlags( flags );
    colourMSE = alphaMSE = 0;

    // initialise the block input
    squish::u8 const* sourceBlock = dxt;
    int bytesPerBlock = ( ( flags & squish::kDxt1 ) != 0 ) ? 8 : 16;

    // loop over blocks
    for( int y = 0; y < height; y += 4 )
    {
        for( int x = 0; x < width; x += 4 )
        {
            // decompress the block
            u8 targetRgba[4*16];
            Decompress( targetRgba, sourceBlock, flags );
            //JonoF: u8 const* sourcePixel = targetRgba;

            // copy across to a similar pixel block
            u8 originalRgba[4*16];
            u8* originalPixel = originalRgba;

            for( int py = 0; py < 4; ++py )
            {
                for( int px = 0; px < 4; ++px )
                {
                    int sx = x + px;
                    int sy = y + py;
                    if( sx < width && sy < height )
                    {
                        u8 const* targetPixel = rgba + pitch*sy + 4*sx;
                        CopyRGBA(targetPixel, originalPixel, flags);
                    }
                    //JonoF: sourcePixel += 4;
                    originalPixel += 4;
                }
            }

            // compute the weighted MSE of the block
            double blockCMSE, blockAMSE;
            ComputeBlockWMSE(originalRgba, targetRgba, std::min(4, width - x), std::min(4, height - y), blockCMSE, blockAMSE);
            colourMSE += blockCMSE;
            alphaMSE += blockAMSE;
            // advance
            sourceBlock += bytesPerBlock;
        }
    }
    colourMSE /= (width * height * 3);
    alphaMSE /= (width * height);
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rax
movq %rdi, 0x120(%rsp)
movl %esi, 0x11c(%rsp)
movl %edx, 0x118(%rsp)
movl %ecx, 0x114(%rsp)
movq %r8, 0x108(%rsp)
movl %r9d, 0x104(%rsp)
movl 0x104(%rsp), %edi
callq 0xa3600
movl %eax, 0x104(%rsp)
movq 0x138(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x130(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x108(%rsp), %rax
movq %rax, 0xf8(%rsp)
movl 0x104(%rsp), %edx
andl $0x1, %edx
movl $0x10, %eax
movl $0x8, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0xf4(%rsp)
movl $0x0, 0xf0(%rsp)
movl 0xf0(%rsp), %eax
cmpl 0x118(%rsp), %eax
jge 0xa402f
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
cmpl 0x11c(%rsp), %eax
jge 0xa4017
leaq 0xa0(%rsp), %rdi
movq 0xf8(%rsp), %rsi
movl 0x104(%rsp), %edx
callq 0xa3680
leaq 0x60(%rsp), %rax
movq %rax, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
cmpl $0x4, 0x54(%rsp)
jge 0xa3f2a
movl $0x0, 0x50(%rsp)
cmpl $0x4, 0x50(%rsp)
jge 0xa3f18
movl 0xec(%rsp), %eax
addl 0x50(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl 0xf0(%rsp), %eax
addl 0x54(%rsp), %eax
movl %eax, 0x48(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x11c(%rsp), %eax
jge 0xa3efa
movl 0x48(%rsp), %eax
cmpl 0x118(%rsp), %eax
jge 0xa3efa
movq 0x120(%rsp), %rax
movl 0x114(%rsp), %ecx
imull 0x48(%rsp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movl 0x4c(%rsp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl 0x104(%rsp), %edx
callq 0xa37b0
movq 0x58(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x58(%rsp)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0xa3e75
jmp 0xa3f1a
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0xa3e62
leaq 0x60(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x4, 0x2c(%rsp)
movl 0x11c(%rsp), %eax
subl 0xec(%rsp), %eax
movl %eax, 0x28(%rsp)
leaq 0x2c(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xa4410
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
movl $0x4, 0x24(%rsp)
movl 0x118(%rsp), %eax
subl 0xf0(%rsp), %eax
movl %eax, 0x20(%rsp)
leaq 0x24(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xa4410
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl 0x1c(%rsp), %edx
movl (%rax), %ecx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
callq 0xa4090
movsd 0x38(%rsp), %xmm0
movq 0x130(%rsp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movsd 0x30(%rsp), %xmm0
movq 0x138(%rsp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl 0xf4(%rsp), %ecx
movq 0xf8(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0xf8(%rsp)
movl 0xec(%rsp), %eax
addl $0x4, %eax
movl %eax, 0xec(%rsp)
jmp 0xa3e20
jmp 0xa4019
movl 0xf0(%rsp), %eax
addl $0x4, %eax
movl %eax, 0xf0(%rsp)
jmp 0xa3e01
movl 0x11c(%rsp), %eax
imull 0x118(%rsp), %eax
imull $0x3, %eax, %eax
cvtsi2sd %eax, %xmm1
movq 0x130(%rsp), %rax
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl 0x11c(%rsp), %eax
imull 0x118(%rsp), %eax
cvtsi2sd %eax, %xmm1
movq 0x138(%rsp), %rax
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
addq $0x128, %rsp            # imm = 0x128
retq
nopl (%rax,%rax)