// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x180, %edx            # imm = 0x180
movb $0x0, %al
callq 0x6430
movw %ax, -0x3a(%rbp)
cwtl
cmpl $-0x1, %eax
jne 0x49b6e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4a0c9
movw $0x0, -0x40(%rbp)
movw $0x0, -0x3e(%rbp)
movswl -0x3e(%rbp), %eax
cmpl $0x400, %eax            # imm = 0x400
jge 0x49bd4
movswq -0x3e(%rbp), %rcx
leaq 0x3b14ef(%rip), %rax    # 0x3fb080
movw (%rax,%rcx,2), %ax
movw %ax, -0x3c(%rbp)
movswl -0x3c(%rbp), %eax
cmpl $-0x1, %eax
je 0x49bc4
movw -0x40(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x40(%rbp)
movswq -0x3c(%rbp), %rcx
leaq 0x3b9cd6(%rip), %rax    # 0x403890
movw (%rax,%rcx,2), %ax
movw %ax, -0x3c(%rbp)
jmp 0x49b99
jmp 0x49bc6
movw -0x3e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x3e(%rbp)
jmp 0x49b7a
movswl 0x3c2ee5(%rip), %eax    # 0x40cac0
cmpl $0x400, %eax            # imm = 0x400
jg 0x49bfb
movswl 0x407ddf(%rip), %eax    # 0x4519c8
cmpl $0x2000, %eax           # imm = 0x2000
jg 0x49bfb
movswl -0x40(%rbp), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jle 0x49c07
movl $0x8, 0x97083(%rip)     # 0xe0c88
jmp 0x49c11
movl $0x7, 0x97077(%rip)     # 0xe0c88
movl 0x97071(%rip), %eax     # 0xe0c88
movl %eax, -0x48(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x48(%rbp), %rsi
movl $0x4, %edx
callq 0x6a20
cmpq $0x4, %rax
je 0x49c37
jmp 0x4a0b9
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x48(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x48(%rbp), %rsi
movl $0x4, %edx
callq 0x6a20
cmpq $0x4, %rax
je 0x49c5d
jmp 0x4a0b9
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x48(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x48(%rbp), %rsi
movl $0x4, %edx
callq 0x6a20
cmpq $0x4, %rax
je 0x49c83
jmp 0x4a0b9
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x48(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x48(%rbp), %rsi
movl $0x4, %edx
callq 0x6a20
cmpq $0x4, %rax
je 0x49ca9
jmp 0x4a0b9
movq -0x30(%rbp), %rax
movw (%rax), %ax
movw %ax, -0x42(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x6a20
cmpq $0x2, %rax
je 0x49cd1
jmp 0x4a0b9
movq -0x38(%rbp), %rax
movw (%rax), %ax
movw %ax, -0x42(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x6a20
cmpq $0x2, %rax
je 0x49cf9
jmp 0x4a0b9
movw 0x3c2dc0(%rip), %ax     # 0x40cac0
movw %ax, -0x42(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x6a20
cmpq $0x2, %rax
je 0x49d21
jmp 0x4a0b9
movw $0x0, -0x3c(%rbp)
movswl -0x3c(%rbp), %eax
movswl 0x3c2d8e(%rip), %ecx    # 0x40cac0
cmpl %ecx, %eax
jge 0x49def
movswq -0x3c(%rbp), %rax
leaq 0x23687a(%rip), %rsi    # 0x2805c0
imulq $0x28, %rax, %rax
addq %rax, %rsi
leaq -0x70(%rbp), %rdi
movl $0x28, %edx
callq 0x60c0
movw -0x70(%rbp), %ax
movw %ax, -0x70(%rbp)
movw -0x6e(%rbp), %ax
movw %ax, -0x6e(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x68(%rbp)
movw -0x64(%rbp), %ax
movw %ax, -0x64(%rbp)
movw -0x62(%rbp), %ax
movw %ax, -0x62(%rbp)
movw -0x60(%rbp), %ax
movw %ax, -0x60(%rbp)
movw -0x5e(%rbp), %ax
movw %ax, -0x5e(%rbp)
movw -0x58(%rbp), %ax
movw %ax, -0x58(%rbp)
movw -0x56(%rbp), %ax
movw %ax, -0x56(%rbp)
movw -0x4e(%rbp), %ax
movw %ax, -0x4e(%rbp)
movw -0x4c(%rbp), %ax
movw %ax, -0x4c(%rbp)
movw -0x4a(%rbp), %ax
movw %ax, -0x4a(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x70(%rbp), %rsi
movl $0x28, %edx
callq 0x6a20
cmpq $0x28, %rax
je 0x49ddc
jmp 0x4a0b9
jmp 0x49dde
movw -0x3c(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x3c(%rbp)
jmp 0x49d27
movw 0x407bd2(%rip), %ax     # 0x4519c8
movw %ax, -0x42(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x6a20
cmpq $0x2, %rax
je 0x49e17
jmp 0x4a0b9
movw $0x0, -0x3c(%rbp)
movswl -0x3c(%rbp), %eax
movswl 0x407ba0(%rip), %ecx    # 0x4519c8
cmpl %ecx, %eax
jge 0x49f13
movswq -0x3c(%rbp), %rcx
leaq 0x1ab774(%rip), %rax    # 0x1f55b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x90(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x88(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x80(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x78(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movw -0x88(%rbp), %ax
movw %ax, -0x88(%rbp)
movw -0x86(%rbp), %ax
movw %ax, -0x86(%rbp)
movw -0x84(%rbp), %ax
movw %ax, -0x84(%rbp)
movw -0x82(%rbp), %ax
movw %ax, -0x82(%rbp)
movw -0x80(%rbp), %ax
movw %ax, -0x80(%rbp)
movw -0x7e(%rbp), %ax
movw %ax, -0x7e(%rbp)
movw -0x76(%rbp), %ax
movw %ax, -0x76(%rbp)
movw -0x74(%rbp), %ax
movw %ax, -0x74(%rbp)
movw -0x72(%rbp), %ax
movw %ax, -0x72(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x90(%rbp), %rsi
movl $0x20, %edx
callq 0x6a20
cmpq $0x20, %rax
je 0x49f00
jmp 0x4a0b9
jmp 0x49f02
movw -0x3c(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x3c(%rbp)
jmp 0x49e1d
movw -0x40(%rbp), %ax
movw %ax, -0x42(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x6a20
cmpq $0x2, %rax
je 0x49f38
jmp 0x4a0b9
movw $0x0, -0x3e(%rbp)
movswl -0x3e(%rbp), %eax
cmpl $0x400, %eax            # imm = 0x400
jge 0x4a0a7
movswq -0x3e(%rbp), %rcx
leaq 0x3b1127(%rip), %rax    # 0x3fb080
movw (%rax,%rcx,2), %ax
movw %ax, -0x3c(%rbp)
movswl -0x3c(%rbp), %eax
cmpl $-0x1, %eax
je 0x4a094
movswq -0x3c(%rbp), %rax
leaq 0x26f896(%rip), %rsi    # 0x2b9810
imulq $0x2c, %rax, %rax
addq %rax, %rsi
leaq -0xbc(%rbp), %rdi
movl $0x2c, %edx
callq 0x60c0
movl -0xbc(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xb8(%rbp), %eax
movl %eax, -0xb8(%rbp)
movl -0xb4(%rbp), %eax
movl %eax, -0xb4(%rbp)
movw -0xb0(%rbp), %ax
movw %ax, -0xb0(%rbp)
movw -0xae(%rbp), %ax
movw %ax, -0xae(%rbp)
movw -0xa4(%rbp), %ax
movw %ax, -0xa4(%rbp)
movw -0xa2(%rbp), %ax
movw %ax, -0xa2(%rbp)
movw -0xa0(%rbp), %ax
movw %ax, -0xa0(%rbp)
movw -0x9e(%rbp), %ax
movw %ax, -0x9e(%rbp)
movw -0x9c(%rbp), %ax
movw %ax, -0x9c(%rbp)
movw -0x9a(%rbp), %ax
movw %ax, -0x9a(%rbp)
movw -0x98(%rbp), %ax
movw %ax, -0x98(%rbp)
movw -0x96(%rbp), %ax
movw %ax, -0x96(%rbp)
movw -0x94(%rbp), %ax
movw %ax, -0x94(%rbp)
movw -0x92(%rbp), %ax
movw %ax, -0x92(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0xbc(%rbp), %rsi
movl $0x2c, %edx
callq 0x6a20
cmpq $0x2c, %rax
je 0x4a07b
jmp 0x4a0b9
movswq -0x3c(%rbp), %rcx
leaq 0x3b9809(%rip), %rax    # 0x403890
movw (%rax,%rcx,2), %ax
movw %ax, -0x3c(%rbp)
jmp 0x49f61
jmp 0x4a096
movw -0x3e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x3e(%rbp)
jmp 0x49f3e
movswl -0x3a(%rbp), %edi
callq 0x6220
movl $0x0, -0x4(%rbp)
jmp 0x4a0c9
movswl -0x3a(%rbp), %edi
callq 0x6220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)