// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq 0xc00(%rdi), %rax
movq %rax, -0x60(%rbp)
movq %rsi, -0x50(%rbp)
movb 0x7f10(%rsi), %dl
movl %edx, %eax
shrb $0x5, %al
movzbl %al, %eax
imull $0x24, %eax, %eax
movl %edx, %ecx
shrb $0x2, %cl
andb $0x7, %cl
movzbl %cl, %ecx
imull $0x24, %ecx, %ecx
andb $0x3, %dl
movzbl %dl, %edx
imull $0x55, %edx, %edx
movq %rdi, -0x40(%rbp)
movl 0xc0c(%rdi), %edi
xorl %esi, %esi
movq -0x60(%rbp), %r8
imull $0xc000, %edi, %edi     # imm = 0xC000
cmpl %edi, %esi
jge 0x1e75
movslq %esi, %rdi
movb %al, (%r8,%rdi)
movb %cl, 0x1(%r8,%rdi)
movb %dl, 0x2(%r8,%rdi)
movq -0x40(%rbp), %rdi
movl 0xc0c(%rdi), %edi
addl %edi, %esi
jmp 0x1e4c
movq -0x50(%rbp), %rax
decq %rax
movq %rax, -0x68(%rbp)
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x22b5
movq %rax, -0x98(%rbp)
imulq $0xc, %rax, %r10
movq -0x40(%rbp), %rax
movb (%rax,%r10), %al
testb $0x1, %al
je 0x1f2f
movl %eax, %r8d
addq -0x40(%rbp), %r10
movzbl 0x1(%r10), %ecx
movzbl 0x3(%r10), %eax
movq %rcx, -0xb8(%rbp)
addl %eax, %ecx
cmpl $0x100, %ecx            # imm = 0x100
ja 0x1f2f
movzbl 0x2(%r10), %ecx
movzbl 0x4(%r10), %edx
movq %rcx, -0xb0(%rbp)
addl %edx, %ecx
cmpl $0xc0, %ecx
ja 0x1f2f
movq %rdx, %rsi
testb $0x10, %r8b
movl $0x100, %ecx            # imm = 0x100
cmovnel %ecx, %eax
testb $0x20, %r8b
movl $0xc0, %ecx
cmovnel %ecx, %esi
movzbl 0x5(%r10), %ecx
movq %rcx, %r9
movl %ecx, %edx
shll $0x8, %edx
movzbl 0x6(%r10), %edi
movq %rdi, -0x38(%rbp)
orl %edi, %edx
movl %eax, %ecx
shrl $0x2, %ecx
movq %rsi, -0x58(%rbp)
imull %ecx, %esi
addl %edx, %esi
cmpl $0x10000, %esi          # imm = 0x10000
jbe 0x1f3e
movq -0x98(%rbp), %rax
incq %rax
jmp 0x1e82
movl %r8d, %edx
shrb $0x2, %dl
movl %r8d, %esi
shrb %sil
movzbl %r8b, %edi
btl $0x1, %edi
setae %r11b
andb $0x6, %r8b
movb %r8b, -0x29(%rbp)
btl $0x2, %edi
setae %dil
andb %sil, %dil
movb %dil, -0x2b(%rbp)
andb %dl, %r11b
movb %r11b, -0x2a(%rbp)
movq -0x58(%rbp), %rdx
movl %edx, %esi
imull %eax, %esi
shrl $0x2, %esi
leal (,%rcx,4), %edx
movq %rdx, -0xc0(%rbp)
shll $0x8, %r9d
movq -0x50(%rbp), %rdx
addq %r9, %rdx
movl %eax, %eax
orl %ecx, %r9d
addq -0x68(%rbp), %r9
movq %rdx, -0x80(%rbp)
leaq (%rdx,%rsi), %rcx
movq %rcx, -0x70(%rbp)
movq %r9, -0x90(%rbp)
addq %r9, %rsi
movq %rsi, -0x88(%rbp)
xorl %r12d, %r12d
movq %rax, -0x78(%rbp)
movq %rax, %rcx
xorl %r15d, %r15d
cmpl -0x58(%rbp), %r15d
je 0x1f2f
movq %r12, -0xa8(%rbp)
shrq $0x2, %r12
movq -0x80(%rbp), %rax
leaq (%rax,%r12), %r9
addq -0x90(%rbp), %r12
movq %rcx, -0xa0(%rbp)
movq %rcx, %rax
shrq $0x2, %rax
movq -0x70(%rbp), %rdx
subq %rax, %rdx
movq -0x88(%rbp), %rcx
subq %rax, %rcx
leal 0x1(%r15), %eax
movl %eax, -0x44(%rbp)
xorl %r11d, %r11d
cmpq %r11, -0xc0(%rbp)
je 0x2292
cmpb $0x0, -0x29(%rbp)
movq %rdx, -0xe0(%rbp)
movq %rcx, -0xd8(%rbp)
je 0x206c
cmpb $0x0, -0x2b(%rbp)
je 0x2076
movq -0x38(%rbp), %rax
movb (%r12,%rax), %al
movl %eax, %ecx
shrb $0x6, %cl
movl %eax, %edx
shrb $0x2, %dl
andb $0xc, %dl
leal (,%rax,4), %esi
andb $0x30, %sil
shlb $0x6, %al
orb %cl, %al
orb %dl, %al
orb %sil, %al
jmp 0x2096
movq -0x38(%rbp), %rax
movb (%r9,%rax), %al
jmp 0x2096
cmpb $0x0, -0x2a(%rbp)
je 0x2085
movq -0x38(%rbp), %rax
movb (%rdx,%rax), %al
jmp 0x2096
cmpb $0x6, -0x29(%rbp)
jne 0x2094
movq -0x38(%rbp), %rax
movb (%rcx,%rax), %al
jmp 0x2048
xorl %eax, %eax
movzbl %al, %r8d
movl %r8d, %eax
shrl $0x6, %eax
movl %r8d, %ecx
shrb $0x4, %cl
movzbl %cl, %ecx
andl $0x3, %ecx
movl %r8d, %edx
shrb $0x2, %dl
movq -0x40(%rbp), %rdi
movslq 0xc0c(%rdi), %rsi
movq %r11, -0xd0(%rbp)
imull %esi, %r11d
imulq -0xb8(%rbp), %rsi
addq -0x60(%rbp), %rsi
movslq 0xc08(%rdi), %rdi
movslq %r15d, %r13
imulq %rdi, %r13
movq %rdi, %r14
imulq -0xb0(%rbp), %r14
addq %rsi, %r14
movzbl %dl, %edx
andl $0x3, %edx
andl $0x3, %r8d
movb 0x7(%r10,%rcx), %dil
movslq %r11d, %rbx
addq %r14, %rbx
movb 0x7(%r10,%rdx), %r11b
movb 0x7(%r10,%r8), %r14b
cmpl $0x3, %eax
jne 0x2158
movq %rcx, -0xc8(%rbp)
movq %r8, %rcx
movl %edi, %r8d
movl %r11d, %edi
movl %r14d, %r11d
movq %r9, %r14
movq %r12, %r9
movq %r15, %r12
movb (%r10), %r15b
movb $0x1, %sil
andb $0x8, %r15b
movq %r12, %r15
movq %r9, %r12
movq %r14, %r9
movl %r11d, %r14d
movl %edi, %r11d
movl %r8d, %edi
movq %rcx, %r8
movq -0xc8(%rbp), %rcx
jne 0x219c
movb 0x7(%r10,%rax), %al
movl %eax, %esi
shrb $0x5, %sil
movzbl %sil, %esi
imull $0x24, %esi, %esi
movb %sil, (%rbx,%r13)
movl %eax, %esi
shrb $0x2, %sil
andb $0x7, %sil
movzbl %sil, %esi
imull $0x24, %esi, %esi
movb %sil, 0x1(%rbx,%r13)
andb $0x3, %al
movzbl %al, %eax
imull $0x55, %eax, %eax
movb %al, 0x2(%rbx,%r13)
movb (%r10), %sil
andb $0x8, %sil
shrb $0x3, %sil
cmpb $0x3, %cl
jne 0x21a8
movb $0x1, %al
testb %sil, %sil
jne 0x21e2
movl %edi, %eax
shrb $0x5, %al
movzbl %al, %eax
imull $0x24, %eax, %eax
movb %al, 0x3(%rbx,%r13)
movl %edi, %eax
shrb $0x2, %al
andb $0x7, %al
movzbl %al, %eax
imull $0x24, %eax, %eax
movb %al, 0x4(%rbx,%r13)
andb $0x3, %dil
movzbl %dil, %eax
imull $0x55, %eax, %eax
movb %al, 0x5(%rbx,%r13)
movb (%r10), %al
andb $0x8, %al
shrb $0x3, %al
cmpb $0x3, %dl
jne 0x21ed
movb $0x1, %cl
testb %al, %al
jne 0x222a
movl %r11d, %eax
shrb $0x5, %al
movzbl %al, %eax
imull $0x24, %eax, %eax
movb %al, 0x6(%rbx,%r13)
movl %r11d, %eax
shrb $0x2, %al
andb $0x7, %al
movzbl %al, %eax
imull $0x24, %eax, %eax
movb %al, 0x7(%rbx,%r13)
andb $0x3, %r11b
movzbl %r11b, %eax
imull $0x55, %eax, %eax
movb %al, 0x8(%rbx,%r13)
movb (%r10), %cl
andb $0x8, %cl
shrb $0x3, %cl
cmpb $0x3, %r8b
movq -0xe0(%rbp), %rdx
jne 0x223b
testb %cl, %cl
jne 0x226f
movl %r14d, %eax
shrb $0x5, %al
movzbl %al, %eax
imull $0x24, %eax, %eax
movb %al, 0x9(%rbx,%r13)
movl %r14d, %eax
shrb $0x2, %al
andb $0x7, %al
movzbl %al, %eax
imull $0x24, %eax, %eax
movb %al, 0xa(%rbx,%r13)
andb $0x3, %r14b
movzbl %r14b, %eax
imull $0x55, %eax, %eax
movb %al, 0xb(%rbx,%r13)
movq -0xd0(%rbp), %r11
addq $0x4, %r11
incq %r9
decq %r12
incq %rdx
movq -0xd8(%rbp), %rcx
decq %rcx
jmp 0x2019
movq -0x78(%rbp), %rax
movq -0xa8(%rbp), %r12
addq %rax, %r12
movq -0xa0(%rbp), %rcx
addq %rax, %rcx
movl -0x44(%rbp), %eax
movl %eax, %r15d
jmp 0x1fcc
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq