// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0x7fffffff, -0x38(%rbp) # imm = 0x7FFFFFFF
movl $0x7fffffff, -0x3c(%rbp) # imm = 0x7FFFFFFF
callq 0xa5720
cmpl $0x8, -0x1c(%rbp)
jle 0x2dc1f
cmpl $0x0, 0x18d037(%rip)    # 0x1bac48
je 0x2dc1f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2de65
movq -0x10(%rbp), %rax
cmpl $0x140, (%rax)          # imm = 0x140
jge 0x2dc35
movq -0x10(%rbp), %rax
movl $0x140, (%rax)          # imm = 0x140
movq -0x18(%rbp), %rax
cmpl $0xc8, (%rax)
jge 0x2dc4b
movq -0x18(%rbp), %rax
movl $0xc8, (%rax)
movq -0x10(%rbp), %rax
cmpl $0xb40, (%rax)          # imm = 0xB40
jle 0x2dc61
movq -0x10(%rbp), %rax
movl $0xb40, (%rax)          # imm = 0xB40
movq -0x18(%rbp), %rax
cmpl $0x708, (%rax)          # imm = 0x708
jle 0x2dc77
movq -0x18(%rbp), %rax
movl $0x708, (%rax)          # imm = 0x708
movq -0x10(%rbp), %rax
movslq (%rax), %rcx
movabsq $0xfffffff8, %rdx       # imm = 0xFFFFFFF8
andq %rdx, %rcx
movl %ecx, (%rax)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
leaq 0x36c772(%rip), %rcx    # 0x39a410
cmpl (%rcx), %eax
jge 0x2dd74
movslq -0x28(%rbp), %rcx
leaq 0x447aff(%rip), %rax    # 0x4757b0
shlq $0x4, %rcx
addq %rcx, %rax
movzbl 0x8(%rax), %eax
cmpl -0x1c(%rbp), %eax
je 0x2dcc6
jmp 0x2dd66
movslq -0x28(%rbp), %rcx
leaq 0x447adf(%rip), %rax    # 0x4757b0
shlq $0x4, %rcx
addq %rcx, %rax
movzbl 0x9(%rax), %eax
cmpl -0x20(%rbp), %eax
je 0x2dce6
jmp 0x2dd66
movslq -0x28(%rbp), %rcx
leaq 0x447abf(%rip), %rax    # 0x4757b0
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
subl (%rcx), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x30(%rbp)
movslq -0x28(%rbp), %rcx
leaq 0x447a9b(%rip), %rax    # 0x4757b0
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
subl (%rcx), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
orl -0x34(%rbp), %eax
cmpl $0x0, %eax
jne 0x2dd42
movl -0x28(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x2dd74
movl -0x30(%rbp), %eax
cmpl -0x38(%rbp), %eax
jg 0x2dd64
movl -0x34(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jg 0x2dd64
movl -0x28(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x2dd66
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x2dc94
cmpl $0x0, -0x24(%rbp)
jne 0x2de1b
movl -0x20(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x2de1b
cmpl $0x0, -0x2c(%rbp)
jl 0x2ddcc
movslq -0x2c(%rbp), %rcx
leaq 0x447a12(%rip), %rax    # 0x4757b0
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x2ddcc
movslq -0x2c(%rbp), %rcx
leaq 0x4479f6(%rip), %rax    # 0x4757b0
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0x2de1b
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
leaq 0x36c633(%rip), %rcx    # 0x39a410
cmpl (%rcx), %eax
jge 0x2de12
movslq -0x28(%rbp), %rcx
leaq 0x4479c4(%rip), %rax    # 0x4757b0
shlq $0x4, %rcx
addq %rcx, %rax
movzbl 0x8(%rax), %eax
cmpl -0x1c(%rbp), %eax
jne 0x2de05
movl $0x7fffffff, -0x4(%rbp) # imm = 0x7FFFFFFF
jmp 0x2de65
jmp 0x2de07
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x2ddd3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2de65
cmpl $0x0, -0x2c(%rbp)
jge 0x2de2a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2de65
movslq -0x2c(%rbp), %rcx
leaq 0x44797b(%rip), %rax    # 0x4757b0
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movslq -0x2c(%rbp), %rcx
leaq 0x447961(%rip), %rax    # 0x4757b0
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop