// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: fillpolygon
static void fillpolygon(int npoints)
{
	int z, zz, x1, y1, x2, y2, miny, maxy, y, xinc, cnt;
	int ox, oy, bx, by, day1, day2;
	intptr_t p;
	short *ptr, *ptr2;

#if USE_POLYMOST && USE_OPENGL
	if (rendmode == 3) { polymost_fillpolygon(npoints); return; }
#endif

	miny = 0x7fffffff; maxy = 0x80000000;
	for(z=npoints-1;z>=0;z--)
		{ y = ry1[z]; miny = min(miny,y); maxy = max(maxy,y); }
	miny = (miny>>12); maxy = (maxy>>12);
	if (miny < 0) miny = 0;
	if (maxy >= ydim) maxy = ydim-1;
	ptr = smost;    //They're pointers! - watch how you optimize this thing
	for(y=miny;y<=maxy;y++)
	{
		dotp1[y] = ptr; dotp2[y] = ptr+(MAXNODESPERLINE>>1);
		ptr += MAXNODESPERLINE;
	}

	for(z=npoints-1;z>=0;z--)
	{
		zz = xb1[z];
		y1 = ry1[z]; day1 = (y1>>12);
		y2 = ry1[zz]; day2 = (y2>>12);
		if (day1 != day2)
		{
			x1 = rx1[z]; x2 = rx1[zz];
			xinc = divscale12(x2-x1,y2-y1);
			if (day2 > day1)
			{
				x1 += mulscale12((day1<<12)+4095-y1,xinc);
				for(y=day1;y<day2;y++) { *dotp2[y]++ = (x1>>12); x1 += xinc; }
			}
			else
			{
				x2 += mulscale12((day2<<12)+4095-y2,xinc);
				for(y=day2;y<day1;y++) { *dotp1[y]++ = (x2>>12); x2 += xinc; }
			}
		}
	}

	globalx1 = mulscale16(globalx1,xyaspect);
	globaly2 = mulscale16(globaly2,xyaspect);

	oy = miny+1-(ydim>>1);
	globalposx += oy*globalx1;
	globalposy += oy*globaly2;

	setuphlineasm4(asm1,asm2);

	ptr = smost;
	for(y=miny;y<=maxy;y++)
	{
		cnt = (int)(dotp1[y]-ptr); ptr2 = ptr+(MAXNODESPERLINE>>1);
		for(z=cnt-1;z>=0;z--)
		{
			day1 = 0; day2 = 0;
			for(zz=z;zz>0;zz--)
			{
				if (ptr[zz] < ptr[day1]) day1 = zz;
				if (ptr2[zz] < ptr2[day2]) day2 = zz;
			}
			x1 = ptr[day1]; ptr[day1] = ptr[z];
			x2 = ptr2[day2]-1; ptr2[day2] = ptr2[z];
			if (x1 > x2) continue;

			if (globalpolytype < 1)
			{
					//maphline
				ox = x2+1-(xdim>>1);
				bx = ox*asm1 + globalposx;
				by = ox*asm2 - globalposy;

				p = ylookup[y]+x2+frameplace;
				hlineasm4(x2-x1,-1L,globalshade<<8,by,bx,(void *)p);
			}
			else
			{
					//maphline
				ox = x1+1-(xdim>>1);
				bx = ox*asm1 + globalposx;
				by = ox*asm2 - globalposy;

				p = ylookup[y]+x1+frameplace;
				if (globalpolytype == 1)
					mhline((void *)globalbufplc,bx,(x2-x1)<<16,0L,by,(void *)p);
				else
				{
					thline((void *)globalbufplc,bx,(x2-x1)<<16,0L,by,(void *)p);
				}
			}
		}
		globalposx += globalx1;
		globalposy += globaly2;
		ptr += MAXNODESPERLINE;
	}
	faketimerhandler();
}

