// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: loadboard
int loadboard(char *filename, char fromwhere, int *daposx, int *daposy, int *daposz,
			 short *daang, short *dacursectnum)
{
	short fil, i, numsprites;
	short maxsectors, maxwalls, maxsprites;

	i = strlen(filename)-1;
	if ((unsigned char)filename[i] == 255) { filename[i] = 0; fromwhere = 1; }	// JBF 20040119: "compatibility"
	if ((fil = kopen4load(filename,fromwhere)) == -1)
		{ mapversion = 7L; return(-1); }

	kread(fil,&mapversion,4); mapversion = B_LITTLE32(mapversion);
	if (mapversion == 7)
	{
		maxsectors = MAXSECTORSV7;
		maxwalls = MAXWALLSV7;
		maxsprites = MAXSPRITESV7;
	}
	else if (mapversion == 8)
	{
		maxsectors = MAXSECTORSV8;
		maxwalls = MAXWALLSV8;
		maxsprites = MAXSPRITESV8;
	}
	else
	{
		kclose(fil);
		return(-2);
	}

	/*
	// Enable this for doing map checksum tests
	clearbufbyte(&wall,   sizeof(wall),   0);
	clearbufbyte(&sector, sizeof(sector), 0);
	clearbufbyte(&sprite, sizeof(sprite), 0);
	*/

	initspritelists();

	clearbuf(&show2dsector[0],(int)((MAXSECTORS+3)>>5),0L);
	clearbuf(&show2dsprite[0],(int)((MAXSPRITES+3)>>5),0L);
	clearbuf(&show2dwall[0],(int)((MAXWALLS+3)>>5),0L);

	kread(fil,daposx,4); *daposx = B_LITTLE32(*daposx);
	kread(fil,daposy,4); *daposy = B_LITTLE32(*daposy);
	kread(fil,daposz,4); *daposz = B_LITTLE32(*daposz);
	kread(fil,daang,2);  *daang  = B_LITTLE16(*daang);
	kread(fil,dacursectnum,2); *dacursectnum = B_LITTLE16(*dacursectnum);

	kread(fil,&numsectors,2); numsectors = B_LITTLE16(numsectors);
	if (numsectors > maxsectors) { kclose(fil); return(-2); }
	kread(fil,&sector[0],sizeof(sectortype)*numsectors);
	for (i=numsectors-1; i>=0; i--) {
		sector[i].wallptr       = B_LITTLE16(sector[i].wallptr);
		sector[i].wallnum       = B_LITTLE16(sector[i].wallnum);
		sector[i].ceilingz      = B_LITTLE32(sector[i].ceilingz);
		sector[i].floorz        = B_LITTLE32(sector[i].floorz);
		sector[i].ceilingstat   = B_LITTLE16(sector[i].ceilingstat);
		sector[i].floorstat     = B_LITTLE16(sector[i].floorstat);
		sector[i].ceilingpicnum = B_LITTLE16(sector[i].ceilingpicnum);
		sector[i].ceilingheinum = B_LITTLE16(sector[i].ceilingheinum);
		sector[i].floorpicnum   = B_LITTLE16(sector[i].floorpicnum);
		sector[i].floorheinum   = B_LITTLE16(sector[i].floorheinum);
		sector[i].lotag         = B_LITTLE16(sector[i].lotag);
		sector[i].hitag         = B_LITTLE16(sector[i].hitag);
		sector[i].extra         = B_LITTLE16(sector[i].extra);
	}

	kread(fil,&numwalls,2); numwalls = B_LITTLE16(numwalls);
	if (numwalls > maxwalls) { kclose(fil); return(-2); }
	kread(fil,&wall[0],sizeof(walltype)*numwalls);
	for (i=numwalls-1; i>=0; i--) {
		wall[i].x          = B_LITTLE32(wall[i].x);
		wall[i].y          = B_LITTLE32(wall[i].y);
		wall[i].point2     = B_LITTLE16(wall[i].point2);
		wall[i].nextwall   = B_LITTLE16(wall[i].nextwall);
		wall[i].nextsector = B_LITTLE16(wall[i].nextsector);
		wall[i].cstat      = B_LITTLE16(wall[i].cstat);
		wall[i].picnum     = B_LITTLE16(wall[i].picnum);
		wall[i].overpicnum = B_LITTLE16(wall[i].overpicnum);
		wall[i].lotag      = B_LITTLE16(wall[i].lotag);
		wall[i].hitag      = B_LITTLE16(wall[i].hitag);
		wall[i].extra      = B_LITTLE16(wall[i].extra);
	}

	kread(fil,&numsprites,2); numsprites = B_LITTLE16(numsprites);
	if (numsprites > maxsprites) { kclose(fil); return(-2); }
	kread(fil,&sprite[0],sizeof(spritetype)*numsprites);
	for (i=numsprites-1; i>=0; i--) {
		sprite[i].x       = B_LITTLE32(sprite[i].x);
		sprite[i].y       = B_LITTLE32(sprite[i].y);
		sprite[i].z       = B_LITTLE32(sprite[i].z);
		sprite[i].cstat   = B_LITTLE16(sprite[i].cstat);
		sprite[i].picnum  = B_LITTLE16(sprite[i].picnum);
		sprite[i].sectnum = B_LITTLE16(sprite[i].sectnum);
		sprite[i].statnum = B_LITTLE16(sprite[i].statnum);
		sprite[i].ang     = B_LITTLE16(sprite[i].ang);
		sprite[i].owner   = B_LITTLE16(sprite[i].owner);
		sprite[i].xvel    = B_LITTLE16(sprite[i].xvel);
		sprite[i].yvel    = B_LITTLE16(sprite[i].yvel);
		sprite[i].zvel    = B_LITTLE16(sprite[i].zvel);
		sprite[i].lotag   = B_LITTLE16(sprite[i].lotag);
		sprite[i].hitag   = B_LITTLE16(sprite[i].hitag);
		sprite[i].extra   = B_LITTLE16(sprite[i].extra);
	}

	for(i=0;i<numsprites;i++) {
		if ((sprite[i].cstat & 48) == 48) sprite[i].cstat &= ~48;
		insertsprite(sprite[i].sectnum,sprite[i].statnum);
	}

		//Must be after loading sectors, etc!
	updatesector(*daposx,*daposy,dacursectnum);

	kclose(fil);

#if USE_POLYMOST && USE_OPENGL
	memset(spriteext, 0, sizeof(spriteext));
#endif
	guniqhudid = 0;

	return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %r12
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
callq 0x6420
decl %eax
movswq %ax, %rax
cmpb $-0x1, (%r15,%rax)
jne 0x30299
movb $0x0, (%r15,%rax)
movb $0x1, %bpl
movsbl %bpl, %esi
movq %r15, %rdi
callq 0x2043c
movl %eax, %ecx
shll $0x10, %ecx
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
jne 0x302c6
movl $0x7, 0x744dc(%rip)     # 0xa4798
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x30447
movswl %ax, %ebp
leaq 0x744c8(%rip), %rsi     # 0xa4798
movl %ebp, %edi
movl $0x4, %edx
callq 0x20684
movl 0x744b6(%rip), %eax     # 0xa4798
cmpl $0x7, %eax
je 0x3030d
cmpl $0x8, %eax
jne 0x3043b
movq %rbx, 0x10(%rsp)
movl $0x4000, 0xc(%rsp)      # imm = 0x4000
movl $0x1000, %r15d          # imm = 0x1000
movl $0x4000, 0x8(%rsp)      # imm = 0x4000
jmp 0x30328
movq %rbx, 0x10(%rsp)
movl $0x1000, 0x8(%rsp)      # imm = 0x1000
movl $0x2000, 0xc(%rsp)      # imm = 0x2000
movl $0x400, %r15d           # imm = 0x400
movq 0x50(%rsp), %rbx
callq 0x25c20
leaq 0x37afa7(%rip), %rdi    # 0x3ab2e0
movl $0x80, %esi
xorl %edx, %edx
callq 0x4d2d8
leaq 0x37b194(%rip), %rdi    # 0x3ab4e0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x4d2d8
leaq 0x37b981(%rip), %rdi    # 0x3abce0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x4d2d8
movl %ebp, %edi
movq %r14, %rsi
movl $0x4, %edx
callq 0x20684
movl %ebp, %edi
movq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x20684
movl %ebp, %edi
movq %r13, %rsi
movl $0x4, %edx
callq 0x20684
movl %ebp, %edi
movq %r12, %rsi
movl $0x2, %edx
callq 0x20684
movl %ebp, %edi
movq %rbx, %rsi
movl $0x2, %edx
callq 0x20684
leaq 0x39fba1(%rip), %rsi    # 0x3cff60
movl %ebp, %edi
movl $0x2, %edx
callq 0x20684
movswl 0x39fb8e(%rip), %eax    # 0x3cff60
cmpl %eax, %r15d
jl 0x3043b
shll $0x3, %eax
leal (%rax,%rax,4), %edx
leaq 0x21367c(%rip), %rsi    # 0x243a60
movl %ebp, %edi
callq 0x20684
leaq 0x3e6876(%rip), %rsi    # 0x416c68
movl %ebp, %edi
movl $0x2, %edx
callq 0x20684
movswl 0x3e6863(%rip), %edx    # 0x416c68
cmpl %edx, 0xc(%rsp)
jl 0x3043b
shll $0x5, %edx
leaq 0x18863b(%rip), %rsi    # 0x1b8a50
movl %ebp, %edi
callq 0x20684
leaq 0x6(%rsp), %r12
movl %ebp, %edi
movq %r12, %rsi
movl $0x2, %edx
callq 0x20684
movswl (%r12), %eax
cmpl %eax, 0x8(%rsp)
jge 0x30458
movl %ebp, %edi
callq 0x20b40
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
imull $0x2c, %eax, %edx
leaq 0x24c84e(%rip), %rsi    # 0x27ccb0
movl %ebp, %edi
callq 0x20684
cmpw $0x0, 0x6(%rsp)
movq 0x10(%rsp), %rbx
jle 0x304be
leaq 0x24c83f(%rip), %r15    # 0x27ccbc
xorl %r13d, %r13d
movl (%r15), %eax
movl %eax, %ecx
notl %ecx
testb $0x30, %cl
jne 0x30495
andl $0xffcf, %eax           # imm = 0xFFCF
movw %ax, (%r15)
movswl 0xc(%r15), %r12d
movswl 0xe(%r15), %edi
callq 0x36ca7
movl %r12d, %edi
callq 0x36d25
incq %r13
movswq 0x6(%rsp), %rax
addq $0x2c, %r15
cmpq %rax, %r13
jl 0x30480
movl (%r14), %edi
movl (%rbx), %esi
movq 0x50(%rsp), %rdx
callq 0x2668d
movl %ebp, %edi
callq 0x20b40
leaq 0x2fc7d5(%rip), %rdi    # 0x32ccb0
xorl %ebx, %ebx
movl $0x30c00, %edx          # imm = 0x30C00
xorl %esi, %esi
callq 0x62f0
movl $0x0, 0x3e6779(%rip)    # 0x416c6c
jmp 0x30447