// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: scansector
static void scansector(short sectnum)
{
	walltype *wal, *wal2;
	spritetype *spr;
	int xs, ys, x1, y1, x2, y2, xp1, yp1, xp2=0, yp2=0, templong;
	short z, zz, startwall, endwall, numscansbefore, scanfirst, bunchfrst;
	short nextsectnum;

	if (sectnum < 0) return;

	if (automapping) show2dsector[sectnum>>3] |= pow2char[sectnum&7];

	sectorborder[0] = sectnum, sectorbordercnt = 1;
	do
	{
		sectnum = sectorborder[--sectorbordercnt];

		for(z=headspritesect[sectnum];z>=0;z=nextspritesect[z])
		{
			spr = &sprite[z];
			if ((((spr->cstat&0x8000) == 0) || (showinvisibility)) &&
				  (spr->xrepeat > 0) && (spr->yrepeat > 0) &&
				  (spritesortcnt < MAXSPRITESONSCREEN))
			{
				xs = spr->x-globalposx; ys = spr->y-globalposy;
				if ((spr->cstat&48) || (xs*cosglobalang+ys*singlobalang > 0))
				{
					copybufbyte(spr,&tsprite[spritesortcnt],sizeof(spritetype));
					tsprite[spritesortcnt++].owner = z;
				}
			}
		}

		gotsector[sectnum>>3] |= pow2char[sectnum&7];

		bunchfrst = numbunches;
		numscansbefore = numscans;

		startwall = sector[sectnum].wallptr;
		endwall = startwall + sector[sectnum].wallnum;
		scanfirst = numscans;
		for(z=startwall,wal=&wall[z];z<endwall;z++,wal++)
		{
			nextsectnum = wal->nextsector;

			wal2 = &wall[wal->point2];
			x1 = wal->x-globalposx; y1 = wal->y-globalposy;
			x2 = wal2->x-globalposx; y2 = wal2->y-globalposy;

			if ((nextsectnum >= 0) && ((wal->cstat&32) == 0))
				if ((gotsector[nextsectnum>>3]&pow2char[nextsectnum&7]) == 0)
				{
					templong = x1*y2-x2*y1;
					if (((unsigned)templong+262144) < 524288)
						if (mulscale5(templong,templong) <= (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
							sectorborder[sectorbordercnt++] = nextsectnum;
				}

			if ((z == startwall) || (wall[z-1].point2 != z))
			{
				xp1 = dmulscale6(y1,cosglobalang,-x1,singlobalang);
				yp1 = dmulscale6(x1,cosviewingrangeglobalang,y1,sinviewingrangeglobalang);
			}
			else
			{
				xp1 = xp2;
				yp1 = yp2;
			}
			xp2 = dmulscale6(y2,cosglobalang,-x2,singlobalang);
			yp2 = dmulscale6(x2,cosviewingrangeglobalang,y2,sinviewingrangeglobalang);
			if ((yp1 < 256) && (yp2 < 256)) goto skipitaddwall;

				//If wall's NOT facing you
			if (dmulscale32(xp1,yp2,-xp2,yp1) >= 0) goto skipitaddwall;

			if (xp1 >= -yp1)
			{
				if ((xp1 > yp1) || (yp1 == 0)) goto skipitaddwall;
				xb1[numscans] = halfxdimen + scale(xp1,halfxdimen,yp1);
				if (xp1 >= 0) xb1[numscans]++;   //Fix for SIGNED divide
				if (xb1[numscans] >= xdimen) xb1[numscans] = xdimen-1;
				yb1[numscans] = yp1;
			}
			else
			{
				if (xp2 < -yp2) goto skipitaddwall;
				xb1[numscans] = 0;
				templong = yp1-yp2+xp1-xp2;
				if (templong == 0) goto skipitaddwall;
				yb1[numscans] = yp1 + scale(yp2-yp1,xp1+yp1,templong);
			}
			if (yb1[numscans] < 256) goto skipitaddwall;

			if (xp2 <= yp2)
			{
				if ((xp2 < -yp2) || (yp2 == 0)) goto skipitaddwall;
				xb2[numscans] = halfxdimen + scale(xp2,halfxdimen,yp2) - 1;
				if (xp2 >= 0) xb2[numscans]++;   //Fix for SIGNED divide
				if (xb2[numscans] >= xdimen) xb2[numscans] = xdimen-1;
				yb2[numscans] = yp2;
			}
			else
			{
				if (xp1 > yp1) goto skipitaddwall;
				xb2[numscans] = xdimen-1;
				templong = xp2-xp1+yp1-yp2;
				if (templong == 0) goto skipitaddwall;
				yb2[numscans] = yp1 + scale(yp2-yp1,yp1-xp1,templong);
			}
			if ((yb2[numscans] < 256) || (xb1[numscans] > xb2[numscans])) goto skipitaddwall;

				//Made it all the way!
			thesector[numscans] = sectnum; thewall[numscans] = z;
			rx1[numscans] = xp1; ry1[numscans] = yp1;
			rx2[numscans] = xp2; ry2[numscans] = yp2;
			p2[numscans] = numscans+1;
			numscans++;
skipitaddwall:

			if ((wall[z].point2 < z) && (scanfirst < numscans))
				p2[numscans-1] = scanfirst, scanfirst = numscans;
		}

		for(z=numscansbefore;z<numscans;z++)
			if ((wall[thewall[z]].point2 != thewall[p2[z]]) || (xb2[z] >= xb1[p2[z]]))
				bunchfirst[numbunches++] = p2[z], p2[z] = -1;

		for(z=bunchfrst;z<numbunches;z++)
		{
			for(zz=bunchfirst[z];p2[zz]>=0;zz=p2[zz]);
			bunchlast[z] = zz;
		}
	} while (sectorbordercnt > 0);
}

// Assembly:
testw %di, %di
js 0x268a0
cmpb $0x0, 0x38539e(%rip)    # 0x3ab4e0
je 0x26162
movl %edi, %eax
andl $0x7, %eax
leaq 0x7d650(%rip), %rcx     # 0xa37a0
movb (%rax,%rcx), %al
movl %edi, %ecx
shrl $0x3, %ecx
leaq 0x384181(%rip), %rdx    # 0x3aa2e0
orb %al, (%rcx,%rdx)
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movw %di, 0x42c999(%rip)     # 0x452b10
movw $0x1, 0x42cb90(%rip)    # 0x452d10
movq $0x0, 0x10(%rsp)
leaq 0x255b20(%rip), %r8     # 0x27bcb0
leaq 0x38f389(%rip), %r9     # 0x3b5520
leaq 0x3b8632(%rip), %r12    # 0x3de7d0
xorl %r14d, %r14d
movzwl 0x42cb68(%rip), %eax    # 0x452d10
decl %eax
movw %ax, 0x42cb5f(%rip)     # 0x452d10
movswq %ax, %rax
leaq 0x42c954(%rip), %rcx    # 0x452b10
movzwl (%rcx,%rax,2), %eax
movw %ax, 0xc(%rsp)
movswq %ax, %rbx
leaq 0x385340(%rip), %rax    # 0x3ab510
movzwl (%rax,%rbx,2), %ebp
testw %bp, %bp
js 0x2629d
movzwl %bp, %r15d
imulq $0x2c, %r15, %rdi
movzwl 0xc(%rdi,%r8), %ecx
testw %cx, %cx
jns 0x261fd
cmpb $0x0, 0x3368d5(%rip)    # 0x35cacc
je 0x26293
addq %r8, %rdi
cmpb $0x0, 0x14(%rdi)
je 0x26293
cmpb $0x0, 0x15(%rdi)
setne %dl
movl 0x3b058d(%rip), %eax    # 0x3d67a4
cmpl $0x800, %eax            # imm = 0x800
setl %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x26293
testb $0x30, %cl
jne 0x26251
movl (%rdi), %ecx
movl 0x4(%rdi), %edx
subl 0x21381b(%rip), %edx    # 0x239a54
subl 0x213811(%rip), %ecx    # 0x239a50
imull 0x24882a(%rip), %ecx    # 0x26ea70
imull 0x24882b(%rip), %edx    # 0x26ea78
addl %ecx, %edx
jle 0x26293
cltq
imulq $0x2c, %rax, %rsi
leaq 0x3ba572(%rip), %r13    # 0x3e07d0
addq %r13, %rsi
movl $0x2c, %edx
callq 0x4ac52
leaq 0x38f2ae(%rip), %r9     # 0x3b5520
leaq 0x255a37(%rip), %r8     # 0x27bcb0
movslq 0x3b0524(%rip), %rax    # 0x3d67a4
leal 0x1(%rax), %ecx
movl %ecx, 0x3b051b(%rip)    # 0x3d67a4
imulq $0x2c, %rax, %rax
movw %bp, 0x1e(%rax,%r13)
movzwl (%r9,%r15,2), %ebp
jmp 0x261d4
movswl %bx, %eax
movl %eax, %ecx
andl $0x7, %ecx
leaq 0x7d4f4(%rip), %r8      # 0xa37a0
movb (%rcx,%r8), %cl
sarl $0x3, %eax
cltq
leaq 0x3a8aa4(%rip), %rdx    # 0x3ced60
orb %cl, (%rax,%rdx)
movswq 0x3b04d5(%rip), %rcx    # 0x3d679c
movzwl 0x3b04cb(%rip), %r8d    # 0x3d679a
leaq (%rbx,%rbx,4), %rax
leaq 0x21c786(%rip), %rdx    # 0x242a60
movswq (%rdx,%rax,8), %r9
movzwl 0x2(%rdx,%rax,8), %eax
addw %r9w, %ax
cmpw %ax, %r9w
movw %r8w, 0xe(%rsp)
movq %rcx, 0x58(%rsp)
jge 0x26780
movzwl 0x3b0496(%rip), %ecx    # 0x3d679a
movzwl 0x42ca05(%rip), %edx    # 0x452d10
movq %rdx, 0x28(%rsp)
movslq 0x213739(%rip), %rdx    # 0x239a50
movq %rdx, 0x70(%rsp)
movslq 0x213731(%rip), %rdx    # 0x239a54
movq %rdx, 0x68(%rsp)
movslq 0x248741(%rip), %rdx    # 0x26ea70
movq %rdx, 0x50(%rsp)
movslq 0x24873d(%rip), %rdx    # 0x26ea78
movq %rdx, 0x48(%rsp)
movslq 0x248735(%rip), %rdx    # 0x26ea7c
movq %rdx, 0x40(%rsp)
movslq 0x248721(%rip), %rdx    # 0x26ea74
movq %rdx, 0x38(%rsp)
movslq 0x3f1b05(%rip), %rdx    # 0x417e64
movq %rdx, 0x20(%rsp)
movl 0x7d4c6(%rip), %edx     # 0xa3830
movq %rdx, 0x18(%rsp)
decl %edx
movl %edx, 0x8(%rsp)
movswq %ax, %rax
movq %r9, %rdx
shlq $0x5, %rdx
leaq 0x1916c9(%rip), %rsi    # 0x1b7a50
leaq (%rdx,%rsi), %rbx
addq $0xe, %rbx
movzwl %r9w, %edx
movq %rdx, 0x30(%rsp)
movq %r9, %rdx
subq %rax, %rdx
movq %rdx, 0x60(%rsp)
xorl %edi, %edi
movl %r8d, %edx
movq 0x10(%rsp), %r8
movq %r9, %r10
movw %dx, 0x10(%rsp)
movswl -0x2(%rbx), %r11d
movswq -0x6(%rbx), %rax
shlq $0x5, %rax
movslq -0xe(%rbx), %rbp
movq 0x70(%rsp), %r15
subq %r15, %rbp
movslq -0xa(%rbx), %r13
movq 0x68(%rsp), %r9
subq %r9, %r13
leaq 0x19166e(%rip), %rsi    # 0x1b7a50
movslq (%rax,%rsi), %rdx
subq %r15, %rdx
movslq 0x4(%rax,%rsi), %rax
subq %r9, %rax
testl %r11d, %r11d
js 0x264af
testb $0x20, (%rbx)
movq %r10, %r9
jne 0x264b2
movl %ecx, 0x4(%rsp)
movq %r9, %r10
movq %r8, %r9
movl %r11d, %r15d
shrl $0x3, %r15d
movl %r11d, %esi
andl $0x7, %esi
leaq 0x7d37c(%rip), %r8      # 0xa37a0
movb (%rsi,%r8), %sil
leaq 0x3a8931(%rip), %r8     # 0x3ced60
testb %sil, (%r15,%r8)
jne 0x264a3
movl %eax, %r15d
imull %ebp, %r15d
movl %edx, %esi
imull %r13d, %esi
subl %esi, %r15d
leal 0x40000(%r15), %esi
cmpl $0x7ffff, %esi          # imm = 0x7FFFF
ja 0x264a3
movslq %r15d, %rsi
imulq %rsi, %rsi
shrq $0x5, %rsi
movl %edx, %r15d
subl %ebp, %r15d
imull %r15d, %r15d
movl %eax, %r8d
subl %r13d, %r8d
imull %r8d, %r8d
addl %r15d, %r8d
cmpl %esi, %r8d
jl 0x264a3
movq 0x28(%rsp), %r8
movswq %r8w, %rsi
incl %r8d
movw %r8w, 0x42c881(%rip)    # 0x452d10
leaq 0x42c67a(%rip), %r15    # 0x452b10
movw %r11w, (%r15,%rsi,2)
movl %r8d, %esi
movq %rsi, 0x28(%rsp)
movq %r9, %r8
movq %r10, %r9
movl 0x4(%rsp), %ecx
jmp 0x264b2
movq %r10, %r9
testq %rdi, %rdi
je 0x264ca
movq 0x30(%rsp), %rsi
addl %edi, %esi
movl %r8d, %r11d
movl %r14d, %r15d
cmpw -0x26(%rbx), %si
je 0x264ff
movq %rbp, %r15
negq %r15
movq 0x50(%rsp), %rsi
imulq %r13, %rsi
imulq 0x48(%rsp), %r15
addq %rsi, %r15
shrq $0x6, %r15
movslq %ebp, %rsi
imulq 0x40(%rsp), %rsi
imulq 0x38(%rsp), %r13
addq %rsi, %r13
shrq $0x6, %r13
movl %r13d, %r11d
movq %rdx, %r14
negq %r14
movq 0x50(%rsp), %rsi
imulq %rax, %rsi
imulq 0x48(%rsp), %r14
addq %rsi, %r14
shrq $0x6, %r14
movslq %edx, %rdx
imulq 0x40(%rsp), %rdx
imulq 0x38(%rsp), %rax
addq %rdx, %rax
movq %rax, %r8
shrq $0x6, %r8
cmpl $0xff, %r11d
jg 0x26546
cmpl $0x100, %r8d            # imm = 0x100
jl 0x26567
movslq %r15d, %rax
movslq %r8d, %r13
movq %r13, %rdx
imulq %rax, %rdx
movl %r14d, %esi
negl %esi
movslq %esi, %rsi
movslq %r11d, %rbp
imulq %rbp, %rsi
addq %rdx, %rsi
js 0x265ac
leaq (%r9,%rdi), %rax
movswq -0x6(%rbx), %rdx
cmpq %rdx, %rax
jle 0x2658d
movzwl 0x10(%rsp), %edx
cmpw %cx, %dx
jge 0x26592
movswq %cx, %rax
movw %dx, -0x2(%r12,%rax,2)
movl %ecx, %edx
jmp 0x26592
movzwl 0x10(%rsp), %edx
incq %rdi
addq $0x20, %rbx
movq 0x60(%rsp), %rax
addq %rdi, %rax
jne 0x263ad
jmp 0x26785
movl %r11d, %edx
negl %edx
cmpl %edx, %r15d
jge 0x265fc
movl %r8d, %eax
negl %eax
cmpl %eax, %r14d
jl 0x26567
movswq %cx, %rbp
leaq 0x2444a5(%rip), %rax    # 0x26aa70
movl $0x0, (%rax,%rbp,4)
leal (%r15,%r11), %eax
leal (%r8,%r14), %esi
movl %eax, %edx
subl %esi, %edx
je 0x26567
movl %r8d, %esi
subl %r11d, %esi
movslq %esi, %rsi
cltq
imulq %rsi, %rax
movslq %edx, %rsi
cqto
idivq %rsi
addl %r11d, %eax
jmp 0x26642
testl %r11d, %r11d
je 0x26567
cmpl %r15d, %r11d
jl 0x26567
movq 0x20(%rsp), %rsi
imulq %rsi, %rax
cqto
idivq %rbp
movswq %cx, %rbp
movl %r15d, %edx
notl %edx
shrl $0x1f, %edx
addl %esi, %edx
addl %eax, %edx
cmpl 0x18(%rsp), %edx
cmovgel 0x8(%rsp), %edx
leaq 0x244434(%rip), %rax    # 0x26aa70
movl %edx, (%rax,%rbp,4)
movl %r11d, %eax
leaq 0x24c447(%rip), %rdx    # 0x272a90
movl %eax, (%rdx,%rbp,4)
movswq %cx, %rbp
cmpl $0x100, (%rdx,%rbp,4)   # imm = 0x100
jl 0x26567
cmpl %r8d, %r14d
jle 0x266a8
movl %r11d, %eax
subl %r15d, %eax
jl 0x26567
leaq 0x24840b(%rip), %rdx    # 0x26ea80
movl 0x8(%rsp), %esi
movl %esi, (%rdx,%rbp,4)
leal (%r11,%r14), %edx
leal (%r15,%r8), %esi
subl %esi, %edx
je 0x26567
movl %r8d, %esi
subl %r11d, %esi
movslq %esi, %rsi
cltq
imulq %rsi, %rax
movslq %edx, %rsi
cqto
idivq %rsi
addl %r11d, %eax
jmp 0x266f0
movl %r8d, %eax
negl %eax
cmpl %eax, %r14d
jl 0x26567
testl %r8d, %r8d
je 0x26567
movslq %r14d, %rax
movq 0x20(%rsp), %rsi
imulq %rsi, %rax
cqto
idivq %r13
movl %r14d, %edx
sarl $0x1f, %edx
addl %esi, %edx
addl %eax, %edx
cmpl 0x18(%rsp), %edx
cmovgel 0x8(%rsp), %edx
leaq 0x248396(%rip), %rax    # 0x26ea80
movl %edx, (%rax,%rbp,4)
movl %r8d, %eax
leaq 0x250399(%rip), %rdx    # 0x276a90
movl %eax, (%rdx,%rbp,4)
cmpl $0x100, %eax            # imm = 0x100
jl 0x26567
leaq 0x244364(%rip), %rax    # 0x26aa70
movl (%rax,%rbp,4), %eax
leaq 0x24836a(%rip), %rdx    # 0x26ea80
cmpl (%rdx,%rbp,4), %eax
jg 0x26567
movq 0x30(%rsp), %rax
addl %edi, %eax
leaq 0x211323(%rip), %rdx    # 0x237a50
movw %ax, (%rdx,%rbp,2)
leaq 0x3e08c8(%rip), %rax    # 0x407000
movl %r15d, (%rax,%rbp,4)
leaq 0x3e48bd(%rip), %rax    # 0x40b000
movl %r11d, (%rax,%rbp,4)
leaq 0x426722(%rip), %rax    # 0x44ce70
movzwl 0xc(%rsp), %edx
movw %dx, (%rax,%rbp,2)
leaq 0x4747b2(%rip), %rax    # 0x49af10
movl %r14d, (%rax,%rbp,4)
leaq 0x4787a7(%rip), %rax    # 0x49ef10
movl %r8d, (%rax,%rbp,4)
incl %ecx
movw %cx, (%r12,%rbp,2)
movw %cx, 0x3b001f(%rip)     # 0x3d679a
jmp 0x26567
movq 0x10(%rsp), %r8
movq %r8, 0x10(%rsp)
movswq 0x3b0008(%rip), %rax    # 0x3d679a
movzwl 0xe(%rsp), %edx
cmpw %ax, %dx
leaq 0x2442cf(%rip), %rbx    # 0x26aa70
jge 0x26835
movswq %dx, %rdx
movzwl 0x3affea(%rip), %edi    # 0x3d679c
leaq (%rdx,%rdx), %rsi
subq %rdx, %rax
shlq $0x2, %rdx
xorl %r8d, %r8d
leaq (%rsi,%r8,2), %r9
leaq 0x211285(%rip), %r15    # 0x237a50
movswq (%r15,%r9), %r10
shlq $0x5, %r10
leaq 0x191275(%rip), %r11    # 0x1b7a50
movzwl 0x8(%r10,%r11), %r11d
movzwl (%r12,%r9), %r9d
movswq %r9w, %r10
cmpw (%r15,%r10,2), %r11w
jne 0x26806
leaq (%rdx,%r8,4), %r11
leaq 0x248284(%rip), %r15    # 0x26ea80
movl (%r15,%r11), %r11d
cmpl (%rbx,%r10,4), %r11d
jl 0x2682d
leaq (%r8,%r8), %r10
movswq %di, %r11
incl %edi
movw %di, 0x3aff85(%rip)     # 0x3d679c
leaq 0x3affa2(%rip), %r15    # 0x3d67c0
movw %r9w, (%r15,%r11,2)
addq %rsi, %r10
movw $0xffff, (%r12,%r10)    # imm = 0xFFFF
incq %r8
cmpq %r8, %rax
jne 0x267c0
movswq 0x3aff5f(%rip), %rax    # 0x3d679c
movq 0x58(%rsp), %rcx
cmpw %ax, %cx
leaq 0x255464(%rip), %r8     # 0x27bcb0
leaq 0x38eccd(%rip), %r9     # 0x3b5520
leaq 0x3aff66(%rip), %rdi    # 0x3d67c0
leaq 0x3b1f5f(%rip), %r10    # 0x3d87c0
jge 0x26884
movzwl (%rdi,%rcx,2), %esi
movl %esi, %edx
movswq %si, %rsi
movzwl (%r12,%rsi,2), %esi
testw %si, %si
jns 0x26867
movw %dx, (%r10,%rcx,2)
incq %rcx
cmpq %rax, %rcx
jne 0x26863
cmpw $0x0, 0x42c484(%rip)    # 0x452d10
jg 0x261a1
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq