// File: /workspace/github_cpp_makefile_star2_O1/khedoros[P]khednes/ppu3.cpp
// Function: ppu::get_Surface(int, int, int, int, int)
SDL_Surface * ppu::get_Surface(int table, int tile, int palnum, int hf, int vf) {
    //cout<<"T: "<<table<<" t: "<<tile<<" pal: "<<palnum<<" hf: "<<hf<<" vf: "<<vf<<endl;
    if(!(tile_cache[table][tile][palnum][hf][vf])) {
        //build the tile (8x8 palettized)
        SDL_Surface * temp = SDL_CreateRGBSurface(0,8,8,8,0,0,0,0);
        SDL_SetSurfacePalette(temp, screen.get_palette());
        SDL_SetColorKey(temp, SDL_TRUE, NES_TRANSPARENT);
        bool needs_lock = SDL_MUSTLOCK(temp);
        if(needs_lock) {
            SDL_LockSurface(temp);
            cout<<"Surfaces need locked!"<<endl;
        }
        int tilenum = table * 256 + tile;
        for(int y = 0; y < 8; ++y) {
            int outy;
            if(vf) outy = 7 - y;
            else outy = y;
            for(int x = 0; x < 8; ++x) {
                int outx;
                if(hf) outx = 7 - x;
                else outx = x;
                int color = get_color(tilenum,x,y);
                if(color == 0) ((uint8_t *)(temp->pixels))[outy*(temp->pitch)+outx]=NES_TRANSPARENT; //Transparent
                else {
                    //cout<<hex<<"Palnum: "<<palnum<<" Colorpart: "<<color<<" Both: "<<((palnum<<(2))+color);
                    color = *(pal[(palnum<<(2)) + color]);
                    //cout<<" val: "<<color<<dec<<endl;
                    ((uint8_t *)(temp->pixels))[outy*(temp->pitch)+outx]=color;
                }
            }
        }
        if(needs_lock) SDL_UnlockSurface(temp);
        tile_cache[table][tile][palnum][hf][vf] = temp;
    }
   return tile_cache[table][tile][palnum][hf][vf];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r13d
movl %ecx, %ebp
movl %esi, %r15d
movq %rdi, %r9
movslq %esi, %rax
shlq $0x10, %rax
addq %rdi, %rax
movslq %edx, %rcx
movslq %ebp, %r10
movslq %r8d, %rsi
movslq %r13d, %rdi
shlq $0x8, %rcx
shlq $0x5, %r10
addq %rcx, %r10
addq %rax, %r10
shlq $0x4, %rsi
addq %r10, %rsi
leaq (%rsi,%rdi,8), %rax
addq $0x1850, %rax           # imm = 0x1850
cmpq $0x0, (%rax)
jne 0xeb55
movl %edx, 0xc(%rsp)
movq %rax, 0x28(%rsp)
movl %r8d, 0x24(%rsp)
xorl %r12d, %r12d
xorl %edi, %edi
movl $0x8, %esi
movl $0x8, %edx
movl $0x8, %ecx
xorl %r8d, %r8d
movq %r9, %r14
xorl %r9d, %r9d
pushq %r12
pushq %r12
callq 0x44e0
addq $0x10, %rsp
movq %rax, %rbx
movq %r14, 0x30(%rsp)
leaq 0x8(%r14), %rdi
callq 0x12900
movq %rbx, %rdi
movq %rax, %rsi
callq 0x41e0
movq %rbx, %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x4460
movq %rbx, 0x10(%rsp)
movl (%rbx), %eax
movl %eax, 0x18(%rsp)
testb $0x2, %al
je 0xea8b
movq 0x10(%rsp), %rdi
callq 0x44a0
movq 0xd560(%rip), %r14      # 0x1bfb0
leaq 0x6c00(%rip), %rsi      # 0x15657
movl $0x15, %edx
movq %r14, %rdi
callq 0x4410
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %r14, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
shll $0x8, %r15d
addl 0xc(%rsp), %r15d
movl %r15d, 0xc(%rsp)
shll $0x2, %ebp
movl %ebp, 0x20(%rsp)
movq 0x30(%rsp), %rdi
movl %r13d, 0x1c(%rsp)
movl $0x7, %ebp
movl $0x7, %r15d
subl %r12d, %r15d
testl %r13d, %r13d
cmovel %r12d, %r15d
xorl %r14d, %r14d
cmpl $0x0, 0x24(%rsp)
movl %ebp, %r13d
cmovel %r14d, %r13d
movl 0xc(%rsp), %esi
movl %r14d, %edx
movq %r12, %rbx
movl %r12d, %ecx
movq %rdi, %r12
callq 0xf936
testl %eax, %eax
je 0xeaf9
addl 0x20(%rsp), %eax
cltq
movq 0x1750(%r12,%rax,8), %rax
movb (%rax), %al
jmp 0xeafb
movb $0x4a, %al
movq %r12, %rdi
movq 0x10(%rsp), %rdx
movq 0x20(%rdx), %rcx
movl 0x18(%rdx), %edx
imull %r15d, %edx
addl %r13d, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %r14d
decl %ebp
cmpl $0x8, %r14d
movq %rbx, %r12
jne 0xeac2
incl %r12d
cmpl $0x8, %r12d
movl 0x1c(%rsp), %r13d
jne 0xeaaa
testb $0x2, 0x18(%rsp)
je 0xeb48
movq 0x10(%rsp), %rdi
callq 0x44f0
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movq (%rax), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop