// File: /workspace/github_cpp_makefile_star2_O3/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, %ebx
movl %ecx, %ebp
movl %esi, %r15d
movslq %esi, %rax
shlq $0x10, %rax
addq %rdi, %rax
movslq %edx, %rcx
movslq %ebp, %rsi
movslq %r8d, %r14
movslq %r9d, %r9
shlq $0x8, %rcx
shlq $0x5, %rsi
addq %rcx, %rsi
addq %rax, %rsi
shlq $0x4, %r14
addq %rsi, %r14
movq 0x1850(%r14,%r9,8), %r12
testq %r12, %r12
jne 0xebbd
movq %r9, 0x8(%rsp)
movl %edx, 0x4(%rsp)
movl %r8d, 0x1c(%rsp)
xorl %eax, %eax
movq %rdi, %r12
xorl %edi, %edi
movl $0x8, %esi
movl $0x8, %edx
movl $0x8, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
callq 0x44e0
addq $0x10, %rsp
movq %rax, %r13
movq %r12, 0x30(%rsp)
leaq 0x8(%r12), %rdi
callq 0x127e4
movq %r13, %rdi
movq %rax, %rsi
callq 0x41e0
movq %r13, %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x4460
movl (%r13), %eax
movl %eax, 0x14(%rsp)
testb $0x2, %al
je 0xeaeb
movq %r13, %rdi
callq 0x44a0
movq 0xd501(%rip), %r12      # 0x1bfb0
leaq 0x6b91(%rip), %rsi      # 0x15647
movl $0x15, %edx
movq %r12, %rdi
callq 0x4410
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %r12, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
movq %r13, 0x20(%rsp)
movq 0x8(%rsp), %rax
leaq (%r14,%rax,8), %rax
addq $0x1850, %rax           # imm = 0x1850
movq %rax, 0x28(%rsp)
shll $0x8, %r15d
addl 0x4(%rsp), %r15d
movl %r15d, 0x4(%rsp)
shll $0x2, %ebp
movl %ebp, 0x8(%rsp)
movq 0x30(%rsp), %r12
xorl %ecx, %ecx
movl %ebx, 0x18(%rsp)
movl $0x7, %ebp
movl $0x7, %r15d
subl %ecx, %r15d
testl %ebx, %ebx
cmovel %ecx, %r15d
xorl %r14d, %r14d
cmpl $0x0, 0x1c(%rsp)
movl %ebp, %ebx
cmovel %r14d, %ebx
movq %r12, %rdi
movl 0x4(%rsp), %esi
movl %r14d, %edx
movq %rcx, %r13
callq 0xf8f8
testl %eax, %eax
je 0xeb6e
addl 0x8(%rsp), %eax
cltq
movq 0x1750(%r12,%rax,8), %rax
movb (%rax), %al
jmp 0xeb70
movb $0x4a, %al
movq 0x20(%rsp), %rdx
movq 0x20(%rdx), %rcx
movl 0x18(%rdx), %edx
imull %r15d, %edx
addl %ebx, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %r14d
decl %ebp
cmpl $0x8, %r14d
movq %r13, %rcx
jne 0xeb3b
incl %ecx
cmpl $0x8, %ecx
movl 0x18(%rsp), %ebx
jne 0xeb24
testb $0x2, 0x14(%rsp)
movq 0x20(%rsp), %r12
je 0xebb5
movq %r12, %rdi
callq 0x44f0
movq 0x28(%rsp), %rax
movq %r12, (%rax)
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop