// File: /workspace/github_c_makefile_star2_O0/qad114[P]yobeboy/src/gpu.c
// Function: renderObjects
static void renderObjects(GPU* gpu, Memory* mem) {
    for (int i = 0xFE00; i < 0xFE9F; i += 4) {
        int yPos = mem->logicalMemory[i] - 16;
        int xPos = mem->logicalMemory[i + 1] - 8;
        if (xPos >= 160) continue;

        int tileIndex = mem->logicalMemory[i + 2];
        uint16_t tileAddress = 0x8000 + (tileIndex * 0x10);
        uint8_t flags = mem->logicalMemory[i + 3];
        //int underBg = getBit(flags, 7);
        int yFlip = getBit(flags, 6);
        int xFlip = getBit(flags, 5);

        //if (underBg) continue;

        uint8_t longObjectMode = getBit(mem->logicalMemory[REG_LCDC], 2); // Is 8x16 mode active?
        int rowIndex = yFlip ? (longObjectMode ? 31 : 15) : 0;
        for (int i = 0; i < (longObjectMode ? 32 : 16); i += 2) {
            uint8_t byte1 = mem->logicalMemory[tileAddress + i];
            uint8_t byte2 = mem->logicalMemory[tileAddress + i + 1];

            uint8_t pixels[8];
            int pixelIndex = (xFlip ? 7 : 0);
            for (int i = 0; i < 8; ++i) {
                pixels[pixelIndex] = getBit(byte2, 7 - i) << 1 | getBit(byte1, 7 - i);
                pixelIndex += (xFlip ? -1 : 1);
            }

            int offset = (yPos * 4 * 160) + ((rowIndex / 2) * 4 * 160) + (xPos * 4);
            for (int i = 0; i < 8; ++i) {
                if (pixels[i] == 0) continue;
                int pos = offset + (i * 4);
                if (pos < 0 || pos >= (160 * 144 * 4) || xPos + i < 0 || xPos + i >= 160) continue; // TODO: Removing the first check should work, but doesn't - possible bug
                int color = getColorNumber(mem, pixels[i], (getBit(flags, 4) ? REG_OBP1 : REG_OBP0));
                memcpy(gpu->framebuffer + pos, &(gpu->colorPalette[color]), 3);
                gpu->framebuffer[pos + 3] = 0xFF;
            }

            rowIndex += (yFlip ? -2 : 2);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xfe00, -0x14(%rbp)    # imm = 0xFE00
cmpl $0xfe9f, -0x14(%rbp)    # imm = 0xFE9F
jge 0xc030
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %eax
subl $0x10, %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
subl $0x8, %eax
movl %eax, -0x1c(%rbp)
cmpl $0xa0, -0x1c(%rbp)
jl 0xbd5e
jmp 0xc022
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
shll $0x4, %eax
addl $0x8000, %eax           # imm = 0x8000
movw %ax, -0x22(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x23(%rbp)
movl $0x6, %esi
movzbl -0x23(%rbp), %edi
callq 0xb9f0
movzbl %al, %eax
movl %eax, -0x28(%rbp)
movl $0x5, %esi
movzbl -0x23(%rbp), %edi
callq 0xb9f0
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movl $0x2, %esi
movzbl 0xff40(%rax), %edi
callq 0xb9f0
movb %al, -0x2d(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0xbdf3
movzbl -0x2d(%rbp), %edx
movl $0xf, %eax
movl $0x1f, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x60(%rbp)
jmp 0xbdfa
xorl %eax, %eax
movl %eax, -0x60(%rbp)
jmp 0xbdfa
movl -0x60(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movzbl -0x2d(%rbp), %esi
movl $0x10, %ecx
movl $0x20, %edx
cmpl $0x0, %esi
cmovnel %edx, %ecx
cmpl %ecx, %eax
jge 0xc020
movq -0x10(%rbp), %rax
movzwl -0x22(%rbp), %ecx
addl -0x38(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x39(%rbp)
movq -0x10(%rbp), %rax
movzwl -0x22(%rbp), %ecx
addl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x3a(%rbp)
movl -0x2c(%rbp), %edx
xorl %eax, %eax
movl $0x7, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
cmpl $0x8, -0x4c(%rbp)
jge 0xbed7
movb -0x3a(%rbp), %al
movl $0x7, %esi
subl -0x4c(%rbp), %esi
movzbl %al, %edi
callq 0xb9f0
movzbl %al, %eax
shll %eax
movl %eax, -0x64(%rbp)
movb -0x39(%rbp), %al
movl $0x7, %esi
subl -0x4c(%rbp), %esi
movzbl %al, %edi
callq 0xb9f0
movb %al, %cl
movl -0x64(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movb %al, %cl
movslq -0x48(%rbp), %rax
movb %cl, -0x42(%rbp,%rax)
movl -0x2c(%rbp), %edx
movl $0x1, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x0, %edx
cmovnel %ecx, %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xbe6b
movl -0x18(%rbp), %eax
shll $0x2, %eax
imull $0xa0, %eax, %eax
movl %eax, -0x68(%rbp)
movl -0x34(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x68(%rbp), %eax
shll $0x2, %ecx
imull $0xa0, %ecx, %ecx
addl %ecx, %eax
movl -0x1c(%rbp), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
cmpl $0x8, -0x54(%rbp)
jge 0xbff9
movslq -0x54(%rbp), %rax
movzbl -0x42(%rbp,%rax), %eax
cmpl $0x0, %eax
jne 0xbf30
jmp 0xbfeb
movl -0x50(%rbp), %eax
movl -0x54(%rbp), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jl 0xbf65
cmpl $0x16800, -0x58(%rbp)   # imm = 0x16800
jge 0xbf65
movl -0x1c(%rbp), %eax
addl -0x54(%rbp), %eax
cmpl $0x0, %eax
jl 0xbf65
movl -0x1c(%rbp), %eax
addl -0x54(%rbp), %eax
cmpl $0xa0, %eax
jl 0xbf6a
jmp 0xbfeb
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movslq -0x54(%rbp), %rax
movzbl -0x42(%rbp,%rax), %eax
movl %eax, -0x6c(%rbp)
movl $0x4, %esi
movzbl -0x23(%rbp), %edi
callq 0xb9f0
movq -0x78(%rbp), %rdi
movl -0x6c(%rbp), %esi
movzbl %al, %edx
movl $0xff48, %eax           # imm = 0xFF48
movl $0xff49, %ecx           # imm = 0xFF49
cmpl $0x0, %edx
cmovnel %ecx, %eax
movzwl %ax, %edx
callq 0xc040
movl %eax, -0x5c(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x58(%rbp), %rcx
movq -0x8(%rbp), %rdx
movslq -0x5c(%rbp), %rsi
movw 0x28(%rdx,%rsi,4), %di
movw %di, (%rax,%rcx)
movb 0x2a(%rdx,%rsi,4), %dl
movb %dl, 0x2(%rax,%rcx)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x58(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movb $-0x1, (%rax,%rcx)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xbf13
movl -0x28(%rbp), %edx
movl $0x2, %eax
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
cmpl $0x0, %edx
cmovnel %ecx, %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x38(%rbp)
jmp 0xbe07
jmp 0xc022
movl -0x14(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x14(%rbp)
jmp 0xbd1a
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)