// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: saveboard
int saveboard(char *filename, int *daposx, int *daposy, int *daposz,
			 short *daang, short *dacursectnum)
{
	short fil, i, j, numsprites, ts;
	int tl;
	sectortype tsect;
	walltype   twall;
	spritetype tspri;

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

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

	if (numsectors > MAXSECTORSV7 || numwalls > MAXWALLSV7 || numsprites > MAXSPRITESV7)
		mapversion = 8;
	else
		mapversion = 7;
	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++) {
		tsect = sector[i];
		tsect.wallptr       = B_LITTLE16(tsect.wallptr);
		tsect.wallnum       = B_LITTLE16(tsect.wallnum);
		tsect.ceilingz      = B_LITTLE32(tsect.ceilingz);
		tsect.floorz        = B_LITTLE32(tsect.floorz);
		tsect.ceilingstat   = B_LITTLE16(tsect.ceilingstat);
		tsect.floorstat     = B_LITTLE16(tsect.floorstat);
		tsect.ceilingpicnum = B_LITTLE16(tsect.ceilingpicnum);
		tsect.ceilingheinum = B_LITTLE16(tsect.ceilingheinum);
		tsect.floorpicnum   = B_LITTLE16(tsect.floorpicnum);
		tsect.floorheinum   = B_LITTLE16(tsect.floorheinum);
		tsect.lotag         = B_LITTLE16(tsect.lotag);
		tsect.hitag         = B_LITTLE16(tsect.hitag);
		tsect.extra         = B_LITTLE16(tsect.extra);
		if (Bwrite(fil,&tsect,sizeof(sectortype)) != sizeof(sectortype)) goto writeerror;
	}

	ts = B_LITTLE16(numwalls);      if (Bwrite(fil,&ts,2) != 2) goto writeerror;
	for (i=0; i<numwalls; i++) {
		twall = wall[i];
		twall.x          = B_LITTLE32(twall.x);
		twall.y          = B_LITTLE32(twall.y);
		twall.point2     = B_LITTLE16(twall.point2);
		twall.nextwall   = B_LITTLE16(twall.nextwall);
		twall.nextsector = B_LITTLE16(twall.nextsector);
		twall.cstat      = B_LITTLE16(twall.cstat);
		twall.picnum     = B_LITTLE16(twall.picnum);
		twall.overpicnum = B_LITTLE16(twall.overpicnum);
		twall.lotag      = B_LITTLE16(twall.lotag);
		twall.hitag      = B_LITTLE16(twall.hitag);
		twall.extra      = B_LITTLE16(twall.extra);
		if (Bwrite(fil,&twall,sizeof(walltype)) != sizeof(walltype)) goto writeerror;
	}

	ts = B_LITTLE16(numsprites);    if (Bwrite(fil,&ts,2) != 2) goto writeerror;

	for(j=0;j<MAXSTATUS;j++)
	{
		i = headspritestat[j];
		while (i != -1)
		{
			tspri = sprite[i];
			tspri.x       = B_LITTLE32(tspri.x);
			tspri.y       = B_LITTLE32(tspri.y);
			tspri.z       = B_LITTLE32(tspri.z);
			tspri.cstat   = B_LITTLE16(tspri.cstat);
			tspri.picnum  = B_LITTLE16(tspri.picnum);
			tspri.sectnum = B_LITTLE16(tspri.sectnum);
			tspri.statnum = B_LITTLE16(tspri.statnum);
			tspri.ang     = B_LITTLE16(tspri.ang);
			tspri.owner   = B_LITTLE16(tspri.owner);
			tspri.xvel    = B_LITTLE16(tspri.xvel);
			tspri.yvel    = B_LITTLE16(tspri.yvel);
			tspri.zvel    = B_LITTLE16(tspri.zvel);
			tspri.lotag   = B_LITTLE16(tspri.lotag);
			tspri.hitag   = B_LITTLE16(tspri.hitag);
			tspri.extra   = B_LITTLE16(tspri.extra);
			if (Bwrite(fil,&tspri,sizeof(spritetype)) != sizeof(spritetype)) goto writeerror;
			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 $0xb8, %rsp
movq %r9, %r14
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %rbp
movl $0x241, %esi            # imm = 0x241
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x6490
movl %eax, %ecx
shll $0x10, %ecx
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
je 0x31c08
movq %r12, 0x10(%rsp)
movq %r15, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movswl %ax, %r12d
xorl %eax, %eax
leaq 0x38bc0f(%rip), %r14    # 0x3bd520
leaq 0x394418(%rip), %r15    # 0x3c5d30
xorl %ebx, %ebx
movzwl (%r14,%rax,2), %ecx
cmpw $-0x1, %cx
je 0x31932
incl %ebx
movswq %cx, %rcx
movzwl (%r15,%rcx,2), %ecx
jmp 0x3191f
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x3191a
cmpw $0x401, 0x39d61a(%rip)  # imm = 0x401
setge %al
movswl 0x3e4318(%rip), %ecx    # 0x415c68
cmpl $0x2001, %ecx           # imm = 0x2001
setge %cl
movswl %bx, %edx
cmpl $0x1001, %edx           # imm = 0x1001
setge %dl
orb %al, %dl
orb %cl, %dl
movzbl %dl, %eax
addl $0x7, %eax
movl %eax, 0x71e23(%rip)     # 0xa3798
leaq 0x1c(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl %r12d, %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x31bfb
movl (%rbp), %eax
leaq 0x1c(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl %r12d, %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x31bfb
movl (%r13), %eax
leaq 0x1c(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl %r12d, %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x31bfb
movq 0x10(%rsp), %rax
movl (%rax), %eax
leaq 0x1c(%rsp), %rsi
movl %eax, (%rsi)
movl $0x4, %edx
movl %r12d, %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x31bfb
movq 0x28(%rsp), %rax
movzwl (%rax), %eax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl %r12d, %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x31bfb
movq 0x20(%rsp), %rax
movzwl (%rax), %eax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl %r12d, %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x31bfb
movzwl 0x39d510(%rip), %eax    # 0x3cef60
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
movl %r12d, 0x10(%rsp)
movl %r12d, %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x31c1c
cmpw $0x0, 0x39d4e4(%rip)    # 0x3cef60
movl 0x10(%rsp), %edi
jle 0x31adc
leaq 0x210fd7(%rip), %r13    # 0x242a60
xorl %ebp, %ebp
leaq 0x30(%rsp), %r12
movq 0x20(%r13), %rax
movq %rax, 0x50(%rsp)
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movl $0x28, %edx
movq %r12, %rsi
callq 0x6af0
cmpq $0x28, %rax
jne 0x31c1c
incq %rbp
movswq 0x39d491(%rip), %rax    # 0x3cef60
addq $0x28, %r13
cmpq %rax, %rbp
movl 0x10(%rsp), %edi
jl 0x31a90
movzwl 0x3e4185(%rip), %eax    # 0x415c68
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
movl $0x2, %edx
callq 0x6af0
cmpq $0x2, %rax
jne 0x31c1c
cmpw $0x0, 0x3e4161(%rip)    # 0x415c68
movl 0x10(%rsp), %edi
jle 0x31b5e
leaq 0x185f3c(%rip), %r13    # 0x1b7a50
xorl %ebp, %ebp
leaq 0x60(%rsp), %r12
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movaps %xmm1, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movl $0x20, %edx
movq %r12, %rsi
callq 0x6af0
cmpq $0x20, %rax
jne 0x31c1c
incq %rbp
movswq 0x3e4117(%rip), %rax    # 0x415c68
addq $0x20, %r13
cmpq %rax, %rbp
movl 0x10(%rsp), %edi
jl 0x31b1b
leaq 0xe(%rsp), %rsi
movw %bx, (%rsi)
movl $0x2, %edx
callq 0x6af0
cmpq $0x2, %rax
jne 0x31c1c
xorl %r13d, %r13d
leaq 0x24a12c(%rip), %rbp    # 0x27bcb0
leaq 0x80(%rsp), %r12
movl 0x10(%rsp), %edi
movzwl (%r14,%r13,2), %eax
cmpw $-0x1, %ax
je 0x31beb
movswq %ax, %rbx
imulq $0x2c, %rbx, %rax
movups (%rax,%rbp), %xmm0
movups 0x10(%rax,%rbp), %xmm1
movups 0x1c(%rax,%rbp), %xmm2
movups %xmm2, 0x9c(%rsp)
movaps %xmm1, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movl $0x2c, %edx
movq %r12, %rsi
callq 0x6af0
cmpq $0x2c, %rax
jne 0x31c1c
movzwl (%r15,%rbx,2), %eax
cmpw $-0x1, %ax
movl 0x10(%rsp), %edi
jne 0x31b9b
incq %r13
xorl %ebx, %ebx
cmpq $0x400, %r13            # imm = 0x400
jne 0x31b90
jmp 0x31c03
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %r12d, %edi
callq 0x6220
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl 0x10(%rsp), %edi
jmp 0x31c03