// File: /workspace/github_c_makefile_star2_O1/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 0x6c714
cmpl $0x9, %ebx
jl 0x1ea7b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x0, 0x15e673(%rip)    # 0x17d0e8
jne 0x1ebe6
cmpl $0x13f, (%r15)          # imm = 0x13F
jg 0x1ea8b
movl $0x140, (%r15)          # imm = 0x140
cmpl $0xc7, (%r14)
jg 0x1ea9b
movl $0xc8, (%r14)
cmpl $0xf01, (%r15)          # imm = 0xF01
jl 0x1eaab
movl $0xf00, (%r15)          # imm = 0xF00
cmpl $0x871, (%r14)          # imm = 0x871
jl 0x1eabb
movl $0x870, (%r14)          # imm = 0x870
movl (%r15), %edx
andl $-0x8, %edx
movl %edx, (%r15)
leaq 0x33dde5(%rip), %rax    # 0x35c8b0
movslq (%rax), %rcx
testq %rcx, %rcx
jle 0x1eb5b
leaq 0x41c372(%rip), %rsi    # 0x43ae50
addq $0x9, %rsi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %edi, %edi
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movzbl -0x1(%rsi), %r10d
cmpl %ebx, %r10d
jne 0x1eb4d
movzbl (%rsi), %r10d
cmpl %r12d, %r10d
jne 0x1eb4d
movl -0x9(%rsi), %r11d
movl -0x5(%rsi), %r13d
subl %edx, %r11d
movl %r11d, %r10d
negl %r10d
cmovsl %r11d, %r10d
subl (%r14), %r13d
movl %r13d, %r11d
negl %r11d
cmovsl %r13d, %r11d
movl %r11d, %r13d
orl %r10d, %r13d
je 0x1eb62
cmpl %r9d, %r10d
setg %r13b
cmpl %r8d, %r11d
setg %bpl
orb %r13b, %bpl
jne 0x1eb4d
movl %edi, %eax
movl %r10d, %r9d
movl %r11d, %r8d
incq %rdi
addq $0x10, %rsi
cmpq %rdi, %rcx
jne 0x1eaf5
jmp 0x1eb64
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1eb64
movl %edi, %eax
andl $0x1, %r12d
orl 0x4(%rsp), %r12d
je 0x1eb8f
testl %eax, %eax
js 0x1ebda
movl %eax, %ecx
shlq $0x4, %rcx
leaq 0x41c2d0(%rip), %rdx    # 0x43ae50
movl (%rdx,%rcx), %esi
movl %esi, (%r15)
movl 0x4(%rdx,%rcx), %ecx
movl %ecx, (%r14)
jmp 0x1ebe6
testl %eax, %eax
js 0x1ebb0
movl %eax, %esi
shlq $0x4, %rsi
leaq 0x41c2b0(%rip), %rdi    # 0x43ae50
cmpl %edx, (%rdi,%rsi)
jne 0x1ebb0
addq %rsi, %rdi
movl 0x4(%rdi), %edx
cmpl (%r14), %edx
je 0x1eb6f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jle 0x1ebe6
shlq $0x4, %rcx
xorl %edx, %edx
leaq 0x41c28a(%rip), %rsi    # 0x43ae50
movzbl 0x8(%rsi,%rdx), %edi
cmpl %ebx, %edi
je 0x1ebe1
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x1ebc6
jmp 0x1ebe6
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1ebe6
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq