// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movl %edx, 0x2c(%rsp)
movl %edi, 0x28(%rsp)
movl %esi, 0x24(%rsp)
leaq 0x114006(%rip), %r8     # 0x1770a8
leaq 0x39f87(%rip), %rax     # 0x9d030
leaq 0x39f84(%rip), %rdx     # 0x9d034
movl %ecx, 0x34(%rsp)
cmpl %ecx, (%r8)
jne 0x630e8
cmpl %edi, (%rax)
jne 0x630e8
cmpl %esi, (%rdx)
jne 0x630e8
leaq 0x113fdc(%rip), %rax    # 0x1770a4
movl 0x2c(%rsp), %ecx
cmpl %ecx, (%rax)
jne 0x630e8
leaq 0x113fe2(%rip), %rax    # 0x1770b9
cmpb $0x0, (%rax)
jne 0x630e8
callq 0x43cfa
xorl %ebx, %ebx
jmp 0x636fd
leaq 0x28(%rsp), %rdi
leaq 0x24(%rsp), %rsi
movl 0x2c(%rsp), %edx
movl 0x34(%rsp), %ecx
xorl %r8d, %r8d
callq 0x1c8dc
pushq $-0x1
popq %rbx
testl %eax, %eax
js 0x636fd
movb 0x6631b5(%rip), %al     # 0x6c62c8
movb %al, 0x33(%rsp)
testb %al, %al
je 0x63122
xorl %edi, %edi
callq 0x62b55
leaq 0x113f97(%rip), %rax    # 0x1770c0
movq (%rax), %rax
testq %rax, %rax
je 0x63133
callq *%rax
callq 0x62829
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
testb $0x1, 0x34(%rsp)
leaq 0x7e20(%rip), %rax      # 0x6af6c
leaq 0x6f38(%rip), %r8       # 0x6a08b
cmoveq %rax, %r8
leaq 0x106a2(%rip), %rdi     # 0x73800
movl 0x2c(%rsp), %ecx
xorl %eax, %eax
callq 0x22427
leaq 0x113f78(%rip), %rax    # 0x1770e8
pushq $0x1
popq %r14
leaq 0x590811(%rip), %rbx    # 0x5f398c
leaq 0x66314e(%rip), %rbp    # 0x6c62d0
leaq 0x1069d(%rip), %r13     # 0x73826
leaq 0x106b1(%rip), %r15     # 0x73841
cmpl $0x0, (%rax)
pushq $0x8
popq %r9
jne 0x63209
pushq $0x15
popq %rdi
movq %r13, %r12
movq %r15, %r13
pushq $0x2
popq %r15
movl %r15d, %esi
callq 0x6b30
pushq $0x11
popq %rdi
movl %r15d, %esi
movq %r13, %r15
movq %r12, %r13
callq 0x6b30
pushq $0x12
popq %rdi
movl %r14d, %esi
callq 0x6b30
pushq $0xf
popq %rdi
movl %r14d, %esi
callq 0x6b30
pushq $0x5
popq %rdi
movl %r14d, %esi
callq 0x6b30
xorl %esi, %esi
cmpl $0x0, (%rbx)
setg %sil
pushq $0xd
popq %rdi
callq 0x6b30
movl (%rbx), %ecx
movl %r14d, %esi
shll %cl, %esi
testl %ecx, %ecx
cmovel %ecx, %esi
pushq $0xe
popq %rdi
callq 0x6b30
pushq $0xa
popq %r9
movl 0x28(%rsp), %ecx
movl %ecx, 0x44(%rsp)
movl 0x24(%rsp), %r8d
movl %r8d, 0x40(%rsp)
testb $0x1, 0x34(%rsp)
je 0x63236
cmpl $0x9, 0x2c(%rsp)
jl 0x6322f
orl $0x1, %r9d
jmp 0x63236
orl $0x1001, %r9d           # imm = 0x1001
movq %rbp, %rdi
movl $0x2fff0000, %esi       # imm = 0x2FFF0000
movl $0x2fff0000, %edx       # imm = 0x2FFF0000
callq 0x6450
movq %rax, 0x663071(%rip)    # 0x6c62c0
testq %rax, %rax
jne 0x63288
callq 0x6b40
movq %r13, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x22427
cmpl $0x0, (%rbx)
jle 0x636fa
movq %r15, %rdi
xorl %eax, %eax
callq 0x22427
andl $0x0, (%rbx)
leaq 0x113e65(%rip), %rax    # 0x1770e8
jmp 0x63190
leaq 0x39cc1(%rip), %rax     # 0x9cf50
movq 0x8(%rax), %rdi
movl (%rax), %esi
movl 0x4(%rax), %edx
leal (,%rsi,4), %r8d
movl $0xff000000, 0x10(%rsp) # imm = 0xFF000000
movl $0xff0000, 0x8(%rsp)    # imm = 0xFF0000
movl $0xff00, (%rsp)         # imm = 0xFF00
pushq $0x20
popq %rcx
movl $0xff, %r9d
callq 0x6760
movq %rax, %r14
movq 0x662ff1(%rip), %rdi    # 0x6c62c0
movq %rax, %rsi
callq 0x6950
movq %r14, %rdi
callq 0x6350
cmpl $0x8, 0x2c(%rsp)
jne 0x633a0
movslq 0x28(%rsp), %rsi
movq %rsi, %r14
andq $-0x4, %r14
addq $0x4, %r14
leaq 0x113de7(%rip), %rax    # 0x1770e8
cmpl $0x0, (%rax)
je 0x633d3
cmpb $0x1, 0x6630bf(%rip)    # 0x6c63d0
jne 0x6346d
leaq 0x10544(%rip), %rdi     # 0x73862
leaq 0x10556(%rip), %rsi     # 0x7387b
callq 0x6840
movq 0x662f8f(%rip), %rdi    # 0x6c62c0
pushq $-0x1
popq %rbx
pushq $0x4
popq %rdx
movl %ebx, %esi
callq 0x6400
movq %rax, 0x664cc3(%rip)    # 0x6c8008
testq %rax, %rax
je 0x636cc
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xff, %r8d
callq 0x69d0
movq 0x664c9f(%rip), %rdi    # 0x6c8008
movl 0x28(%rsp), %ecx
movl 0x24(%rsp), %r8d
pushq $0x1
popq %rdx
movl $0x16762004, %esi       # imm = 0x16762004
callq 0x63e0
movq %rax, 0x664c8a(%rip)    # 0x6c8010
testq %rax, %rax
jne 0x634a3
callq 0x6b40
leaq 0x10505(%rip), %rdi     # 0x738a0
jmp 0x636d8
movq 0x662f19(%rip), %rdi    # 0x6c62c0
callq 0x6770
movq %rax, 0x664c6d(%rip)    # 0x6c8020
testq %rax, %rax
je 0x6345c
callq 0x44ef8
testl %eax, %eax
je 0x6352d
callq 0x62829
jmp 0x636fa
movl %esi, 0x3c(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x662eda(%rip), %rdi    # 0x6c62c0
callq 0x6770
movq %rax, 0x664c2e(%rip)    # 0x6c8020
testq %rax, %rax
je 0x63677
callq 0x44ef8
testl %eax, %eax
jne 0x6368d
movq 0x662eb1(%rip), %rdi    # 0x6c62c0
leaq 0x3c(%rsp), %r15
leaq 0x38(%rsp), %r12
movq %r15, %rsi
movq %r12, %rdx
callq 0x69b0
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
movl (%r15), %r8d
movl (%r12), %r9d
leaq 0x664bee(%rip), %rdi    # 0x6c8028
movl %r14d, %ecx
callq 0x45eee
testl %eax, %eax
js 0x6368d
leaq 0x113c97(%rip), %rax    # 0x1770e8
cmpl $0x0, (%rax)
jne 0x6369a
jmp 0x634a3
callq 0x6b40
leaq 0x10470(%rip), %rdi     # 0x738d8
jmp 0x636f0
movl 0x24(%rsp), %edx
andl $0x0, 0x8(%rsp)
movl $0xff0000, (%rsp)       # imm = 0xFF0000
pushq $0x20
popq %rcx
xorl %edi, %edi
movl $0xff, %r8d
movl $0xff00, %r9d           # imm = 0xFF00
callq 0x6900
movq %rax, 0x664b7e(%rip)    # 0x6c8018
testq %rax, %rax
je 0x636e4
movslq 0x24(%rsp), %r15
imulq %r14, %r15
movq %r15, %rdi
callq 0x69a0
movq %rax, 0x664bad(%rip)    # 0x6c8068
testq %rax, %rax
je 0x636be
leaq 0x113be5(%rip), %rcx    # 0x1770b0
movq %rax, (%rcx)
leaq 0x113daf(%rip), %rbx    # 0x177284
movl %r14d, (%rbx)
leaq 0x113da9(%rip), %rax    # 0x177288
movl %r15d, (%rax)
leaq 0x3b8997(%rip), %rax    # 0x41be80
movl $0x1, (%rax)
movl %r14d, %edi
callq 0x64198
movslq 0x24(%rsp), %rax
movl (%rbx), %ecx
xorl %edx, %edx
leaq 0x3ac799(%rip), %rsi    # 0x40fca0
xorl %edi, %edi
leaq 0x39b20(%rip), %r15     # 0x9d030
leaq 0x39b1d(%rip), %r12     # 0x9d034
leaq 0x113b8a(%rip), %rbx    # 0x1770a8
cmpq %rax, %rdi
jg 0x63573
movl %edx, (%rsi,%rdi,4)
incq %rdi
addl %ecx, %edx
jmp 0x6351e
callq 0x46917
leaq 0x113b77(%rip), %rax    # 0x1770b0
andq $0x0, (%rax)
leaq 0x113d40(%rip), %rax    # 0x177284
andl $0x0, (%rax)
leaq 0x113d3a(%rip), %rax    # 0x177288
andl $0x0, (%rax)
leaq 0x3b8928(%rip), %rax    # 0x41be80
movl $0x7f, (%rax)
leaq 0x39acb(%rip), %r15     # 0x9d030
leaq 0x39ac8(%rip), %r12     # 0x9d034
leaq 0x113b35(%rip), %rbx    # 0x1770a8
movq 0x662d46(%rip), %rdi    # 0x6c62c0
callq 0x67a0
movl 0x28(%rsp), %eax
movl %eax, (%r15)
movl 0x24(%rsp), %eax
movl %eax, (%r12)
leaq 0x113b0f(%rip), %rax    # 0x1770a4
movl 0x2c(%rsp), %ecx
movl %ecx, (%rax)
movl 0x34(%rsp), %eax
movl %eax, (%rbx)
cmpq $0x0, 0x664a77(%rip)    # 0x6c8020
je 0x635c9
leaq 0x39a76(%rip), %rax     # 0x9d028
movl (%rax), %edi
callq 0x6580
testl %eax, %eax
jns 0x635c9
leaq 0x1037b(%rip), %rdi     # 0x7393f
callq 0x30c8d
cmpl $0x8, 0x2c(%rsp)
jne 0x63609
leaq 0x113b11(%rip), %rax    # 0x1770e8
cmpl $0x0, (%rax)
jne 0x63609
movq 0x662cdd(%rip), %rdi    # 0x6c62c0
leaq 0x44(%rsp), %rbx
leaq 0x40(%rsp), %r14
movq %rbx, %rsi
movq %r14, %rdx
callq 0x69b0
movl (%rbx), %esi
movl (%r14), %edx
leaq 0x664a24(%rip), %rdi    # 0x6c8028
callq 0x462e7
leaq 0x113aa9(%rip), %rax    # 0x1770b9
movb $0x0, (%rax)
leaq 0x113aae(%rip), %rax    # 0x1770c8
movq (%rax), %rax
testq %rax, %rax
je 0x63624
callq *%rax
movl (%r15), %edi
movl (%r12), %esi
callq 0x43cfa
leaq 0x14e408(%rip), %rax    # 0x1b1a3f
movzbl (%rax), %edi
movzbl 0x1(%rax), %esi
movzbl 0x2(%rax), %edx
leaq 0x14e3fa(%rip), %rax    # 0x1b1a43
movzbl (%rax), %ecx
callq 0x388e5
cmpb $0x0, 0x33(%rsp)
je 0x63660
pushq $0x1
popq %rdi
callq 0x62b55
callq 0x1c013
pushq $0x1
popq %rsi
movl $0x303, %edi            # imm = 0x303
callq 0x63b0
jmp 0x630e1
callq 0x6b40
leaq 0x10255(%rip), %rdi     # 0x738d8
movq %rax, %rsi
xorl %eax, %eax
callq 0x22427
leaq 0x113a54(%rip), %rax    # 0x1770e8
movl $0x1, (%rax)
leaq 0x1025a(%rip), %rdi     # 0x738fb
callq 0x30c8d
movl 0x28(%rsp), %edi
movl 0x24(%rsp), %esi
pushq $0x8
popq %rdx
movl 0x34(%rsp), %ecx
callq 0x63081
movl %eax, %ebx
jmp 0x636fd
leaq 0x1025a(%rip), %rdi     # 0x7391f
callq 0x30c8d
jmp 0x636fa
callq 0x6b40
leaq 0x101ab(%rip), %rdi     # 0x73883
movq %rax, %rsi
xorl %eax, %eax
callq 0x22427
jmp 0x636fd
callq 0x6b40
leaq 0x101cc(%rip), %rdi     # 0x738bc
movq %rax, %rsi
xorl %eax, %eax
callq 0x22427
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq