// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/sdlayer2.c
// Function: setvideomode
int setvideomode(int x, int y, int c, int fs)
{
	int regrab = 0;
	int flags;
	int winx, winy;

	if ((fs == fullscreen) && (x == xres) && (y == yres) && (c == bpp) &&
		!videomodereset) {
		OSD_ResizeDisplay(xres,yres);
		return 0;
	}

	if (checkvideomode(&x,&y,c,fs,0) < 0) return -1;	// Will return if GL mode not available.

	if (mouseacquired) {
		regrab = 1;
		grabmouse(0);
	}

	if (baselayer_videomodewillchange) baselayer_videomodewillchange();
	shutdownvideo();

	buildprintf("Setting video mode %dx%d (%d-bpp %s)\n", x,y,c,
		(fs & 1) ? "fullscreen" : "windowed");

	do {
		flags = SDL_WINDOW_HIDDEN;

#if USE_OPENGL
		if (!glunavailable) {
#if (USE_OPENGL == USE_GLES2)
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif (USE_OPENGL == USE_GL3)
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#else
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
			SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
#endif

			SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
			SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
#if USE_POLYMOST
			SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, glmultisample > 0);
			SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, glmultisample ? (1 << glmultisample) : 0);
#endif

			flags |= SDL_WINDOW_OPENGL;
		}
#endif

		winx = x; winy = y;
		if (fs & 1) {
			if (c > 8) flags |= SDL_WINDOW_FULLSCREEN;
			else flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
		}

		sdl_window = SDL_CreateWindow(wintitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, winx, winy, flags);
		if (!sdl_window) {
			buildprintf("Error creating window: %s\n", SDL_GetError());

#if USE_POLYMOST && USE_OPENGL
			if (glmultisample > 0) {
				buildprintf("Retrying without multisampling.\n");
				glmultisample = 0;
				continue;
			}
#endif

			return -1;
		}
		break;
	} while (1);

#ifndef __APPLE__
	{
		SDL_Surface *icon = loadappicon();
		SDL_SetWindowIcon(sdl_window, icon);
		SDL_FreeSurface(icon);
	}
#endif

	if (c == 8) {
		int i, j, pitch;

		// Round up to a multiple of 4.
		pitch = (((x|1) + 4) & ~3);

#if USE_OPENGL
		if (glunavailable) {
#endif
			if (usesdlrenderer) {
				// 8-bit software with no GL shader blitting goes via the SDL rendering apparatus.
				SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");

				sdl_renderer = SDL_CreateRenderer(sdl_window, -1, SDL_RENDERER_PRESENTVSYNC);
				if (!sdl_renderer) {
					buildprintf("Error creating renderer: %s\n", SDL_GetError());
					return -1;
				}
				SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 255);

				sdl_texture = SDL_CreateTexture(sdl_renderer,
					B_LITTLE_ENDIAN ? SDL_PIXELFORMAT_ABGR8888 : SDL_PIXELFORMAT_RGBA8888,
					SDL_TEXTUREACCESS_STREAMING, x, y);
				if (!sdl_texture) {
					buildprintf("Error creating texture: %s\n", SDL_GetError());
					return -1;
				}
			} else {
				// 8-bit software with no GL shader blitting goes via the SDL rendering apparatus.
				sdl_surface = SDL_CreateRGBSurface(0, x, y, 32,
					255l<<(8*offsetof(palette_t, r)),
					255l<<(8*offsetof(palette_t, g)),
					255l<<(8*offsetof(palette_t, b)),
					0);
				if (!sdl_surface) {
					buildprintf("Error creating surface: %s\n", SDL_GetError());
					return -1;
				}
			}
#if USE_OPENGL
		} else {
			// Prepare the GLSL shader for 8-bit blitting.
			int winx = x, winy = y;

			sdl_glcontext = SDL_GL_CreateContext(sdl_window);
			if (!sdl_glcontext) {
				buildprintf("Error creating OpenGL context: %s\n", SDL_GetError());
				glunavailable = 1;
			} else if (glbuild_init()) {
				glunavailable = 1;
			} else {
				SDL_GL_GetDrawableSize(sdl_window, &winx, &winy);
				if (glbuild_prepare_8bit_shader(&gl8bit, x, y, pitch, winx, winy) < 0) {
					glunavailable = 1;
				}
			}
			if (glunavailable) {
				// Try again but without OpenGL.
				buildputs("Falling back to non-OpenGL render.\n");
				return setvideomode(x, y, c, fs);
			}
		}
#endif

		frame = (unsigned char *) malloc(pitch * y);
		if (!frame) {
			buildputs("Unable to allocate framebuffer\n");
			return -1;
		}

		frameplace = (intptr_t) frame;
		bytesperline = pitch;
		imageSize = bytesperline * y;
		numpages = 1;

		setvlinebpl(bytesperline);
		for (i = j = 0; i <= y; i++) {
			ylookup[i] = j;
			j += bytesperline;
		}

	} else {
#if USE_OPENGL
		sdl_glcontext = SDL_GL_CreateContext(sdl_window);
		if (!sdl_glcontext) {
			buildprintf("Error creating OpenGL context: %s\n", SDL_GetError());
			return -1;
		}

		if (glbuild_init()) {
			shutdownvideo();
			return -1;
		}
#if USE_POLYMOST
		polymost_glreset();
#endif

		frameplace = 0;
		bytesperline = 0;
		imageSize = 0;
		numpages = 127;
#else
		return -1;
#endif
	}

	SDL_ShowWindow(sdl_window);

	xres = x;
	yres = y;
	bpp = c;
	fullscreen = fs;

#if USE_OPENGL
	if (sdl_glcontext) {
		if (SDL_GL_SetSwapInterval(glswapinterval) < 0) {
			buildputs("note: OpenGL swap interval could not be changed\n");
		}
	}
	if (c == 8 && !glunavailable) {
		// The drawable size may have changed once the window was shown.
		SDL_GL_GetDrawableSize(sdl_window, &winx, &winy);
		glbuild_update_window_size(&gl8bit, winx, winy);
	}
#endif

	videomodereset = 0;
	if (baselayer_videomodedidchange) baselayer_videomodedidchange();
	OSD_ResizeDisplay(xres,yres);

	// setpalettefade will set the palette according to whether gamma worked
	setpalettefade(palfadergb.r, palfadergb.g, palfadergb.b, palfadedelta);

	if (regrab) grabmouse(1);

	startwin_close();

	// Start listening for character input.
	SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %ebp
