// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: setgamemode
int setgamemode(char davidoption, int daxdim, int daydim, int dabpp)
{
	int i, j;

	if ((qsetmode == 200) && (videomodereset == 0) &&
	    (davidoption == fullscreen) && (xdim == daxdim) && (ydim == daydim) && (bpp == dabpp))
		return(0);

	strcpy(kensmessage,"!!!! BUILD engine&tools programmed by Ken Silverman of E.G. RI.  (c) Copyright 1995 Ken Silverman.  Summary:  BUILD = Ken. !!!!");

#if USE_POLYMOST && USE_OPENGL
	int oldbpp = bpp;
#endif
	//bytesperline is set in this function
	if (setvideomode(daxdim,daydim,dabpp,davidoption) < 0) return(-1);

#ifndef USING_A_C
	// it's possible the previous call protected our code sections again
	makeasmwriteable();
#endif

#if USE_POLYMOST && USE_OPENGL
	if (dabpp > 8) rendmode = 3;	// GL renderer
	else if (dabpp == 8 && oldbpp != 8) rendmode = 0;	// going from GL to software activates classic
#endif

	xdim = xres; ydim = yres;

	// determine the corrective factor for pixel-squareness. Build
	// is built around the non-square pixels of Mode 13h, so to get
	// things back square on VGA screens, things need to be "compressed"
	// vertically a little.
	widescreen = 0;
	tallscreen = 0;
	if ((xdim == 320 && ydim == 200) || (xdim == 640 && ydim == 400)) {
		pixelaspect = 65536;
	} else {
		int ratio = divscale16(ydim*320, xdim*240);
		pixelaspect = divscale16(240*320L,320*200L);

		if (ratio < 65536) {
			widescreen = 1;
		} else if (ratio > 65536) {
			tallscreen = 1;

			// let tall screens (eg. 1280x1024) stretch the 2D elements
			// vertically a little until something better is thought of
			pixelaspect = divscale16(ydim*320L,xdim*200L);
		}
	}

	j = ydim*4*sizeof(int);  //Leave room for horizlookup&horizlookup2

	if (lookups != NULL) { kfree((void *)lookups); lookups = NULL; }
	if ((lookups = kmalloc(j<<1)) == NULL) {
		engineerrstr = "Failed to allocate lookups memory";
		return -1;
	}

	horizlookup = (int *)(lookups);
	horizlookup2 = (int *)((intptr_t)lookups+j);
	horizycent = ((ydim*4)>>1);

	//Force drawrooms to call dosetaspect & recalculate stuff
	oxyaspect = oxdimen = oviewingrange = -1;

	setvlinebpl(bytesperline);
	j = 0;
	for(i=0;i<=ydim;i++) ylookup[i] = j, j += bytesperline;

	setview(0L,0L,xdim-1,ydim-1);

#if USE_POLYMOST && USE_OPENGL
	if (rendmode == 3) {
		polymost_glreset();
		polymost_glinit();
	}
#endif

	setbrightness(curbrightness,&palette[0],0);
	clearallviews(0L);

	if (searchx < 0) { searchx = halfxdimen; searchy = (ydimen>>1); }

	qsetmode = 200;

	//memset(ratelimitlast,0,sizeof(ratelimitlast));
	//ratelimitn = 0;

	return(0);
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl %ecx, %ebp
movl %edi, %ecx
cmpl $0xc8, 0x184c0e(%rip)   # 0x1b8a28
leaq 0x14a283(%rip), %rax    # 0x17e0a4
movl (%rax), %r14d
jne 0x33e65
leaq 0x14a28c(%rip), %rax    # 0x17e0b9
cmpb $0x0, (%rax)
jne 0x33e65
leaq 0x14a26f(%rip), %rax    # 0x17e0a8
movl (%rax), %eax
xorl %ecx, %eax
movl 0x3d41a9(%rip), %edi    # 0x407fec
xorl %esi, %edi
movl 0x3d41a4(%rip), %r8d    # 0x407ff0
xorl %edx, %r8d
orl %edi, %r8d
orl %eax, %r8d
movl %r14d, %eax
xorl %ebp, %eax
xorl %ebx, %ebx
orl %r8d, %eax
je 0x3412f
movl %esi, %edi
movl %edx, %esi
movl %ebp, %edx
callq 0x6e817
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x3412f
movl $0x3, %eax
cmpl $0x8, %ebp
jg 0x33e9a
sete %al
cmpl $0x8, %r14d
setne %cl
andb %al, %cl
xorl %eax, %eax
cmpb $0x1, %cl
jne 0x33ea3
leaq 0x5c6aaf(%rip), %rcx    # 0x5fa950
movl %eax, (%rcx)
leaq 0x70186(%rip), %rax     # 0xa4030
movslq (%rax), %rsi
movl %esi, 0x3d4139(%rip)    # 0x407fec
leaq 0x7017a(%rip), %rax     # 0xa4034
movl (%rax), %ebp
movl %ebp, 0x3d412e(%rip)    # 0x407ff0
xorl %eax, %eax
movl %eax, 0x3e2da6(%rip)    # 0x416c70
movl %eax, 0x3e2da4(%rip)    # 0x416c74
movl %esi, %eax
xorl $0x140, %eax            # imm = 0x140
movl %ebp, %ecx
xorl $0xc8, %ecx
orl %eax, %ecx
je 0x33efe
movslq %ebp, %rax
movl %esi, %ecx
xorl $0x280, %ecx            # imm = 0x280
movl %eax, %edx
xorl $0x190, %edx            # imm = 0x190
orl %ecx, %edx
jne 0x34136
movl $0x10000, 0x3e2d70(%rip) # imm = 0x10000
movq 0x3785f1(%rip), %rdi    # 0x3ac500
testq %rdi, %rdi
je 0x33f19
callq 0x61c0
movl %ebp, %eax
shll $0x5, %eax
movslq %eax, %rdi
callq 0x69a0
movq %rax, 0x3785d3(%rip)    # 0x3ac500
testq %rax, %rax
je 0x34121
shll $0x4, %ebp
movq %rax, 0x3e2d40(%rip)    # 0x416c80
movslq %ebp, %rcx
addq %rax, %rcx
movq %rcx, 0x3e2d3b(%rip)    # 0x416c88
movl 0x3d409d(%rip), %eax    # 0x407ff0
addl %eax, %eax
movl %eax, 0x3e2d35(%rip)    # 0x416c90
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x70cea(%rip)     # 0xa4c50
movl %eax, 0x70ce0(%rip)     # 0xa4c4c
movl %eax, 0x70cd6(%rip)     # 0xa4c48
leaq 0x14a30b(%rip), %rbx    # 0x17e284
movl (%rbx), %edi
callq 0x6fa48
movslq 0x3d4069(%rip), %rcx    # 0x407ff0
testq %rcx, %rcx
js 0x340ac
movl (%rbx), %eax
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movl %ecx, %edx
shrl $0x2, %edx
andl $0x1fffffff, %edx       # imm = 0x1FFFFFFF
shlq $0x4, %rdx
addq $0x10, %rdx
leal (%rax,%rax,2), %esi
leal (,%rax,4), %edi
leal (%rax,%rax), %r8d
movdqa 0x4010b(%rip), %xmm1    # 0x740d0
movdqa 0x40113(%rip), %xmm2    # 0x740e0
xorl %r9d, %r9d
movdqa 0x40118(%rip), %xmm3    # 0x740f0
pxor %xmm3, %xmm0
leaq 0x3e2cbd(%rip), %r10    # 0x416ca0
pcmpeqd %xmm4, %xmm4
movdqa 0x430b1(%rip), %xmm5    # 0x770a0
xorl %r11d, %r11d
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %ebx
notl %ebx
testb $0x1, %bl
je 0x3402a
movl %r9d, (%r11,%r10)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %ebx
testb $0x1, %bl
je 0x34041
leal (%rax,%r9), %ebx
movl %ebx, 0x4(%r11,%r10)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %ebx
testb $0x1, %bl
je 0x34081
leal (%r8,%r9), %ebx
movl %ebx, 0x8(%r11,%r10)
pextrw $0x4, %xmm6, %ebx
testb $0x1, %bl
je 0x34094
leal (%rsi,%r9), %ebx
movl %ebx, 0xc(%r11,%r10)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %r11
addl %edi, %r9d
cmpq %r11, %rdx
jne 0x33ff2
movl 0x3d3f3a(%rip), %edx    # 0x407fec
decl %edx
decl %ecx
xorl %edi, %edi
xorl %esi, %esi
callq 0x3419e
leaq 0x5c688a(%rip), %rax    # 0x5fa950
cmpl $0x3, (%rax)
jne 0x340d5
callq 0x50163
callq 0x5030c
movl 0x184929(%rip), %edi    # 0x1b8a04
leaq 0x2484ce(%rip), %rsi    # 0x27c5b0
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x348a4
xorl %edi, %edi
callq 0x34af3
cmpl $0x0, 0x70b47(%rip)     # 0xa4c40
jns 0x34115
movl 0x3e4d63(%rip), %eax    # 0x418e64
movl %eax, 0x70b39(%rip)     # 0xa4c40
movl 0x247987(%rip), %eax    # 0x27ba94
sarl %eax
movl %eax, 0x3e4d53(%rip)    # 0x418e68
movl $0xc8, 0x184909(%rip)   # 0x1b8a28
jmp 0x3412f
leaq 0x4336e(%rip), %rax     # 0x77496
movq %rax, 0x1848f1(%rip)    # 0x1b8a20
movl %ebx, %eax
popq %rbx
popq %r14
popq %rbp
retq
imulq $0xf0, %rsi, %rdi
shlq $0x16, %rax
leaq (%rax,%rax,4), %rcx
movq %rcx, %rax
cqto
idivq %rdi
movl $0x13333, 0x3e2b21(%rip) # imm = 0x13333
cmpl $0xffff, %eax           # imm = 0xFFFF
jg 0x3416d
movl $0x1, 0x3e2b08(%rip)    # 0x416c70
jmp 0x33f08
cmpl $0x10000, %eax          # imm = 0x10000
je 0x33f08
movl $0x1, 0x3e2af2(%rip)    # 0x416c74
imull $0xc8, %esi, %eax
movslq %eax, %rsi
movq %rcx, %rax
cqto
idivq %rsi
movl %eax, 0x3e2adf(%rip)    # 0x416c78
jmp 0x33f08