// File: /workspace/github_c_makefile_star2_O3/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, %ebx
movl %edx, 0x10(%rsp)
movl %edi, 0xc(%rsp)
movl %esi, 0x8(%rsp)
leaq 0x10f86e(%rip), %rdx    # 0x17e0a8
leaq 0x357ef(%rip), %rax     # 0xa4030
leaq 0x357ec(%rip), %rcx     # 0xa4034
cmpl %ebx, (%rdx)
jne 0x6e87c
cmpl %edi, (%rax)
jne 0x6e87c
cmpl %esi, (%rcx)
jne 0x6e87c
leaq 0x10f849(%rip), %rax    # 0x17e0a4
movl 0x10(%rsp), %ecx
cmpl %ecx, (%rax)
jne 0x6e87c
leaq 0x10f84f(%rip), %rax    # 0x17e0b9
cmpb $0x0, (%rax)
jne 0x6e87c
callq 0x4c7f6
xorl %r14d, %r14d
jmp 0x6efa9
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
movl %ebx, %ecx
xorl %r8d, %r8d
callq 0x1eec0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x6efa9
movb 0x65ea20(%rip), %al     # 0x6cd2c8
movb %al, 0x17(%rsp)
testb %al, %al
je 0x6e8b7
xorl %edi, %edi
callq 0x6e2a9
leaq 0x10f802(%rip), %rax    # 0x17e0c0
movq (%rax), %rax
testq %rax, %rax
je 0x6e8c8
callq *%rax
callq 0x6df6f
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
testb $0x1, %bl
leaq 0x774d(%rip), %rax      # 0x7602c
leaq 0x6865(%rip), %r8       # 0x7514b
cmoveq %rax, %r8
leaq 0x100af(%rip), %rdi     # 0x7e9a0
movl 0x10(%rsp), %ecx
xorl %eax, %eax
callq 0x25221
leaq 0x10f7e5(%rip), %rax    # 0x17e0e8
leaq 0x58c082(%rip), %r14    # 0x5fa98c
leaq 0x65e9bf(%rip), %r15    # 0x6cd2d0
leaq 0x100ae(%rip), %r12     # 0x7e9c6
leaq 0x100c2(%rip), %r13     # 0x7e9e1
movl $0x8, %r9d
movq %rax, %rbp
cmpl $0x0, (%rax)
jne 0x6e9ab
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, %bl
je 0x6e9d6
cmpl $0x9, 0x10(%rsp)
jl 0x6e9cf
orl $0x1, %r9d
jmp 0x6e9d6
orl $0x1001, %r9d           # imm = 0x1001
movq %r15, %rdi
movl $0x2fff0000, %esi       # imm = 0x2FFF0000
movl $0x2fff0000, %edx       # imm = 0x2FFF0000
callq 0x6450
movq %rax, 0x65e8d1(%rip)    # 0x6cd2c0
testq %rax, %rax
jne 0x6ea29
callq 0x6b40
movq %r12, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x25221
cmpl $0x0, (%r14)
jle 0x6efa3
movq %r13, %rdi
xorl %eax, %eax
callq 0x25221
movl $0x0, (%r14)
movq %rbp, %rax
jmp 0x6e91f
leaq 0x35520(%rip), %rax     # 0xa3f50
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 0x65e84e(%rip), %rdi    # 0x6cd2c0
movq %rax, %rsi
callq 0x6950
movq %r15, %rdi
callq 0x6350
cmpl $0x8, 0x10(%rsp)
jne 0x6eb47
movslq 0xc(%rsp), %rsi
movq %rsi, %r15
andq $-0x4, %r15
addq $0x4, %r15
cmpl $0x0, (%rbp)
je 0x6eb80
cmpb $0x1, 0x65e922(%rip)    # 0x6cd3d0
jne 0x6ec15
leaq 0xff47(%rip), %rdi      # 0x7ea02
leaq 0xff59(%rip), %rsi      # 0x7ea1b
callq 0x6840
movq 0x65e7f2(%rip), %rdi    # 0x6cd2c0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x4, %edx
callq 0x6400
movq %rax, 0x66051e(%rip)    # 0x6cf008
testq %rax, %rax
je 0x6ef75
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xff, %r8d
callq 0x69d0
movq 0x6604fa(%rip), %rdi    # 0x6cf008
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r8d
movl $0x16762004, %esi       # imm = 0x16762004
movl $0x1, %edx
callq 0x63e0
movq %rax, 0x6604e3(%rip)    # 0x6cf010
testq %rax, %rax
jne 0x6ec4c
callq 0x6b40
leaq 0xfefe(%rip), %rdi      # 0x7ea40
jmp 0x6ef99
movq 0x65e772(%rip), %rdi    # 0x6cd2c0
callq 0x6770
movq %rax, 0x6604c6(%rip)    # 0x6cf020
testq %rax, %rax
je 0x6ec04
callq 0x4deb0
testl %eax, %eax
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
je 0x6edd9
callq 0x6df6f
jmp 0x6efa9
movl %esi, 0x1c(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x65e72d(%rip), %rdi    # 0x6cd2c0
callq 0x6770
movq %rax, 0x660481(%rip)    # 0x6cf020
testq %rax, %rax
je 0x6ef25
callq 0x4deb0
testl %eax, %eax
jne 0x6ef3b
movq 0x65e704(%rip), %rdi    # 0x6cd2c0
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 0x660440(%rip), %rdi    # 0x6cf028
movl %r15d, %ecx
callq 0x4f6b6
testl %eax, %eax
js 0x6ef3b
cmpl $0x0, (%rbp)
jne 0x6ef42
jmp 0x6ec4c
callq 0x6b40
leaq 0xfe68(%rip), %rdi      # 0x7ea78
jmp 0x6ef99
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, 0x6603d5(%rip)    # 0x6cf018
testq %rax, %rax
je 0x6ef8d
movslq 0x8(%rsp), %r12
imulq %r15, %r12
movq %r12, %rdi
callq 0x69a0
movq %rax, 0x660404(%rip)    # 0x6cf068
testq %rax, %rax
je 0x6ef67
leaq 0x10f43c(%rip), %rcx    # 0x17e0b0
movq %rax, (%rcx)
leaq 0x10f606(%rip), %r14    # 0x17e284
movl %r15d, (%r14)
leaq 0x10f600(%rip), %rax    # 0x17e288
movl %r12d, (%rax)
leaq 0x3b41ee(%rip), %rax    # 0x422e80
movl $0x1, (%rax)
movl %r15d, %edi
callq 0x6fa48
movslq 0x8(%rsp), %rax
testq %rax, %rax
leaq 0x35381(%rip), %r15     # 0xa4030
js 0x6ee14
movl (%r14), %ecx
movq %rax, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %eax
andl $0x1fffffff, %eax       # imm = 0x1FFFFFFF
shlq $0x4, %rax
addq $0x10, %rax
leal (%rcx,%rcx,2), %edx
leal (,%rcx,4), %esi
leal (%rcx,%rcx), %edi
movdqa 0x53e9(%rip), %xmm1     # 0x740d0
movdqa 0x53f1(%rip), %xmm2     # 0x740e0
xorl %r8d, %r8d
movdqa 0x53f6(%rip), %xmm3     # 0x740f0
pxor %xmm3, %xmm0
leaq 0x3a7f9b(%rip), %r9     # 0x416ca0
pcmpeqd %xmm4, %xmm4
movdqa 0x838f(%rip), %xmm5     # 0x770a0
xorl %r10d, %r10d
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %r11d
notl %r11d
testb $0x1, %r11b
je 0x6ed4f
movl %r8d, (%r9,%r10)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %r11d
testb $0x1, %r11b
je 0x6ed68
leal (%rcx,%r8), %r11d
movl %r11d, 0x4(%r9,%r10)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %r11d
testb $0x1, %r11b
je 0x6edaa
leal (%rdi,%r8), %r11d
movl %r11d, 0x8(%r9,%r10)
pextrw $0x4, %xmm6, %r11d
testb $0x1, %r11b
je 0x6edbf
leal (%rdx,%r8), %r11d
movl %r11d, 0xc(%r9,%r10)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %r10
addl %esi, %r8d
cmpq %r10, %rax
jne 0x6ed14
jmp 0x6ee14
callq 0x50163
leaq 0x10f2cb(%rip), %rax    # 0x17e0b0
movq $0x0, (%rax)
leaq 0x10f491(%rip), %rax    # 0x17e284
xorl %ecx, %ecx
movl %ecx, (%rax)
leaq 0x10f48a(%rip), %rax    # 0x17e288
movl %ecx, (%rax)
leaq 0x3b4079(%rip), %rax    # 0x422e80
movl $0x7f, (%rax)
leaq 0x3521c(%rip), %r15     # 0xa4030
movq 0x65e4a5(%rip), %rdi    # 0x6cd2c0
callq 0x67a0
movl 0xc(%rsp), %eax
movl %eax, (%r15)
movl 0x8(%rsp), %eax
leaq 0x35202(%rip), %r14     # 0xa4034
movl %eax, (%r14)
leaq 0x10f268(%rip), %rax    # 0x17e0a4
movl 0x10(%rsp), %ecx
movl %ecx, (%rax)
leaq 0x10f25f(%rip), %rax    # 0x17e0a8
movl %ebx, (%rax)
cmpq $0x0, 0x6601cd(%rip)    # 0x6cf020
je 0x6ee73
leaq 0x351cc(%rip), %rax     # 0xa4028
movl (%rax), %edi
callq 0x6580
testl %eax, %eax
jns 0x6ee73
leaq 0xfc71(%rip), %rdi      # 0x7eadf
callq 0x32d18
cmpl $0x8, 0x10(%rsp)
jne 0x6eeb4
cmpl $0x0, (%rbp)
jne 0x6eeb4
movq 0x65e439(%rip), %rdi    # 0x6cd2c0
leaq 0x24(%rsp), %rbx
leaq 0x20(%rsp), %r14
movq %rbx, %rsi
movq %r14, %rdx
callq 0x69b0
movl (%rbx), %esi
movl (%r14), %edx
leaq 0x3518c(%rip), %r14     # 0xa4034
leaq 0x660179(%rip), %rdi    # 0x6cf028
callq 0x4fada
leaq 0x10f1fe(%rip), %rax    # 0x17e0b9
movb $0x0, (%rax)
leaq 0x10f203(%rip), %rax    # 0x17e0c8
movq (%rax), %rax
testq %rax, %rax
je 0x6eecf
callq *%rax
movl (%r15), %edi
movl (%r14), %esi
callq 0x4c7f6
leaq 0x149b5e(%rip), %rax    # 0x1b8a3f
movzbl (%rax), %edi
movzbl 0x1(%rax), %esi
movzbl 0x2(%rax), %edx
leaq 0x149b50(%rip), %rax    # 0x1b8a43
movzbl (%rax), %ecx
callq 0x3c39a
cmpb $0x0, 0x17(%rsp)
je 0x6ef0c
movl $0x1, %edi
callq 0x6e2a9
callq 0x1e58e
movl $0x303, %edi            # imm = 0x303
movl $0x1, %esi
callq 0x63b0
jmp 0x6e874
callq 0x6b40
leaq 0xfb47(%rip), %rdi      # 0x7ea78
movq %rax, %rsi
xorl %eax, %eax
callq 0x25221
movl $0x1, (%rbp)
leaq 0xfb52(%rip), %rdi      # 0x7ea9b
callq 0x32d18
movl 0xc(%rsp), %edi
movl 0x8(%rsp), %esi
movl $0x8, %edx
movl %ebx, %ecx
callq 0x6e817
movl %eax, %r14d
jmp 0x6efa9
leaq 0xfb51(%rip), %rdi      # 0x7eabf
callq 0x32d18
jmp 0x6efa3
callq 0x6b40
leaq 0xfaa2(%rip), %rdi      # 0x7ea23
movq %rax, %rsi
xorl %eax, %eax
callq 0x25221
jmp 0x6efa9
callq 0x6b40
leaq 0xfac3(%rip), %rdi      # 0x7ea5c
movq %rax, %rsi
xorl %eax, %eax
callq 0x25221
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq