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

	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

		if (fs & 1) {
			if (c > 8) flags |= SDL_WINDOW_FULLSCREEN;
			else flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
			flags |= SDL_WINDOW_ALLOW_HIGHDPI;
		}

		sdl_window = SDL_CreateWindow(wintitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, x, y, 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
#ifdef SDLAYER_USE_RENDERER
			// 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_RenderSetLogicalSize(sdl_renderer, x, y);
			SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 255);
			SDL_RenderClear(sdl_renderer);

			sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, 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, 8, 0, 0, 0, 0);
			if (!sdl_surface) {
				buildprintf("Error creating surface: %s\n", SDL_GetError());
				return -1;
			}
#endif
#if USE_OPENGL
		} else {
			// Prepare the GLSL shader for 8-bit blitting.
			int winx = x, winy = y;
			if (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) {
				SDL_DisplayMode d;
				if (SDL_GetWindowDisplayMode(sdl_window, &d) == 0)
					winx = d.w, winy = d.h;
			}
			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 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");
		}
	}
#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
movq %rsp, %rbp
subq $0x70, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x14(%rbp), %eax
leaq 0x114b43(%rip), %rcx    # 0x1bac08
cmpl (%rcx), %eax
jne 0xa6122
movl -0x8(%rbp), %eax
leaq 0x3a321(%rip), %rcx     # 0xe03f4
cmpl (%rcx), %eax
jne 0xa6122
movl -0xc(%rbp), %eax
leaq 0x3a317(%rip), %rcx     # 0xe03f8
cmpl (%rcx), %eax
jne 0xa6122
movl -0x10(%rbp), %eax
leaq 0x114b15(%rip), %rcx    # 0x1bac04
cmpl (%rcx), %eax
jne 0xa6122
leaq 0x114b1f(%rip), %rax    # 0x1bac19
cmpb $0x0, (%rax)
jne 0xa6122
leaq 0x3a2ee(%rip), %rax     # 0xe03f4
movl (%rax), %edi
leaq 0x3a2e9(%rip), %rax     # 0xe03f8
movl (%rax), %esi
callq 0x6df30
movl $0x0, -0x4(%rbp)
jmp 0xa6727
movl -0x10(%rbp), %edx
movl -0x14(%rbp), %ecx
leaq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rsi
xorl %r8d, %r8d
callq 0x2dbd0
cmpl $0x0, %eax
jge 0xa6149
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa6727
cmpb $0x0, 0x61a158(%rip)    # 0x6c02a8
je 0xa6160
movl $0x1, -0x18(%rbp)
xorl %edi, %edi
callq 0xa5460
leaq 0x114ab9(%rip), %rax    # 0x1bac20
cmpq $0x0, (%rax)
je 0xa6176
leaq 0x114aac(%rip), %rax    # 0x1bac20
callq *(%rax)
callq 0xa5020
movl -0x8(%rbp), %esi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
movl -0x14(%rbp), %edi
andl $0x1, %edi
leaq 0xbcab(%rip), %r8       # 0xb1e3c
leaq 0xadc3(%rip), %rax      # 0xb0f5b
cmpl $0x0, %edi
cmovneq %rax, %r8
leaq 0x1467f(%rip), %rdi     # 0xba825
movb $0x0, %al
callq 0x37c90
movl $0x8, -0x1c(%rbp)
leaq 0x114a8d(%rip), %rax    # 0x1bac48
cmpl $0x0, (%rax)
jne 0xa6269
movl $0x15, %edi
movl $0x2, %esi
callq 0x6a50
movl $0x11, %edi
movl $0x2, %esi
callq 0x6a50
movl $0x12, %edi
movl $0x1, %esi
callq 0x6a50
movl $0xf, %edi
movl $0x1, %esi
callq 0x6a50
movl $0x5, %edi
movl $0x1, %esi
callq 0x6a50
leaq 0x547746(%rip), %rax    # 0x5ed95c
cmpl $0x0, (%rax)
setg %al
andb $0x1, %al
movzbl %al, %esi
movl $0xd, %edi
callq 0x6a50
leaq 0x54772a(%rip), %rax    # 0x5ed95c
cmpl $0x0, (%rax)
je 0xa624c
leaq 0x54771e(%rip), %rax    # 0x5ed95c
movl (%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x5c(%rbp)
jmp 0xa6253
xorl %eax, %eax
movl %eax, -0x5c(%rbp)
jmp 0xa6253
movl -0x5c(%rbp), %esi
movl $0xe, %edi
callq 0x6a50
movl -0x1c(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x1c(%rbp)
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xa629b
cmpl $0x8, -0x10(%rbp)
jle 0xa6285
movl -0x1c(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xa6290
movl -0x1c(%rbp), %eax
orl $0x1001, %eax           # imm = 0x1001
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
orl $0x2000, %eax           # imm = 0x2000
movl %eax, -0x1c(%rbp)
movl -0x8(%rbp), %ecx
movl -0xc(%rbp), %r8d
movl -0x1c(%rbp), %r9d
leaq 0x61a003(%rip), %rdi    # 0x6c02b0
movl $0x2fff0000, %edx       # imm = 0x2FFF0000
movl %edx, %esi
callq 0x6400
movq %rax, 0x619fe0(%rip)    # 0x6c02a0
cmpq $0x0, 0x619fd8(%rip)    # 0x6c02a0
jne 0xa6315
callq 0x6a60
movq %rax, %rsi
leaq 0x14572(%rip), %rdi     # 0xba84b
movb $0x0, %al
callq 0x37c90
leaq 0x547675(%rip), %rax    # 0x5ed95c
cmpl $0x0, (%rax)
jle 0xa6309
leaq 0x14573(%rip), %rdi     # 0xba866
movb $0x0, %al
callq 0x37c90
leaq 0x54765b(%rip), %rax    # 0x5ed95c
movl $0x0, (%rax)
jmp 0xa6317
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa6727
jmp 0xa6323
movb $0x1, %al
testb $0x1, %al
jne 0xa61ad
jmp 0xa6323
callq 0xa6730
movq %rax, -0x28(%rbp)
movq 0x619f6d(%rip), %rdi    # 0x6c02a0
movq -0x28(%rbp), %rsi
callq 0x68b0
movq -0x28(%rbp), %rdi
callq 0x6320
cmpl $0x8, -0x10(%rbp)
jne 0xa6593
movl -0x8(%rbp), %eax
orl $0x1, %eax
addl $0x4, %eax
andl $-0x4, %eax
movl %eax, -0x34(%rbp)
leaq 0x1148e3(%rip), %rax    # 0x1bac48
cmpl $0x0, (%rax)
je 0xa63ca
movl -0x8(%rbp), %esi
movl -0xc(%rbp), %edx
xorl %r9d, %r9d
movl $0x8, %ecx
movl %r9d, %edi
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x6850
movq %rax, 0x61bc3f(%rip)    # 0x6c1fd8
cmpq $0x0, 0x61bc37(%rip)    # 0x6c1fd8
jne 0xa63c5
callq 0x6a60
movq %rax, %rsi
leaq 0x144d5(%rip), %rdi     # 0xba887
movb $0x0, %al
callq 0x37c90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa6727
jmp 0xa64c7
movl -0x8(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
andl $0x1001, %eax           # imm = 0x1001
cmpl $0x0, %eax
je 0xa6406
movq 0x619eb6(%rip), %rdi    # 0x6c02a0
leaq -0x58(%rbp), %rsi
callq 0x6410
cmpl $0x0, %eax
jne 0xa6404
movl -0x54(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0xa6406
movq 0x619e93(%rip), %rdi    # 0x6c02a0
callq 0x66d0
movq %rax, 0x61bbc7(%rip)    # 0x6c1fe0
cmpq $0x0, 0x61bbbf(%rip)    # 0x6c1fe0
jne 0xa6448
callq 0x6a60
movq %rax, %rsi
leaq 0x14471(%rip), %rdi     # 0xba8a3
movb $0x0, %al
callq 0x37c90
leaq 0x114808(%rip), %rax    # 0x1bac48
movl $0x1, (%rax)
jmp 0xa6494
callq 0x703b0
cmpl $0x0, %eax
je 0xa6461
leaq 0x1147ef(%rip), %rax    # 0x1bac48
movl $0x1, (%rax)
jmp 0xa6492
movl -0x8(%rbp), %esi
movl -0xc(%rbp), %edx
movl -0x34(%rbp), %ecx
movl -0x38(%rbp), %r8d
movl -0x3c(%rbp), %r9d
leaq 0x61bb6f(%rip), %rdi    # 0x6c1fe8
callq 0x715d0
cmpl $0x0, %eax
jge 0xa6490
leaq 0x1147be(%rip), %rax    # 0x1bac48
movl $0x1, (%rax)
jmp 0xa6492
jmp 0xa6494
leaq 0x1147ad(%rip), %rax    # 0x1bac48
cmpl $0x0, (%rax)
je 0xa64c5
leaq 0x1441f(%rip), %rdi     # 0xba8c6
callq 0x4a720
movl -0x8(%rbp), %edi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movl -0x14(%rbp), %ecx
callq 0xa60a0
movl %eax, -0x4(%rbp)
jmp 0xa6727
jmp 0xa64c7
movl -0x34(%rbp), %eax
imull -0xc(%rbp), %eax
movslq %eax, %rdi
callq 0x68f0
movq %rax, 0x61bb33(%rip)    # 0x6c2010
cmpq $0x0, 0x61bb2b(%rip)    # 0x6c2010
jne 0xa64ff
leaq 0x143fc(%rip), %rdi     # 0xba8ea
callq 0x4a720
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa6727
movq 0x61bb0a(%rip), %rcx    # 0x6c2010
leaq 0x114703(%rip), %rax    # 0x1bac10
movq %rcx, (%rax)
movl -0x34(%rbp), %ecx
leaq 0x1148ca(%rip), %rax    # 0x1bade4
movl %ecx, (%rax)
leaq 0x1148c1(%rip), %rax    # 0x1bade4
movl (%rax), %ecx
imull -0xc(%rbp), %ecx
leaq 0x1148b8(%rip), %rax    # 0x1bade8
movl %ecx, (%rax)
leaq 0x3b7187(%rip), %rax    # 0x45d6c0
movl $0x1, (%rax)
leaq 0x11489e(%rip), %rax    # 0x1bade4
movl (%rax), %edi
callq 0xa7550
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jg 0xa658e
movl -0x30(%rbp), %edx
movslq -0x2c(%rbp), %rcx
leaq 0x3ab50f(%rip), %rax    # 0x451a80
movl %edx, (%rax,%rcx,4)
leaq 0x114869(%rip), %rax    # 0x1bade4
movl (%rax), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xa655b
jmp 0xa6627
movq 0x619d06(%rip), %rdi    # 0x6c02a0
callq 0x66d0
movq %rax, 0x61ba3a(%rip)    # 0x6c1fe0
cmpq $0x0, 0x61ba32(%rip)    # 0x6c1fe0
jne 0xa65d2
callq 0x6a60
movq %rax, %rsi
leaq 0x142e4(%rip), %rdi     # 0xba8a3
movb $0x0, %al
callq 0x37c90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa6727
callq 0x703b0
cmpl $0x0, %eax
je 0xa65ed
callq 0xa5020
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa6727
callq 0x72930
leaq 0x114617(%rip), %rax    # 0x1bac10
movq $0x0, (%rax)
leaq 0x1147dd(%rip), %rax    # 0x1bade4
movl $0x0, (%rax)
leaq 0x1147d4(%rip), %rax    # 0x1bade8
movl $0x0, (%rax)
leaq 0x3b709f(%rip), %rax    # 0x45d6c0
movl $0x7f, (%rax)
movq 0x619c72(%rip), %rdi    # 0x6c02a0
callq 0x6700
movl -0x8(%rbp), %ecx
leaq 0x39db7(%rip), %rax     # 0xe03f4
movl %ecx, (%rax)
movl -0xc(%rbp), %ecx
leaq 0x39daf(%rip), %rax     # 0xe03f8
movl %ecx, (%rax)
movl -0x10(%rbp), %ecx
leaq 0x1145af(%rip), %rax    # 0x1bac04
movl %ecx, (%rax)
movl -0x14(%rbp), %ecx
leaq 0x1145a7(%rip), %rax    # 0x1bac08
movl %ecx, (%rax)
cmpq $0x0, 0x61b975(%rip)    # 0x6c1fe0
je 0xa668e
leaq 0x39d78(%rip), %rax     # 0xe03ec
movl (%rax), %edi
callq 0x6520
cmpl $0x0, %eax
jge 0xa668c
leaq 0x14283(%rip), %rdi     # 0xba90a
callq 0x4a720
jmp 0xa668e
leaq 0x114584(%rip), %rax    # 0x1bac19
movb $0x0, (%rax)
leaq 0x114589(%rip), %rax    # 0x1bac28
cmpq $0x0, (%rax)
je 0xa66ae
leaq 0x11457c(%rip), %rax    # 0x1bac28
callq *(%rax)
leaq 0x39d3f(%rip), %rax     # 0xe03f4
movl (%rax), %edi
leaq 0x39d3a(%rip), %rax     # 0xe03f8
movl (%rax), %esi
callq 0x6df30
leaq 0x14eed3(%rip), %rax    # 0x1f559f
movb (%rax), %sil
leaq 0x14eec9(%rip), %rax    # 0x1f559f
movb 0x1(%rax), %dl
leaq 0x14eebf(%rip), %rax    # 0x1f559f
movb 0x2(%rax), %cl
leaq 0x14eeb9(%rip), %rax    # 0x1f55a3
movzbl %sil, %edi
movzbl %dl, %esi
movzbl %cl, %edx
movzbl (%rax), %ecx
callq 0x57bd0
cmpl $0x0, -0x18(%rbp)
je 0xa670c
movl $0x1, %edi
callq 0xa5460
callq 0x2ce40
movl $0x303, %edi            # imm = 0x303
movl $0x1, %esi
callq 0x6380
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq