// File: /workspace/github_c_makefile_star2_O3/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 %rbx
subq $0x30, %rsp
leaq 0x10f10b(%rip), %rax    # 0x17e0e8
cmpl $0x0, (%rax)
je 0x6f015
cmpb $0x1, 0x65e3e7(%rip)    # 0x6cd3d0
jne 0x6f066
movq 0x66001e(%rip), %rdi    # 0x6cf010
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
xorl %esi, %esi
callq 0x6b70
testl %eax, %eax
je 0x6f09b
callq 0x6b40
jmp 0x6f28c
leaq 0x10f088(%rip), %rax    # 0x17e0a4
cmpl $0x8, (%rax)
jne 0x6f051
movq 0x660040(%rip), %rsi    # 0x6cf068
leaq 0x10f255(%rip), %rax    # 0x17e284
movl (%rax), %edx
leaq 0x34ffc(%rip), %rax     # 0xa4034
movl (%rax), %ecx
leaq 0x65ffe7(%rip), %rbx    # 0x6cf028
movq %rbx, %rdi
callq 0x4fd30
movq %rbx, %rdi
callq 0x4fd8d
movq 0x65e268(%rip), %rdi    # 0x6cd2c0
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0x6340
movq 0x65ffab(%rip), %rdi    # 0x6cf018
callq 0x6560
testl %eax, %eax
je 0x6f084
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0x6b40
movq 0x65ff8d(%rip), %rax    # 0x6cf018
movq 0x20(%rax), %rcx
movq %rcx, 0x10(%rsp)
movl 0x18(%rax), %eax
movl %eax, 0xc(%rsp)
leaq 0x34f92(%rip), %r15     # 0xa4034
movl (%r15), %eax
testl %eax, %eax
jle 0x6f11a
movq 0x65ffb8(%rip), %rcx    # 0x6cf068
leaq 0x34f79(%rip), %rdx     # 0xa4030
leaq 0x3c7cf2(%rip), %rsi    # 0x436db0
leaq 0x10f1bf(%rip), %rdi    # 0x17e284
movslq (%rdx), %r9
testq %r9, %r9
jle 0x6f0fe
movq %r9, %r8
incq %r8
leal -0x4(,%r9,4), %r9d
movq 0x10(%rsp), %r10
movslq %r9d, %r9
movzbl -0x2(%rcx,%r8), %r11d
movl (%rsi,%r11,4), %r11d
movl %r11d, (%r10,%r9)
decq %r8
addl $-0x4, %r9d
cmpq $0x1, %r8
ja 0x6f0db
leal -0x1(%rax), %r8d
movslq 0xc(%rsp), %r9
addq %r9, 0x10(%rsp)
movslq (%rdi), %r9
addq %r9, %rcx
cmpl $0x1, %eax
movl %r8d, %eax
jg 0x6f0c5
cmpb $0x1, 0x65e2af(%rip)    # 0x6cd3d0
jne 0x6f131
movq 0x65fee6(%rip), %rdi    # 0x6cf010
callq 0x6300
jmp 0x6f13d
movq 0x65fee0(%rip), %rdi    # 0x6cf018
callq 0x6a20
cmpb $0x1, 0x65e28c(%rip)    # 0x6cd3d0
jne 0x6f16b
movq 0x65febb(%rip), %rdi    # 0x6cf008
leaq 0x1c(%rsp), %rbx
leaq 0x18(%rsp), %r14
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6990
movl (%rbx), %edi
xorl %ebx, %ebx
movl (%r14), %r8d
jmp 0x6f193
movq 0x65e14e(%rip), %rdi    # 0x6cd2c0
callq 0x64e0
testq %rax, %rax
je 0x6f00b
movq %rax, %rbx
movl 0x10(%rax), %edi
movl %edi, 0x1c(%rsp)
movl 0x14(%rax), %r8d
movl %r8d, 0x18(%rsp)
movslq %edi, %rcx
shlq $0x10, %rcx
movslq %r8d, %r9
movq %rcx, %rax
cqto
idivq %r9
movq %rax, %rsi
leaq 0x34e81(%rip), %rax     # 0xa4030
movslq (%rax), %rax
movslq (%r15), %r10
shlq $0x10, %rax
cqto
idivq %r10
movslq %eax, %r10
cmpl %eax, %esi
jge 0x6f1dc
movq %rcx, %rax
cqto
idivq %r10
subl %eax, %r8d
movl %r8d, %ecx
sarl %ecx
xorl %edx, %edx
movl %eax, %r8d
jmp 0x6f1f0
imulq %r9, %r10
shrq $0x10, %r10
subl %r10d, %edi
sarl %edi
xorl %ecx, %ecx
movl %edi, %edx
movl %r10d, %edi
movl %edx, 0x20(%rsp)
movl %edi, 0x28(%rsp)
movl %r8d, 0x2c(%rsp)
movl %ecx, 0x24(%rsp)
cmpb $0x1, 0x65e1c8(%rip)    # 0x6cd3d0
jne 0x6f247
movq 0x65fdf7(%rip), %rdi    # 0x6cf008
callq 0x6750
movq 0x65fdeb(%rip), %rdi    # 0x6cf008
movq 0x65fdec(%rip), %rsi    # 0x6cf010
leaq 0x20(%rsp), %rcx
xorl %edx, %edx
callq 0x6100
testl %eax, %eax
je 0x6f239
callq 0x6b40
movq 0x65fdc8(%rip), %rdi    # 0x6cf008
callq 0x6b10
jmp 0x6f28c
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 0x65fdaf(%rip), %rdi    # 0x6cf018
leaq 0x20(%rsp), %rcx
xorl %esi, %esi
movq %rbx, %rdx
callq 0x6700
testl %eax, %eax
js 0x6f00b
movq 0x65e039(%rip), %rdi    # 0x6cd2c0
callq 0x6c00
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq