// File: /workspace/github_c_makefile_star2_O3/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 $0x178, %rsp            # imm = 0x178
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 0x30548
movb $0x0, (%r13,%rax)
movb $0x1, %bpl
movsbl %bpl, %esi
movq %r13, %rdi
callq 0x2043c
movl %eax, %ecx
shll $0x10, %ecx
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
jne 0x30575
movl $0x5, 0x7422d(%rip)     # 0xa4798
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x30e4a
movswl %ax, %ebp
leaq 0x74219(%rip), %rsi     # 0xa4798
movl %ebp, %edi
movl $0x4, %edx
callq 0x20684
cmpl $0x4, %eax
jne 0x30e3e
movl 0x741fe(%rip), %eax     # 0xa4798
addl $-0x7, %eax
cmpl $-0x4, %eax
ja 0x305b3
movl %ebp, %edi
callq 0x20b40
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
jmp 0x30e4a
callq 0x25c20
leaq 0x37ad21(%rip), %rdi    # 0x3ab2e0
movl $0x80, %esi
xorl %edx, %edx
callq 0x4d2d8
leaq 0x37af0e(%rip), %rdi    # 0x3ab4e0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x4d2d8
leaq 0x37b6fb(%rip), %rdi    # 0x3abce0
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
callq 0x4d2d8
movl %ebp, %edi
movq %r14, %rsi
movl $0x4, %edx
callq 0x20684
cmpl $0x4, %eax
jne 0x30e3e
movl %ebp, %edi
movq %rbx, %rsi
movl $0x4, %edx
callq 0x20684
cmpl $0x4, %eax
jne 0x30e3e
movl %ebp, %edi
movq %r12, %rsi
movl $0x4, %edx
callq 0x20684
cmpl $0x4, %eax
jne 0x30e3e
movl %ebp, %edi
movq %r15, %rsi
movl $0x2, %edx
callq 0x20684
cmpl $0x2, %eax
jne 0x30e3e
movq 0x1b0(%rsp), %rsi
movl %ebp, %edi
movl $0x2, %edx
callq 0x20684
cmpl $0x2, %eax
jne 0x30e3e
movl 0x74124(%rip), %eax     # 0xa4798
cmpl $0x4, %eax
jne 0x306d1
leaq 0x39f8e0(%rip), %rsi    # 0x3cff60
movl %ebp, %edi
movl $0x2, %edx
callq 0x20684
cmpl $0x2, %eax
jne 0x30e3e
leaq 0x3e65cc(%rip), %rsi    # 0x416c68
movl %ebp, %edi
movl $0x2, %edx
callq 0x20684
cmpl $0x2, %eax
jne 0x30e3e
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
movl $0x2, %edx
callq 0x20684
cmpl $0x2, %eax
jne 0x30e3e
movl 0x740c7(%rip), %eax     # 0xa4798
cmpl $0x5, %eax
jl 0x306f2
leaq 0x39f883(%rip), %rsi    # 0x3cff60
movl %ebp, %edi
movl $0x2, %edx
callq 0x20684
cmpl $0x2, %eax
jne 0x30e3e
movswl 0x39f867(%rip), %eax    # 0x3cff60
cmpl $0x1000, %eax           # imm = 0x1000
jg 0x30b48
testw %ax, %ax
jle 0x3099e
leaq 0x21334c(%rip), %r12    # 0x243a60
leaq 0x18(%rsp), %r13
xorl %r15d, %r15d
movl 0x74076(%rip), %eax     # 0xa4798
cmpl $0x6, %eax
je 0x30969
cmpl $0x5, %eax
je 0x30911
cmpl $0x4, %eax
jne 0x30986
movl %ebp, %edi
leaq 0x98(%rsp), %rsi
callq 0x30e5e
testl %eax, %eax
jne 0x30e3e
movzwl 0x98(%rsp), %eax
movw %ax, 0x16(%rsp)
movzwl 0x9a(%rsp), %ecx
movw %cx, 0x14(%rsp)
movw %ax, 0x40(%rsp)
movw %cx, 0x42(%rsp)
movzwl 0xa4(%rsp), %eax
movw %ax, 0x12(%rsp)
movw %ax, 0x44(%rsp)
movzwl 0xb0(%rsp), %eax
movw %ax, 0x10(%rsp)
movw %ax, 0x46(%rsp)
movzwl 0xa6(%rsp), %eax
movw %ax, 0xe(%rsp)
movw %ax, 0x48(%rsp)
movzwl 0xb2(%rsp), %eax
movw %ax, 0xc(%rsp)
movw %ax, 0x4a(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0xe4(%rsp)
movl %eax, 0x4c(%rsp)
movl 0xac(%rsp), %r11d
movl %r11d, 0x50(%rsp)
movb 0x9f(%rsp), %r13b
movb %r13b, 0x54(%rsp)
movb 0xab(%rsp), %r9b
movb %r9b, 0x55(%rsp)
movb 0x9c(%rsp), %al
movb %al, 0xb(%rsp)
movb 0x9d(%rsp), %r8b
movb %r8b, 0x56(%rsp)
movb 0xa9(%rsp), %dil
movb %dil, 0x57(%rsp)
movb 0x9e(%rsp), %sil
movb %sil, 0x58(%rsp)
movb 0xaa(%rsp), %dl
movb %dl, 0x59(%rsp)
movb %al, 0x5a(%rsp)
movb 0xa8(%rsp), %cl
movb %cl, 0x5b(%rsp)
movl $0x0, %eax
movw %ax, 0x5c(%rsp)
movb %al, 0x5e(%rsp)
movl 0xb4(%rsp), %eax
movw %ax, 0x60(%rsp)
movzwl 0x16(%rsp), %r10d
movw %r10w, 0x18(%rsp)
movzwl 0x14(%rsp), %r10d
movw %r10w, 0x1a(%rsp)
movzwl 0x12(%rsp), %r10d
movw %r10w, 0x1c(%rsp)
movzwl 0x10(%rsp), %r10d
movw %r10w, 0x1e(%rsp)
movzwl 0xe(%rsp), %r10d
movw %r10w, 0x20(%rsp)
movzwl 0xc(%rsp), %r10d
movw %r10w, 0x22(%rsp)
movl 0xe4(%rsp), %r10d
movl %r10d, 0x24(%rsp)
movl %r11d, 0x28(%rsp)
movb %r13b, 0x2c(%rsp)
leaq 0x18(%rsp), %r13
movb %r9b, 0x2d(%rsp)
movb %r8b, 0x2e(%rsp)
movb %dil, 0x2f(%rsp)
movb %sil, 0x30(%rsp)
movb %dl, 0x31(%rsp)
movb 0xb(%rsp), %dl
movb %dl, 0x32(%rsp)
movb %cl, 0x33(%rsp)
xorl %ecx, %ecx
movw %cx, 0x34(%rsp)
movb %cl, 0x36(%rsp)
movw %ax, 0x38(%rsp)
shrl $0x10, %eax
movw %ax, 0x62(%rsp)
movw $0xffff, 0x64(%rsp)     # imm = 0xFFFF
movw %ax, 0x3a(%rsp)
movw $0xffff, %ax            # imm = 0xFFFF
jmp 0x30962
movl %ebp, %edi
leaq 0x40(%rsp), %rsi
callq 0x3110b
testl %eax, %eax
jne 0x30e3e
movq 0x40(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0x20(%rsp)
movdqu 0x4c(%rsp), %xmm0
movdqu %xmm0, 0x24(%rsp)
movb 0x5c(%rsp), %al
movb %al, 0x34(%rsp)
movzwl 0x5d(%rsp), %eax
movw %ax, 0x35(%rsp)
movl 0x60(%rsp), %eax
movl %eax, 0x38(%rsp)
movzwl 0x64(%rsp), %eax
movw %ax, 0x3c(%rsp)
jmp 0x3097b
movl %ebp, %edi
movq %r13, %rsi
callq 0x31330
testl %eax, %eax
jne 0x30e3e
movq %r13, %rdi
movq %r12, %rsi
callq 0x31006
incq %r15
movswq 0x39f5cf(%rip), %rax    # 0x3cff60
addq $0x28, %r12
cmpq %rax, %r15
jl 0x3071c
cmpl $0x5, 0x73df3(%rip)     # 0xa4798
jl 0x309c3
leaq 0x3e62ba(%rip), %rsi    # 0x416c68
movl %ebp, %edi
movl $0x2, %edx
callq 0x20684
cmpl $0x2, %eax
jne 0x30e3e
movswl 0x3e629e(%rip), %eax    # 0x416c68
cmpl $0x4000, %eax           # imm = 0x4000
jg 0x30b48
testw %ax, %ax
jle 0x30b19
leaq 0x18806b(%rip), %r12    # 0x1b8a50
xorl %r13d, %r13d
leaq 0x158(%rsp), %r15
movl 0x73da2(%rip), %eax     # 0xa4798
cmpl $0x6, %eax
je 0x30ae4
cmpl $0x5, %eax
je 0x30ab8
cmpl $0x4, %eax
jne 0x30b01
movl %ebp, %edi
leaq 0x110(%rsp), %rsi
callq 0x31555
testl %eax, %eax
jne 0x30e3e
movq 0x110(%rsp), %rax
movq %rax, 0xe8(%rsp)
movzwl 0x118(%rsp), %eax
movw %ax, 0xf0(%rsp)
movl 0x120(%rsp), %eax
movl %eax, 0xf2(%rsp)
movb 0x11b(%rsp), %al
movb %al, 0xf6(%rsp)
movzbl 0x11a(%rsp), %eax
movw %ax, 0xf8(%rsp)
movl 0x11c(%rsp), %eax
movl %eax, 0xfa(%rsp)
movq 0x124(%rsp), %rax
movq %rax, 0xfe(%rsp)
movl 0x12c(%rsp), %eax
movw %ax, 0x106(%rsp)
shrl $0x10, %eax
movw %ax, 0x108(%rsp)
movw $0xffff, 0x10a(%rsp)    # imm = 0xFFFF
jmp 0x30acf
movl %ebp, %edi
leaq 0xe8(%rsp), %rsi
callq 0x31825
testl %eax, %eax
jne 0x30e3e
leaq 0xe8(%rsp), %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x316e4
jmp 0x30af6
movl %ebp, %edi
movq %r15, %rsi
callq 0x319e6
testl %eax, %eax
jne 0x30e3e
movq %r15, %rdi
movq %r12, %rsi
callq 0x317ae
incq %r13
movswq 0x3e615c(%rip), %rax    # 0x416c68
addq $0x20, %r12
cmpq %rax, %r13
jl 0x309f0
cmpl $0x5, 0x73c78(%rip)     # 0xa4798
jl 0x30b3c
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
movl $0x2, %edx
callq 0x20684
cmpl $0x2, %eax
jne 0x30e3e
movswl 0x8(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jle 0x30b54
movl %ebp, %edi
callq 0x20b40
jmp 0x3056b
testw %ax, %ax
jle 0x30e04
leaq 0x24c14c(%rip), %r12    # 0x27ccb0
xorl %r15d, %r15d
leaq 0x68(%rsp), %r13
movl 0x73c26(%rip), %eax     # 0xa4798
cmpl $0x6, %eax
je 0x30d80
cmpl $0x5, %eax
je 0x30cc5
cmpl $0x4, %eax
jne 0x30d9d
movl %ebp, %edi
leaq 0x130(%rsp), %rsi
callq 0x31b8e
testl %eax, %eax
jne 0x30e3e
movl 0x130(%rsp), %edi
movl 0x134(%rsp), %r8d
movl %edi, 0xb8(%rsp)
movl %r8d, 0xbc(%rsp)
movl 0x138(%rsp), %r9d
movl %r9d, 0xc0(%rsp)
movzbl 0x13c(%rsp), %r10d
movb %r10b, 0xc4(%rsp)
movb 0x13d(%rsp), %r11b
movb %r11b, 0xc5(%rsp)
movb 0x13e(%rsp), %al
movb %al, 0xc6(%rsp)
movb 0x13f(%rsp), %cl
movb %cl, 0xc7(%rsp)
movdqu 0x140(%rsp), %xmm0
movdqu %xmm0, 0xc8(%rsp)
movl 0x150(%rsp), %edx
movw %dx, 0xd8(%rsp)
movl %edx, %esi
shrl $0x10, %esi
movw %si, 0xda(%rsp)
movw $0xffff, 0xdc(%rsp)     # imm = 0xFFFF
movl %edi, 0x68(%rsp)
movl %r8d, 0x6c(%rsp)
movl %r9d, 0x70(%rsp)
movw %r10w, 0x74(%rsp)
movb %r11b, 0x76(%rsp)
pextrw $0x6, %xmm0, %edi
movswq %di, %rdi
leaq (%rdi,%rdi,4), %rdi
leaq 0x212dec(%rip), %r8     # 0x243a60
leaq (%r8,%rdi,8), %rdi
movzwl 0xc(%rdi), %r8d
xorl %r9d, %r9d
testb $0x1, %r8b
sete %r9b
movb 0x15(%rdi,%r9,8), %dil
movb %dil, 0x77(%rsp)
movb $0x20, 0x78(%rsp)
movb %al, 0x79(%rsp)
movb %cl, 0x7a(%rsp)
movw $0x0, 0x7b(%rsp)
movdqu %xmm0, 0x7e(%rsp)
movw %dx, 0x8e(%rsp)
movw %si, 0x90(%rsp)
movw $0xffff, %ax            # imm = 0xFFFF
jmp 0x30d76
movl %ebp, %edi
leaq 0xb8(%rsp), %rsi
callq 0x31dd5
testl %eax, %eax
jne 0x30e3e
movq 0xb8(%rsp), %rax
movq %rax, 0x68(%rsp)
movl 0xc0(%rsp), %eax
movl %eax, 0x70(%rsp)
movzbl 0xc4(%rsp), %eax
movw %ax, 0x74(%rsp)
movb 0xc5(%rsp), %al
movb %al, 0x76(%rsp)
movb $0x20, 0x78(%rsp)
movzwl 0xc6(%rsp), %eax
movw %ax, 0x79(%rsp)
movw $0x0, 0x7b(%rsp)
movdqu 0xc8(%rsp), %xmm0
pextrw $0x6, %xmm0, %eax
movswq %ax, %rax
leaq (%rax,%rax,4), %rax
leaq 0x212d1e(%rip), %rcx    # 0x243a60
leaq (%rcx,%rax,8), %rax
movzwl 0xc(%rax), %ecx
xorl %edx, %edx
testb $0x1, %cl
sete %dl
movb 0x15(%rax,%rdx,8), %al
movb %al, 0x77(%rsp)
movdqu %xmm0, 0x7e(%rsp)
movl 0xd8(%rsp), %eax
movl %eax, 0x8e(%rsp)
movzwl 0xdc(%rsp), %eax
movw %ax, 0x92(%rsp)
jmp 0x30d92
movl %ebp, %edi
movq %r13, %rsi
callq 0x31f96
testl %eax, %eax
jne 0x30e3e
movq %r13, %rdi
movq %r12, %rsi
callq 0x31d36
incq %r15
movswq 0x8(%rsp), %rax
addq $0x2c, %r12
cmpq %rax, %r15
jl 0x30b6c
testw %ax, %ax
jle 0x30e04
leaq 0x24befd(%rip), %r12    # 0x27ccbc
xorl %r13d, %r13d
movl (%r12), %eax
movl %eax, %ecx
notl %ecx
testb $0x30, %cl
jne 0x30dd9
andl $0xffcf, %eax           # imm = 0xFFCF
movw %ax, (%r12)
movswl 0xc(%r12), %r15d
movswl 0xe(%r12), %edi
callq 0x36ca7
movl %r15d, %edi
callq 0x36d25
incq %r13
movswq 0x8(%rsp), %rax
addq $0x2c, %r12
cmpq %rax, %r13
jl 0x30dc2
movl (%r14), %edi
movl (%rbx), %esi
movq 0x1b0(%rsp), %rdx
callq 0x2668d
movl %ebp, %edi
callq 0x20b40
leaq 0x2fbe8c(%rip), %rdi    # 0x32ccb0
xorl %ebx, %ebx
movl $0x30c00, %edx          # imm = 0x30C00
xorl %esi, %esi
callq 0x62f0
movl $0x0, 0x3e5e30(%rip)    # 0x416c6c
jmp 0x30e4a
movl %ebp, %edi
callq 0x20b40
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
movl %ebx, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq