// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: rotatesprite
void rotatesprite(int sx, int sy, int z, short a, short picnum, signed char dashade,
	unsigned char dapalnum, unsigned char dastat, int cx1, int cy1, int cx2, int cy2)
{
	int i, gap = -1;
	permfifotype *per, *per2;

	if ((cx1 > cx2) || (cy1 > cy2)) return;
	if (z <= 16) return;
	if (picanm[picnum]&192) picnum += animateoffs(picnum,(short)0xc000);
	if ((tilesizx[picnum] <= 0) || (tilesizy[picnum] <= 0)) return;

	if (((dastat&128) == 0) || (numpages < 2) || (beforedrawrooms != 0)) {
		dorotatesprite(sx,sy,z,a,picnum,dashade,dapalnum,dastat,cx1,cy1,cx2,cy2,guniqhudid);
	}

	if ((dastat&64) && (cx1 <= 0) && (cy1 <= 0) && (cx2 >= xdim-1) && (cy2 >= ydim-1) &&
		 (sx == (160<<16)) && (sy == (100<<16)) && (z == 65536L) && (a == 0) && ((dastat&1) == 0))
		permhead = permtail = 0;

	if ((dastat&128) == 0) return;
	if (numpages >= 2)
	{
		if (((permhead+1)&(MAXPERMS-1)) == permtail)
		{
			for(i=permtail;i!=permhead;i=((i+1)&(MAXPERMS-1)))
			{
				if ((permfifo[i].pagesleft&127) == 0)
					{ if (gap < 0) gap = i; }
				else if (gap >= 0)
				{
					copybufbyte(&permfifo[i], &permfifo[gap], sizeof(permfifotype));
					permfifo[i].pagesleft = 0;
					for (;gap!=i;gap=((gap+1)&(MAXPERMS-1)))
						if ((permfifo[gap].pagesleft&127) == 0)
							break;
					if (gap==i) gap = -1;
				}
			}
			if (gap >= 0) permhead = gap;
			else permtail = ((permtail+1)&(MAXPERMS-1));
		}
		per = &permfifo[permhead];
		per->sx = sx; per->sy = sy; per->z = z; per->a = a;
		per->picnum = picnum;
		per->dashade = dashade; per->dapalnum = dapalnum;
		per->dastat = dastat;
		per->pagesleft = numpages+((beforedrawrooms&1)<<7);
		per->cx1 = cx1; per->cy1 = cy1; per->cx2 = cx2; per->cy2 = cy2;
		per->uniqid = guniqhudid;	//JF extension

			//Would be better to optimize out true bounding boxes
		if (dastat&64)  //If non-masking write, checking for overlapping cases
		{
			for(i=permtail;i!=permhead;i=((i+1)&(MAXPERMS-1)))
			{
				per2 = &permfifo[i];
				if ((per2->pagesleft&127) == 0) continue;
				if (per2->sx != per->sx) continue;
				if (per2->sy != per->sy) continue;
				if (per2->z != per->z) continue;
				if (per2->a != per->a) continue;
				if (tilesizx[per2->picnum] > tilesizx[per->picnum]) continue;
				if (tilesizy[per2->picnum] > tilesizy[per->picnum]) continue;
				if (per2->cx1 < per->cx1) continue;
				if (per2->cy1 < per->cy1) continue;
				if (per2->cx2 > per->cx2) continue;
				if (per2->cy2 > per->cy2) continue;
				per2->pagesleft = 0;
			}
			if (per->a == 0)
				for(i=permtail;i!=permhead;i=((i+1)&(MAXPERMS-1)))
				{
					per2 = &permfifo[i];
					if ((per2->pagesleft&127) == 0) continue;
					if (per2->z != per->z) continue;
					if (per2->a != 0) continue;
					if (per2->cx1 < per->cx1) continue;
					if (per2->cy1 < per->cy1) continue;
					if (per2->cx2 > per->cx2) continue;
					if (per2->cy2 > per->cy2) continue;
					if ((per2->sx>>16) < (per->sx>>16)) continue;
					if ((per2->sy>>16) < (per->sy>>16)) continue;
					if (per2->sx+(tilesizx[per2->picnum]*per2->z) > per->sx+(tilesizx[per->picnum]*per->z)) continue;
					if (per2->sy+(tilesizy[per2->picnum]*per2->z) > per->sy+(tilesizy[per->picnum]*per->z)) continue;
					per2->pagesleft = 0;
				}
		}

		permhead = ((permhead+1)&(MAXPERMS-1));
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, 0x1c(%rsp)
movl %ecx, 0xc(%rsp)
cmpl $0x11, %edx
jl 0x38586
movl 0x80(%rsp), %r15d
movl 0x70(%rsp), %r12d
cmpl %r15d, %r12d
jg 0x38586
movl 0x88(%rsp), %eax
movl 0x78(%rsp), %ecx
cmpl %eax, %ecx
jg 0x38586
movl %r8d, %r13d
movl %edx, %ebx
movl %esi, %ebp
movl %edi, %r14d
movswq %r13w, %r8
leaq 0x1fb844(%rip), %rax    # 0x233a60
testb $-0x40, (%rax,%r8,4)
je 0x38238
movswl %r13w, %edi
xorl %esi, %esi
callq 0x2163b
addl %r13d, %eax
movswq %ax, %r8
movl %eax, %r13d
leaq 0x3d0de1(%rip), %rax    # 0x409020
cmpw $0x0, (%rax,%r8,2)
jle 0x38586
leaq 0x3c258e(%rip), %rax    # 0x3fa7e0
cmpw $0x0, (%rax,%r8,2)
jle 0x38586
cmpb $0x0, 0x68(%rsp)
movq %r8, 0x20(%rsp)
jns 0x3827c
cmpl $0x2, 0x3e3c0f(%rip)    # 0x41be80
jl 0x3827c
cmpl $0x0, 0x6550e(%rip)     # 0x9d788
je 0x382cf
movl 0x3d79ea(%rip), %eax    # 0x40fc6c
subq $0x8, %rsp
movzbl 0x70(%rsp), %r10d
movzbl 0x68(%rsp), %r11d
movswl 0x14(%rsp), %ecx
movswl %r13w, %r8d
movsbl 0x24(%rsp), %r9d
movl %r14d, %edi
movl %ebp, %esi
movl %ebx, %edx
pushq %rax
movl 0x98(%rsp), %eax
pushq %rax
pushq %r15
movl 0x98(%rsp), %eax
pushq %rax
pushq %r12
pushq %r10
pushq %r11
callq 0x31846
movq 0x60(%rsp), %r8
addq $0x40, %rsp
testb $0x40, 0x68(%rsp)
je 0x38339
testl %r12d, %r12d
jg 0x38339
movl 0x78(%rsp), %eax
testl %eax, %eax
jg 0x38339
movl 0x3c8d03(%rip), %eax    # 0x400fec
decl %eax
cmpl %r15d, %eax
jg 0x38339
testb $0x1, 0x68(%rsp)
jne 0x38339
cmpw $0x0, 0xc(%rsp)
jne 0x38339
cmpl $0x10000, %ebx          # imm = 0x10000
jne 0x38339
cmpl $0x640000, %ebp         # imm = 0x640000
jne 0x38339
cmpl $0xa00000, %r14d        # imm = 0xA00000
jne 0x38339
movl 0x3c8cd2(%rip), %eax    # 0x400ff0
decl %eax
movl 0x88(%rsp), %ecx
cmpl %ecx, %eax
jg 0x38339
andl $0x0, 0x3d9b3a(%rip)    # 0x411e6c
andl $0x0, 0x3d9b37(%rip)    # 0x411e70
cmpb $0x0, 0x68(%rsp)
jns 0x38586
cmpl $0x2, 0x3e3b35(%rip)    # 0x41be80
jl 0x38586
movl %r14d, 0x10(%rsp)
movl %ebp, 0x14(%rsp)
movl 0x3d9b10(%rip), %eax    # 0x411e70
leal 0x1(%rax), %r15d
andl $0x3ff, %r15d           # imm = 0x3FF
leaq 0x3d9b0e(%rip), %r12    # 0x411e80
cmpl 0x3d9af3(%rip), %r15d   # 0x411e6c
jne 0x38408
movl %ebx, 0x18(%rsp)
pushq $-0x1
popq %rbx
movl %ebx, %r14d
cmpl %eax, %r15d
je 0x3840d
movl %r15d, %ecx
imulq $0x28, %rcx, %rbp
testb $0x7f, 0x13(%rbp,%r12)
je 0x383eb
movl %ebx, %ecx
testl %r14d, %r14d
js 0x383f9
addq %r12, %rbp
movl %r14d, %eax
imulq $0x28, %rax, %rsi
addq %r12, %rsi
movq %rbp, %rdi
pushq $0x28
popq %rdx
callq 0x44524
movq 0x20(%rsp), %r8
movb $0x0, 0x13(%rbp)
movl 0x3d9aa5(%rip), %eax    # 0x411e70
cmpl %r15d, %r14d
je 0x383f7
movl %r14d, %ecx
imulq $0x28, %rcx, %rcx
testb $0x7f, 0x13(%rcx,%r12)
je 0x383f2
incl %r14d
andl $0x3ff, %r14d           # imm = 0x3FF
jmp 0x383cb
testl %r14d, %r14d
cmovsl %r15d, %r14d
movl %r14d, %ecx
jmp 0x383f9
movl %ebx, %ecx
incl %r15d
andl $0x3ff, %r15d           # imm = 0x3FF
movl %ecx, %r14d
jmp 0x38389
movl %eax, %r14d
jmp 0x38436
testl %r14d, %r14d
js 0x3841b
movl %r14d, 0x3d9a57(%rip)   # 0x411e70
jmp 0x38432
movl 0x3d9a4b(%rip), %ecx    # 0x411e6c
incl %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
movl %ecx, 0x3d9a3d(%rip)    # 0x411e6c
movl %eax, %r14d
movl 0x18(%rsp), %ebx
movl 0x14(%rsp), %r11d
movl 0x10(%rsp), %ebp
movl 0x80(%rsp), %r15d
movslq %r14d, %rax
imulq $0x28, %rax, %rax
movl %ebp, (%rax,%r12)
movl %r11d, 0x4(%rax,%r12)
movl %ebx, 0x8(%rax,%r12)
movl 0xc(%rsp), %ecx
movw %cx, 0xc(%rax,%r12)
movw %r13w, 0xe(%rax,%r12)
movl 0x1c(%rsp), %ecx
movb %cl, 0x10(%rax,%r12)
movb 0x60(%rsp), %cl
movb %cl, 0x11(%rax,%r12)
movb 0x68(%rsp), %dl
movb %dl, 0x12(%rax,%r12)
movl 0x652fb(%rip), %ecx     # 0x9d788
shll $0x7, %ecx
addl 0x3e39ea(%rip), %ecx    # 0x41be80
movb %cl, 0x13(%rax,%r12)
movl 0x70(%rsp), %ecx
movl %ecx, 0x14(%rax,%r12)
movl 0x78(%rsp), %ecx
movl %ecx, 0x18(%rax,%r12)
movl %r15d, 0x1c(%rax,%r12)
movl 0x88(%rsp), %ecx
movl %ecx, 0x20(%rax,%r12)
movl 0x3d77a8(%rip), %ecx    # 0x40fc6c
movl %ecx, 0x24(%rax,%r12)
testb $0x40, %dl
je 0x38575
movl 0x3d9994(%rip), %eax    # 0x411e6c
movl %eax, %ecx
leaq 0x3d0b3f(%rip), %r9     # 0x409020
cmpl %r14d, %ecx
je 0x3856d
movslq %ecx, %rdx
imulq $0x28, %rdx, %rdx
testb $0x7f, 0x13(%rdx,%r12)
je 0x38560
addq %r12, %rdx
cmpl %ebp, (%rdx)
jne 0x38560
cmpl %r11d, 0x4(%rdx)
jne 0x38560
cmpl %ebx, 0x8(%rdx)
jne 0x38560
movl 0xc(%rsp), %esi
cmpw %si, 0xc(%rdx)
jne 0x38560
movswq 0xe(%rdx), %rsi
movzwl (%r9,%rsi,2), %edi
cmpw (%r9,%r8,2), %di
jg 0x38560
leaq 0x3c22b3(%rip), %rdi    # 0x3fa7e0
movzwl (%rdi,%rsi,2), %esi
cmpw (%rdi,%r8,2), %si
jg 0x38560
movl 0x70(%rsp), %esi
cmpl %esi, 0x14(%rdx)
jl 0x38560
movl 0x78(%rsp), %esi
cmpl %esi, 0x18(%rdx)
jl 0x38560
cmpl %r15d, 0x1c(%rdx)
jg 0x38560
movl 0x88(%rsp), %esi
cmpl %esi, 0x20(%rdx)
jg 0x38560
movb $0x0, 0x13(%rdx)
incl %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
jmp 0x384e1
cmpw $0x0, 0xc(%rsp)
je 0x38595
incl %r14d
andl $0x3ff, %r14d           # imm = 0x3FF
movl %r14d, 0x3d98ea(%rip)   # 0x411e70
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %ecx
sarl $0x10, %ecx
movl %r11d, %edx
sarl $0x10, %edx
cmpl %r14d, %eax
je 0x38575
movslq %eax, %rsi
imulq $0x28, %rsi, %rsi
testb $0x7f, 0x13(%rsi,%r12)
je 0x38679
addq %r12, %rsi
cmpl %ebx, 0x8(%rsi)
jne 0x38679
cmpw $0x0, 0xc(%rsi)
jne 0x38679
movl 0x70(%rsp), %edi
cmpl %edi, 0x14(%rsi)
jl 0x38679
movl 0x78(%rsp), %edi
cmpl %edi, 0x18(%rsi)
jl 0x38679
cmpl %r15d, 0x1c(%rsi)
jg 0x38679
movl 0x88(%rsp), %edi
cmpl %edi, 0x20(%rsi)
jg 0x38679
movl (%rsi), %r8d
movl %r8d, %edi
sarl $0x10, %edi
cmpl %ecx, %edi
jl 0x38679
movl 0x4(%rsi), %edi
movl %edi, %r9d
sarl $0x10, %r9d
cmpl %edx, %r9d
jl 0x38679
movswq 0xe(%rsi), %r9
leaq 0x3d09f9(%rip), %r13    # 0x409020
movswl (%r13,%r9,2), %r10d
imull %ebx, %r10d
addl %r8d, %r10d
movq 0x20(%rsp), %r8
movswl (%r13,%r8,2), %r8d
imull %ebx, %r8d
addl %ebp, %r8d
cmpl %r8d, %r10d
movq 0x20(%rsp), %r10
jg 0x38679
leaq 0x3c2189(%rip), %r13    # 0x3fa7e0
movswl (%r13,%r9,2), %r8d
imull %ebx, %r8d
addl %edi, %r8d
movswl (%r13,%r10,2), %edi
imull %ebx, %edi
addl %r11d, %edi
cmpl %edi, %r8d
jg 0x38679
movb $0x0, 0x13(%rsi)
incl %eax
andl $0x3ff, %eax            # imm = 0x3FF
jmp 0x385a0