// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpl $0x4, 0x96b7e(%rip)     # 0xe0c88
jne 0x4a124
leaq 0x69099(%rip), %rdi     # 0xb31ac
callq 0x4a720
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x4a70b
cmpl $0x5, 0x96b5d(%rip)     # 0xe0c88
je 0x4a14e
cmpl $0x6, 0x96b54(%rip)     # 0xe0c88
je 0x4a14e
leaq 0x6909a(%rip), %rdi     # 0xb31d7
callq 0x4a720
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x4a70b
jmp 0x4a150
movw $0x0, -0x40(%rbp)
movw $0x0, -0x3e(%rbp)
movswl -0x3e(%rbp), %eax
cmpl $0x400, %eax            # imm = 0x400
jge 0x4a1b6
movswq -0x3e(%rbp), %rcx
leaq 0x3b0f0d(%rip), %rax    # 0x3fb080
movw (%rax,%rcx,2), %ax
movw %ax, -0x3c(%rbp)
movswl -0x3c(%rbp), %eax
cmpl $-0x1, %eax
je 0x4a1a6
movw -0x40(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x40(%rbp)
movswq -0x3c(%rbp), %rcx
leaq 0x3b96f4(%rip), %rax    # 0x403890
movw (%rax,%rcx,2), %ax
movw %ax, -0x3c(%rbp)
jmp 0x4a17b
jmp 0x4a1a8
movw -0x3e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x3e(%rbp)
jmp 0x4a15c
movl 0x96acc(%rip), %eax     # 0xe0c88
movl %eax, -0x134(%rbp)
subl $0x5, %eax
je 0x4a1d9
jmp 0x4a1c9
movl -0x134(%rbp), %eax
subl $0x6, %eax
je 0x4a241
jmp 0x4a2a9
movswl 0x3c28e0(%rip), %eax    # 0x40cac0
cmpl $0x400, %eax            # imm = 0x400
jg 0x4a200
movswl 0x4077da(%rip), %eax    # 0x4519c8
cmpl $0x1000, %eax           # imm = 0x1000
jg 0x4a200
movswl -0x40(%rbp), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jle 0x4a23f
movswl 0x3c28b9(%rip), %esi    # 0x40cac0
movswl 0x4077ba(%rip), %ecx    # 0x4519c8
movswl -0x40(%rbp), %r9d
leaq 0x68fe3(%rip), %rdi     # 0xb31fd
movl $0x400, %edx            # imm = 0x400
movl $0x1000, %r8d           # imm = 0x1000
movl $0x1000, (%rsp)         # imm = 0x1000
movb $0x0, %al
callq 0x37c90
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x4a70b
jmp 0x4a2a9
movswl 0x3c2878(%rip), %eax    # 0x40cac0
cmpl $0x400, %eax            # imm = 0x400
jg 0x4a268
movswl 0x407772(%rip), %eax    # 0x4519c8
cmpl $0x2000, %eax           # imm = 0x2000
jg 0x4a268
movswl -0x40(%rbp), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jle 0x4a2a7
movswl 0x3c2851(%rip), %esi    # 0x40cac0
movswl 0x407752(%rip), %ecx    # 0x4519c8
movswl -0x40(%rbp), %r9d
leaq 0x68fd1(%rip), %rdi     # 0xb3253
movl $0x400, %edx            # imm = 0x400
movl $0x2000, %r8d           # imm = 0x2000
movl $0x1000, (%rsp)         # imm = 0x1000
movb $0x0, %al
callq 0x37c90
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x4a70b
jmp 0x4a2a9
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 0x4a2d4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4a70b
movl 0x969ae(%rip), %eax     # 0xe0c88
movl %eax, -0x48(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x48(%rbp), %rsi
movl $0x4, %edx
callq 0x6a20
cmpq $0x4, %rax
je 0x4a2fa
jmp 0x4a6fb
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 0x4a320
jmp 0x4a6fb
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 0x4a346
jmp 0x4a6fb
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 0x4a36c
jmp 0x4a6fb
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 0x4a394
jmp 0x4a6fb
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 0x4a3bc
jmp 0x4a6fb
movw 0x3c26fd(%rip), %ax     # 0x40cac0
movw %ax, -0x42(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x6a20
cmpq $0x2, %rax
je 0x4a3e4
jmp 0x4a6fb
movw $0x0, -0x3c(%rbp)
movswl -0x3c(%rbp), %eax
movswl 0x3c26cb(%rip), %ecx    # 0x40cac0
cmpl %ecx, %eax
jge 0x4a4b7
movl 0x96885(%rip), %eax     # 0xe0c88
movl %eax, -0x138(%rbp)
subl $0x5, %eax
je 0x4a45c
jmp 0x4a410
movl -0x138(%rbp), %eax
subl $0x6, %eax
jne 0x4a4a4
jmp 0x4a421
movswq -0x3c(%rbp), %rax
leaq 0x236193(%rip), %rdi    # 0x2805c0
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq -0xe4(%rbp), %rsi
callq 0x4a780
movswl -0x3a(%rbp), %edi
leaq -0xe4(%rbp), %rsi
callq 0x4a920
cmpl $0x0, %eax
je 0x4a45a
jmp 0x4a6fb
jmp 0x4a4a4
movswq -0x3c(%rbp), %rax
leaq 0x236158(%rip), %rdi    # 0x2805c0
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq -0xe4(%rbp), %rsi
callq 0x4a780
leaq -0xe4(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4acf0
movswl -0x3a(%rbp), %edi
leaq -0x70(%rbp), %rsi
callq 0x4ae50
cmpl $0x0, %eax
je 0x4a4a2
jmp 0x4a6fb
jmp 0x4a4a4
jmp 0x4a4a6
movw -0x3c(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x3c(%rbp)
jmp 0x4a3ea
movw 0x40750a(%rip), %ax     # 0x4519c8
movw %ax, -0x42(%rbp)
movswl -0x3a(%rbp), %edi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x6a20
cmpq $0x2, %rax
je 0x4a4df
jmp 0x4a6fb
movw $0x0, -0x3c(%rbp)
movswl -0x3c(%rbp), %eax
movswl 0x4074d8(%rip), %ecx    # 0x4519c8
cmpl %ecx, %eax
jge 0x4a5b8
movl 0x9678a(%rip), %eax     # 0xe0c88
movl %eax, -0x13c(%rbp)
subl $0x5, %eax
je 0x4a557
jmp 0x4a50b
movl -0x13c(%rbp), %eax
subl $0x6, %eax
jne 0x4a5a5
jmp 0x4a51c
movswq -0x3c(%rbp), %rax
leaq 0x1ab088(%rip), %rdi    # 0x1f55b0
shlq $0x5, %rax
addq %rax, %rdi
leaq -0x104(%rbp), %rsi
callq 0x4b220
movswl -0x3a(%rbp), %edi
leaq -0x104(%rbp), %rsi
callq 0x4b330
cmpl $0x0, %eax
je 0x4a555
jmp 0x4a6fb
jmp 0x4a5a5
movswq -0x3c(%rbp), %rax
leaq 0x1ab04d(%rip), %rdi    # 0x1f55b0
shlq $0x5, %rax
addq %rax, %rdi
leaq -0x104(%rbp), %rsi
callq 0x4b220
leaq -0x104(%rbp), %rdi
leaq -0x94(%rbp), %rsi
callq 0x4b630
movswl -0x3a(%rbp), %edi
leaq -0x94(%rbp), %rsi
callq 0x4b750
cmpl $0x0, %eax
je 0x4a5a3
jmp 0x4a6fb
jmp 0x4a5a5
jmp 0x4a5a7
movw -0x3c(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x3c(%rbp)
jmp 0x4a4e5
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 0x4a5dd
jmp 0x4a6fb
movw $0x0, -0x3e(%rbp)
movswl -0x3e(%rbp), %eax
cmpl $0x400, %eax            # imm = 0x400
jge 0x4a6e9
movswq -0x3e(%rbp), %rcx
leaq 0x3b0a82(%rip), %rax    # 0x3fb080
movw (%rax,%rcx,2), %ax
movw %ax, -0x3c(%rbp)
movswl -0x3c(%rbp), %eax
cmpl $-0x1, %eax
je 0x4a6d6
movl 0x9666f(%rip), %eax     # 0xe0c88
movl %eax, -0x140(%rbp)
subl $0x5, %eax
je 0x4a672
jmp 0x4a626
movl -0x140(%rbp), %eax
subl $0x6, %eax
jne 0x4a6bd
jmp 0x4a637
movswq -0x3c(%rbp), %rax
leaq 0x26f1cd(%rip), %rdi    # 0x2b9810
imulq $0x2c, %rax, %rax
addq %rax, %rdi
leaq -0x130(%rbp), %rsi
callq 0x4ba90
movswl -0x3a(%rbp), %edi
leaq -0x130(%rbp), %rsi
callq 0x4bbf0
cmpl $0x0, %eax
je 0x4a670
jmp 0x4a6fb
jmp 0x4a6bd
movswq -0x3c(%rbp), %rax
leaq 0x26f192(%rip), %rdi    # 0x2b9810
imulq $0x2c, %rax, %rax
addq %rax, %rdi
leaq -0x130(%rbp), %rsi
callq 0x4ba90
leaq -0x130(%rbp), %rdi
leaq -0xbc(%rbp), %rsi
callq 0x4bfe0
movswl -0x3a(%rbp), %edi
leaq -0xbc(%rbp), %rsi
callq 0x4c110
cmpl $0x0, %eax
je 0x4a6bb
jmp 0x4a6fb
jmp 0x4a6bd
movswq -0x3c(%rbp), %rcx
leaq 0x3b91c7(%rip), %rax    # 0x403890
movw (%rax,%rcx,2), %ax
movw %ax, -0x3c(%rbp)
jmp 0x4a606
jmp 0x4a6d8
movw -0x3e(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x3e(%rbp)
jmp 0x4a5e3
movswl -0x3a(%rbp), %edi
callq 0x6220
movl $0x0, -0x4(%rbp)
jmp 0x4a70b
movswl -0x3a(%rbp), %edi
callq 0x6220
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw (%rax,%rax)