movl %edx, %ebx
movl %edi, 0xc(%rsp)
movl %esi, 0x8(%rsp)
leaq 0x110577(%rip), %rdx    # 0x17d0a8
cmpl %ecx, (%rdx)
jne 0x6cb74
movl %edi, %ecx
leaq 0x364f2(%rip), %rdx     # 0xa3030
movl (%rdx), %edi
cmpl %ecx, %edi
jne 0x6cb74
movl %esi, %eax
leaq 0x364e7(%rip), %rcx     # 0xa3034
movl (%rcx), %esi
cmpl %eax, %esi
jne 0x6cb74
leaq 0x11054a(%rip), %rax    # 0x17d0a4
cmpl %ebx, (%rax)
jne 0x6cb74
leaq 0x110554(%rip), %rax    # 0x17d0b9
cmpb $0x0, (%rax)
jne 0x6cb74
callq 0x4a332
jmp 0x6d1d9
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl %ebx, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x1ea44
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x6d1dc
movb 0x65f73a(%rip), %al     # 0x6cc2d8
movb %al, 0x13(%rsp)
testb %al, %al
je 0x6cbad
xorl %edi, %edi
callq 0x6c577
leaq 0x11050c(%rip), %rax    # 0x17d0c0
movq (%rax), %rax
testq %rax, %rax
je 0x6cbbe
callq *%rax
callq 0x6c233
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
testb $0x1, %bpl
leaq 0x8366(%rip), %rax      # 0x74f3c
leaq 0x7486(%rip), %r8       # 0x74063
cmoveq %rax, %r8
leaq 0x10918(%rip), %rdi     # 0x7d500
movl %ebx, %ecx
xorl %eax, %eax
callq 0x24cb4
leaq 0x58cd94(%rip), %r14    # 0x5f998c
leaq 0x65f6e1(%rip), %r15    # 0x6cc2e0
leaq 0x10920(%rip), %r12     # 0x7d526
leaq 0x10934(%rip), %r13     # 0x7d541
movl $0x8, %r9d
leaq 0x1104ce(%rip), %rax    # 0x17d0e8
cmpl $0x0, (%rax)
jne 0x6cc9d
movl $0x15, %edi
movl $0x2, %esi
callq 0x6b30
movl $0x11, %edi
movl $0x2, %esi
callq 0x6b30
movl $0x12, %edi
movl $0x1, %esi
callq 0x6b30
movl $0xf, %edi
movl $0x1, %esi
callq 0x6b30
movl $0x5, %edi
movl $0x1, %esi
callq 0x6b30
xorl %esi, %esi
cmpl $0x0, (%r14)
setg %sil
movl $0xd, %edi
callq 0x6b30
movl (%r14), %ecx
movl $0x1, %esi
shll %cl, %esi
testl %ecx, %ecx
cmovel %ecx, %esi
movl $0xe, %edi
callq 0x6b30
movl $0xa, %r9d
movl 0xc(%rsp), %ecx
movl %ecx, 0x24(%rsp)
movl 0x8(%rsp), %r8d
movl %r8d, 0x20(%rsp)
testb $0x1, %bpl
je 0x6ccc7
cmpl $0x9, %ebx
jl 0x6ccc0
orl $0x1, %r9d
jmp 0x6ccc7
orl $0x1001, %r9d           # imm = 0x1001
movq %r15, %rdi
movl $0x2fff0000, %esi       # imm = 0x2FFF0000
movl $0x2fff0000, %edx       # imm = 0x2FFF0000
callq 0x6450
movq %rax, 0x65f5f0(%rip)    # 0x6cc2d0
testq %rax, %rax
jne 0x6cd17
callq 0x6b40
movq %r12, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x24cb4
cmpl $0x0, (%r14)
jle 0x6cf05
movq %r13, %rdi
xorl %eax, %eax
callq 0x24cb4
movl $0x0, (%r14)
jmp 0x6cc0d
leaq 0x36232(%rip), %rax     # 0xa2f50
movq 0x8(%rax), %rdi
movl (%rax), %esi
movl 0x4(%rax), %edx
leal (,%rsi,4), %r8d
subq $0x8, %rsp
movl $0x20, %ecx
movl $0xff, %r9d
pushq $-0x1000000             # imm = 0xFF000000
pushq $0xff0000               # imm = 0xFF0000
pushq $0xff00                 # imm = 0xFF00
callq 0x6760
addq $0x20, %rsp
movq %rax, %r15
movq 0x65f570(%rip), %rdi    # 0x6cc2d0
movq %rax, %rsi
callq 0x6950
movq %r15, %rdi
callq 0x6350
cmpl $0x8, %ebx
jne 0x6ce39
movslq 0xc(%rsp), %rsi
movq %rsi, %r15
andq $-0x4, %r15
addq $0x4, %r15
leaq 0x110358(%rip), %rax    # 0x17d0e8
cmpl $0x0, (%rax)
je 0x6ce72
cmpb $0x1, 0x65f640(%rip)    # 0x6cc3e0
jne 0x6cf10
leaq 0x107b5(%rip), %rdi     # 0x7d562
leaq 0x107c7(%rip), %rsi     # 0x7d57b
callq 0x6840
movq 0x65f510(%rip), %rdi    # 0x6cc2d0
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x4, %edx
callq 0x6400
movq %rax, 0x66123c(%rip)    # 0x6ce018
testq %rax, %rax
je 0x6d0a2
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xff, %r8d
callq 0x69d0
movq 0x661218(%rip), %rdi    # 0x6ce018
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r8d
movl $0x16762004, %esi       # imm = 0x16762004
movl $0x1, %edx
callq 0x63e0
movq %rax, 0x661201(%rip)    # 0x6ce020
testq %rax, %rax
jne 0x6d009
callq 0x6b40
leaq 0x1076c(%rip), %rdi     # 0x7d5a0
jmp 0x6cf53
movq 0x65f490(%rip), %rdi    # 0x6cc2d0
callq 0x6770
movq %rax, 0x6611e4(%rip)    # 0x6ce030
testq %rax, %rax
je 0x6ceef
callq 0x4b714
testl %eax, %eax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
je 0x6cf6b
callq 0x6c233
jmp 0x6d1dc
movl %esi, 0x1c(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x65f44b(%rip), %rdi    # 0x6cc2d0
callq 0x6770
movq %rax, 0x66119f(%rip)    # 0x6ce030
testq %rax, %rax
je 0x6cfa4
callq 0x4b714
testl %eax, %eax
jne 0x6cfba
movq 0x65f422(%rip), %rdi    # 0x6cc2d0
leaq 0x1c(%rsp), %r13
leaq 0x18(%rsp), %r12
movq %r13, %rsi
movq %r12, %rdx
callq 0x69b0
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
movl (%r13), %r8d
movl (%r12), %r9d
leaq 0x66115e(%rip), %rdi    # 0x6ce038
movl %r15d, %ecx
callq 0x4cf38
testl %eax, %eax
jns 0x6cfc7
jmp 0x6cfba
callq 0x6b40
leaq 0x106dd(%rip), %rdi     # 0x7d5d8
movq %rax, %rsi
xorl %eax, %eax
callq 0x24cb4
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x6d1dc
movl 0x8(%rsp), %edx
xorl %edi, %edi
movl $0x20, %ecx
movl $0xff, %r8d
movl $0xff00, %r9d           # imm = 0xFF00
pushq $0x0
pushq $0xff0000               # imm = 0xFF0000
callq 0x6900
addq $0x10, %rsp
movq %rax, 0x6610ea(%rip)    # 0x6ce028
testq %rax, %rax
jne 0x6d009
callq 0x6b40
leaq 0x10669(%rip), %rdi     # 0x7d5bc
xorl %r14d, %r14d
movq %rax, %rsi
xorl %eax, %eax
callq 0x24cb4
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x6d0c2
callq 0x4d9ef
leaq 0x110139(%rip), %rax    # 0x17d0b0
movq $0x0, (%rax)
leaq 0x1102ff(%rip), %rax    # 0x17d284
xorl %ecx, %ecx
movl %ecx, (%rax)
leaq 0x1102f8(%rip), %rax    # 0x17d288
movl %ecx, (%rax)
leaq 0x3b4ee7(%rip), %rax    # 0x421e80
movl $0x7f, (%rax)
jmp 0x6d0cb
callq 0x6b40
leaq 0x10628(%rip), %rdi     # 0x7d5d8
movq %rax, %rsi
xorl %eax, %eax
callq 0x24cb4
leaq 0x110127(%rip), %rax    # 0x17d0e8
movl $0x1, (%rax)
leaq 0x11011a(%rip), %rax    # 0x17d0e8
movl (%rax), %r14d
testl %r14d, %r14d
je 0x6cffb
leaq 0x1061e(%rip), %rdi     # 0x7d5fb
callq 0x32200
movl 0xc(%rsp), %edi
movl 0x8(%rsp), %esi
movl $0x8, %edx
movl %ebp, %ecx
callq 0x6cb10
movl %eax, %r12d
jmp 0x6cffb
testl %r14d, %r14d
jne 0x6d09d
movl %r12d, 0x14(%rsp)
movslq 0x8(%rsp), %r12
imulq %r15, %r12
movq %r12, %rdi
callq 0x69a0
movq %rax, 0x661057(%rip)    # 0x6ce078
testq %rax, %rax
je 0x6d08b
leaq 0x110083(%rip), %rcx    # 0x17d0b0
movq %rax, (%rcx)
leaq 0x11024d(%rip), %r13    # 0x17d284
movl %r15d, (%r13)
leaq 0x110246(%rip), %rax    # 0x17d288
movl %r12d, (%rax)
leaq 0x3b4e34(%rip), %rax    # 0x421e80
movl $0x1, (%rax)
movl %r15d, %edi
callq 0x6dcbc
movl 0x8(%rsp), %eax
movb $0x1, %r14b
testl %eax, %eax
js 0x6d0bd
movl (%r13), %ecx
incq %rax
xorl %edx, %edx
leaq 0x3a8c2b(%rip), %rsi    # 0x415ca0
xorl %edi, %edi
movl 0x14(%rsp), %r12d
movl %edx, (%rsi,%rdi,4)
incq %rdi
addl %ecx, %edx
cmpq %rdi, %rax
jne 0x6d07c
jmp 0x6d0c2
leaq 0x1058d(%rip), %rdi     # 0x7d61f
callq 0x32200
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %r14d, %r14d
jmp 0x6d0c2
callq 0x6b40
leaq 0x104d5(%rip), %rdi     # 0x7d583
xorl %r14d, %r14d
movq %rax, %rsi
xorl %eax, %eax
callq 0x24cb4
jmp 0x6d0c2
movl 0x14(%rsp), %r12d
testb %r14b, %r14b
je 0x6d1dc
movq 0x65f1fe(%rip), %rdi    # 0x6cc2d0
callq 0x67a0
movl 0xc(%rsp), %eax
leaq 0x35f4e(%rip), %r15     # 0xa3030
movl %eax, (%r15)
movl 0x8(%rsp), %eax
leaq 0x35f44(%rip), %r12     # 0xa3034
movl %eax, (%r12)
leaq 0x10ffa9(%rip), %rax    # 0x17d0a4
movl %ebx, (%rax)
leaq 0x10ffa4(%rip), %rax    # 0x17d0a8
movl %ebp, (%rax)
cmpq $0x0, 0x660f22(%rip)    # 0x6ce030
je 0x6d12e
leaq 0x35f11(%rip), %rax     # 0xa3028
movl (%rax), %edi
callq 0x6580
testl %eax, %eax
jns 0x6d12e
leaq 0x10516(%rip), %rdi     # 0x7d63f
callq 0x32200
cmpl $0x8, %ebx
jne 0x6d16c
leaq 0x10ffae(%rip), %rax    # 0x17d0e8
cmpl $0x0, (%rax)
jne 0x6d16c
movq 0x65f18a(%rip), %rdi    # 0x6cc2d0
leaq 0x24(%rsp), %rbx
leaq 0x20(%rsp), %r14
movq %rbx, %rsi
movq %r14, %rdx
callq 0x69b0
movl (%rbx), %esi
movl (%r14), %edx
leaq 0x660ed1(%rip), %rdi    # 0x6ce038
callq 0x4d36c
leaq 0x10ff46(%rip), %rax    # 0x17d0b9
movb $0x0, (%rax)
leaq 0x10ff4b(%rip), %rax    # 0x17d0c8
movq (%rax), %rax
testq %rax, %rax
je 0x6d187
callq *%rax
movl (%r15), %edi
movl (%r12), %esi
callq 0x4a332
leaq 0x14a8a5(%rip), %rax    # 0x1b7a3f
movzbl (%rax), %edi
movzbl 0x1(%rax), %esi
movzbl 0x2(%rax), %edx
leaq 0x14a897(%rip), %rax    # 0x1b7a43
movzbl (%rax), %ecx
callq 0x3b0d4
cmpb $0x0, 0x13(%rsp)
je 0x6d1c5
movl $0x1, %edi
callq 0x6c577
callq 0x1e111
movl $0x303, %edi            # imm = 0x303
movl $0x1, %esi
callq 0x63b0
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq