// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: saveoldboard
int saveoldboard(char *filename, int *daposx, int *daposy, int *daposz,
			 short *daang, short *dacursectnum)
{
	short fil, i, j, numsprites, ts;
	int tl;
	struct sectortypev5 v5sect;
	struct walltypev5   v5wall;
	struct spritetypev5 v5spr;
	struct sectortypev6 v6sect;
	struct walltypev6   v6wall;
	struct spritetypev6 v6spr;

	if (mapversion == 4) {
		buildputs("saveoldboard: map version 4 not supported\n");
		return -2;
	} else if (mapversion != 5 && mapversion != 6) {
		buildputs("saveoldboard: map version not 5 or 6\n");
		return -2;
	}

	numsprites = 0;
	for(j=0;j<MAXSTATUS;j++)
	{
		i = headspritestat[j];
		while (i != -1)
		{
			numsprites++;
			i = nextspritestat[i];
		}
	}

	switch (mapversion) {
		case 5:
			if (numsectors > MAXSECTORSV5 || numwalls > MAXWALLSV5 || numsprites > MAXSPRITESV5) {
				buildprintf("saveoldboard: too many sectors/walls/sprites for map version 5 (%d/%d, %d/%d, %d/%d)\n",
					numsectors, MAXSECTORSV5, numwalls, MAXWALLSV5, numsprites, MAXSPRITESV5);
				return -2;
			}
			break;
		case 6:
			if (numsectors > MAXSECTORSV6 || numwalls > MAXWALLSV6 || numsprites > MAXSPRITESV6) {
				buildprintf("saveoldboard: too many sectors/walls/sprites for map version 6 (%d/%d, %d/%d, %d/%d)\n",
					numsectors, MAXSECTORSV6, numwalls, MAXWALLSV6, numsprites, MAXSPRITESV6);
				return -2;
			}
			break;
	}

	if ((fil = Bopen(filename,BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY,BS_IREAD|BS_IWRITE)) == -1)
		return(-1);

	tl = B_LITTLE32(mapversion);
	if (Bwrite(fil,&tl,4) != 4) goto writeerror;

	tl = B_LITTLE32(*daposx);
	if (Bwrite(fil,&tl,4) != 4) goto writeerror;
	tl = B_LITTLE32(*daposy);
	if (Bwrite(fil,&tl,4) != 4) goto writeerror;
	tl = B_LITTLE32(*daposz);
	if (Bwrite(fil,&tl,4) != 4) goto writeerror;
	ts = B_LITTLE16(*daang);
	if (Bwrite(fil,&ts,2) != 2) goto writeerror;
	ts = B_LITTLE16(*dacursectnum);
	if (Bwrite(fil,&ts,2) != 2) goto writeerror;

	ts = B_LITTLE16(numsectors);
	if (Bwrite(fil,&ts,2) != 2) goto writeerror;
	for (i=0; i<numsectors; i++) {
		switch (mapversion) {
			case 6:
				convertv7sectv6(&sector[i], &v6sect);
				if (writev6sect(fil, &v6sect)) goto writeerror;
				break;
			case 5:
				convertv7sectv6(&sector[i], &v6sect);
				convertv6sectv5(&v6sect, &v5sect);
				if (writev5sect(fil, &v5sect)) goto writeerror;
				break;
		}
	}

	ts = B_LITTLE16(numwalls);
	if (Bwrite(fil,&ts,2) != 2) goto writeerror;
	for (i=0; i<numwalls; i++) {
		switch (mapversion) {
			case 6:
				convertv7wallv6(&wall[i], &v6wall);
				if (writev6wall(fil, &v6wall)) goto writeerror;
				break;
			case 5:
				convertv7wallv6(&wall[i], &v6wall);
				convertv6wallv5(&v6wall, &v5wall);
				if (writev5wall(fil, &v5wall)) goto writeerror;
				break;
		}
	}

	ts = B_LITTLE16(numsprites);
	if (Bwrite(fil,&ts,2) != 2) goto writeerror;
	for(j=0;j<MAXSTATUS;j++)
	{
		i = headspritestat[j];
		while (i != -1)
		{
			switch (mapversion) {
				case 6:
					convertv7sprv6(&sprite[i], &v6spr);
					if (writev6sprite(fil, &v6spr)) goto writeerror;
					break;
				case 5:
					convertv7sprv6(&sprite[i], &v6spr);
					convertv6sprv5(&v6spr, &v5spr);
					if (writev5sprite(fil, &v5spr)) goto writeerror;
					break;
			}
			i = nextspritestat[i];
		}
	}

	Bclose(fil);
	return(0);

writeerror:
	Bclose(fil);
	return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl 0x71b5a(%rip), %eax     # 0xa3798
cmpl $0x4, %eax
jne 0x31c4c
leaq 0x445cf(%rip), %rdi     # 0x76219
jmp 0x31c5e
movq %rcx, %r12
leal -0x7(%rax), %ecx
cmpl $-0x2, %ecx
jae 0x31c7c
leaq 0x445e6(%rip), %rdi     # 0x76244
callq 0x32200
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movl %edx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r9, %r10
movq %rdx, %r13
movq %rsi, %rbp
xorl %ecx, %ecx
leaq 0x38b892(%rip), %r14    # 0x3bd520
leaq 0x39409b(%rip), %rbx    # 0x3c5d30
xorl %r15d, %r15d
movzwl (%r14,%rcx,2), %edx
cmpw $-0x1, %dx
je 0x31cb0
incl %r15d
movswq %dx, %rdx
movzwl (%rbx,%rdx,2), %edx
jmp 0x31c9d
incq %rcx
cmpq $0x400, %rcx            # imm = 0x400
jne 0x31c98
cmpl $0x6, %eax
je 0x31d1b
cmpl $0x5, %eax
jne 0x31d7b
movswl 0x39d28f(%rip), %esi    # 0x3cef60
cmpl $0x401, %esi            # imm = 0x401
setge %al
movswl 0x3e3f87(%rip), %ecx    # 0x415c68
movl $0x1001, %edx           # imm = 0x1001
cmpl %edx, %ecx
setge %r11b
movswl %r15w, %r9d
cmpl %edx, %r9d
setge %dl
orb %al, %dl
orb %r11b, %dl
cmpb $0x1, %dl
jne 0x31d7b
movl $0x1000, (%rsp)         # imm = 0x1000
leaq 0x4455c(%rip), %rdi     # 0x7626a
movl $0x400, %edx            # imm = 0x400
movl $0x1000, %r8d           # imm = 0x1000
jmp 0x31d6f
movswl 0x39d23e(%rip), %esi    # 0x3cef60
cmpl $0x401, %esi            # imm = 0x401
setge %al
movswl 0x3e3f36(%rip), %ecx    # 0x415c68
cmpl $0x2001, %ecx           # imm = 0x2001
setge %dl
movswl %r15w, %r9d
cmpl $0x1001, %r9d           # imm = 0x1001
setge %r11b
orb %al, %r11b
orb %dl, %r11b
cmpb $0x1, %r11b
jne 0x31d7b
movl $0x1000, (%rsp)         # imm = 0x1000
leaq 0x4455c(%rip), %rdi     # 0x762c0
movl $0x400, %edx            # imm = 0x400
movl $0x2000, %r8d           # imm = 0x2000
xorl %eax, %eax
callq 0x24cb4
jmp 0x31c63
movq %r8, 0x68(%rsp)
movq %r10, 0x60(%rsp)
movl $0x241, %esi            # imm = 0x241
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x6490
movl %eax, %ecx
shll $0x10, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
je 0x31c68
movswl %ax, %edi
movl 0x719e3(%rip), %eax     # 0xa3798
leaq 0x18(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl %edi, 0x10(%rsp)
callq 0x6af0
cmpq $0x4, %rax
jne 0x321ed
movl (%rbp), %eax
leaq 0x18(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl 0x10(%rsp), %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x321ed
movl (%r13), %eax
leaq 0x18(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl 0x10(%rsp), %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x321ed
movl (%r12), %eax
leaq 0x18(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl 0x10(%rsp), %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x321ed
movq 0x68(%rsp), %rax
movzwl (%rax), %eax
leaq 0x16(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl 0x10(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x321ed
movq 0x60(%rsp), %rax
movzwl (%rax), %eax
leaq 0x16(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl 0x10(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x321ed
movzwl 0x39d0cd(%rip), %eax    # 0x3cef60
leaq 0x16(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl 0x10(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x321ed
cmpw $0x0, 0x39d0a5(%rip)    # 0x3cef60
jle 0x31fa2
leaq 0x210b98(%rip), %r13    # 0x242a60
xorl %r12d, %r12d
movl 0x718c7(%rip), %eax     # 0xa3798
cmpl $0x5, %eax
je 0x31f00
cmpl $0x6, %eax
jne 0x31f8a
movq %r13, %rdi
leaq 0x74(%rsp), %rbp
movq %rbp, %rsi
callq 0x32238
movl 0x10(%rsp), %edi
movq %rbp, %rsi
callq 0x322f9
jmp 0x31f82
movq %r13, %rdi
leaq 0x74(%rsp), %rsi
callq 0x32238
movl 0x74(%rsp), %eax
movl %eax, 0xf0(%rsp)
movups 0x78(%rsp), %xmm0
movups %xmm0, 0xf4(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x104(%rsp)
movzwl 0x90(%rsp), %eax
movw %ax, 0x10c(%rsp)
movb 0x92(%rsp), %al
movb %al, 0x10e(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x110(%rsp)
movzwl 0x98(%rsp), %eax
movw %ax, 0x114(%rsp)
movl 0x10(%rsp), %edi
leaq 0xf0(%rsp), %rsi
callq 0x3259e
testl %eax, %eax
jne 0x321ed
incq %r12
movswq 0x39cfcb(%rip), %rax    # 0x3cef60
addq $0x28, %r13
cmpq %rax, %r12
jl 0x31ecb
movzwl 0x3e3cbf(%rip), %eax    # 0x415c68
leaq 0x16(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl 0x10(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x321ed
cmpw $0x0, 0x3e3c97(%rip)    # 0x415c68
jle 0x320a6
leaq 0x185a72(%rip), %r13    # 0x1b7a50
xorl %r12d, %r12d
movl 0x717b1(%rip), %eax     # 0xa3798
cmpl $0x5, %eax
je 0x32013
cmpl $0x6, %eax
jne 0x3208e
movq %r13, %rdi
leaq 0x1c(%rsp), %rbp
movq %rbp, %rsi
callq 0x32843
movl 0x10(%rsp), %edi
movq %rbp, %rsi
callq 0x328ba
jmp 0x32086
movq %r13, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x32843
movq 0x1c(%rsp), %rax
movq %rax, 0x3c(%rsp)
movzwl 0x24(%rsp), %eax
movw %ax, 0x44(%rsp)
movl 0x26(%rsp), %eax
movl %eax, 0x52(%rsp)
movl $0xffffffff, 0x56(%rsp) # imm = 0xFFFFFFFF
movl 0x2a(%rsp), %eax
movl %eax, 0x46(%rsp)
movb 0x2e(%rsp), %al
movb %al, 0x4a(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x4c(%rsp)
movzwl 0x34(%rsp), %eax
movw %ax, 0x50(%rsp)
movzwl 0x36(%rsp), %eax
movw %ax, 0x5a(%rsp)
movl 0x38(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl 0x10(%rsp), %edi
leaq 0x3c(%rsp), %rsi
callq 0x32add
testl %eax, %eax
jne 0x321ed
incq %r12
movswq 0x3e3bcf(%rip), %rax    # 0x415c68
addq $0x20, %r13
cmpq %rax, %r12
jl 0x31fe1
leaq 0x16(%rsp), %rsi
movw %r15w, (%rsi)
movl $0x2, %edx
movl 0x10(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x321ed
xorl %r13d, %r13d
leaq 0xc4(%rsp), %r12
movzwl (%r14,%r13,2), %eax
cmpw $-0x1, %ax
je 0x321cd
movl 0x716b1(%rip), %ecx     # 0xa3798
movswq %ax, %r15
cmpl $0x5, %ecx
je 0x32120
cmpl $0x6, %ecx
jne 0x321c3
imulq $0x2c, %r15, %rdi
leaq 0x249bac(%rip), %rax    # 0x27bcb0
addq %rax, %rdi
movq %r12, %rsi
callq 0x32d30
movl 0x10(%rsp), %edi
movq %r12, %rsi
callq 0x32dcb
jmp 0x321bf
imulq $0x2c, %r15, %rdi
leaq 0x249b85(%rip), %rax    # 0x27bcb0
addq %rax, %rdi
movq %r12, %rsi
callq 0x32d30
movl 0xc4(%rsp), %eax
movl %eax, 0x9c(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0xa0(%rsp)
movb 0xd0(%rsp), %al
movb %al, 0xa8(%rsp)
movb 0xd2(%rsp), %al
movb %al, 0xa9(%rsp)
movzwl 0xd5(%rsp), %eax
movw %ax, 0xaa(%rsp)
movups 0xda(%rsp), %xmm0
movups %xmm0, 0xac(%rsp)
movl 0xea(%rsp), %eax
movl %eax, 0xbc(%rsp)
movzwl 0xee(%rsp), %eax
movw %ax, 0xc0(%rsp)
movl 0x10(%rsp), %edi
leaq 0x9c(%rsp), %rsi
callq 0x33096
testl %eax, %eax
jne 0x321ed
movzwl (%rbx,%r15,2), %eax
jmp 0x320d7
incq %r13
cmpq $0x400, %r13            # imm = 0x400
jne 0x320d2
movl 0x10(%rsp), %edi
callq 0x6220
xorl %edx, %edx
jmp 0x31c68
movl 0x10(%rsp), %edi
callq 0x6220
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jmp 0x31c68