// File: /workspace/github_c_makefile_star2_O0/qad114[P]yobeboy/src/gpu.c
// Function: renderWindow
static void renderWindow(GPU* gpu, Memory* mem) {
    uint8_t* internalFramebuffer = malloc(256 * 256); // freed at the end of this function
    uint8_t WX = mem->logicalMemory[REG_WX];
    uint8_t WY = mem->logicalMemory[REG_WY];
    uint8_t LCDC = mem->logicalMemory[REG_LCDC];

    for (int mapY = 0; mapY < 32; ++mapY) {
        for (int mapX = 0; mapX < 32; ++mapX) {
            uint16_t address = (getBit(LCDC, 6) ? 0x9C00 : 0x9800) + (mapY * 0x20) + mapX;
            uint16_t tileAddress = getBit(LCDC, 4)
                ? 0x8000 + (mem->logicalMemory[address] * 0x10)
                : 0x9000 + (((int8_t) (mem->logicalMemory[address])) * 0x10);

            for (int i = 0; i < 16; i += 2) {
                uint8_t byte1 = mem->logicalMemory[tileAddress + i];
                uint8_t byte2 = mem->logicalMemory[tileAddress + i + 1];
                
                int offset = (mapY * 8 * 256) + (((int)(i / 2)) * 256) + (mapX * 8);
                for (int i = 0; i < 8; ++i) {
                    internalFramebuffer[offset + i] = getBit(byte2, 7 - i) << 1 | getBit(byte1, 7 - i);
                }
            }
        }
    }

    int xLowerBound = (WX - 7) < 0 ? 0 : (WX - 7);
    for (int y = WY; y < 144; ++y) {
        for (int x = xLowerBound; x < 160; ++x) {
            int pos = (y * 160 * 4) + (x * 4);
            int color = getColorNumber(mem, internalFramebuffer[((y - WY) * 256) + (x - WX + 7)], REG_BGP);
            memcpy(gpu->framebuffer + pos, &(gpu->colorPalette[color]), 3);
            gpu->framebuffer[pos + 3] = 0xFF;
        }
    }

    gpu->fbUpdated = true;
    free(internalFramebuffer);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x10000, %edi          # imm = 0x10000
callq 0x2210
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movb 0xff4b(%rax), %al
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
movb 0xff4a(%rax), %al
movb %al, -0x1a(%rbp)
movq -0x10(%rbp), %rax
movb 0xff40(%rax), %al
movb %al, -0x1b(%rbp)
movl $0x0, -0x20(%rbp)
cmpl $0x20, -0x20(%rbp)
jge 0xbbee
movl $0x0, -0x24(%rbp)
cmpl $0x20, -0x24(%rbp)
jge 0xbbde
movl $0x6, %esi
movzbl -0x1b(%rbp), %edi
callq 0xb9f0
movzbl %al, %edx
movl $0x9800, %eax           # imm = 0x9800
movl $0x9c00, %ecx           # imm = 0x9C00
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl -0x20(%rbp), %ecx
shll $0x5, %ecx
addl %ecx, %eax
addl -0x24(%rbp), %eax
movw %ax, -0x26(%rbp)
movl $0x4, %esi
movzbl -0x1b(%rbp), %edi
callq 0xb9f0
movzbl %al, %eax
cmpl $0x0, %eax
je 0xbad6
movq -0x10(%rbp), %rax
movzwl -0x26(%rbp), %ecx
movzbl (%rax,%rcx), %eax
shll $0x4, %eax
addl $0x8000, %eax           # imm = 0x8000
movl %eax, -0x50(%rbp)
jmp 0xbaed
movq -0x10(%rbp), %rax
movzwl -0x26(%rbp), %ecx
movsbl (%rax,%rcx), %eax
shll $0x4, %eax
addl $0x9000, %eax           # imm = 0x9000
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
movw %ax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x10, -0x2c(%rbp)
jge 0xbbce
movq -0x10(%rbp), %rax
movzwl -0x28(%rbp), %ecx
addl -0x2c(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x2d(%rbp)
movq -0x10(%rbp), %rax
movzwl -0x28(%rbp), %ecx
addl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x2e(%rbp)
movl -0x20(%rbp), %eax
shll $0x3, %eax
shll $0x8, %eax
movl %eax, -0x54(%rbp)
movl -0x2c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x54(%rbp), %eax
shll $0x8, %ecx
addl %ecx, %eax
movl -0x24(%rbp), %ecx
shll $0x3, %ecx
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
cmpl $0x8, -0x38(%rbp)
jge 0xbbbe
movb -0x2e(%rbp), %al
movl $0x7, %esi
subl -0x38(%rbp), %esi
movzbl %al, %edi
callq 0xb9f0
movzbl %al, %eax
shll %eax
movl %eax, -0x58(%rbp)
movb -0x2d(%rbp), %al
movl $0x7, %esi
subl -0x38(%rbp), %esi
movzbl %al, %edi
callq 0xb9f0
movb %al, %cl
movl -0x58(%rbp), %eax
movzbl %cl, %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
addl -0x38(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xbb63
jmp 0xbbc0
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x2c(%rbp)
jmp 0xbafb
jmp 0xbbd0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xba6d
jmp 0xbbe0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xba5c
movzbl -0x19(%rbp), %eax
subl $0x7, %eax
cmpl $0x0, %eax
jge 0xbc01
xorl %eax, %eax
movl %eax, -0x5c(%rbp)
jmp 0xbc0b
movzbl -0x19(%rbp), %eax
subl $0x7, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movzbl -0x1a(%rbp), %eax
movl %eax, -0x40(%rbp)
cmpl $0x90, -0x40(%rbp)
jge 0xbcdb
movl -0x3c(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0xa0, -0x44(%rbp)
jge 0xbccb
imull $0xa0, -0x40(%rbp), %eax
shll $0x2, %eax
movl -0x44(%rbp), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
movzbl -0x1a(%rbp), %edx
subl %edx, %ecx
shll $0x8, %ecx
movl -0x44(%rbp), %edx
movzbl -0x19(%rbp), %esi
subl %esi, %edx
addl $0x7, %edx
addl %edx, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %esi
movl $0xff47, %edx           # imm = 0xFF47
callq 0xc040
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x48(%rbp), %rcx
movq -0x8(%rbp), %rdx
movslq -0x4c(%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 -0x48(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movb $-0x1, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xbc2b
jmp 0xbccd
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xbc18
movq -0x8(%rbp), %rax
movb $0x1, 0x10(%rax)
movq -0x18(%rbp), %rdi
callq 0x21c0
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)