// File: /workspace/github_c_makefile_star2_O2/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 0x5c6baf(%rip), %rax    # 0x5f3950
cmpl $0x3, (%rax)
je 0x52713
decl %edi
movl $0x80000000, %eax       # imm = 0x80000000
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq 0x3d8243(%rip), %rcx    # 0x405000
movl %edi, %esi
testl %esi, %esi
js 0x2cddc
movl %esi, %r8d
movl (%rcx,%r8,4), %r8d
cmpl %r8d, %edx
cmovgel %r8d, %edx
cmpl %r8d, %eax
cmovlel %r8d, %eax
decl %esi
jmp 0x2cdbf
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
sarl $0xc, %edx
sarl $0xc, %eax
xorl %r14d, %r14d
testl %edx, %edx
cmovgl %edx, %r14d
movl 0x3d41f1(%rip), %edx    # 0x400ff0
leal -0x1(%rdx), %esi
cmpl %edx, %eax
cmovll %eax, %esi
movslq %esi, %rax
movq %rax, 0x8(%rsp)
leaq (,%r14,8), %rax
leaq 0x480702(%rip), %rdx    # 0x4ad520
leaq 0x57e95b(%rip), %rsi    # 0x5ab780
movq %r14, %r8
cmpq 0x8(%rsp), %r8
jg 0x2ce4f
leaq 0x57a5ca(%rip), %r9     # 0x5a7400
movq %rdx, (%rax,%r9)
leaq 0x2a(%rdx), %r9
movq %r9, (%rax,%rsi)
incq %r8
addq $0x8, %rax
addq $0x54, %rdx
jmp 0x2ce28
leaq 0x237c1a(%rip), %r8     # 0x264a70
leaq 0x3d41a3(%rip), %r9     # 0x401000
testl %edi, %edi
js 0x2cf48
movl %edi, %eax
movslq (%r8,%rax,4), %rdx
movl (%rcx,%rax,4), %ebx
movl %ebx, %r13d
sarl $0xc, %r13d
movl (%rcx,%rdx,4), %r15d
movl %r15d, %ebp
sarl $0xc, %ebp
cmpl %ebp, %r13d
jne 0x2ce88
decl %edi
jmp 0x2ce5d
movl (%r9,%rax,4), %r10d
movl (%r9,%rdx,4), %r11d
movl %r11d, %eax
subl %r10d, %eax
movl %r15d, %edx
subl %ebx, %edx
cltq
shlq $0xc, %rax
movslq %edx, %r12
cqto
idivq %r12
movslq %eax, %r12
movslq %r13d, %rdx
cmpl %r13d, %ebp
jle 0x2cefc
notl %ebx
andl $0xfff, %ebx            # imm = 0xFFF
imulq %rbx, %r12
shrq $0xc, %r12
addl %r12d, %r10d
movslq %ebp, %r11
subq %rdx, %r11
shlq $0x3, %rdx
xorl %ebx, %ebx
cmpq %rbx, %r11
je 0x2ce84
movl %r10d, %ebp
shrl $0xc, %ebp
leaq (%rdx,%rbx,8), %r15
movq (%rsi,%r15), %r12
leaq 0x2(%r12), %r13
movq %r13, (%rsi,%r15)
movw %bp, (%r12)
addl %eax, %r10d
incq %rbx
jmp 0x2ced3
movslq %ebp, %r10
notl %r15d
andl $0xfff, %r15d           # imm = 0xFFF
imulq %r15, %r12
shrq $0xc, %r12
addl %r12d, %r11d
leaq 0x57a4e5(%rip), %rbx    # 0x5a7400
leaq (%rbx,%r10,8), %rbx
cmpq %rdx, %r10
jge 0x2ce84
movl %r11d, %ebp
shrl $0xc, %ebp
movq (%rbx), %r15
leaq 0x2(%r15), %r12
movq %r12, (%rbx)
movw %bp, (%r15)
addl %eax, %r11d
incq %r10
addq $0x8, %rbx
jmp 0x2cf1f
movslq 0x3e08e9(%rip), %rax    # 0x40d838
movslq 0x329962(%rip), %rcx    # 0x3568b8
imulq %rcx, %rax
shrq $0x10, %rax
movl %eax, 0x3e08d4(%rip)    # 0x40d838
movslq 0x3e08d9(%rip), %rdx    # 0x40d844
imulq %rcx, %rdx
shrq $0x10, %rdx
movl 0x3d4077(%rip), %ecx    # 0x400ff0
sarl %ecx
movl %r14d, %esi
subl %ecx, %esi
incl %esi
imull %esi, %eax
addl %eax, 0x206ac5(%rip)    # 0x233a50
imull %edx, %esi
addl %esi, 0x206ac0(%rip)    # 0x233a54
movl %edx, 0x3e08aa(%rip)    # 0x40d844
movl 0x3e2cb4(%rip), %edi    # 0x40fc54
movl 0x3e2cb2(%rip), %esi    # 0x40fc58
callq 0x641d3
leaq 0x48056e(%rip), %r13    # 0x4ad520
movq %r13, %rcx
negq %rcx
leaq 0x48058b(%rip), %r12    # 0x4ad54a
leaq 0x14a0ea(%rip), %rbx    # 0x1770b0
cmpq 0x8(%rsp), %r14
jg 0x2d17b
leaq 0x57a428(%rip), %rax    # 0x5a7400
movq (%rax,%r14,8), %rbp
movq %rcx, 0x10(%rsp)
addq %rcx, %rbp
shrq %rbp
movl %ebp, %r15d
decl %ebp
testl %r15d, %r15d
jle 0x2d14a
movl %ebp, %eax
leaq (,%rax,2), %rax
addq %r13, %rax
decq %r15
movl %r15d, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl %esi, %r10d
movl %edx, %edi
testl %ecx, %ecx
jle 0x2d049
movzwl (%rax), %r11d
movl %ecx, %r8d
movl %ecx, %r9d
cmpw (%r13,%r10,2), %r11w
jl 0x2d02a
movl %esi, %r9d
movzwl (%r12,%rcx,2), %esi
cmpw 0x2a(%r13,%rdi,2), %si
jl 0x2d03a
movl %edx, %r8d
decq %rcx
addq $-0x2, %rax
movl %r9d, %esi
movl %r8d, %edx
jmp 0x2d00c
movswl (%r13,%r10,2), %eax
movzwl (%r13,%r15,2), %ecx
movw %cx, (%r13,%r10,2)
movswl 0x2a(%r13,%rdi,2), %ecx
movzwl 0x2a(%r13,%r15,2), %edx
movw %dx, 0x2a(%r13,%rdi,2)
cmpw %ax, %cx
jle 0x2cfea
movslq %ecx, %rdx
decq %rdx
movb 0x3e07b1(%rip), %r10b   # 0x40d834
testb %r10b, %r10b
je 0x2d0ea
movl 0x3d3f5e(%rip), %ecx    # 0x400fec
sarl %ecx
negl %ecx
leal (%rax,%rcx), %r8d
incl %r8d
movl 0x3e2bb5(%rip), %esi    # 0x40fc54
imull %r8d, %esi
addl 0x2069a7(%rip), %esi    # 0x233a50
imull 0x3e2ba7(%rip), %r8d    # 0x40fc58
subl 0x20699c(%rip), %r8d    # 0x233a54
leaq 0x3e2be1(%rip), %rcx    # 0x40fca0
movslq (%rcx,%r14,4), %rcx
movslq %eax, %r9
addq %rcx, %r9
addq (%rbx), %r9
subl %eax, %edx
shll $0x10, %edx
movq 0x3e0748(%rip), %rdi    # 0x40d820
xorl %ecx, %ecx
cmpb $0x1, %r10b
jne 0x2d140
callq 0x64497
jmp 0x2cfea
movl 0x3d3efc(%rip), %esi    # 0x400fec
sarl %esi
subl %esi, %ecx
movl 0x3e2b59(%rip), %r8d    # 0x40fc54
imull %ecx, %r8d
addl 0x20694a(%rip), %r8d    # 0x233a50
imull 0x3e2b4b(%rip), %ecx    # 0x40fc58
subl 0x206941(%rip), %ecx    # 0x233a54
leaq 0x3e2b86(%rip), %rsi    # 0x40fca0
movslq (%rsi,%r14,4), %r9
addq %rdx, %r9
addq (%rbx), %r9
subl %eax, %edx
movl 0x3e0700(%rip), %eax    # 0x40d82c
shll $0x8, %eax
movl %edx, %edi
pushq $-0x1
popq %rsi
movl %eax, %edx
callq 0x641e0
jmp 0x2cfea
callq 0x64533
jmp 0x2cfea
movl 0x3e06e8(%rip), %eax    # 0x40d838
addl %eax, 0x2068fa(%rip)    # 0x233a50
movl 0x3e06e8(%rip), %eax    # 0x40d844
addl %eax, 0x2068f2(%rip)    # 0x233a54
addq $0x54, %r13
incq %r14
movq 0x10(%rsp), %rcx
addq $-0x54, %rcx
addq $0x54, %r12
jmp 0x2cfc6
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9fd2