// File: /workspace/github_c_makefile_star2_O3/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 0x5cab5a(%rip), %rax    # 0x5fa950
cmpl $0x3, (%rax)
jne 0x2fe00
jmp 0x5ca69
leal -0x1(%rdi), %eax
movl %eax, %ecx
testl %edi, %edi
jle 0x2fe47
leaq (,%rcx,4), %rsi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x80000000, %edx       # imm = 0x80000000
leaq 0x3dc1de(%rip), %r8     # 0x40c000
movl %edx, %r9d
movl (%rsi,%r8), %edx
cmpl %edx, %eax
cmovgel %edx, %eax
cmpl %edx, %r9d
cmovgl %r9d, %edx
addq $-0x4, %rsi
cmpq $-0x4, %rsi
jne 0x2fe22
sarl $0xc, %eax
sarl $0xc, %edx
jmp 0x2fe51
movl $0xfff80000, %edx       # imm = 0xFFF80000
movl $0x7ffff, %eax          # imm = 0x7FFFF
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
xorl %r14d, %r14d
testl %eax, %eax
cmovgl %eax, %r14d
movl 0x3d8182(%rip), %eax    # 0x407ff0
leal -0x1(%rax), %esi
cmpl %eax, %edx
cmovll %edx, %esi
movq %rsi, 0x8(%rsp)
cmpl %esi, %r14d
jg 0x2fec5
movl %r14d, %edx
movl 0x8(%rsp), %esi
incq %rsi
subq %rdx, %rsi
shlq $0x3, %rdx
leaq 0x484688(%rip), %r8     # 0x4b4520
xorl %r9d, %r9d
leaq 0x57e55e(%rip), %r10    # 0x5ae400
leaq 0x5828d7(%rip), %r11    # 0x5b2780
leaq (%rdx,%r9,8), %rbx
movq %r8, (%r10,%rbx)
leaq 0x2a(%r8), %r15
movq %r15, (%r11,%rbx)
incq %r9
addq $0x54, %r8
cmpq %r9, %rsi
jne 0x2fea9
testl %edi, %edi
jle 0x2fff6
movq %r14, 0x10(%rsp)
leaq 0x23bb97(%rip), %rsi    # 0x26ba70
leaq 0x3dc120(%rip), %rdi    # 0x40c000
leaq 0x3d8119(%rip), %r8     # 0x408000
leaq 0x582892(%rip), %r9     # 0x5b2780
leaq 0x57e50b(%rip), %r10    # 0x5ae400
movslq (%rsi,%rcx,4), %rax
movl (%rdi,%rcx,4), %r13d
movl %r13d, %r11d
sarl $0xc, %r11d
movl (%rdi,%rax,4), %r12d
movl %r12d, %r15d
sarl $0xc, %r15d
cmpl %r15d, %r11d
je 0x2ffdb
movl (%r8,%rcx,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 0x2ff8c
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 0x2ff63
jmp 0x2ffdb
jge 0x2ffdb
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 0x2ffb6
leaq -0x1(%rcx), %rax
testq %rcx, %rcx
movq %rax, %rcx
jg 0x2fef5
movl 0x3d7fff(%rip), %eax    # 0x407ff0
movq 0x10(%rsp), %r14
movslq 0x3e483b(%rip), %rcx    # 0x414838
movslq 0x32d8b4(%rip), %rdx    # 0x35d8b8
imulq %rdx, %rcx
shrq $0x10, %rcx
movl %ecx, 0x3e4826(%rip)    # 0x414838
movslq 0x3e482b(%rip), %rsi    # 0x414844
imulq %rdx, %rsi
shrq $0x10, %rsi
sarl %eax
negl %eax
addl %r14d, %eax
incl %eax
imull %eax, %ecx
addl %ecx, 0x20aa1d(%rip)    # 0x23aa50
imull %esi, %eax
addl %eax, 0x20aa18(%rip)    # 0x23aa54
movl %esi, 0x3e4802(%rip)    # 0x414844
movl 0x3e6c0c(%rip), %edi    # 0x416c54
movl 0x3e6c0a(%rip), %esi    # 0x416c58
callq 0x6fa83
cmpl 0x8(%rsp), %r14d
jg 0x3024d
movl %r14d, %r14d
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
leaq 0x4844ac(%rip), %r15    # 0x4b4520
movq %r15, %rax
negq %rax
movq %rax, 0x10(%rsp)
leaq 0x57e37a(%rip), %rax    # 0x5ae400
movq (%rax,%r14,8), %rbx
movq %rbx, %rax
subq %r15, %rax
shrq %rax
testl %eax, %eax
jle 0x3021d
addq 0x10(%rsp), %rbx
shrq %rbx
movl %ebx, %r13d
decl %ebx
leaq (%r15,%rbx,2), %rbp
movq %r13, %r12
cmpq $0x2, %r13
jb 0x30101
xorl %edx, %edx
movq %rbp, %rax
movl %ebx, %ecx
xorl %r8d, %r8d
movl %r8d, %edi
movzwl (%rax), %r9d
movl %ecx, %esi
cmpw (%r15,%rdi,2), %r9w
jl 0x300d2
movl %r8d, %esi
movzwl 0x2a(%rax), %r8d
movl %edx, %r9d
movl %ecx, %edi
cmpw 0x2a(%r15,%r9,2), %r8w
jl 0x300e6
movl %edx, %edi
leal -0x1(%rcx), %r9d
addq $-0x2, %rax
movl %edi, %edx
movl %esi, %r8d
cmpl $0x1, %ecx
movl %r9d, %ecx
jg 0x300bf
movl %esi, %ecx
movl %edi, %edx
jmp 0x30105
xorl %ecx, %ecx
xorl %edx, %edx
leaq -0x1(%r12), %r13
movswl (%r15,%rcx,2), %eax
movzwl (%r15,%r13,2), %esi
movw %si, (%r15,%rcx,2)
movswl 0x2a(%r15,%rdx,2), %ecx
movzwl 0x2a(%r15,%r13,2), %esi
movw %si, 0x2a(%r15,%rdx,2)
cmpw %ax, %cx
jle 0x3020d
movslq %ecx, %rdx
decq %rdx
movb 0x3e46f3(%rip), %r10b   # 0x414834
testb %r10b, %r10b
je 0x301ac
movl 0x3d7ea0(%rip), %ecx    # 0x407fec
sarl %ecx
negl %ecx
leal (%rax,%rcx), %r8d
incl %r8d
movl 0x3e6af7(%rip), %esi    # 0x416c54
imull %r8d, %esi
addl 0x20a8e9(%rip), %esi    # 0x23aa50
imull 0x3e6ae9(%rip), %r8d    # 0x416c58
subl 0x20a8de(%rip), %r8d    # 0x23aa54
leaq 0x3e6b23(%rip), %rcx    # 0x416ca0
movslq (%rcx,%r14,4), %rcx
movslq %eax, %r9
addq %rcx, %r9
leaq 0x14df22(%rip), %rcx    # 0x17e0b0
addq (%rcx), %r9
subl %eax, %edx
shll $0x10, %edx
movq 0x3e4683(%rip), %rdi    # 0x414820
xorl %ecx, %ecx
cmpb $0x1, %r10b
jne 0x30208
callq 0x6fd7c
jmp 0x3020d
movl 0x3d7e3a(%rip), %esi    # 0x407fec
sarl %esi
subl %esi, %ecx
movl 0x3e6a97(%rip), %r8d    # 0x416c54
imull %ecx, %r8d
addl 0x20a888(%rip), %r8d    # 0x23aa50
imull 0x3e6a89(%rip), %ecx    # 0x416c58
subl 0x20a87f(%rip), %ecx    # 0x23aa54
leaq 0x3e6ac4(%rip), %rsi    # 0x416ca0
movslq (%rsi,%r14,4), %r9
addq %rdx, %r9
leaq 0x14dec6(%rip), %rsi    # 0x17e0b0
addq (%rsi), %r9
subl %eax, %edx
movl 0x3e4637(%rip), %eax    # 0x41482c
shll $0x8, %eax
movl %edx, %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %eax, %edx
callq 0x6fa90
jmp 0x3020d
callq 0x6fe20
decl %ebx
addq $-0x2, %rbp
cmpq $0x1, %r12
ja 0x300ac
movl 0x3e4615(%rip), %eax    # 0x414838
addl %eax, 0x20a827(%rip)    # 0x23aa50
movl 0x3e4615(%rip), %eax    # 0x414844
addl %eax, 0x20a81f(%rip)    # 0x23aa54
addq $0x54, %r15
incq %r14
addq $-0x54, 0x10(%rsp)
cmpq 0x8(%rsp), %r14
jne 0x3007f
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xacd7