// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/baselayer.c
// Function: checkvideomode
int checkvideomode(int *x, int *y, int c, int fs, int forced)
{
	int i, nearest=-1, dx, dy, odx=INT_MAX, ody=INT_MAX;

	getvalidmodes();

#if USE_OPENGL
	if (c > 8 && glunavailable) return -1;
#else
	if (c > 8) return -1;
#endif

	// fix up the passed resolution values to be multiples of 8
	// and at least 320x200 or at most MAXXDIMxMAXYDIM
	if (*x < 320) *x = 320;
	if (*y < 200) *y = 200;
	if (*x > MAXXDIM) *x = MAXXDIM;
	if (*y > MAXYDIM) *y = MAXYDIM;
	*x &= 0xfffffff8l;

	for (i=0; i<validmodecnt; i++) {
		if (validmode[i].bpp != c) continue;
		if (validmode[i].fs != fs) continue;
		dx = abs(validmode[i].xdim - *x);
		dy = abs(validmode[i].ydim - *y);
		if (!(dx | dy)) { 	// perfect match
			nearest = i;
			break;
		}
		if ((dx <= odx) && (dy <= ody)) {
			nearest = i;
			odx = dx; ody = dy;
		}
	}

#ifdef ANY_WINDOWED_SIZE
	if (!forced && (fs&1) == 0 && (nearest < 0 || validmode[nearest].xdim!=*x || validmode[nearest].ydim!=*y)) {
		// check the colour depth is recognised at the very least
		for (i=0;i<validmodecnt;i++)
			if (validmode[i].bpp == c)
				return 0x7fffffffl;
		return -1;	// strange colour depth
	}
#endif

	if (nearest < 0) {
		// no mode that will match (eg. if no fullscreen modes)
		return -1;
	}

	*x = validmode[nearest].xdim;
	*y = validmode[nearest].ydim;

	return nearest;		// JBF 20031206: Returns the mode number
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, 0x4(%rsp)
movl %ecx, %r12d
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
callq 0x6e442
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x9, %ebx
jl 0x1eef7
cmpl $0x0, 0x15f1f7(%rip)    # 0x17e0e8
jne 0x1f07b
movl (%r15), %ecx
cmpl $0x13f, %ecx            # imm = 0x13F
jg 0x1ef0e
movl $0x140, (%r15)          # imm = 0x140
movl $0x140, %ecx            # imm = 0x140
movl (%r14), %edx
cmpl $0xc7, %edx
jg 0x1ef28
movl $0xc8, (%r14)
movl (%r15), %ecx
movl $0xc8, %edx
cmpl $0xf01, %ecx            # imm = 0xF01
jl 0x1ef3f
movl $0xf00, (%r15)          # imm = 0xF00
movl (%r14), %edx
movl $0xf00, %ecx            # imm = 0xF00
cmpl $0x871, %edx            # imm = 0x871
jl 0x1ef51
movl $0x870, (%r14)          # imm = 0x870
movl (%r15), %ecx
andl $-0x8, %ecx
movl %ecx, (%r15)
leaq 0x33e952(%rip), %rdx    # 0x35d8b0
movslq (%rdx), %rsi
testq %rsi, %rsi
jle 0x1f07b
leaq 0x41cedf(%rip), %rax    # 0x43be50
leaq 0x9(%rax), %rdi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %r8d, %r8d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movzbl -0x1(%rdi), %r11d
cmpl %ebx, %r11d
jne 0x1efe0
movzbl (%rdi), %r11d
cmpl %r12d, %r11d
jne 0x1efe0
movl -0x9(%rdi), %r13d
movl -0x5(%rdi), %edx
subl %ecx, %r13d
movl %r13d, %r11d
negl %r11d
cmovsl %r13d, %r11d
subl (%r14), %edx
movl %edx, %r13d
negl %r13d
cmovsl %edx, %r13d
movl %r13d, %edx
orl %r11d, %edx
je 0x1efee
cmpl %r10d, %r11d
setg %dl
cmpl %r9d, %r13d
setg %bpl
orb %dl, %bpl
jne 0x1efe0
movl %r8d, %eax
movl %r11d, %r10d
movl %r13d, %r9d
incq %r8
addq $0x10, %rdi
cmpq %r8, %rsi
jne 0x1ef89
jmp 0x1eff1
movl %r8d, %eax
andl $0x1, %r12d
orl 0x4(%rsp), %r12d
je 0x1f025
testl %eax, %eax
leaq 0x41ce4b(%rip), %r9     # 0x43be50
js 0x1f06f
movl %eax, %edi
movq %rdi, %rcx
shlq $0x4, %rcx
movl (%r9,%rcx), %ecx
movl %ecx, (%r15)
shlq $0x4, %rdi
movl 0x4(%r9,%rdi), %ecx
movl %ecx, (%r14)
jmp 0x1f07b
testl %eax, %eax
leaq 0x41ce22(%rip), %r9     # 0x43be50
js 0x1f04b
movl %eax, %edi
movq %rdi, %r8
shlq $0x4, %r8
cmpl %ecx, (%r9,%r8)
jne 0x1f04b
addq %r9, %r8
movl 0x4(%r8), %edx
cmpl (%r14), %edx
je 0x1f014
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %esi, %esi
jle 0x1f07b
shlq $0x4, %rsi
xorl %ecx, %ecx
movzbl 0x8(%r9,%rcx), %edx
cmpl %ebx, %edx
je 0x1f076
addq $0x10, %rcx
cmpq %rcx, %rsi
jne 0x1f05a
jmp 0x1f07b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1f07b
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq