// File: /workspace/github_c_makefile_star2_O2/qad114[P]yobeboy/src/gpu.c
// Function: GPU_renderToFrameBuffer
void GPU_renderToFrameBuffer(GPU* gpu, Memory* mem) {
    uint8_t LCDC = mem->logicalMemory[REG_LCDC];
    if (!getBit(LCDC, 7)) {
        // LCD disabled, return a blank screen
        for (int i = 0; i < (160 * 144 * 4); i += 4) {
            memcpy(gpu->framebuffer + i, &(gpu->colorPalette[0]), 3);
            gpu->framebuffer[i + 3] = 0xFF;
        }
        gpu->fbUpdated = true;
        return;
    }

    if (getBit(LCDC, 0)) {
        //renderBackground(gpu, mem);
        if (getBit(LCDC, 5)) renderWindow(gpu, mem);
    };
    if (getBit(LCDC, 1)) renderObjects(gpu, mem);
    gpu->fbUpdated = true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movb 0xff40(%rsi), %r12b
testb %r12b, %r12b
js 0x5667
leaq 0x28(%rbx), %rax
xorl %ecx, %ecx
cmpq $0x167ff, %rcx          # imm = 0x167FF
ja 0x5852
movq 0x8(%rbx), %rdx
movb 0x2(%rax), %sil
movb %sil, 0x2(%rdx,%rcx)
movzwl (%rax), %esi
movw %si, (%rdx,%rcx)
movq 0x8(%rbx), %rdx
movb $-0x1, 0x3(%rdx,%rcx)
addq $0x4, %rcx
jmp 0x5637
movq %rsi, %r14
movl %r12d, %eax
notb %al
testb $0x21, %al
jne 0x584c
movl $0x10000, %edi          # imm = 0x10000
callq 0x2200
movq %rax, %r10
movzbl 0xff4b(%r14), %ecx
movl %ecx, 0x3c(%rsp)
movzbl 0xff4a(%r14), %r13d
movl %r12d, %ecx
shrb $0x6, %cl
movzbl %cl, %ecx
shll $0xa, %ecx
orq $0x9800, %rcx           # imm = 0x9800
movq %rcx, 0x10(%rsp)
xorl %ecx, %ecx
pushq $0x7
popq %r9
movq %rax, 0x8(%rsp)
movq %r13, 0x18(%rsp)
cmpq $0x20, %rcx
je 0x579e
movq %rcx, 0x28(%rsp)
shlq $0x5, %rcx
addq 0x10(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq %r10, 0x20(%rsp)
xorl %r13d, %r13d
cmpq $0x20, %r13
je 0x5780
movq 0x30(%rsp), %rax
addq %r13, %rax
movzbl (%r14,%rax), %eax
movsbl %al, %ecx
shll $0x4, %eax
orl $0x8000, %eax           # imm = 0x8000
shll $0x4, %ecx
addl $0x9000, %ecx           # imm = 0x9000
movl %r12d, %r11d
testb $0x10, %r12b
cmovneq %rax, %rcx
movzwl %cx, %esi
movq %r10, %r12
xorl %edi, %edi
cmpq $0x10, %rdi
jae 0x5771
movq %rdi, %rax
orq %rsi, %rax
movzbl (%r14,%rax), %edx
movzbl 0x1(%r14,%rax), %r15d
movl %r9d, %ecx
xorl %r8d, %r8d
cmpq $0x8, %r8
je 0x5764
movl %r15d, %eax
shrl %cl, %eax
addb %al, %al
andb $0x2, %al
btl %ecx, %edx
setb %bpl
orb %al, %bpl
movb %bpl, (%r12,%r8)
incq %r8
decl %ecx
jmp 0x5740
addq $0x2, %rdi
addq $0x100, %r12            # imm = 0x100
jmp 0x5723
incq %r13
addq $0x8, %r10
movl %r11d, %r12d
jmp 0x56e5
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %r10
addq $0x800, %r10            # imm = 0x800
movq 0x18(%rsp), %r13
jmp 0x56c0
leaq 0x28(%rbx), %rdi
movl 0x3c(%rsp), %ecx
cmpb $0x8, %cl
pushq $0x7
popq %rax
cmovael %ecx, %eax
leaq -0x7(%rax), %r8
imulq $0xa0, %r13, %r9
addq %rax, %r9
subl %ecx, %eax
cltq
movq 0x8(%rsp), %rsi
addq %rax, %rsi
pushq $0x3
popq %rbp
cmpq $0x8f, %r13
ja 0x583e
movq %r9, %rax
movq %rsi, %r10
movq %r8, %r11
cmpq $0xa0, %r11
jae 0x582b
movb (%r10), %cl
addb %cl, %cl
movzbl 0xff47(%r14), %edx
movl %ebp, %r15d
shll %cl, %r15d
andl %edx, %r15d
shrl %cl, %r15d
movq 0x8(%rbx), %rcx
movb 0x2(%rdi,%r15,4), %dl
movb %dl, -0x1a(%rcx,%rax,4)
movzwl (%rdi,%r15,4), %edx
movw %dx, -0x1c(%rcx,%rax,4)
movq 0x8(%rbx), %rcx
movb $-0x1, -0x19(%rcx,%rax,4)
incq %r11
incq %r10
incq %rax
jmp 0x57de
incq %r13
addq $0x100, %rsi            # imm = 0x100
addq $0xa0, %r9
jmp 0x57cc
movb $0x1, 0x10(%rbx)
movq 0x8(%rsp), %rdi
callq 0x21b0
testb $0x2, %r12b
jne 0x5865
movb $0x1, 0x10(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28(%rbx), %r8
movl $0xfe00, %eax           # imm = 0xFE00
cmpq $0xfe9e, %rax           # imm = 0xFE9E
ja 0x5852
movq %rax, 0x8(%rsp)
movzbl 0x1(%r14,%rax), %r13d
cmpq $0xa7, %r13
jbe 0x5895
movq 0x8(%rsp), %rax
addq $0x4, %rax
jmp 0x586e
movq 0x8(%rsp), %rax
movzbl (%r14,%rax), %ecx
movl %ecx, 0x28(%rsp)
movzbl 0x2(%r14,%rax), %ecx
shll $0x4, %ecx
orq $0x8000, %rcx           # imm = 0x8000
movq %rcx, 0x20(%rsp)
movb 0x3(%r14,%rax), %al
movb 0xff40(%r14), %cl
andb $0x4, %cl
shrb $0x2, %cl
movzbl %cl, %r10d
shll $0x4, %r10d
leal 0xf(%r10), %esi
xorl %ecx, %ecx
testb $0x40, %al
sete %cl
movl $0x0, %edx
cmovel %edx, %esi
movl %eax, %edx
shrb $0x5, %dl
movl %eax, %edi
andb $0x10, %dil
cmpb $0x1, %dil
movl $0xff49, %r9d           # imm = 0xFF49
sbbq $0x0, %r9
movzbl %dl, %edi
andl $0x1, %edi
negq %rdi
orq $0x1, %rdi
addq $0x10, %r10
movq %r10, 0x30(%rsp)
testb $0x20, %al
leaq 0x47(%rsp), %r10
leaq 0x40(%rsp), %rax
cmoveq %rax, %r10
leal -0x2(,%rcx,4), %eax
movl %eax, 0x18(%rsp)
leal -0x2820(,%r13,4), %eax
movl %eax, 0x10(%rsp)
addq $-0x8, %r13
xorl %r11d, %r11d
cmpq 0x30(%rsp), %r11
jae 0x588a
movq 0x20(%rsp), %rax
leaq (%rax,%r11), %rcx
movzbl (%r14,%rcx), %eax
movzbl 0x1(%r14,%rcx), %edx
pushq $0x7
popq %rcx
movq %r10, %r12
cmpl $-0x1, %ecx
je 0x598c
movl %edx, %ebp
shrl %cl, %ebp
addb %bpl, %bpl
andb $0x2, %bpl
btl %ecx, %eax
setb %r15b
orb %bpl, %r15b
movb %r15b, (%r12)
addq %rdi, %r12
decl %ecx
jmp 0x5967
movl %esi, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
addl 0x28(%rsp), %eax
imull $0x280, %eax, %eax      # imm = 0x280
addl 0x10(%rsp), %eax
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x5a0c
movzbl 0x40(%rsp,%rdx), %ecx
testl %ecx, %ecx
je 0x5a04
leal (%rdx,%r13), %ebp
cmpl $0x9f, %ebp
ja 0x5a04
cmpl $0x167ff, %eax          # imm = 0x167FF
ja 0x5a04
addl %ecx, %ecx
pushq $0x3
popq %rbp
movl %ebp, %r15d
shll %cl, %r15d
movzbl (%r14,%r9), %ebp
andl %ebp, %r15d
shrl %cl, %r15d
movq 0x8(%rbx), %rcx
movl %eax, %r12d
movb 0x2(%r8,%r15,4), %bpl
movb %bpl, 0x2(%rcx,%r12)
movzwl (%r8,%r15,4), %ebp
movw %bp, (%rcx,%r12)
movq 0x8(%rbx), %rcx
leal 0x3(%rax), %r15d
movb $-0x1, (%rcx,%r15)
incq %rdx
addl $0x4, %eax
jmp 0x59a4
addl 0x18(%rsp), %esi
addq $0x2, %r11
jmp 0x5942
nopl (%rax)