// File: /workspace/github_c_makefile_star2_O2/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 0x62cee
cmpl $0x9, %ebx
jl 0x1c912
cmpl $0x0, 0x15a7e0(%rip)    # 0x1770e8
je 0x1c912
pushq $-0x1
popq %rax
jmp 0x1ca80
movl (%r15), %ecx
cmpl $0x13f, %ecx            # imm = 0x13F
jg 0x1c929
movl $0x140, (%r15)          # imm = 0x140
movl $0x140, %ecx            # imm = 0x140
movl (%r14), %eax
cmpl $0xc7, %eax
jg 0x1c942
movl $0xc8, (%r14)
movl (%r15), %ecx
movl $0xc8, %eax
cmpl $0xf01, %ecx            # imm = 0xF01
jl 0x1c959
movl $0xf00, (%r15)          # imm = 0xF00
movl (%r14), %eax
movl $0xf00, %ecx            # imm = 0xF00
cmpl $0x871, %eax            # imm = 0x871
jl 0x1c96a
movl $0x870, (%r14)          # imm = 0x870
movl (%r15), %ecx
andl $-0x8, %ecx
movl %ecx, (%r15)
leaq 0x339f39(%rip), %rax    # 0x3568b0
movl (%rax), %esi
xorl %edi, %edi
testl %esi, %esi
cmovlel %edi, %esi
leaq 0x4184c9(%rip), %rax    # 0x434e50
leaq 0x9(%rax), %r8
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
pushq $-0x1
popq %rax
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
cmpq %rdi, %rsi
je 0x1ca01
movzbl -0x1(%r8), %r11d
cmpl %ebx, %r11d
jne 0x1c9f6
movzbl (%r8), %r11d
cmpl %r12d, %r11d
jne 0x1c9f6
movl -0x9(%r8), %r13d
movl -0x5(%r8), %ebp
subl %ecx, %r13d
movl %r13d, %r11d
negl %r11d
cmovsl %r13d, %r11d
subl (%r14), %ebp
movl %ebp, %r13d
negl %r13d
cmovsl %ebp, %r13d
movl %r13d, %ebp
orl %r11d, %ebp
je 0x1c9ff
cmpl %r9d, %r11d
setg %bpl
cmpl %r10d, %r13d
setg %dl
orb %bpl, %dl
jne 0x1c9f6
movl %edi, %eax
movl %r11d, %r9d
movl %r13d, %r10d
incq %rdi
addq $0x10, %r8
jmp 0x1c99a
movl %edi, %eax
andl $0x1, %r12d
orl 0x4(%rsp), %r12d
leaq 0x41843f(%rip), %r9     # 0x434e50
je 0x1ca39
testl %eax, %eax
js 0x1c90a
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 0x1ca80
testl %eax, %eax
js 0x1ca58
movl %eax, %edi
movq %rdi, %r8
shlq $0x4, %r8
cmpl %ecx, (%r9,%r8)
jne 0x1ca58
addq %r9, %r8
movl 0x4(%r8), %edx
cmpl (%r14), %edx
je 0x1ca28
shlq $0x4, %rsi
addq $0x10, %rsi
xorl %ecx, %ecx
pushq $-0x1
popq %rax
leaq 0x10(%rcx), %rdi
cmpq %rdi, %rsi
je 0x1ca80
movzbl 0x8(%r9,%rcx), %edx
movq %rdi, %rcx
cmpl %ebx, %edx
jne 0x1ca65
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq