// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/sdlayer2.c
// Function: showframe
void showframe(void)
{
#if USE_OPENGL
	if (!glunavailable) {
		if (bpp == 8) {
			glbuild_update_8bit_frame(&gl8bit, frame, bytesperline, yres);
			glbuild_draw_8bit_frame(&gl8bit);
		}

		SDL_GL_SwapWindow(sdl_window);
		return;
	}
#endif

	unsigned char *pixels, *in;
	int pitch, y, x;
	int rendx, rendy, rendaspect, frameaspect;
	SDL_Rect destrect;
	SDL_Surface *winsurface = NULL;

	if (usesdlrenderer) {
		if (SDL_LockTexture(sdl_texture, NULL, (void**)&pixels, &pitch)) {
			debugprintf("Could not lock texture: %s\n", SDL_GetError());
			return;
		}
	} else {
		if (SDL_LockSurface(sdl_surface)) {
			debugprintf("Could not lock surface: %s\n", SDL_GetError());
			return;
		}
		pixels = (unsigned char *)sdl_surface->pixels;
		pitch = sdl_surface->pitch;
	}

	in = frame;
	for (y = yres - 1; y >= 0; y--) {
		for (x = xres - 1; x >= 0; x--) {
			memcpy(&pixels[x<<2], &curpalettefaded[in[x]], 4);
		}
		pixels += pitch;
		in += bytesperline;
	}

	if (usesdlrenderer) {
		SDL_UnlockTexture(sdl_texture);
	} else {
		SDL_UnlockSurface(sdl_surface);
	}

	if (usesdlrenderer) {
		SDL_GetRendererOutputSize(sdl_renderer, &rendx, &rendy);
	} else {
		winsurface = SDL_GetWindowSurface(sdl_window);
		if (!winsurface) {
			debugprintf("Could not get window surface: %s\n", SDL_GetError());
			return;
		}
		rendx = winsurface->w;
		rendy = winsurface->h;
	}

	rendaspect = divscale16(rendx, rendy);
	frameaspect = divscale16(xres, yres);
	if (rendaspect >= frameaspect) {
		// Renderer is at least as wide as the frame. We maximise frame height and centre on width.
		destrect.y = 0;
		destrect.h = rendy;
		destrect.w = mulscale16(rendy, frameaspect);
		destrect.x = (rendx - destrect.w) >> 1;
	} else {
		// Renderer is narrower than the frame. We maximise frame width and centre on height.
		destrect.x = 0;
		destrect.w = rendx;
		destrect.h = divscale16(rendx, frameaspect);
		destrect.y = (rendy - destrect.h) >> 1;
	}

	if (usesdlrenderer) {
		SDL_RenderClear(sdl_renderer);
		if (SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, &destrect)) {
			debugprintf("Could not copy render texture: %s\n", SDL_GetError());
		}
		SDL_RenderPresent(sdl_renderer);
	} else {
		SDL_FillRect(winsurface, NULL, SDL_MapRGB(winsurface->format, 0, 0, 0));
		if (SDL_BlitScaled(sdl_surface, NULL, winsurface, &destrect) < 0) {
			debugprintf("Could not blit surface: %s\n", SDL_GetError());
			return;
		}
		SDL_UpdateWindowSurface(sdl_window);
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x28, %rsp
leaq 0x1139b6(%rip), %rax    # 0x1770e8
cmpl $0x0, (%rax)
je 0x6376a
cmpb $0x1, 0x662c92(%rip)    # 0x6c63d0
jne 0x637bd
movq 0x6648c9(%rip), %rdi    # 0x6c8010
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
xorl %esi, %esi
callq 0x6b70
testl %eax, %eax
je 0x637f4
callq 0x6b40
jmp 0x639d1
leaq 0x113933(%rip), %rax    # 0x1770a4
cmpl $0x8, (%rax)
jne 0x637a6
movq 0x6648eb(%rip), %rsi    # 0x6c8068
leaq 0x113b00(%rip), %rax    # 0x177284
movl (%rax), %edx
leaq 0x398a7(%rip), %rax     # 0x9d034
movl (%rax), %ecx
leaq 0x664892(%rip), %rbx    # 0x6c8028
movq %rbx, %rdi
callq 0x4651f
movq %rbx, %rdi
callq 0x4657c
movq 0x662b13(%rip), %rdi    # 0x6c62c0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x6340
movq 0x664854(%rip), %rdi    # 0x6c8018
callq 0x6560
testl %eax, %eax
je 0x637dd
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x6b40
movq 0x664834(%rip), %rax    # 0x6c8018
movq 0x20(%rax), %rcx
movq %rcx, 0x8(%rsp)
movl 0x18(%rax), %eax
movl %eax, 0x4(%rsp)
movq 0x66486d(%rip), %rax    # 0x6c8068
leaq 0x39832(%rip), %r15     # 0x9d034
movl (%r15), %ecx
leaq 0x39824(%rip), %r12     # 0x9d030
leaq 0x3cc59d(%rip), %rdx    # 0x42fdb0
leaq 0x113a6a(%rip), %rsi    # 0x177284
testl %ecx, %ecx
jle 0x63865
decl %ecx
movslq (%r12), %rdi
leal -0x4(,%rdi,4), %r8d
movq 0x8(%rsp), %r9
testq %rdi, %rdi
jle 0x63850
movslq %r8d, %r8
movzbl -0x1(%rax,%rdi), %r10d
decq %rdi
movl (%rdx,%r10,4), %r10d
movl %r10d, (%r9,%r8)
addl $-0x4, %r8d
jmp 0x6382c
movslq 0x4(%rsp), %rdi
addq %rdi, %r9
movq %r9, 0x8(%rsp)
movslq (%rsi), %rdi
addq %rdi, %rax
jmp 0x6381a
cmpb $0x1, 0x662b64(%rip)    # 0x6c63d0
jne 0x6387c
movq 0x66479b(%rip), %rdi    # 0x6c8010
callq 0x6300
jmp 0x63888
movq 0x664795(%rip), %rdi    # 0x6c8018
callq 0x6a20
cmpb $0x1, 0x662b41(%rip)    # 0x6c63d0
jne 0x638b6
movq 0x664770(%rip), %rdi    # 0x6c8008
leaq 0x14(%rsp), %rbx
leaq 0x10(%rsp), %r14
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6990
movl (%rbx), %edi
xorl %ebx, %ebx
movl (%r14), %r8d
jmp 0x638de
movq 0x662a03(%rip), %rdi    # 0x6c62c0
callq 0x64e0
testq %rax, %rax
je 0x63760
movq %rax, %rbx
movl 0x10(%rax), %edi
movl %edi, 0x14(%rsp)
movl 0x14(%rax), %r8d
movl %r8d, 0x10(%rsp)
movslq %edi, %rcx
shlq $0x10, %rcx
movslq %r8d, %r9
movq %rcx, %rax
cqto
idivq %r9
movq %rax, %rsi
movslq (%r12), %rax
movslq (%r15), %r10
shlq $0x10, %rax
cqto
idivq %r10
movslq %eax, %r10
cmpl %eax, %esi
jge 0x63921
movq %rcx, %rax
cqto
idivq %r10
subl %eax, %r8d
movl %r8d, %ecx
sarl %ecx
xorl %edx, %edx
movl %eax, %r8d
jmp 0x63935
imulq %r9, %r10
shrq $0x10, %r10
subl %r10d, %edi
sarl %edi
xorl %ecx, %ecx
movl %edi, %edx
movl %r10d, %edi
movl %edx, 0x18(%rsp)
movl %edi, 0x20(%rsp)
movl %r8d, 0x24(%rsp)
movl %ecx, 0x1c(%rsp)
cmpb $0x1, 0x662a83(%rip)    # 0x6c63d0
jne 0x6398c
movq 0x6646b2(%rip), %rdi    # 0x6c8008
callq 0x6750
movq 0x6646a6(%rip), %rdi    # 0x6c8008
movq 0x6646a7(%rip), %rsi    # 0x6c8010
leaq 0x18(%rsp), %rcx
xorl %edx, %edx
callq 0x6100
testl %eax, %eax
je 0x6397e
callq 0x6b40
movq 0x664683(%rip), %rdi    # 0x6c8008
callq 0x6b10
jmp 0x639d1
movq 0x8(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x6470
movq %rbx, %rdi
xorl %esi, %esi
movl %eax, %edx
callq 0x6240
movq 0x66466a(%rip), %rdi    # 0x6c8018
leaq 0x18(%rsp), %rcx
xorl %esi, %esi
movq %rbx, %rdx
callq 0x6700
testl %eax, %eax
js 0x63760
movq 0x6628f4(%rip), %rdi    # 0x6c62c0
callq 0x6c00
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq