// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movl $0x241, %esi            # imm = 0x241
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x6490
movl %eax, %ecx
shll $0x10, %ecx
cmpl $0xffff0000, %ecx       # imm = 0xFFFF0000
jne 0x2f2dd
pushq $-0x1
popq %rbx
jmp 0x2f4b6
movq %r12, 0x10(%rsp)
movq %r15, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movswl %ax, %r12d
xorl %eax, %eax
leaq 0x388227(%rip), %r14    # 0x3b7520
leaq 0x390a30(%rip), %rbp    # 0x3bfd30
xorl %r15d, %r15d
cmpq $0x400, %rax            # imm = 0x400
je 0x2f32e
leaq (%r14,%rax,2), %rcx
movswq (%rcx), %rcx
cmpq $-0x1, %rcx
je 0x2f329
incl %r15d
leaq (,%rcx,2), %rcx
addq %rbp, %rcx
jmp 0x2f30f
incq %rax
jmp 0x2f303
cmpw $0x401, 0x399c29(%rip)  # imm = 0x401
setge %al
cmpw $0x2001, 0x3e0925(%rip) # imm = 0x2001
setge %cl
orb %al, %cl
cmpw $0x1001, %r15w          # imm = 0x1001
setge %al
orb %cl, %al
movzbl %al, %eax
addl $0x7, %eax
movl %eax, 0x6e439(%rip)     # 0x9d798
leaq 0x1c(%rsp), %rsi
movl %eax, (%rsi)
pushq $0x4
popq %rdx
movl %r12d, %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x2f4ab
movl (%rbx), %eax
leaq 0x1c(%rsp), %rsi
movl %eax, (%rsi)
pushq $0x4
popq %rdx
movl %r12d, %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x2f4ab
movl (%r13), %eax
leaq 0x1c(%rsp), %rsi
movl %eax, (%rsi)
pushq $0x4
popq %rdx
movl %r12d, %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x2f4ab
movq 0x10(%rsp), %rax
movl (%rax), %eax
leaq 0x1c(%rsp), %rsi
movl %eax, (%rsi)
pushq $0x4
popq %rdx
movl %r12d, %edi
callq 0x6af0
cmpq $0x4, %rax
jne 0x2f4ab
movq 0x20(%rsp), %rax
movzwl (%rax), %eax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
pushq $0x2
popq %rdx
movl %r12d, %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x2f4ab
movq 0x28(%rsp), %rax
movzwl (%rax), %eax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
pushq $0x2
popq %rdx
movl %r12d, %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x2f4ab
movzwl 0x399b33(%rip), %eax    # 0x3c8f60
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
pushq $0x2
popq %rdx
movl %r12d, 0x10(%rsp)
movl %r12d, %edi
callq 0x6af0
cmpq $0x2, %rax
jne 0x2f5e6
leaq 0x20d60a(%rip), %rcx    # 0x23ca60
pushq $-0x1
popq %r13
pushq $0x28
popq %r12
movswq 0x399afa(%rip), %rax    # 0x3c8f60
incq %r13
cmpq %rax, %r13
movl 0x10(%rsp), %edi
jge 0x2f4ca
leaq 0x28(%rcx), %rbx
movq 0x20(%rcx), %rax
movq %rax, 0x50(%rsp)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
leaq 0x30(%rsp), %rsi
movq %r12, %rdx
callq 0x6af0
movq %rbx, %rcx
cmpq $0x28, %rax
je 0x2f45e
jmp 0x2f5e6
movl %r12d, %edi
pushq $-0x1
popq %rbx
callq 0x6220
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x3e0797(%rip), %eax    # 0x40fc68
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
pushq $0x2
popq %rdx
callq 0x6af0
cmpq $0x2, %rax
jne 0x2f5e6
leaq 0x18255e(%rip), %rcx    # 0x1b1a50
pushq $-0x1
popq %r13
pushq $0x20
popq %r12
movswq 0x3e0766(%rip), %rax    # 0x40fc68
incq %r13
cmpq %rax, %r13
movl 0x10(%rsp), %edi
jge 0x2f53e
leaq 0x20(%rcx), %rbx
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movaps %xmm1, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
leaq 0x60(%rsp), %rsi
movq %r12, %rdx
callq 0x6af0
movq %rbx, %rcx
cmpq $0x20, %rax
je 0x2f4fa
jmp 0x2f5e6
leaq 0xe(%rsp), %rsi
movw %r15w, (%rsi)
pushq $0x2
popq %rdx
callq 0x6af0
cmpq $0x2, %rax
jne 0x2f5e6
xorl %eax, %eax
leaq 0x24674e(%rip), %r13    # 0x275cb0
leaq 0x80(%rsp), %rbx
pushq $0x2c
popq %r15
movl 0x10(%rsp), %edi
cmpq $0x400, %rax            # imm = 0x400
je 0x2f5ef
movq %rax, 0x20(%rsp)
leaq (%r14,%rax,2), %rax
movswq (%rax), %r12
cmpq $-0x1, %r12
je 0x2f5dc
imulq $0x2c, %r12, %rax
movups (%rax,%r13), %xmm0
movups 0x10(%rax,%r13), %xmm1
movups 0x1c(%rax,%r13), %xmm2
movups %xmm2, 0x9c(%rsp)
movaps %xmm1, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movq %rbx, %rsi
movq %r15, %rdx
callq 0x6af0
cmpq $0x2c, %rax
jne 0x2f5e6
leaq (,%r12,2), %rax
addq %rbp, %rax
movl 0x10(%rsp), %edi
jmp 0x2f583
movq 0x20(%rsp), %rax
incq %rax
jmp 0x2f572
movl 0x10(%rsp), %edi
jmp 0x2f4ae
xorl %ebx, %ebx
jmp 0x2f4b1