// File: /workspace/github_c_makefile_star2_O1/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), %r14
cmpl $0x0, 0xc0c(%rdi)
jle 0x1f27
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, (%r14,%r8)
movb %cl, 0x1(%r14,%r8)
movb %dl, 0x2(%r14,%r8)
movl 0xc0c(%rdi), %r9d
addl %r9d, %r8d
shll $0xe, %r9d
leal (%r9,%r9,2), %r9d
cmpl %r9d, %r8d
jl 0x1eff
movq %rsi, -0x78(%rbp)
leaq -0x1(%rsi), %rax
movq %rax, -0x70(%rbp)
xorl %eax, %eax
movq %rdi, -0xb8(%rbp)
movq %r14, -0xb0(%rbp)
movq %rax, -0xa8(%rbp)
leaq (%rax,%rax,2), %rax
movb (%rdi,%rax,4), %cl
testb $0x1, %cl
je 0x234b
movl %ecx, %edx
leaq (%rdi,%rax,4), %rax
movzbl 0x1(%rax), %ecx
movq %rax, -0x50(%rbp)
movzbl 0x3(%rax), %eax
movq %rcx, -0xd0(%rbp)
movq %rax, %rsi
addl %ecx, %eax
cmpl $0x100, %eax            # imm = 0x100
ja 0x234b
movq -0x50(%rbp), %rax
movzbl 0x2(%rax), %ecx
movzbl 0x4(%rax), %eax
movq %rcx, -0xc8(%rbp)
movq %rax, %r8
addl %ecx, %eax
cmpl $0xc0, %eax
ja 0x234b
testb $0x10, %dl
movl $0x100, %eax            # imm = 0x100
cmovnel %eax, %esi
testb $0x20, %dl
movl $0xc0, %eax
cmovnel %eax, %r8d
movq -0x50(%rbp), %rcx
movzbl 0x5(%rcx), %eax
movq %rax, %r9
shll $0x8, %eax
movzbl 0x6(%rcx), %r10d
movq %r10, -0x40(%rbp)
orl %r10d, %eax
movq %rsi, -0x58(%rbp)
shrl $0x2, %esi
movq %r8, -0x48(%rbp)
movl %r8d, %ecx
imull %esi, %ecx
addl %eax, %ecx
cmpl $0x10000, %ecx          # imm = 0x10000
ja 0x234b
cmpl $0x0, -0x48(%rbp)
je 0x234b
movq %rsi, %r8
movl %edx, %eax
shrb %al
shrb $0x2, %dl
testb $0x1, %al
sete %cl
movl %eax, %esi
orb %dl, %sil
movb %sil, -0x2d(%rbp)
andb %dl, %cl
movb %cl, -0x2b(%rbp)
andb $0x1, %dl
movb %dl, -0x2a(%rbp)
sete %cl
movb %al, -0x29(%rbp)
andb %al, %cl
movb %cl, -0x2c(%rbp)
movq -0x48(%rbp), %rax
movl %eax, %edx
movq -0x58(%rbp), %rax
imull %eax, %edx
shrl $0x2, %edx
movl %r8d, %esi
shll $0x8, %r9d
addq %r9, %rdx
movq -0x70(%rbp), %rcx
leaq (%rcx,%rsi), %r10
addq %rdx, %r10
movq %r10, -0x80(%rbp)
movl %eax, %r10d
movq -0x78(%rbp), %rax
addq %rax, %rdx
movq %rdx, -0x90(%rbp)
orq %r9, %rsi
addq %rcx, %rsi
movq %rsi, -0x88(%rbp)
addq %rax, %r9
movq %r9, -0x98(%rbp)
shll $0x2, %r8d
movq %r8, -0xc0(%rbp)
movq $0x0, -0x68(%rbp)
movq %r10, -0xa0(%rbp)
movq %r10, -0x60(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x4, -0x58(%rbp)
jae 0x20cb
movl -0x34(%rbp), %ecx
incl %ecx
movq -0xa0(%rbp), %rax
addq %rax, -0x60(%rbp)
addq %rax, -0x68(%rbp)
movl %ecx, -0x34(%rbp)
cmpl -0x48(%rbp), %ecx
jne 0x20a4
jmp 0x234b
movq -0x60(%rbp), %rax
shrq $0x2, %rax
movq -0x80(%rbp), %rcx
subq %rax, %rcx
movq -0x90(%rbp), %rdx
subq %rax, %rdx
movq -0x68(%rbp), %rsi
shrq $0x2, %rsi
movq -0x88(%rbp), %rax
leaq (%rax,%rsi), %r9
addq -0x98(%rbp), %rsi
xorl %r12d, %r12d
testb $0x1, -0x2d(%rbp)
movq %rcx, -0xe0(%rbp)
movq %rdx, -0xd8(%rbp)
jne 0x211e
movq -0x40(%rbp), %rax
movb (%rsi,%rax), %al
jmp 0x2173
cmpb $0x0, -0x2c(%rbp)
je 0x212e
movq -0x40(%rbp), %rax
movb (%r9,%rax), %al
jmp 0x2150
cmpb $0x0, -0x2b(%rbp)
je 0x213d
movq -0x40(%rbp), %rax
movb (%rdx,%rax), %al
jmp 0x2173
movb -0x29(%rbp), %al
testb %al, -0x2a(%rbp)
je 0x2344
movq -0x40(%rbp), %rax
movb (%rcx,%rax), %al
movl %eax, %ecx
shrb $0x6, %cl
movl %eax, %edx
shrb $0x2, %dl
andb $0xc, %dl
leal (,%rax,4), %r8d
andb $0x30, %r8b
shlb $0x6, %al
orb %cl, %al
orb %dl, %al
orb %r8b, %al
movzbl %al, %edx
movl %edx, %eax
shrl $0x6, %eax
movl %edx, %ecx
shrb $0x4, %cl
movzbl %cl, %ecx
andl $0x3, %ecx
movl %edx, %r10d
shrb $0x2, %r10b
movslq 0xc0c(%rdi), %r8
movl %r12d, %ebx
imull %r8d, %ebx
movq %r8, %r11
imulq -0xd0(%rbp), %r11
addq %r14, %r11
movslq 0xc08(%rdi), %r14
movslq -0x34(%rbp), %r8
imulq %r14, %r8
imulq -0xc8(%rbp), %r14
addq %r11, %r14
movzbl %r10b, %r10d
andl $0x3, %r10d
andl $0x3, %edx
movq -0x50(%rbp), %rdi
movb 0x7(%rdi,%rcx), %r11b
movslq %ebx, %r15
addq %r14, %r15
movb 0x7(%rdi,%r10), %r14b
movb 0x7(%rdi,%rdx), %bl
cmpl $0x3, %eax
jne 0x21f4
movb (%rdi), %r13b
andb $0x8, %r13b
jne 0x223a
movb 0x7(%rdi,%rax), %al
movl %eax, %r13d
shrb $0x5, %r13b
movzbl %r13b, %r13d
shll $0x2, %r13d
leal (%r13,%r13,8), %r13d
movb %r13b, (%r15,%r8)
movl %eax, %r13d
shrb $0x2, %r13b
andb $0x7, %r13b
movzbl %r13b, %r13d
shll $0x2, %r13d
leal (%r13,%r13,8), %r13d
movb %r13b, 0x1(%r15,%r8)
andb $0x3, %al
movzbl %al, %eax
imull $0x55, %eax, %eax
movb %al, 0x2(%r15,%r8)
cmpb $0x3, %cl
jne 0x2245
movb (%rdi), %al
andb $0x8, %al
jne 0x227f
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)
cmpb $0x3, %r10b
movq -0xe0(%rbp), %rcx
jne 0x2292
movb (%rdi), %al
andb $0x8, %al
jne 0x22cc
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)
cmpb $0x3, %dl
jne 0x22d7
movb (%rdi), %al
andb $0x8, %al
jne 0x230d
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, %r12
decq %rcx
movq -0xd8(%rbp), %rdx
incq %rdx
decq %r9
incq %rsi
cmpq %r12, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %r14
jne 0x2101
jmp 0x20aa
xorl %eax, %eax
jmp 0x2173
movq -0xa8(%rbp), %rax
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x1f43
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq