// File: /workspace/github_c_makefile_star2_O3/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)
cmpl $0x11, %edx
jl 0x3c119
movl 0x80(%rsp), %r14d
movl 0x70(%rsp), %r12d
cmpl %r14d, %r12d
jg 0x3c119
movl %r8d, %r13d
movl %ecx, %r8d
movl 0x88(%rsp), %eax
movl 0x78(%rsp), %ecx
cmpl %eax, %ecx
jg 0x3c119
movl %edx, %ebx
movl %esi, %ebp
movl %edi, %r15d
movswq %r13w, %r9
leaq 0x1fedf6(%rip), %rax    # 0x23aa60
testb $-0x40, (%rax,%r9,4)
movl %r8d, 0xc(%rsp)
je 0x3bc90
movswl %r13w, %edi
xorl %esi, %esi
callq 0x242cd
movl 0xc(%rsp), %r8d
addl %r13d, %eax
movswq %ax, %r9
movl %eax, %r13d
leaq 0x3d4389(%rip), %rax    # 0x410020
cmpw $0x0, (%rax,%r9,2)
jle 0x3c119
leaq 0x3c5b36(%rip), %rax    # 0x4017e0
cmpw $0x0, (%rax,%r9,2)
jle 0x3c119
movb 0x68(%rsp), %sil
testb %sil, %sil
movq %r9, 0x10(%rsp)
jns 0x3bcd7
cmpl $0x2, 0x3e71b4(%rip)    # 0x422e80
jl 0x3bcd7
cmpl $0x0, 0x68ab3(%rip)     # 0xa4788
je 0x3bd34
movl 0x3daf8f(%rip), %eax    # 0x416c6c
subq $0x8, %rsp
movzbl %sil, %r10d
movzbl 0x68(%rsp), %r11d
movswl %r8w, %ecx
movswl %r13w, %r8d
movsbl 0x24(%rsp), %r9d
movl %r15d, %edi
movl %ebp, %esi
movl %ebx, %edx
pushq %rax
movl 0x98(%rsp), %eax
pushq %rax
pushq %r14
movl 0x98(%rsp), %eax
pushq %rax
pushq %r12
pushq %r10
pushq %r11
callq 0x34e76
movb 0xa8(%rsp), %sil
movl 0x4c(%rsp), %r8d
movq 0x50(%rsp), %r9
addq $0x40, %rsp
testb $0x40, %sil
je 0x3bd9a
testl %r12d, %r12d
jg 0x3bd9a
movl 0x78(%rsp), %eax
testl %eax, %eax
jg 0x3bd9a
movl 0x3cc29f(%rip), %eax    # 0x407fec
decl %eax
cmpl %r14d, %eax
jg 0x3bd9a
testb $0x1, %sil
jne 0x3bd9a
testw %r8w, %r8w
jne 0x3bd9a
cmpl $0x10000, %ebx          # imm = 0x10000
jne 0x3bd9a
cmpl $0x640000, %ebp         # imm = 0x640000
jne 0x3bd9a
cmpl $0xa00000, %r15d        # imm = 0xA00000
jne 0x3bd9a
movl 0x3cc271(%rip), %eax    # 0x407ff0
decl %eax
movl 0x88(%rsp), %ecx
cmpl %ecx, %eax
jg 0x3bd9a
xorl %eax, %eax
movl %eax, 0x3dd0d8(%rip)    # 0x418e6c
movl %eax, 0x3dd0d6(%rip)    # 0x418e70
testb %sil, %sil
jns 0x3c119
cmpl $0x2, 0x3e70d6(%rip)    # 0x422e80
jl 0x3c119
movl 0x3dd0b9(%rip), %r12d   # 0x418e70
leal 0x1(%r12), %eax
andl $0x3ff, %eax            # imm = 0x3FF
cmpl 0x3dd0a5(%rip), %eax    # 0x418e6c
jne 0x3bec7
cmpl %r12d, %eax
je 0x3beb4
movl %r15d, 0x20(%rsp)
movl %ebp, 0x24(%rsp)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
leaq 0x3dd094(%rip), %r15    # 0x418e80
movl %eax, %r12d
movl %ebx, 0x18(%rsp)
movl %r12d, %eax
leaq (%rax,%rax,4), %rax
testb $0x7f, 0x13(%r15,%rax,8)
je 0x3be0f
testl %r14d, %r14d
jns 0x3be18
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x3be55
testl %r14d, %r14d
cmovsl %r12d, %r14d
jmp 0x3be55
movl %esi, %ebx
leaq (%r15,%rax,8), %rbp
movl %r14d, %eax
leaq (%rax,%rax,4), %rax
leaq (%r15,%rax,8), %rsi
movq %rbp, %rdi
movl $0x28, %edx
callq 0x4d437
movb $0x0, 0x13(%rbp)
cmpl %r12d, %r14d
jne 0x3be6a
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r9
movl 0xc(%rsp), %r8d
movl %ebx, %esi
movl 0x18(%rsp), %ebx
incl %r12d
andl $0x3ff, %r12d           # imm = 0x3FF
cmpl 0x3dd00a(%rip), %r12d   # 0x418e70
jne 0x3bdf3
jmp 0x3be9d
movq 0x10(%rsp), %r9
movl 0xc(%rsp), %r8d
movl %ebx, %esi
movl 0x18(%rsp), %ebx
movl %r14d, %eax
leaq (%rax,%rax,4), %rax
testb $0x7f, 0x13(%r15,%rax,8)
je 0x3be55
incl %r14d
andl $0x3ff, %r14d           # imm = 0x3FF
cmpl %r12d, %r14d
jne 0x3be7a
jmp 0x3be07
testl %r14d, %r14d
movl 0x24(%rsp), %ebp
movl 0x20(%rsp), %r15d
js 0x3beb4
movl %r14d, 0x3dcfbe(%rip)   # 0x418e70
jmp 0x3beca
movl 0x3dcfb2(%rip), %eax    # 0x418e6c
incl %eax
andl $0x3ff, %eax            # imm = 0x3FF
movl %eax, 0x3dcfa5(%rip)    # 0x418e6c
movl %r12d, %r14d
movslq %r14d, %rax
leaq (%rax,%rax,4), %rcx
leaq 0x3dcfa8(%rip), %rax    # 0x418e80
movl %r15d, (%rax,%rcx,8)
movl %ebp, 0x4(%rax,%rcx,8)
movl %ebx, 0x8(%rax,%rcx,8)
movw %r8w, 0xc(%rax,%rcx,8)
movw %r13w, 0xe(%rax,%rcx,8)
movl 0x1c(%rsp), %edx
movb %dl, 0x10(%rax,%rcx,8)
movb 0x60(%rsp), %dl
movb %dl, 0x11(%rax,%rcx,8)
movb %sil, 0x12(%rax,%rcx,8)
movl 0x6887d(%rip), %edx     # 0xa4788
shll $0x7, %edx
addl 0x3e6f6c(%rip), %edx    # 0x422e80
movb %dl, 0x13(%rax,%rcx,8)
movl 0x70(%rsp), %edx
movl %edx, 0x14(%rax,%rcx,8)
movl 0x78(%rsp), %edx
movl %edx, 0x18(%rax,%rcx,8)
movl 0x80(%rsp), %r12d
movl %r12d, 0x1c(%rax,%rcx,8)
movl 0x88(%rsp), %edx
movl %edx, 0x20(%rax,%rcx,8)
movl 0x3dad26(%rip), %edx    # 0x416c6c
movl %edx, 0x24(%rax,%rcx,8)
testb $0x40, %sil
je 0x3c108
movl 0x3dcf12(%rip), %ecx    # 0x418e6c
cmpl %r14d, %ecx
je 0x3c108
movl %ecx, %edx
leaq 0x3d40b4(%rip), %r10    # 0x410020
movslq %edx, %rsi
leaq (%rsi,%rsi,4), %rsi
testb $0x7f, 0x13(%rax,%rsi,8)
je 0x3bfea
leaq (%rax,%rsi,8), %rsi
cmpl %r15d, (%rsi)
jne 0x3bfea
cmpl %ebp, 0x4(%rsi)
jne 0x3bfea
cmpl %ebx, 0x8(%rsi)
jne 0x3bfea
cmpw %r8w, 0xc(%rsi)
jne 0x3bfea
movswq 0xe(%rsi), %rdi
movzwl (%r10,%rdi,2), %r8d
cmpw (%r10,%r9,2), %r8w
movl 0xc(%rsp), %r8d
jg 0x3bfea
leaq 0x3c582f(%rip), %r8     # 0x4017e0
movzwl (%r8,%rdi,2), %edi
cmpw (%r8,%r9,2), %di
movl 0xc(%rsp), %r8d
jg 0x3bfea
movl 0x70(%rsp), %edi
cmpl %edi, 0x14(%rsi)
jl 0x3bfea
movl 0x78(%rsp), %edi
cmpl %edi, 0x18(%rsi)
jl 0x3bfea
cmpl %r12d, 0x1c(%rsi)
jg 0x3bfea
movl 0x88(%rsp), %edi
cmpl %edi, 0x20(%rsi)
jg 0x3bfea
movb $0x0, 0x13(%rsi)
incl %edx
andl $0x3ff, %edx            # imm = 0x3FF
cmpl %r14d, %edx
jne 0x3bf6c
testw %r8w, %r8w
jne 0x3c108
cmpl %r14d, %ecx
je 0x3c108
movl %r15d, %edx
sarl $0x10, %edx
movl %ebp, %esi
sarl $0x10, %esi
movslq %ecx, %rdi
leaq (%rdi,%rdi,4), %rdi
testb $0x7f, 0x13(%rax,%rdi,8)
je 0x3c0f7
leaq (%rax,%rdi,8), %rdi
cmpl %ebx, 0x8(%rdi)
jne 0x3c0f7
cmpw $0x0, 0xc(%rdi)
jne 0x3c0f7
movl 0x70(%rsp), %r8d
cmpl %r8d, 0x14(%rdi)
jl 0x3c0f7
movl 0x78(%rsp), %r8d
cmpl %r8d, 0x18(%rdi)
jl 0x3c0f7
cmpl %r12d, 0x1c(%rdi)
jg 0x3c0f7
movl 0x88(%rsp), %r8d
cmpl %r8d, 0x20(%rdi)
jg 0x3c0f7
movl (%rdi), %r9d
movl %r9d, %r8d
sarl $0x10, %r8d
cmpl %edx, %r8d
jl 0x3c0f7
movl 0x4(%rdi), %r8d
movl %r8d, %r10d
sarl $0x10, %r10d
cmpl %esi, %r10d
jl 0x3c0f7
movswq 0xe(%rdi), %r10
leaq 0x3d3f7c(%rip), %r13    # 0x410020
movswl (%r13,%r10,2), %r11d
imull %ebx, %r11d
addl %r9d, %r11d
movq 0x10(%rsp), %r9
movswl (%r13,%r9,2), %r9d
imull %ebx, %r9d
addl %r15d, %r9d
cmpl %r9d, %r11d
movq 0x10(%rsp), %r11
jg 0x3c0f7
leaq 0x3c570c(%rip), %r13    # 0x4017e0
movswl (%r13,%r10,2), %r9d
imull %ebx, %r9d
addl %r8d, %r9d
movswl (%r13,%r11,2), %r8d
imull %ebx, %r8d
addl %ebp, %r8d
cmpl %r8d, %r9d
jg 0x3c0f7
movb $0x0, 0x13(%rdi)
incl %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
cmpl %r14d, %ecx
jne 0x3c019
incl %r14d
andl $0x3ff, %r14d           # imm = 0x3FF
movl %r14d, 0x3dcd57(%rip)   # 0x418e70
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq