// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movl %r9d, %ebx
movl %r8d, %r15d
movl %ecx, %ebp
movslq %esi, %rax
shlq $0x10, %rax
addq %rdi, %rax
movslq %edx, %rcx
movslq %ebp, %r8
movslq %r15d, %r14
movslq %r9d, %r9
shlq $0x8, %rcx
shlq $0x5, %r8
addq %rcx, %r8
addq %rax, %r8
shlq $0x4, %r14
addq %r8, %r14
movq 0x1850(%r14,%r9,8), %rax
testq %rax, %rax
jne 0xd14b
movq %r9, 0x28(%rsp)
movl %edx, 0x18(%rsp)
movl %esi, 0x1c(%rsp)
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
pushq $0x8
popq %rcx
movq %rdi, %r13
xorl %edi, %edi
movl %ecx, %esi
movl %ecx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x44d0
movq %rax, %r12
movq %r13, 0x40(%rsp)
leaq 0x8(%r13), %rdi
callq 0x1062c
movq %r12, %rdi
movq %rax, %rsi
callq 0x41d0
pushq $0x1
popq %rsi
pushq $0x4a
popq %rdx
movq %r12, %rdi
callq 0x4430
movq %r12, %rax
movl (%r12), %ecx
movl %ecx, 0x34(%rsp)
testb $0x2, %cl
movq %r12, 0x20(%rsp)
je 0xd060
movq %rax, %rdi
callq 0x4480
movq 0xdf69(%rip), %rdi      # 0x1afb0
leaq 0x65a7(%rip), %rsi      # 0x135f5
callq 0x4390
movq %rax, %rdi
callq 0x41c0
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq (%r14,%rcx,8), %rcx
addq $0x1850, %rcx           # imm = 0x1850
movq %rcx, 0x28(%rsp)
movl 0x1c(%rsp), %esi
shll $0x8, %esi
addl 0x18(%rsp), %esi
shll $0x2, %ebp
movq 0x40(%rsp), %rdi
xorl %ecx, %ecx
movl %ebx, 0x38(%rsp)
movl %ebp, 0x18(%rsp)
movl %r15d, 0x3c(%rsp)
cmpl $0x8, %ecx
je 0xd12f
pushq $0x7
popq %rdx
movl %edx, %r8d
subl %ecx, %r8d
testl %ebx, %ebx
cmovel %ecx, %r8d
movl %r8d, 0x1c(%rsp)
xorl %r14d, %r14d
movl %edx, %ebx
cmpl $0x8, %r14d
je 0xd124
testl %r15d, %r15d
movl %ebx, %r15d
cmovel %r14d, %r15d
movq %rdi, %rbp
movl %esi, %r13d
movl %r14d, %edx
movl %ecx, %r12d
callq 0xdd86
testl %eax, %eax
je 0xd0f1
addl 0x18(%rsp), %eax
cltq
movq 0x1750(%rbp,%rax,8), %rax
movb (%rax), %sil
jmp 0xd0f4
movb $0x4a, %sil
movq %rbp, %rdi
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rcx
movl 0x18(%rax), %edx
imull 0x1c(%rsp), %edx
addl %r15d, %edx
movslq %edx, %rdx
movb %sil, (%rcx,%rdx)
incl %r14d
decl %ebx
movl 0x3c(%rsp), %r15d
movl %r13d, %esi
movl %r12d, %ecx
jmp 0xd0b9
incl %ecx
movl 0x38(%rsp), %ebx
jmp 0xd097
testb $0x2, 0x34(%rsp)
je 0xd143
movq %rax, %rdi
callq 0x44e0
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq