// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdi, %rbx
movb 0xff40(%rsi), %bpl
testb %bpl, %bpl
js 0x5e6d
leaq 0x28(%rbx), %rax
movq $-0x4, %rcx
movq 0x8(%rbx), %rdx
movb 0x2(%rax), %sil
movb %sil, 0x6(%rdx,%rcx)
movzwl (%rax), %esi
movw %si, 0x4(%rdx,%rcx)
movq 0x8(%rbx), %rdx
movb $-0x1, 0x7(%rdx,%rcx)
addq $0x4, %rcx
cmpq $0x167fc, %rcx          # imm = 0x167FC
jb 0x5e3d
jmp 0x62ba
movq %rsi, %r14
movl %ebp, %eax
notb %al
testb $0x21, %al
jne 0x60f9
movl $0x10000, %edi          # imm = 0x10000
callq 0x2200
movzbl 0xff4b(%r14), %ecx
movl %ecx, 0x8(%rsp)
movzbl 0xff4a(%r14), %esi
movl %ebp, %ecx
shrb $0x6, %cl
movzbl %cl, %ecx
shll $0xa, %ecx
orq $0x9800, %rcx           # imm = 0x9800
movq %rcx, 0x18(%rsp)
leaq 0x1(%r14), %rdi
xorl %r8d, %r8d
movdqa 0x2aa0(%rip), %xmm0     # 0x8960
movdqa 0x2aa8(%rip), %xmm1     # 0x8970
movq %rax, 0x10(%rsp)
movq %r8, %r10
shlq $0x5, %r10
addq 0x18(%rsp), %r10
movq %rax, %r9
xorl %r15d, %r15d
leaq (%r10,%r15), %r11
movzbl (%r14,%r11), %r11d
movsbl %r11b, %r12d
shll $0x4, %r11d
orl $0x8000, %r11d          # imm = 0x8000
shll $0x4, %r12d
addl $0x9000, %r12d          # imm = 0x9000
testb $0x10, %bpl
cmovneq %r11, %r12
movzwl %r12w, %r12d
addq %rdi, %r12
movq $-0x2, %r13
movq %r9, %r11
movzbl 0x1(%r12,%r13), %edx
movzbl 0x2(%r12,%r13), %ecx
movd %ecx, %xmm2
pshufd $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
movd %edx, %xmm3
pshufd $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
movdqa %xmm2, %xmm4
psrld $0x1, %xmm4
punpckhqdq %xmm2, %xmm4    # xmm4 = xmm4[1],xmm2[1]
movdqa %xmm2, %xmm5
psrld $0x2, %xmm5
movdqa %xmm2, %xmm6
psrld $0x3, %xmm6
punpcklqdq %xmm5, %xmm6    # xmm6 = xmm6[0],xmm5[0]
shufps $0xcc, %xmm4, %xmm6     # xmm6 = xmm6[0,3],xmm4[0,3]
movdqa %xmm2, %xmm4
psrld $0x4, %xmm4
movdqa %xmm2, %xmm5
psrld $0x5, %xmm5
punpckhqdq %xmm4, %xmm5    # xmm5 = xmm5[1],xmm4[1]
movdqa %xmm2, %xmm4
psrld $0x6, %xmm4
psrld $0x7, %xmm2
punpcklqdq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0]
shufps $0xcc, %xmm5, %xmm2     # xmm2 = xmm2[0,3],xmm5[0,3]
packuswb %xmm6, %xmm2
packuswb %xmm2, %xmm2
paddb %xmm2, %xmm2
pand %xmm0, %xmm2
movdqa %xmm3, %xmm4
psrld $0x1, %xmm4
punpckhqdq %xmm3, %xmm4    # xmm4 = xmm4[1],xmm3[1]
movdqa %xmm3, %xmm5
psrld $0x2, %xmm5
movdqa %xmm3, %xmm6
psrld $0x3, %xmm6
punpcklqdq %xmm5, %xmm6    # xmm6 = xmm6[0],xmm5[0]
shufps $0xcc, %xmm4, %xmm6     # xmm6 = xmm6[0,3],xmm4[0,3]
movdqa %xmm3, %xmm4
psrld $0x4, %xmm4
movdqa %xmm3, %xmm5
psrld $0x5, %xmm5
punpckhqdq %xmm4, %xmm5    # xmm5 = xmm5[1],xmm4[1]
movdqa %xmm3, %xmm4
psrld $0x6, %xmm4
psrld $0x7, %xmm3
punpcklqdq %xmm4, %xmm3    # xmm3 = xmm3[0],xmm4[0]
shufps $0xcc, %xmm5, %xmm3     # xmm3 = xmm3[0,3],xmm5[0,3]
packuswb %xmm6, %xmm3
packuswb %xmm3, %xmm3
pand %xmm1, %xmm3
por %xmm2, %xmm3
movq %xmm3, (%r11)
addq $0x2, %r13
addq $0x100, %r11            # imm = 0x100
cmpq $0xe, %r13
jb 0x5f1b
incq %r15
addq $0x8, %r9
cmpq $0x20, %r15
jne 0x5edf
incq %r8
addq $0x800, %rax            # imm = 0x800
cmpq $0x20, %r8
jne 0x5ecd
cmpb $-0x71, %sil
movq 0x10(%rsp), %rdi
ja 0x60f0
leaq 0x28(%rbx), %rax
movl 0x8(%rsp), %edx
cmpb $0x8, %dl
movl $0x7, %ecx
cmovael %edx, %ecx
leaq -0xa7(%rcx), %r13
leaq (%rsi,%rsi,4), %r8
shll $0x5, %r8d
addq %rcx, %r8
subl %edx, %ecx
movslq %ecx, %rcx
movq %rdi, %r9
addq %rcx, %r9
movq %r8, %r10
movq %r9, %r11
movq %r13, %r15
cmpb $-0x5a, 0x8(%rsp)
ja 0x60d6
movb (%r11), %cl
addb %cl, %cl
movzbl 0xff47(%r14), %edx
movl $0x3, %r12d
shll %cl, %r12d
andl %edx, %r12d
shrl %cl, %r12d
movq 0x8(%rbx), %rcx
movb 0x2(%rax,%r12,4), %dl
movb %dl, -0x1a(%rcx,%r10,4)
movzwl (%rax,%r12,4), %edx
movw %dx, -0x1c(%rcx,%r10,4)
movq 0x8(%rbx), %rcx
movb $-0x1, -0x19(%rcx,%r10,4)
incq %r11
incq %r10
incq %r15
jne 0x608c
incq %rsi
addq $0x100, %r9             # imm = 0x100
addq $0xa0, %r8
cmpq $0x90, %rsi
jne 0x607c
movb $0x1, 0x10(%rbx)
callq 0x21b0
testb $0x2, %bpl
je 0x62ba
leaq 0x28(%rbx), %rax
movl $0xfe00, %ecx           # imm = 0xFE00
movq %rcx, 0x28(%rsp)
movzbl 0x1(%r14,%rcx), %r9d
cmpq $0xa7, %r9
ja 0x62a4
movq 0x28(%rsp), %rcx
movzbl (%r14,%rcx), %edx
movl %edx, 0x18(%rsp)
movzbl 0x2(%r14,%rcx), %edx
shll $0x4, %edx
orq $0x8000, %rdx           # imm = 0x8000
movq %rdx, 0x8(%rsp)
movb 0x3(%r14,%rcx), %cl
movb 0xff40(%r14), %dl
andb $0x4, %dl
shrb $0x2, %dl
movzbl %dl, %r8d
shll $0x4, %r8d
leal 0xf(%r8), %ebp
xorl %edx, %edx
testb $0x40, %cl
sete %dl
movl $0x0, %esi
cmovel %esi, %ebp
movl %ecx, %esi
shrb $0x5, %sil
movl %ecx, %edi
andb $0x10, %dil
cmpb $0x1, %dil
movl $0xff49, %r12d          # imm = 0xFF49
sbbq $0x0, %r12
movzbl %sil, %r13d
andl $0x1, %r13d
negq %r13
orq $0x1, %r13
addq $0x10, %r8
movq %r8, 0x10(%rsp)
testb $0x20, %cl
leaq 0x37(%rsp), %r15
leaq 0x30(%rsp), %rcx
cmoveq %rcx, %r15
leal -0x2(,%rdx,4), %ecx
movl %ecx, 0x24(%rsp)
leal -0x2820(,%r9,4), %ecx
movl %ecx, 0x20(%rsp)
addq $-0x8, %r9
xorl %esi, %esi
movq 0x8(%rsp), %rcx
addq %rsi, %rcx
movzbl (%r14,%rcx), %r10d
movzbl 0x1(%r14,%rcx), %r8d
movl $0x7, %ecx
movq %r15, %rdi
movl %r8d, %r11d
shrl %cl, %r11d
addb %r11b, %r11b
andb $0x2, %r11b
btl %ecx, %r10d
setb %dl
orb %r11b, %dl
movb %dl, (%rdi)
addq %r13, %rdi
addl $-0x1, %ecx
jb 0x61f0
movl %ebp, %ecx
shrl $0x1f, %ecx
addl %ebp, %ecx
shrl %ecx
addl 0x18(%rsp), %ecx
leal (%rcx,%rcx,4), %r10d
shll $0x7, %r10d
addl 0x20(%rsp), %r10d
xorl %r11d, %r11d
movzbl 0x30(%rsp,%r11), %ecx
testl %ecx, %ecx
je 0x6284
leal (%r9,%r11), %edx
cmpl $0x9f, %edx
ja 0x6284
cmpl $0x167ff, %r10d         # imm = 0x167FF
ja 0x6284
addl %ecx, %ecx
movl $0x3, %edx
shll %cl, %edx
movzbl (%r14,%r12), %edi
andl %edi, %edx
shrl %cl, %edx
movq 0x8(%rbx), %rcx
movl %r10d, %edi
movb 0x2(%rax,%rdx,4), %r8b
movb %r8b, 0x2(%rcx,%rdi)
movzwl (%rax,%rdx,4), %edx
movw %dx, (%rcx,%rdi)
movq 0x8(%rbx), %rcx
leal 0x3(%r10), %edx
movb $-0x1, (%rcx,%rdx)
incq %r11
addl $0x4, %r10d
cmpq $0x8, %r11
jne 0x622e
addl 0x24(%rsp), %ebp
addq $0x2, %rsi
cmpq 0x10(%rsp), %rsi
jb 0x61d5
movq 0x28(%rsp), %rdx
leaq 0x4(%rdx), %rcx
cmpq $0xfe9b, %rdx           # imm = 0xFE9B
jb 0x610c
movb $0x1, 0x10(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)