// File: /workspace/github_c_makefile_star2_O3/minkcv[P]vm/machine/gpu.c
// Function: drawSprites
void drawSprites(GPU* gpu, uint8_t memory[MEMORY_SEGMENT_COUNT][MEMORY_SEGMENT_SIZE])
{
    int i;
    uint8_t* pixels = gpu->pixels;
    uint8_t bgColorIndex = memory[BACK_COLOR_SEG][BACK_COLOR_OFFSET];
    uint8_t bgRed = getRed(bgColorIndex);
    uint8_t bgGreen = getGreen(bgColorIndex);
    uint8_t bgBlue = getBlue(bgColorIndex);
    for (i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT * gpu->bytesPerPixel; i+=gpu->bytesPerPixel)
    {
        *(pixels + i) = bgRed;
        *(pixels + i + 1) = bgGreen;
        *(pixels + i + 2) = bgBlue;
    }
    for (i = 0; i < NUM_SPRITES; i++)
    {
        if (gpu->sprAttrs[i].active && 
                gpu->sprAttrs[i].x + gpu->sprAttrs[i].width <= SCREEN_WIDTH &&
                gpu->sprAttrs[i].y + gpu->sprAttrs[i].height <= SCREEN_HEIGHT)
        {
            int w, h;
            int x = gpu->sprAttrs[i].x;
            int y = gpu->sprAttrs[i].y;
            int flipHor = gpu->sprAttrs[i].flipHor;
            int flipVer = gpu->sprAttrs[i].flipVer;
            int fullWidth = gpu->sprAttrs[i].fullWidth;
            int fullHeight = gpu->sprAttrs[i].fullHeight;
            int width = gpu->sprAttrs[i].width;
            int height = gpu->sprAttrs[i].height;
            if (fullWidth)
                width = SCREEN_WIDTH;
            if (fullHeight)
                height = SCREEN_HEIGHT;
            uint32_t end = gpu->sprAttrs[i].segmentAddr * MEMORY_SEGMENT_SIZE + gpu->sprAttrs[i].byteAddr;
            end += height * (width / 4);
            if (end > MEMORY_SEGMENT_COUNT * MEMORY_SEGMENT_SIZE) {
                // sprite data extends past the end of memory, skip it
                continue;
            }
            uint8_t* sprite = &memory[gpu->sprAttrs[i].segmentAddr][gpu->sprAttrs[i].byteAddr];
            for (h = 0; h < height; h++)
            {
                for (w = 0; w < width / 4; w++)
                {
                    uint8_t fourPixels = 0;
                    if (flipHor == 0 && flipVer == 0)
                        fourPixels = *(sprite + w + (h * width / 4));
                    else if (flipHor == 1 && flipVer == 0)
                    {
                        uint8_t fourReversedPixels = *(sprite + (width / 4) - w - 1 + (h * width / 4));
                        fourPixels |= (fourReversedPixels & 0xC0) >> 6;
                        fourPixels |= (fourReversedPixels & 0x30) >> 2;
                        fourPixels |= (fourReversedPixels & 0x0C) << 2;
                        fourPixels |= (fourReversedPixels & 0x3) << 6;
                    }
                    else if (flipHor == 0 && flipVer == 1)
                    {
                        fourPixels = *(sprite + w + (height * width / 4) - ((h + 1) * width / 4));
                    }
                    else if (flipHor == 1 && flipVer == 1)
                    {
                        uint8_t fourReversedPixels = *(sprite + (width / 4) - w - 1 + (height * width / 4) - ((h + 1) * width / 4));
                        fourPixels |= (fourReversedPixels & 0xC0) >> 6;
                        fourPixels |= (fourReversedPixels & 0x30) >> 2;
                        fourPixels |= (fourReversedPixels & 0x0C) << 2;
                        fourPixels |= (fourReversedPixels & 0x3) << 6;
                    }
                    // Sprite color palette indicies
                    uint8_t bits1 = (fourPixels >> 6) & 0x3; // MSB
                    uint8_t bits2 = (fourPixels >> 4) & 0x3;
                    uint8_t bits3 = (fourPixels >> 2) & 0x3;
                    uint8_t bits4 = fourPixels & 0x3;
                    // These are full color palette indicies
                    uint8_t pixel1 = gpu->sprAttrs[i].colors[bits1];
                    uint8_t pixel2 = gpu->sprAttrs[i].colors[bits2];
                    uint8_t pixel3 = gpu->sprAttrs[i].colors[bits3];
                    uint8_t pixel4 = gpu->sprAttrs[i].colors[bits4];
                    uint8_t* curPixel = pixels + (x * gpu->bytesPerPixel) + (y * gpu->pitch) + (w * gpu->bytesPerPixel * 4) + (h * gpu->pitch);
                    if (!(gpu->sprAttrs[i].color4Alpha && bits1 == 0x3))
                    {
                        *curPixel = getRed(pixel1);
                        *(curPixel + 1) = getGreen(pixel1);
                        *(curPixel + 2) = getBlue(pixel1);
                    }
                    if (!(gpu->sprAttrs[i].color4Alpha && bits2 == 0x3))
                    {
                        *(curPixel + 3) = getRed(pixel2);
                        *(curPixel + 4) = getGreen(pixel2);
                        *(curPixel + 5) = getBlue(pixel2);
                    }
                    if (!(gpu->sprAttrs[i].color4Alpha && bits3 == 0x3))
                    {
                        *(curPixel + 6) = getRed(pixel3);
                        *(curPixel + 7) = getGreen(pixel3);
                        *(curPixel + 8) = getBlue(pixel3);
                    }
                    if (!(gpu->sprAttrs[i].color4Alpha && bits4 == 0x3))
                    {
                        *(curPixel + 9) = getRed(pixel4);
                        *(curPixel + 10) = getGreen(pixel4);
                        *(curPixel + 11) = getBlue(pixel4);
                    }
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq 0xc00(%rdi), %r10
cmpl $0x0, 0xc0c(%rdi)
jle 0x1e6f
movb 0x7f10(%rsi), %dl
movl %edx, %eax
shrb $0x5, %al
movzbl %al, %eax
shll $0x2, %eax
leal (%rax,%rax,8), %eax
movl %edx, %ecx
shrb $0x2, %cl
andb $0x7, %cl
movzbl %cl, %ecx
shll $0x2, %ecx
leal (%rcx,%rcx,8), %ecx
andb $0x3, %dl
movzbl %dl, %edx
imull $0x55, %edx, %edx
xorl %r8d, %r8d
movslq %r8d, %r8
movb %al, (%r10,%r8)
movb %cl, 0x1(%r10,%r8)
movb %dl, 0x2(%r10,%r8)
movl 0xc0c(%rdi), %r9d
addl %r9d, %r8d
shll $0xe, %r9d
leal (%r9,%r9,2), %r9d
cmpl %r9d, %r8d
jl 0x1e47
movq %rsi, -0x68(%rbp)
leaq -0x1(%rsi), %rax
movq %rax, -0x60(%rbp)
xorl %eax, %eax
movq %rdi, %rdx
movq %r10, %rsi
movq %rax, -0x98(%rbp)
leaq (%rax,%rax,2), %rax
movb (%rdx,%rax,4), %cl
testb $0x1, %cl
je 0x22b9
movl %ecx, %edi
leaq (%rdx,%rax,4), %r12
movzbl 0x1(%r12), %eax
movzbl 0x3(%r12), %ecx
movq %rax, -0xb0(%rbp)
addl %ecx, %eax
cmpl $0x100, %eax            # imm = 0x100
ja 0x22b9
movq %rcx, %r8
movzbl 0x2(%r12), %eax
movzbl 0x4(%r12), %ecx
movq %rax, -0xa8(%rbp)
addl %ecx, %eax
cmpl $0xc0, %eax
ja 0x22b9
movq %rcx, %r9
testb $0x10, %dil
movl $0x100, %eax            # imm = 0x100
cmovnel %eax, %r8d
testb $0x20, %dil
movl $0xc0, %eax
cmovnel %eax, %r9d
movzbl 0x5(%r12), %eax
movq %rax, %r10
shll $0x8, %eax
movzbl 0x6(%r12), %r11d
movq %r11, -0x38(%rbp)
orl %r11d, %eax
movq %r8, -0x48(%rbp)
shrl $0x2, %r8d
movq %r9, -0x40(%rbp)
movl %r9d, %ecx
imull %r8d, %ecx
addl %eax, %ecx
cmpl $0x10000, %ecx          # imm = 0x10000
ja 0x22b9
cmpl $0x0, -0x40(%rbp)
je 0x22b9
movq %r8, %r9
movl %edi, %eax
shrb %al
movl %edi, %ecx
shrb $0x2, %cl
testb $0x1, %al
sete %r8b
andb $0x6, %dil
movb %dil, -0x29(%rbp)
testb $0x1, %cl
sete %dil
andb %al, %dil
movb %dil, -0x2b(%rbp)
andb %cl, %r8b
movb %r8b, -0x2a(%rbp)
movl %r9d, %edi
movq -0x40(%rbp), %rax
movl %eax, %r8d
movq -0x48(%rbp), %rax
imull %eax, %r8d
shrl $0x2, %r8d
shll $0x8, %r10d
addq %r10, %r8
movq -0x60(%rbp), %rcx
leaq (%rcx,%rdi), %r11
addq %r8, %r11
movq %r11, -0x70(%rbp)
movl %eax, %r11d
movq -0x68(%rbp), %rax
addq %rax, %r8
movq %r8, -0x78(%rbp)
orq %r10, %rdi
addq %rcx, %rdi
movq %rdi, -0x80(%rbp)
addq %rax, %r10
movq %r10, -0x88(%rbp)
shll $0x2, %r9d
movq %r9, -0xa0(%rbp)
movq $0x0, -0x58(%rbp)
movq %r11, -0x90(%rbp)
movq %r11, -0x50(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x4, -0x48(%rbp)
jb 0x2299
movq -0x50(%rbp), %rax
shrq $0x2, %rax
movq -0x70(%rbp), %rcx
subq %rax, %rcx
movq -0x78(%rbp), %rdi
subq %rax, %rdi
movq -0x58(%rbp), %r8
shrq $0x2, %r8
movq -0x80(%rbp), %rax
leaq (%rax,%r8), %r9
addq -0x88(%rbp), %r8
xorl %r13d, %r13d
cmpb $0x0, -0x29(%rbp)
movq %rcx, -0xd8(%rbp)
movq %rdi, -0xd0(%rbp)
movq %r8, -0xc8(%rbp)
movq %r9, -0xc0(%rbp)
je 0x2078
cmpb $0x0, -0x2b(%rbp)
je 0x2082
movq -0x38(%rbp), %rax
movb (%r9,%rax), %al
movl %eax, %ecx
shrb $0x6, %cl
movl %eax, %edi
shrb $0x2, %dil
andb $0xc, %dil
leal (,%rax,4), %r8d
andb $0x30, %r8b
shlb $0x6, %al
orb %cl, %al
orb %dil, %al
orb %r8b, %al
jmp 0x20a2
movq -0x38(%rbp), %rax
movb (%r8,%rax), %al
jmp 0x20a2
cmpb $0x0, -0x2a(%rbp)
je 0x2091
movq -0x38(%rbp), %rax
movb (%rdi,%rax), %al
jmp 0x20a2
cmpb $0x6, -0x29(%rbp)
jne 0x20a0
movq -0x38(%rbp), %rax
movb (%rcx,%rax), %al
jmp 0x2050
xorl %eax, %eax
movzbl %al, %edi
movl %edi, %eax
shrl $0x6, %eax
movl %edi, %ecx
shrb $0x4, %cl
movzbl %cl, %r10d
andl $0x3, %r10d
movl %edi, %r9d
shrb $0x2, %r9b
movslq 0xc0c(%rdx), %rcx
movl %r13d, %ebx
imull %ecx, %ebx
imulq -0xb0(%rbp), %rcx
addq %rsi, %rcx
movslq 0xc08(%rdx), %r11
movslq -0x30(%rbp), %r8
imulq %r11, %r8
movq %r11, %r14
imulq -0xa8(%rbp), %r14
addq %rcx, %r14
movzbl %r9b, %r9d
andl $0x3, %r9d
andl $0x3, %edi
movb 0x7(%r12,%r10), %r11b
movslq %ebx, %r15
addq %r14, %r15
movb 0x7(%r12,%r9), %r14b
movb 0x7(%r12,%rdi), %bl
movq %rax, -0xb8(%rbp)
cmpl $0x3, %eax
jne 0x2136
movq %rsi, %rax
movq %rdx, %rsi
movb (%r12), %dl
movb $0x1, %cl
andb $0x8, %dl
movq %rsi, %rdx
movq %rax, %rsi
jne 0x2181
movq -0xb8(%rbp), %rax
movb 0x7(%r12,%rax), %al
movl %eax, %ecx
shrb $0x5, %cl
movzbl %cl, %ecx
shll $0x2, %ecx
leal (%rcx,%rcx,8), %ecx
movb %cl, (%r15,%r8)
movl %eax, %ecx
shrb $0x2, %cl
andb $0x7, %cl
movzbl %cl, %ecx
shll $0x2, %ecx
leal (%rcx,%rcx,8), %ecx
movb %cl, 0x1(%r15,%r8)
andb $0x3, %al
movzbl %al, %eax
imull $0x55, %eax, %eax
movb %al, 0x2(%r15,%r8)
movb (%r12), %cl
andb $0x8, %cl
shrb $0x3, %cl
cmpb $0x3, %r10b
jne 0x218d
movb $0x1, %al
testb %cl, %cl
jne 0x21d0
movl %r11d, %eax
shrb $0x5, %al
movzbl %al, %eax
shll $0x2, %eax
leal (%rax,%rax,8), %eax
movb %al, 0x3(%r15,%r8)
movl %r11d, %eax
shrb $0x2, %al
andb $0x7, %al
movzbl %al, %eax
shll $0x2, %eax
leal (%rax,%rax,8), %eax
movb %al, 0x4(%r15,%r8)
andb $0x3, %r11b
movzbl %r11b, %eax
imull $0x55, %eax, %eax
movb %al, 0x5(%r15,%r8)
movb (%r12), %al
andb $0x8, %al
shrb $0x3, %al
cmpb $0x3, %r9b
jne 0x21dc
movb $0x1, %cl
testb %al, %al
jne 0x2220
movl %r14d, %eax
shrb $0x5, %al
movzbl %al, %eax
shll $0x2, %eax
leal (%rax,%rax,8), %eax
movb %al, 0x6(%r15,%r8)
movl %r14d, %eax
shrb $0x2, %al
andb $0x7, %al
movzbl %al, %eax
shll $0x2, %eax
leal (%rax,%rax,8), %eax
movb %al, 0x7(%r15,%r8)
andb $0x3, %r14b
movzbl %r14b, %eax
imull $0x55, %eax, %eax
movb %al, 0x8(%r15,%r8)
movb (%r12), %cl
andb $0x8, %cl
shrb $0x3, %cl
cmpb $0x3, %dil
movq -0xc0(%rbp), %r9
jne 0x2231
testb %cl, %cl
jne 0x2267
movl %ebx, %eax
shrb $0x5, %al
movzbl %al, %eax
shll $0x2, %eax
leal (%rax,%rax,8), %eax
movb %al, 0x9(%r15,%r8)
movl %ebx, %eax
shrb $0x2, %al
andb $0x7, %al
movzbl %al, %eax
shll $0x2, %eax
leal (%rax,%rax,8), %eax
movb %al, 0xa(%r15,%r8)
andb $0x3, %bl
movzbl %bl, %eax
imull $0x55, %eax, %eax
movb %al, 0xb(%r15,%r8)
addq $0x4, %r13
movq -0xd8(%rbp), %rcx
decq %rcx
movq -0xd0(%rbp), %rdi
incq %rdi
decq %r9
movq -0xc8(%rbp), %r8
incq %r8
cmpq %r13, -0xa0(%rbp)
jne 0x2020
movl -0x30(%rbp), %ecx
incl %ecx
movq -0x90(%rbp), %rax
addq %rax, -0x50(%rbp)
addq %rax, -0x58(%rbp)
movl %ecx, -0x30(%rbp)
cmpl -0x40(%rbp), %ecx
jne 0x1fe6
movq -0x98(%rbp), %rax
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x1e83
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq