// File: /workspace/github_c_makefile_star2_O1/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:
cmpl $0x11, %edx
jl 0x3ae5a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, %r10d
movl 0x70(%rsp), %r9d
movl 0x60(%rsp), %r12d
cmpl %r9d, %r12d
jg 0x3ae4c
movl %r8d, %r14d
movl %ecx, %r8d
movl 0x78(%rsp), %eax
movl 0x68(%rsp), %ecx
cmpl %eax, %ecx
jg 0x3ae4c
movl %edi, %ebp
movswq %r14w, %rax
leaq 0x1ff109(%rip), %rcx    # 0x239a60
testb $-0x40, (%rcx,%rax,4)
je 0x3a994
movswl %r14w, %edi
movl %ebp, %r15d
movl %esi, %ebp
xorl %esi, %esi
movl %r8d, %ebx
movl %edx, %r13d
movl %r10d, %r12d
callq 0x23d49
movl %r12d, %r10d
movl 0x70(%rsp), %r9d
movl 0x60(%rsp), %r12d
movl %ebp, %esi
movl %r15d, %ebp
movl %r13d, %edx
movl %ebx, %r8d
addl %r14d, %eax
movl %eax, %r14d
movswq %r14w, %rax
leaq 0x3d4681(%rip), %r13    # 0x40f020
cmpw $0x0, (%r13,%rax,2)
jle 0x3ae4c
leaq 0x3c5e2d(%rip), %rbx    # 0x4007e0
cmpw $0x0, (%rbx,%rax,2)
jle 0x3ae4c
movb 0x58(%rsp), %r15b
testb %r15b, %r15b
movl %r10d, 0xc(%rsp)
jns 0x3a9df
cmpl $0x2, 0x3e74ac(%rip)    # 0x421e80
jl 0x3a9df
cmpl $0x0, 0x68dab(%rip)     # 0xa3788
je 0x3aa55
movl 0x3db287(%rip), %eax    # 0x415c6c
subq $0x8, %rsp
movzbl %r15b, %r10d
movzbl 0x58(%rsp), %r11d
movswl %r8w, %ecx
movl %r8d, 0xc(%rsp)
movswl %r14w, %r8d
movsbl 0x14(%rsp), %r9d
movl %ebp, %edi
movl %esi, %ebx
movl %edx, %r13d
pushq %rax
movl 0x88(%rsp), %eax
pushq %rax
movl 0x88(%rsp), %eax
pushq %rax
movl 0x88(%rsp), %eax
pushq %rax
pushq %r12
pushq %r10
pushq %r11
callq 0x33d17
movl 0xb0(%rsp), %r9d
movl %ebx, %esi
leaq 0x3c5d9e(%rip), %rbx    # 0x4007e0
movl %r13d, %edx
leaq 0x3d45d4(%rip), %r13    # 0x40f020
movl 0x44(%rsp), %r8d
addq $0x40, %rsp
testb $0x40, %r15b
je 0x3aab7
testl %r12d, %r12d
jg 0x3aab7
movl 0x68(%rsp), %eax
testl %eax, %eax
jg 0x3aab7
movl 0x3cc57e(%rip), %eax    # 0x406fec
decl %eax
cmpl %r9d, %eax
jg 0x3aab7
testb $0x1, %r15b
jne 0x3aab7
testw %r8w, %r8w
jne 0x3aab7
cmpl $0x10000, %edx          # imm = 0x10000
jne 0x3aab7
cmpl $0x640000, %esi         # imm = 0x640000
jne 0x3aab7
cmpl $0xa00000, %ebp         # imm = 0xA00000
jne 0x3aab7
movl 0x3cc551(%rip), %eax    # 0x406ff0
decl %eax
movl 0x78(%rsp), %ecx
cmpl %ecx, %eax
jg 0x3aab7
xorl %eax, %eax
movl %eax, 0x3dd3bb(%rip)    # 0x417e6c
movl %eax, 0x3dd3b9(%rip)    # 0x417e70
testb %r15b, %r15b
jns 0x3ae4c
cmpl $0x2, 0x3e73b9(%rip)    # 0x421e80
jl 0x3ae4c
movl %ebp, 0x10(%rsp)
movl %edx, 0x14(%rsp)
movl 0x3dd395(%rip), %eax    # 0x417e70
incl %eax
andl $0x3ff, %eax            # imm = 0x3FF
movl 0x3dd384(%rip), %ebp    # 0x417e6c
cmpl %ebp, %eax
jne 0x3abf0
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl 0x3dd374(%rip), %ebp    # 0x417e70
jne 0x3ab13
testl %r15d, %r15d
js 0x3abbd
movl %r15d, 0x3dd362(%rip)   # 0x417e70
jmp 0x3abd0
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
leaq 0x3dd360(%rip), %r12    # 0x417e80
movl %r8d, 0x4(%rsp)
movl %esi, 0x8(%rsp)
movl %ebp, %eax
leaq (%rax,%rax,4), %rax
testb $0x7f, 0x13(%r12,%rax,8)
je 0x3ab72
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %r13d, %r13d
js 0x3ab7c
leaq (%r12,%rax,8), %rbx
movl %r13d, %eax
leaq (%rax,%rax,4), %rax
leaq (%r12,%rax,8), %rsi
movq %rbx, %rdi
movl $0x28, %edx
callq 0x4ac52
movb $0x0, 0x13(%rbx)
cmpl %ebp, %r13d
jne 0x3ab94
movl 0x4(%rsp), %r8d
movl 0x8(%rsp), %esi
jmp 0x3ab7c
testl %r13d, %r13d
cmovsl %ebp, %r13d
movl %r13d, %r15d
incl %ebp
andl $0x3ff, %ebp            # imm = 0x3FF
movl %r15d, %r13d
cmpl 0x3dd2e3(%rip), %ebp    # 0x417e70
jne 0x3ab29
jmp 0x3aafe
movl 0x4(%rsp), %r8d
movl 0x8(%rsp), %esi
movl %r13d, %eax
leaq (%rax,%rax,4), %rax
testb $0x7f, 0x13(%r12,%rax,8)
je 0x3ab79
incl %r13d
andl $0x3ff, %r13d           # imm = 0x3FF
cmpl %ebp, %r13d
jne 0x3ab9d
jmp 0x3ab7c
movl 0x3dd2a9(%rip), %eax    # 0x417e6c
incl %eax
andl $0x3ff, %eax            # imm = 0x3FF
movl %eax, 0x3dd29c(%rip)    # 0x417e6c
leaq 0x3d4449(%rip), %r13    # 0x40f020
leaq 0x3c5c02(%rip), %rbx    # 0x4007e0
movl 0x70(%rsp), %r15d
movq %r15, %r9
movl 0x60(%rsp), %r12d
movb 0x58(%rsp), %r15b
movslq 0x3dd279(%rip), %rax    # 0x417e70
leaq (%rax,%rax,4), %rdx
leaq 0x3dd27e(%rip), %rcx    # 0x417e80
movl 0x10(%rsp), %edi
movl %edi, (%rcx,%rdx,8)
movl %esi, 0x4(%rcx,%rdx,8)
movl 0x14(%rsp), %esi
movl %esi, 0x8(%rcx,%rdx,8)
movw %r8w, 0xc(%rcx,%rdx,8)
movw %r14w, 0xe(%rcx,%rdx,8)
movl 0xc(%rsp), %esi
movb %sil, 0x10(%rcx,%rdx,8)
movb 0x50(%rsp), %sil
movb %sil, 0x11(%rcx,%rdx,8)
movb %r15b, 0x12(%rcx,%rdx,8)
movl 0x68b49(%rip), %esi     # 0xa3788
shll $0x7, %esi
addl 0x3e7238(%rip), %esi    # 0x421e80
movb %sil, 0x13(%rcx,%rdx,8)
movl %r12d, 0x14(%rcx,%rdx,8)
movl 0x68(%rsp), %esi
movl %esi, 0x18(%rcx,%rdx,8)
movl %r9d, 0x1c(%rcx,%rdx,8)
movl 0x78(%rsp), %esi
movl %esi, 0x20(%rcx,%rdx,8)
movl 0x3dafff(%rip), %esi    # 0x415c6c
movl %esi, 0x24(%rcx,%rdx,8)
testb $0x40, %r15b
je 0x3ae3f
leaq (%rcx,%rdx,8), %rdx
movl 0x3dd1e7(%rip), %esi    # 0x417e6c
cmpl %eax, %esi
je 0x3ad2b
movl %esi, %edi
movslq %edi, %r8
leaq (%r8,%r8,4), %r8
testb $0x7f, 0x13(%rcx,%r8,8)
je 0x3ad1b
leaq (%rcx,%r8,8), %r8
movl (%r8), %r9d
cmpl (%rdx), %r9d
jne 0x3ad1b
movl 0x4(%r8), %r9d
cmpl 0x4(%rdx), %r9d
jne 0x3ad1b
movl 0x8(%r8), %r9d
cmpl 0x8(%rdx), %r9d
jne 0x3ad1b
movzwl 0xc(%r8), %r9d
cmpw 0xc(%rdx), %r9w
jne 0x3ad1b
movswq 0xe(%r8), %r9
movzwl (%r13,%r9,2), %r11d
movswq 0xe(%rdx), %r10
cmpw (%r13,%r10,2), %r11w
jg 0x3ad1b
movzwl (%rbx,%r9,2), %r9d
cmpw (%rbx,%r10,2), %r9w
jg 0x3ad1b
movl 0x14(%r8), %r9d
cmpl 0x14(%rdx), %r9d
jl 0x3ad1b
movl 0x18(%r8), %r9d
cmpl 0x18(%rdx), %r9d
jl 0x3ad1b
movl 0x1c(%r8), %r9d
cmpl 0x1c(%rdx), %r9d
jg 0x3ad1b
movl 0x20(%r8), %r9d
cmpl 0x20(%rdx), %r9d
jg 0x3ad1b
movb $0x0, 0x13(%r8)
incl %edi
andl $0x3ff, %edi            # imm = 0x3FF
cmpl %eax, %edi
jne 0x3ac8f
cmpw $0x0, 0xc(%rdx)
setne %dil
cmpl %eax, %esi
sete %r8b
orb %dil, %r8b
jne 0x3ae3f
movslq %esi, %rdi
leaq (%rdi,%rdi,4), %rdi
testb $0x7f, 0x13(%rcx,%rdi,8)
je 0x3ae2f
leaq (%rcx,%rdi,8), %rdi
movl 0x8(%rdi), %r8d
movl 0x8(%rdx), %r9d
cmpl %r9d, %r8d
jne 0x3ae2f
cmpw $0x0, 0xc(%rdi)
jne 0x3ae2f
movl 0x14(%rdi), %r10d
cmpl 0x14(%rdx), %r10d
jl 0x3ae2f
movl 0x18(%rdi), %r10d
cmpl 0x18(%rdx), %r10d
jl 0x3ae2f
movl 0x1c(%rdi), %r10d
cmpl 0x1c(%rdx), %r10d
jg 0x3ae2f
movl 0x20(%rdi), %r10d
cmpl 0x20(%rdx), %r10d
jg 0x3ae2f
movl (%rdi), %ebp
movl %ebp, %r10d
sarl $0x10, %r10d
movl (%rdx), %ebx
movl %ebx, %r11d
sarl $0x10, %r11d
cmpl %r11d, %r10d
jl 0x3ae2f
movl 0x4(%rdi), %r11d
movl %r11d, %r14d
sarl $0x10, %r14d
movl 0x4(%rdx), %r10d
movl %r10d, %r15d
sarl $0x10, %r15d
cmpl %r15d, %r14d
jl 0x3ae2f
movswq 0xe(%rdi), %r14
movswl (%r13,%r14,2), %r12d
imull %r8d, %r12d
addl %ebp, %r12d
movswq 0xe(%rdx), %r15
movswl (%r13,%r15,2), %ebp
imull %r9d, %ebp
addl %ebx, %ebp
cmpl %ebp, %r12d
leaq 0x3c59d4(%rip), %r12    # 0x4007e0
jg 0x3ae2f
movswl (%r12,%r14,2), %ebx
imull %ebx, %r8d
addl %r11d, %r8d
movswl (%r12,%r15,2), %r11d
imull %r11d, %r9d
addl %r10d, %r9d
cmpl %r9d, %r8d
jg 0x3ae2f
movb $0x0, 0x13(%rdi)
incl %esi
andl $0x3ff, %esi            # imm = 0x3FF
cmpl %eax, %esi
jne 0x3ad43
incl %eax
andl $0x3ff, %eax            # imm = 0x3FF
movl %eax, 0x3dd024(%rip)    # 0x417e70
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq