// File: /workspace/github_c_makefile_star2_O3/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 0x7203c(%rip), %eax     # 0xa4798
cmpl $0x4, %eax
jne 0x3276a
leaq 0x44c31(%rip), %rdi     # 0x77399
jmp 0x3277c
movq %rcx, %r12
leal -0x7(%rax), %ecx
cmpl $-0x2, %ecx
jae 0x3279a
leaq 0x44c48(%rip), %rdi     # 0x773c4
callq 0x32d18
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 0x38bd74(%rip), %r14    # 0x3be520
leaq 0x39457d(%rip), %rbx    # 0x3c6d30
xorl %r15d, %r15d
movzwl (%r14,%rcx,2), %edx
cmpw $-0x1, %dx
je 0x327ce
incl %r15d
movswq %dx, %rdx
movzwl (%rbx,%rdx,2), %edx
jmp 0x327bb
incq %rcx
cmpq $0x400, %rcx            # imm = 0x400
jne 0x327b6
cmpl $0x6, %eax
je 0x32839
cmpl $0x5, %eax
jne 0x32899
movswl 0x39d771(%rip), %esi    # 0x3cff60
cmpl $0x401, %esi            # imm = 0x401
setge %al
movswl 0x3e4469(%rip), %ecx    # 0x416c68
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 0x32899
movl $0x1000, (%rsp)         # imm = 0x1000
leaq 0x44bbe(%rip), %rdi     # 0x773ea
movl $0x400, %edx            # imm = 0x400
movl $0x1000, %r8d           # imm = 0x1000
jmp 0x3288d
movswl 0x39d720(%rip), %esi    # 0x3cff60
cmpl $0x401, %esi            # imm = 0x401
setge %al
movswl 0x3e4418(%rip), %ecx    # 0x416c68
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 0x32899
movl $0x1000, (%rsp)         # imm = 0x1000
leaq 0x44bbe(%rip), %rdi     # 0x77440
movl $0x400, %edx            # imm = 0x400
movl $0x2000, %r8d           # imm = 0x2000
xorl %eax, %eax
callq 0x25221
jmp 0x32781
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 0x32786
movswl %ax, %edi
movl 0x71ec5(%rip), %eax     # 0xa4798
leaq 0x14(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl %edi, 0xc(%rsp)
callq 0x6af0
cmpq $0x4, %rax
jne 0x32d05
movl (%rbp), %eax
leaq 0x14(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl 0xc(%rsp), %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x32d05
movl (%r13), %eax
leaq 0x14(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl 0xc(%rsp), %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x32d05
movl (%r12), %eax
leaq 0x14(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl 0xc(%rsp), %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x32d05
movq 0x68(%rsp), %rax
movzwl (%rax), %eax
leaq 0x12(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl 0xc(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x32d05
movq 0x60(%rsp), %rax
movzwl (%rax), %eax
leaq 0x12(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl 0xc(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x32d05
movzwl 0x39d5af(%rip), %eax    # 0x3cff60
leaq 0x12(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl 0xc(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x32d05
cmpw $0x0, 0x39d587(%rip)    # 0x3cff60
jle 0x32aba
leaq 0x21107a(%rip), %r13    # 0x243a60
xorl %r12d, %r12d
movl 0x71da9(%rip), %eax     # 0xa4798
cmpl $0x5, %eax
je 0x32a1b
cmpl $0x6, %eax
jne 0x32aa2
movq %r13, %rdi
leaq 0x70(%rsp), %rbp
movq %rbp, %rsi
callq 0x32d50
movl 0xc(%rsp), %edi
movq %rbp, %rsi
callq 0x32e11
jmp 0x32a9a
movq %r13, %rdi
leaq 0x70(%rsp), %rsi
callq 0x32d50
movq 0x70(%rsp), %rax
movq %rax, 0xf0(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0xf8(%rsp)
movups 0x7c(%rsp), %xmm0
movups %xmm0, 0xfc(%rsp)
movb 0x8c(%rsp), %al
movb %al, 0x10c(%rsp)
movzwl 0x8d(%rsp), %eax
movw %ax, 0x10d(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x110(%rsp)
movzwl 0x94(%rsp), %eax
movw %ax, 0x114(%rsp)
movl 0xc(%rsp), %edi
leaq 0xf0(%rsp), %rsi
callq 0x330b6
testl %eax, %eax
jne 0x32d05
incq %r12
movswq 0x39d4b3(%rip), %rax    # 0x3cff60
addq $0x28, %r13
cmpq %rax, %r12
jl 0x329e9
movzwl 0x3e41a7(%rip), %eax    # 0x416c68
leaq 0x12(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl 0xc(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x32d05
cmpw $0x0, 0x3e417f(%rip)    # 0x416c68
jle 0x32bbe
leaq 0x185f5a(%rip), %r13    # 0x1b8a50
xorl %r12d, %r12d
movl 0x71c99(%rip), %eax     # 0xa4798
cmpl $0x5, %eax
je 0x32b2b
cmpl $0x6, %eax
jne 0x32ba6
movq %r13, %rdi
leaq 0x18(%rsp), %rbp
movq %rbp, %rsi
callq 0x3335b
movl 0xc(%rsp), %edi
movq %rbp, %rsi
callq 0x333d2
jmp 0x32b9e
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3335b
movq 0x18(%rsp), %rax
movq %rax, 0x38(%rsp)
movzwl 0x20(%rsp), %eax
movw %ax, 0x40(%rsp)
movl 0x22(%rsp), %eax
movl %eax, 0x4e(%rsp)
movl $0xffffffff, 0x52(%rsp) # imm = 0xFFFFFFFF
movl 0x26(%rsp), %eax
movl %eax, 0x42(%rsp)
movb 0x2a(%rsp), %al
movb %al, 0x46(%rsp)
movzwl 0x2c(%rsp), %eax
movw %ax, 0x48(%rsp)
movl 0x2e(%rsp), %eax
movl %eax, 0x4a(%rsp)
movl 0x32(%rsp), %eax
movl %eax, 0x56(%rsp)
movzwl 0x36(%rsp), %eax
movw %ax, 0x5a(%rsp)
movl 0xc(%rsp), %edi
leaq 0x38(%rsp), %rsi
callq 0x335f5
testl %eax, %eax
jne 0x32d05
incq %r12
movswq 0x3e40b7(%rip), %rax    # 0x416c68
addq $0x20, %r13
cmpq %rax, %r12
jl 0x32af9
leaq 0x12(%rsp), %rsi
movw %r15w, (%rsi)
movl $0x2, %edx
movl 0xc(%rsp), %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x32d05
xorl %r13d, %r13d
leaq 0xc0(%rsp), %r12
movzwl (%r14,%r13,2), %eax
cmpw $-0x1, %ax
je 0x32ce5
movl 0x71b99(%rip), %ecx     # 0xa4798
movswq %ax, %r15
cmpl $0x5, %ecx
je 0x32c38
cmpl $0x6, %ecx
jne 0x32cdb
imulq $0x2c, %r15, %rdi
leaq 0x24a094(%rip), %rax    # 0x27ccb0
addq %rax, %rdi
movq %r12, %rsi
callq 0x33848
movl 0xc(%rsp), %edi
movq %r12, %rsi
callq 0x338e3
jmp 0x32cd7
imulq $0x2c, %r15, %rdi
leaq 0x24a06d(%rip), %rax    # 0x27ccb0
addq %rax, %rdi
movq %r12, %rsi
callq 0x33848
movq 0xc0(%rsp), %rax
movq %rax, 0x98(%rsp)
movl 0xc8(%rsp), %eax
movl %eax, 0xa0(%rsp)
movb 0xcc(%rsp), %al
movb %al, 0xa4(%rsp)
movb 0xce(%rsp), %al
movb %al, 0xa5(%rsp)
movzwl 0xd1(%rsp), %eax
movw %ax, 0xa6(%rsp)
movups 0xd6(%rsp), %xmm0
movups %xmm0, 0xa8(%rsp)
movl 0xe6(%rsp), %eax
movl %eax, 0xb8(%rsp)
movzwl 0xea(%rsp), %eax
movw %ax, 0xbc(%rsp)
movl 0xc(%rsp), %edi
leaq 0x98(%rsp), %rsi
callq 0x33bae
testl %eax, %eax
jne 0x32d05
movzwl (%rbx,%r15,2), %eax
jmp 0x32bef
incq %r13
cmpq $0x400, %r13            # imm = 0x400
jne 0x32bea
movl 0xc(%rsp), %edi
callq 0x6220
xorl %edx, %edx
jmp 0x32786
movl 0xc(%rsp), %edi
callq 0x6220
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jmp 0x32786