// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: loadoldboard
int loadoldboard(char *filename, char fromwhere, int *daposx, int *daposy, int *daposz,
			 short *daang, short *dacursectnum)
{
	short fil, i, numsprites;
	struct sectortypev4 v4sect;
	struct walltypev4   v4wall;
	struct spritetypev4 v4spr;
	struct sectortypev5 v5sect;
	struct walltypev5   v5wall;
	struct spritetypev5 v5spr;
	struct sectortypev6 v6sect;
	struct walltypev6   v6wall;
	struct spritetypev6 v6spr;

	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 = 5L; return(-1); }

	if (kread(fil,&mapversion,4) != 4) goto readerror;
	mapversion = B_LITTLE32(mapversion);
	if (mapversion != 4L && mapversion != 5L && mapversion != 6L) {
		kclose(fil);
		return(-2);
	}

	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);

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

	if (mapversion == 4) {
		if (kread(fil,&numsectors,2) != 2) goto readerror;
		if (kread(fil,&numwalls,2) != 2) goto readerror;
		if (kread(fil,&numsprites,2) != 2) goto readerror;
	}

	if (mapversion > 4 && kread(fil,&numsectors,2) != 2) goto readerror;
	numsectors = B_LITTLE16(numsectors);
	if (numsectors > MAXSECTORS) {
		kclose(fil);
		return(-1);
	}

	for (i=0; i<numsectors; i++) {
		switch (mapversion) {
			case 4:
				if (readv4sect(fil,&v4sect)) goto readerror;
				convertv4sectv5(&v4sect,&v5sect);
				convertv5sectv6(&v5sect,&v6sect);
				convertv6sectv7(&v6sect,&sector[i]);
				break;
			case 5:
				if (readv5sect(fil,&v5sect)) goto readerror;
				convertv5sectv6(&v5sect,&v6sect);
				convertv6sectv7(&v6sect,&sector[i]);
				break;
			case 6:
				if (readv6sect(fil,&v6sect)) goto readerror;
				convertv6sectv7(&v6sect,&sector[i]);
				break;
		}
	}

	if (mapversion > 4 && kread(fil,&numwalls,2) != 2) goto readerror;
	numwalls = B_LITTLE16(numwalls);
	if (numwalls > MAXWALLS) {
		kclose(fil);
		return(-1);
	}

	for (i=0; i<numwalls; i++) {
		switch (mapversion) {
			case 4:
				if (readv4wall(fil,&v4wall)) goto readerror;
				convertv4wallv5(&v4wall,&v5wall);
				convertv5wallv6(&v5wall,&v6wall,i);
				convertv6wallv7(&v6wall,&wall[i]);
				break;
			case 5:
				if (readv5wall(fil,&v5wall)) goto readerror;
				convertv5wallv6(&v5wall,&v6wall,i);
				convertv6wallv7(&v6wall,&wall[i]);
				break;
			case 6:
				if (readv6wall(fil,&v6wall)) goto readerror;
				convertv6wallv7(&v6wall,&wall[i]);
				break;
		}
	}

	if (mapversion > 4 && kread(fil,&numsprites,2) != 2) goto readerror;
	numsprites = B_LITTLE16(numsprites);
	if (numsprites > MAXSPRITES) {
		kclose(fil);
		return(-1);
	}

	for (i=0; i<numsprites; i++) {
		switch (mapversion) {
			case 4:
				if (readv4sprite(fil,&v4spr)) goto readerror;
				convertv4sprv5(&v4spr,&v5spr);
				convertv5sprv6(&v5spr,&v6spr);
				convertv6sprv7(&v6spr,&sprite[i]);
				break;
			case 5:
				if (readv5sprite(fil,&v5spr)) goto readerror;
				convertv5sprv6(&v5spr,&v6spr);
				convertv6sprv7(&v6spr,&sprite[i]);
				break;
			case 6:
				if (readv6sprite(fil,&v6spr)) goto readerror;
				convertv6sprv7(&v6spr,&sprite[i]);
				break;
		}
	}

	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);

readerror:
	kclose(fil);
	return -3;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r9, %r15