// Assembly:
leaq 0x5ca6d2(%rip), %rax    # 0x5f9950
cmpl $0x3, (%rax)
jne 0x2f288
jmp 0x5a842
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leal -0x1(%rdi), %eax
movl %eax, %esi
leaq 0x3dbd5e(%rip), %rcx    # 0x40b000
testl %edi, %edi
jle 0x2f2db
leaq (%rcx,%rsi,4), %r8
leaq 0x1(%rsi), %r9
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x80000000, %eax       # imm = 0x80000000
movl %eax, %r10d
movl (%r8), %eax
cmpl %eax, %edx
cmovgel %eax, %edx
cmpl %eax, %r10d
cmovgl %r10d, %eax
addq $-0x4, %r8
decq %r9
jg 0x2f2b8
sarl $0xc, %edx
sarl $0xc, %eax
jmp 0x2f2e5
movl $0xfff80000, %eax       # imm = 0xFFF80000
movl $0x7ffff, %edx          # imm = 0x7FFFF
xorl %r14d, %r14d
testl %edx, %edx
cmovgl %edx, %r14d
movl 0x3d7cfc(%rip), %edx    # 0x406ff0
leal -0x1(%rdx), %r8d
cmpl %edx, %eax
cmovll %eax, %r8d
movq %r8, 0x8(%rsp)
cmpl %r8d, %r14d
jg 0x2f34d
movl %r14d, %eax
movq 0x8(%rsp), %rdx
incl %edx
subq %rax, %rdx
shlq $0x3, %rax
leaq 0x484200(%rip), %r8     # 0x4b3520
xorl %r9d, %r9d
leaq 0x57e0d6(%rip), %r10    # 0x5ad400
leaq 0x58244f(%rip), %r11    # 0x5b1780
leaq (%rax,%r9,8), %rbx
movq %r8, (%r10,%rbx)
leaq 0x2a(%r8), %r15
movq %r15, (%r11,%rbx)
incq %r9
addq $0x54, %r8
cmpq %r9, %rdx
jne 0x2f331
movq %r14, 0x10(%rsp)
testl %edi, %edi
jle 0x2f46c
leaq 0x23b70f(%rip), %rdi    # 0x26aa70
leaq 0x3d7c98(%rip), %r8     # 0x407000
leaq 0x582411(%rip), %r9     # 0x5b1780
leaq 0x57e08a(%rip), %r10    # 0x5ad400
movslq (%rdi,%rsi,4), %rax
movl (%rcx,%rsi,4), %r13d
movl %r13d, %r11d
sarl $0xc, %r11d
movl (%rcx,%rax,4), %r12d
movl %r12d, %r15d
sarl $0xc, %r15d
cmpl %r15d, %r11d
je 0x2f45c
movl (%r8,%rsi,4), %ebp
movl (%r8,%rax,4), %ebx
movl %ebx, %eax
subl %ebp, %eax
movl %r12d, %edx
subl %r13d, %edx
cltq
shlq $0xc, %rax
movslq %edx, %r14
cqto
idivq %r14
cmpl %r11d, %r15d
jle 0x2f40d
notl %r13d
andl $0xfff, %r13d           # imm = 0xFFF
movslq %eax, %rdx
imulq %r13, %rdx
shrq $0xc, %rdx
addl %edx, %ebp
movslq %r11d, %rdx
movslq %r15d, %r11
subq %rdx, %r11
shlq $0x3, %rdx
xorl %ebx, %ebx
movl %ebp, %r14d
shrl $0xc, %r14d
leaq (%rdx,%rbx,8), %r15
movq (%r9,%r15), %r12
leaq 0x2(%r12), %r13
movq %r13, (%r9,%r15)
movw %r14w, (%r12)
addl %eax, %ebp
incq %rbx
cmpq %rbx, %r11
jne 0x2f3e4
jmp 0x2f45c
jge 0x2f45c
movslq %eax, %rdx
notl %r12d
andl $0xfff, %r12d           # imm = 0xFFF
imulq %rdx, %r12
shrq $0xc, %r12
addl %r12d, %ebx
movslq %r15d, %rdx
movslq %r11d, %r11
subq %rdx, %r11
shlq $0x3, %rdx
xorl %r15d, %r15d
movl %ebx, %ebp
shrl $0xc, %ebp
leaq (%rdx,%r15,8), %r14
movq (%r10,%r14), %r12
leaq 0x2(%r12), %r13
movq %r13, (%r10,%r14)
movw %bp, (%r12)
addl %eax, %ebx
incq %r15
cmpq %r15, %r11
jne 0x2f437
leaq -0x1(%rsi), %rax
testq %rsi, %rsi
movq %rax, %rsi
jg 0x2f376
movslq 0x3e43c5(%rip), %rax    # 0x413838
movslq 0x32d43e(%rip), %rcx    # 0x35c8b8
imulq %rcx, %rax
shrq $0x10, %rax
movl %eax, 0x3e43b0(%rip)    # 0x413838
movslq 0x3e43b5(%rip), %rdx    # 0x413844
imulq %rcx, %rdx
shrq $0x10, %rdx
movl 0x3d7b53(%rip), %ecx    # 0x406ff0
sarl %ecx
negl %ecx
movq 0x10(%rsp), %rbx
addl %ebx, %ecx
incl %ecx
imull %ecx, %eax
addl %eax, 0x20a59d(%rip)    # 0x239a50
imull %edx, %ecx
addl %ecx, 0x20a598(%rip)    # 0x239a54
movl %edx, 0x3e4382(%rip)    # 0x413844
movl 0x3e678c(%rip), %edi    # 0x415c54
movl 0x3e678a(%rip), %esi    # 0x415c58
callq 0x6dcf7
cmpl 0x8(%rsp), %ebx
jg 0x2f6bb
movl %ebx, %r14d
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
leaq 0x48402d(%rip), %r15    # 0x4b3520
movq %r15, %r12
negq %r12
leaq 0x57df00(%rip), %rax    # 0x5ad400
movq (%rax,%r14,8), %rax
movq %rax, %rcx
subq %r15, %rcx
shrq %rcx
testl %ecx, %ecx
jle 0x2f68d
addq %r12, %rax
shrq %rax
movl %eax, %ebp
decl %eax
leaq (%r15,%rax,2), %rbx
movq %rbp, %r13
cmpq $0x2, %rbp
jl 0x2f574
xorl %edx, %edx
movq %rbx, %rax
movl %r13d, %ecx
xorl %edi, %edi
movl %edi, %r8d
decl %ecx
movzwl (%rax), %r9d
movl %ecx, %esi
cmpw (%r15,%r8,2), %r9w
jl 0x2f54a
movl %edi, %esi
movzwl 0x2a(%rax), %edi
movl %edx, %r9d
movl %ecx, %r8d
cmpw 0x2a(%r15,%r9,2), %di
jl 0x2f55f
movl %edx, %r8d
addq $-0x2, %rax
movl %r8d, %edx
movl %esi, %edi
cmpl $0x1, %ecx
jg 0x2f536
movl %esi, %ecx
movl %r8d, %edx
jmp 0x2f578
xorl %ecx, %ecx
xorl %edx, %edx
leaq -0x1(%r13), %rbp
movswl (%r15,%rcx,2), %eax
movzwl (%r15,%rbp,2), %esi
movw %si, (%r15,%rcx,2)
movswl 0x2a(%r15,%rdx,2), %ecx
movzwl 0x2a(%r15,%rbp,2), %esi
movw %si, 0x2a(%r15,%rdx,2)
cmpw %ax, %cx
jle 0x2f67f
movslq %ecx, %rdx
decq %rdx
movb 0x3e4281(%rip), %r10b   # 0x413834
testb %r10b, %r10b
je 0x2f61e
movl 0x3d7a2e(%rip), %ecx    # 0x406fec
sarl %ecx
negl %ecx
leal (%rax,%rcx), %r8d
incl %r8d
movl 0x3e6685(%rip), %esi    # 0x415c54
imull %r8d, %esi
addl 0x20a477(%rip), %esi    # 0x239a50
imull 0x3e6677(%rip), %r8d    # 0x415c58
subl 0x20a46c(%rip), %r8d    # 0x239a54
leaq 0x3e66b1(%rip), %rcx    # 0x415ca0
movslq (%rcx,%r14,4), %rcx
movslq %eax, %r9
addq %rcx, %r9
leaq 0x14dab0(%rip), %rcx    # 0x17d0b0
addq (%rcx), %r9
subl %eax, %edx
shll $0x10, %edx
movq 0x3e4211(%rip), %rdi    # 0x413820
xorl %ecx, %ecx
cmpb $0x1, %r10b
jne 0x2f67a
callq 0x6dff4
jmp 0x2f67f
movl 0x3d79c8(%rip), %esi    # 0x406fec
sarl %esi
subl %esi, %ecx
movl 0x3e6625(%rip), %r8d    # 0x415c54
imull %ecx, %r8d
addl 0x20a416(%rip), %r8d    # 0x239a50
imull 0x3e6617(%rip), %ecx    # 0x415c58
subl 0x20a40d(%rip), %ecx    # 0x239a54
leaq 0x3e6652(%rip), %rsi    # 0x415ca0
movslq (%rsi,%r14,4), %r9
addq %rdx, %r9
leaq 0x14da54(%rip), %rsi    # 0x17d0b0
addq (%rsi), %r9
subl %eax, %edx
movl 0x3e41c5(%rip), %eax    # 0x41382c
shll $0x8, %eax
movl %edx, %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %eax, %edx
callq 0x6dd04
jmp 0x2f67f
callq 0x6e098
addq $-0x2, %rbx
cmpq $0x1, %r13
jg 0x2f523
movl 0x3e41a5(%rip), %eax    # 0x413838
addl %eax, 0x20a3b7(%rip)    # 0x239a50
movl 0x3e41a5(%rip), %eax    # 0x413844
addl %eax, 0x20a3af(%rip)    # 0x239a54
addq $0x54, %r15
incq %r14
addq $-0x54, %r12
cmpq 0x8(%rsp), %r14
jne 0x2f4f9
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xab51