movq %r8, %r12
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r13
callq 0x6420
decl %eax
movswq %ax, %rax
cmpb $-0x1, (%r13,%rax)
jne 0x2f9b6
movb $0x0, (%r13,%rax)
movb $0x1, %bpl
movsbl %bpl, %esi
movq %r13, %rdi
callq 0x1ff50
movl %eax, %ecx
shll $0x10, %ecx
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
jne 0x2f9e3
movl $0x5, 0x73dbf(%rip)     # 0xa3798
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x30325
movswl %ax, %ebp
leaq 0x73dab(%rip), %rsi     # 0xa3798
movl %ebp, %edi
movl $0x4, %edx
callq 0x201c2
cmpl $0x4, %eax
jne 0x30319
movl 0x73d90(%rip), %eax     # 0xa3798
addl $-0x7, %eax
cmpl $-0x4, %eax
ja 0x2fa21
movl %ebp, %edi
callq 0x20671
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
jmp 0x30325
callq 0x2565f
leaq 0x37a8b3(%rip), %rdi    # 0x3aa2e0
movl $0x80, %esi
xorl %edx, %edx
callq 0x4abc9
leaq 0x37aaa0(%rip), %rdi    # 0x3aa4e0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x4abc9
leaq 0x37b28d(%rip), %rdi    # 0x3aace0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x4abc9
movl %ebp, %edi
movq %r14, %rsi
movl $0x4, %edx
callq 0x201c2
cmpl $0x4, %eax
jne 0x30319
movl %ebp, %edi
movq %rbx, %rsi
movl $0x4, %edx
callq 0x201c2
cmpl $0x4, %eax
jne 0x30319
movl %ebp, %edi
movq %r12, %rsi
movl $0x4, %edx
callq 0x201c2
cmpl $0x4, %eax
jne 0x30319
movl %ebp, %edi
movq %r15, %rsi
movl $0x2, %edx
callq 0x201c2
cmpl $0x2, %eax
jne 0x30319
movq 0x190(%rsp), %rsi
movl %ebp, %edi
movl $0x2, %edx
callq 0x201c2
cmpl $0x2, %eax
jne 0x30319
cmpl $0x4, 0x73cb5(%rip)     # 0xa3798
jne 0x2fb37
leaq 0x39f474(%rip), %rsi    # 0x3cef60
movl %ebp, %edi
movl $0x2, %edx
callq 0x201c2
cmpl $0x2, %eax
jne 0x30319
leaq 0x3e6160(%rip), %rsi    # 0x415c68
movl %ebp, %edi
movl $0x2, %edx
callq 0x201c2
cmpl $0x2, %eax
jne 0x30319
leaq 0x6(%rsp), %rsi
movl %ebp, %edi
movl $0x2, %edx
callq 0x201c2
cmpl $0x2, %eax
jne 0x30319
cmpl $0x5, 0x73c5a(%rip)     # 0xa3798
jl 0x2fb5c
leaq 0x39f419(%rip), %rsi    # 0x3cef60
movl %ebp, %edi
movl $0x2, %edx
callq 0x201c2
cmpl $0x2, %eax
jne 0x30319
cmpw $0x1000, 0x39f3fb(%rip) # imm = 0x1000
jg 0x2ffae
cmpw $0x0, 0x39f3ed(%rip)    # 0x3cef60
jle 0x2fde7
leaq 0x212ee0(%rip), %r12    # 0x242a60
leaq 0x5c(%rsp), %r13
xorl %r15d, %r15d
movl 0x73c0a(%rip), %eax     # 0xa3798
cmpl $0x6, %eax
je 0x2fdb2
cmpl $0x5, %eax
je 0x2fd41
cmpl $0x4, %eax
jne 0x2fdcf
movl %ebp, %edi
leaq 0xac(%rsp), %rsi
callq 0x30339
testl %eax, %eax
jne 0x30319
movzwl 0xac(%rsp), %eax
movw %ax, 0x84(%rsp)
movw %ax, 0x5c(%rsp)
movzwl 0xae(%rsp), %eax
movw %ax, 0x86(%rsp)
movw %ax, 0x5e(%rsp)
movzwl 0xb8(%rsp), %eax
movw %ax, 0x88(%rsp)
movw %ax, 0x60(%rsp)
movzwl 0xc4(%rsp), %eax
movw %ax, 0x8a(%rsp)
movw %ax, 0x62(%rsp)
movzwl 0xba(%rsp), %eax
movw %ax, 0x8c(%rsp)
movw %ax, 0x64(%rsp)
movzwl 0xc6(%rsp), %eax
movw %ax, 0x8e(%rsp)
movw %ax, 0x66(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, 0x90(%rsp)
movl %eax, 0x68(%rsp)
movl 0xc0(%rsp), %eax
movl %eax, 0x94(%rsp)
movl %eax, 0x6c(%rsp)
movb 0xb3(%rsp), %al
movb %al, 0x98(%rsp)
movb 0xbf(%rsp), %cl
movb %cl, 0x99(%rsp)
movb %al, 0x70(%rsp)
movb 0xb0(%rsp), %al
movb %cl, 0x71(%rsp)
movb 0xb1(%rsp), %cl
movb %cl, 0x9a(%rsp)
movb %cl, 0x72(%rsp)
movb 0xbd(%rsp), %cl
movb %cl, 0x9b(%rsp)
movb %cl, 0x73(%rsp)
movb 0xb2(%rsp), %cl
movb %cl, 0x9c(%rsp)
movb %cl, 0x74(%rsp)
movb 0xbe(%rsp), %cl
movb %cl, 0x9d(%rsp)
movb %al, 0x9e(%rsp)
movb %cl, 0x75(%rsp)
movb 0xbc(%rsp), %cl
movb %cl, 0x9f(%rsp)
movl $0x0, %edx
movw %dx, 0xa0(%rsp)
movb %dl, 0xa2(%rsp)
movb %al, 0x76(%rsp)
movb %cl, 0x77(%rsp)
movl 0xc8(%rsp), %eax
movw %ax, 0xa4(%rsp)
movw %dx, 0x78(%rsp)
movb %dl, 0x7a(%rsp)
movw %ax, 0x7c(%rsp)
shrl $0x10, %eax
movw %ax, 0xa6(%rsp)
movw %ax, 0x7e(%rsp)
movw $0xffff, %ax            # imm = 0xFFFF
movw %ax, 0xa8(%rsp)
jmp 0x2fda8
movl %ebp, %edi
leaq 0x84(%rsp), %rsi
callq 0x305e6
testl %eax, %eax
jne 0x30319
movl 0x84(%rsp), %eax
movl %eax, 0x5c(%rsp)
movups 0x88(%rsp), %xmm0
movups %xmm0, 0x60(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x70(%rsp)
movzwl 0xa0(%rsp), %eax
movw %ax, 0x78(%rsp)
movb 0xa2(%rsp), %al
movb %al, 0x7a(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x7c(%rsp)
movzwl 0xa8(%rsp), %eax
movw %ax, 0x80(%rsp)
jmp 0x2fdc4
movl %ebp, %edi
movq %r13, %rsi
callq 0x3080b
testl %eax, %eax
jne 0x30319
movq %r13, %rdi
movq %r12, %rsi
callq 0x304e1
incq %r15
movswq 0x39f186(%rip), %rax    # 0x3cef60
addq $0x28, %r12
cmpq %rax, %r15
jl 0x2fb88
cmpl $0x5, 0x739aa(%rip)     # 0xa3798
jl 0x2fe0c
leaq 0x3e5e71(%rip), %rsi    # 0x415c68
movl %ebp, %edi
movl $0x2, %edx
callq 0x201c2
cmpl $0x2, %eax
jne 0x30319
cmpw $0x4000, 0x3e5e53(%rip) # imm = 0x4000
jg 0x2ffae
cmpw $0x0, 0x3e5e45(%rip)    # 0x415c68
jle 0x2ff82
leaq 0x187c20(%rip), %r12    # 0x1b7a50
xorl %r13d, %r13d
leaq 0x138(%rsp), %r15
movl 0x73957(%rip), %eax     # 0xa3798
cmpl $0x6, %eax
je 0x2ff4d
cmpl $0x5, %eax
je 0x2ff21
cmpl $0x4, %eax
jne 0x2ff6a
movl %ebp, %edi
leaq 0x118(%rsp), %rsi
callq 0x30a30
testl %eax, %eax
jne 0x30319
movq 0x118(%rsp), %rax
movq %rax, 0xf4(%rsp)
movzwl 0x120(%rsp), %eax
movw %ax, 0xfc(%rsp)
movl 0x128(%rsp), %eax
movl %eax, 0xfe(%rsp)
movb 0x123(%rsp), %al
movb %al, 0x102(%rsp)
movzbl 0x122(%rsp), %eax
movw %ax, 0x104(%rsp)
movl 0x124(%rsp), %eax
movl %eax, 0x106(%rsp)
movl 0x12c(%rsp), %eax
movl %eax, 0x10a(%rsp)
movzwl 0x130(%rsp), %eax
movw %ax, 0x10e(%rsp)
movzwl 0x132(%rsp), %eax
movw %ax, 0x110(%rsp)
movl 0x134(%rsp), %eax
movw %ax, 0x112(%rsp)
shrl $0x10, %eax
movw %ax, 0x114(%rsp)
movw $0xffff, 0x116(%rsp)    # imm = 0xFFFF
jmp 0x2ff38
movl %ebp, %edi
leaq 0xf4(%rsp), %rsi
callq 0x30d07
testl %eax, %eax
jne 0x30319
leaq 0xf4(%rsp), %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x30bbf
jmp 0x2ff5f
movl %ebp, %edi
movq %r15, %rsi
callq 0x30ec8
testl %eax, %eax
jne 0x30319
movq %r15, %rdi
movq %r12, %rsi
callq 0x30c90
incq %r13
movswq 0x3e5cf3(%rip), %rax    # 0x415c68
addq $0x20, %r12
cmpq %rax, %r13
jl 0x2fe3b
cmpl $0x5, 0x7380f(%rip)     # 0xa3798
jl 0x2ffa5
leaq 0x6(%rsp), %rsi
movl %ebp, %edi
movl $0x2, %edx
callq 0x201c2
cmpl $0x2, %eax
jne 0x30319
cmpw $0x4000, 0x6(%rsp)      # imm = 0x4000
jle 0x2ffba
movl %ebp, %edi
callq 0x20671
jmp 0x2f9d9
cmpw $0x0, 0x6(%rsp)
jle 0x3028b
leaq 0x24bce3(%rip), %r12    # 0x27bcb0
xorl %r15d, %r15d
leaq 0x8(%rsp), %r13
movl 0x737bd(%rip), %eax     # 0xa3798
cmpl $0x6, %eax
je 0x30258
cmpl $0x5, %eax
je 0x30175
cmpl $0x4, %eax
jne 0x30275
movl %ebp, %edi
leaq 0xcc(%rsp), %rsi
callq 0x31070
testl %eax, %eax
jne 0x30319
movswq 0xe8(%rsp), %rax
movl 0xec(%rsp), %ecx
movl %ecx, %edx
shrl $0x10, %edx
leaq (%rax,%rax,4), %rsi
leaq 0x212a33(%rip), %rdi    # 0x242a60
leaq (%rdi,%rsi,8), %rsi
movzwl 0xc(%rsi), %edi
xorl %r8d, %r8d
testb $0x1, %dil
sete %r8b
movb 0x15(%rsi,%r8,8), %sil
movl 0xcc(%rsp), %edi
movl %edi, 0x34(%rsp)
movl %edi, 0x8(%rsp)
movl 0xd0(%rsp), %edi
movl %edi, 0x38(%rsp)
movl %edi, 0xc(%rsp)
movl 0xd4(%rsp), %edi
movl %edi, 0x3c(%rsp)
movl %edi, 0x10(%rsp)
movzbl 0xd8(%rsp), %edi
movb %dil, 0x40(%rsp)
movw %di, 0x14(%rsp)
movb 0xd9(%rsp), %dil
movb %dil, 0x41(%rsp)
movb %dil, 0x16(%rsp)
movb 0xda(%rsp), %dil
movb %dil, 0x42(%rsp)
movb %sil, 0x17(%rsp)
movb 0xdb(%rsp), %sil
movb %sil, 0x43(%rsp)
movb $0x20, 0x18(%rsp)
movb %dil, 0x19(%rsp)
movb %sil, 0x1a(%rsp)
movzwl 0xdc(%rsp), %esi
movw %si, 0x44(%rsp)
movw $0x0, 0x1b(%rsp)
movw %si, 0x1e(%rsp)
movzwl 0xde(%rsp), %esi
movw %si, 0x46(%rsp)
movw %si, 0x20(%rsp)
movzwl 0xe0(%rsp), %esi
movw %si, 0x48(%rsp)
movw %si, 0x22(%rsp)
movzwl 0xe2(%rsp), %esi
movw %si, 0x4a(%rsp)
movw %si, 0x24(%rsp)
movzwl 0xe4(%rsp), %esi
movw %si, 0x4c(%rsp)
movw %si, 0x26(%rsp)
movzwl 0xe6(%rsp), %esi
movw %si, 0x4e(%rsp)
movw %si, 0x28(%rsp)
movw %ax, 0x50(%rsp)
movw %ax, 0x2a(%rsp)
movzwl 0xea(%rsp), %eax
movw %ax, 0x52(%rsp)
movw %ax, 0x2c(%rsp)
movw %cx, 0x54(%rsp)
movw %cx, 0x2e(%rsp)
movw %dx, 0x56(%rsp)
movw %dx, 0x30(%rsp)
movw $0xffff, %ax            # imm = 0xFFFF
movw %ax, 0x58(%rsp)
jmp 0x30251
movl %ebp, %edi
leaq 0x34(%rsp), %rsi
callq 0x312b7
testl %eax, %eax
jne 0x30319
movl 0x34(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0xc(%rsp)
movzbl 0x40(%rsp), %eax
movw %ax, 0x14(%rsp)
movb 0x41(%rsp), %al
movb %al, 0x16(%rsp)
movswq 0x50(%rsp), %rax
leaq (%rax,%rax,4), %rcx
leaq 0x2128a2(%rip), %rdx    # 0x242a60
leaq (%rdx,%rcx,8), %rcx
movzwl 0xc(%rcx), %edx
xorl %esi, %esi
testb $0x1, %dl
sete %sil
movb 0x15(%rcx,%rsi,8), %cl
movb %cl, 0x17(%rsp)
movb $0x20, 0x18(%rsp)
movzwl 0x42(%rsp), %ecx
movw %cx, 0x19(%rsp)
movw $0x0, 0x1b(%rsp)
movzwl 0x44(%rsp), %ecx
movw %cx, 0x1e(%rsp)
movzwl 0x46(%rsp), %ecx
movw %cx, 0x20(%rsp)
movzwl 0x48(%rsp), %ecx
movw %cx, 0x22(%rsp)
movzwl 0x4a(%rsp), %ecx
movw %cx, 0x24(%rsp)
movzwl 0x4c(%rsp), %ecx
movw %cx, 0x26(%rsp)
movzwl 0x4e(%rsp), %ecx
movw %cx, 0x28(%rsp)
movw %ax, 0x2a(%rsp)
movzwl 0x52(%rsp), %eax
movw %ax, 0x2c(%rsp)
movzwl 0x54(%rsp), %eax
movw %ax, 0x2e(%rsp)
movzwl 0x56(%rsp), %eax
movw %ax, 0x30(%rsp)
movzwl 0x58(%rsp), %eax
movw %ax, 0x32(%rsp)
jmp 0x3026a
movl %ebp, %edi
movq %r13, %rsi
callq 0x31478
testl %eax, %eax
jne 0x30319
movq %r13, %rdi
movq %r12, %rsi
callq 0x31218
incq %r15
movswq 0x6(%rsp), %rax
addq $0x2c, %r12
cmpq %rax, %r15
jl 0x2ffd5
cmpw $0x0, 0x6(%rsp)
jle 0x302df
leaq 0x24ba22(%rip), %r12    # 0x27bcbc
xorl %r13d, %r13d
movl (%r12), %eax
movl %eax, %ecx
notl %ecx
testb $0x30, %cl
jne 0x302b4
andl $0xffcf, %eax           # imm = 0xFFCF
movw %ax, (%r12)
movswl 0xc(%r12), %r15d
movswl 0xe(%r12), %edi
callq 0x35a4f
movl %r15d, %edi
callq 0x35acd
incq %r13
movswq 0x6(%rsp), %rax
addq $0x2c, %r12
cmpq %rax, %r13
jl 0x3029d
movl (%r14), %edi
movl (%rbx), %esi
movq 0x190(%rsp), %rdx
callq 0x25f34
movl %ebp, %edi
callq 0x20671
leaq 0x2fb9b1(%rip), %rdi    # 0x32bcb0
xorl %ebx, %ebx
movl $0x30c00, %edx          # imm = 0x30C00
xorl %esi, %esi
callq 0x62f0
movl $0x0, 0x3e5955(%rip)    # 0x415c6c
jmp 0x30325
movl %ebp, %edi
callq 0x20671
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
movl %ebx, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq