// File: /workspace/github_c_makefile_star2_O3/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 0x27058
cmpb $0x0, 0x385c42(%rip)    # 0x3ac4e0
je 0x268be
movl %edi, %eax
andl $0x7, %eax
leaq 0x7def4(%rip), %rcx     # 0xa47a0
movb (%rax,%rcx), %al
movl %edi, %ecx
shrl $0x3, %ecx
leaq 0x384a25(%rip), %rdx    # 0x3ab2e0
orb %al, (%rcx,%rdx)
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movw %di, 0x42d23a(%rip)     # 0x453b10
xorl %esi, %esi
movw $0x1, %ax
leaq 0x385c2d(%rip), %r15    # 0x3ac510
leaq 0x2563c6(%rip), %r9     # 0x27ccb0
leaq 0x38fc2f(%rip), %r10    # 0x3b6520
xorl %r12d, %r12d
decl %eax
movw %ax, 0x42d413(%rip)     # 0x453d10
movzwl %ax, %eax
leaq 0x42d209(%rip), %rcx    # 0x453b10
movzwl (%rcx,%rax,2), %eax
movw %ax, 0x1c(%rsp)
movswq %ax, %rbx
movzwl (%r15,%rbx,2), %ebp
testw %bp, %bp
js 0x26a01
movb 0x3371a4(%rip), %al     # 0x35dacc
movzwl %bp, %r14d
imulq $0x2c, %r14, %rdi
movzwl 0xc(%rdi,%r9), %edx
testw %dx, %dx
jns 0x26946
testb %al, %al
jne 0x26946
xorl %eax, %eax
jmp 0x269f3
addq %r9, %rdi
cmpb $0x0, 0x14(%rdi)
je 0x269f3
movq %rsi, %r15
cmpb $0x0, 0x15(%rdi)
setne %sil
movl 0x3b0e40(%rip), %ecx    # 0x3d77a4
cmpl $0x800, %ecx            # imm = 0x800
setl %r8b
andb %sil, %r8b
cmpb $0x1, %r8b
jne 0x269f0
testb $0x30, %dl
jne 0x269a2
movl (%rdi), %edx
movl 0x4(%rdi), %esi
subl 0x2140cd(%rip), %esi    # 0x23aa54
subl 0x2140c3(%rip), %edx    # 0x23aa50
imull 0x2490dc(%rip), %edx    # 0x26fa70
imull 0x2490dd(%rip), %esi    # 0x26fa78
addl %edx, %esi
movq %r15, %rsi
jle 0x269f3
movslq %ecx, %rax
imulq $0x2c, %rax, %rsi
leaq 0x3bae20(%rip), %r13    # 0x3e17d0
addq %r13, %rsi
movl $0x2c, %edx
callq 0x4d437
movq %r15, %rsi
leaq 0x38fb59(%rip), %r10    # 0x3b6520
leaq 0x2562e2(%rip), %r9     # 0x27ccb0
movslq 0x3b0dcf(%rip), %rax    # 0x3d77a4
leal 0x1(%rax), %ecx
movl %ecx, 0x3b0dc6(%rip)    # 0x3d77a4
imulq $0x2c, %rax, %rax
movw %bp, 0x1e(%rax,%r13)
movb 0x3370de(%rip), %al     # 0x35dacc
jmp 0x269f3
movq %r15, %rsi
movzwl (%r10,%r14,2), %ebp
testw %bp, %bp
jns 0x26928
movswl %bx, %eax
movl %eax, %ecx
andl $0x7, %ecx
leaq 0x7dd90(%rip), %rdx     # 0xa47a0
movb (%rcx,%rdx), %cl
sarl $0x3, %eax
cltq
leaq 0x3a9341(%rip), %rdx    # 0x3cfd60
orb %cl, (%rax,%rdx)
leaq (%rbx,%rbx,4), %rax
leaq 0x21d033(%rip), %rcx    # 0x243a60
movswq (%rcx,%rax,8), %rdx
movzwl 0x2(%rcx,%rax,8), %eax
addw %dx, %ax
cmpw %ax, %dx
jge 0x26f03
movzwl 0x3b0d52(%rip), %ecx    # 0x3d779c
movw %cx, 0x12(%rsp)
movswq %cx, %rcx
movq %rcx, 0x68(%rsp)
movzwl 0x3b0d3b(%rip), %edi    # 0x3d779a
movswq %di, %rcx
movq %rcx, 0x70(%rsp)
movzwl 0x42d2a1(%rip), %ecx    # 0x453d10
movq %rcx, 0x48(%rsp)
movslq 0x213fd5(%rip), %r9     # 0x23aa50
movslq 0x213fd2(%rip), %rcx    # 0x23aa54
movq %rcx, 0x80(%rsp)
movslq 0x248fdf(%rip), %r11    # 0x26fa70
movslq 0x248fe0(%rip), %r8     # 0x26fa78
movslq 0x248fdd(%rip), %rcx    # 0x26fa7c
movq %rcx, 0x60(%rsp)
movslq 0x248fc9(%rip), %rcx    # 0x26fa74
movq %rcx, 0x58(%rsp)
movslq 0x3f23ad(%rip), %rcx    # 0x418e64
movq %rcx, 0x40(%rsp)
movl 0x7dd6d(%rip), %r10d    # 0xa4830
movswq %ax, %rax
movq %rdx, %rcx
subq %rax, %rcx
movq %rcx, 0x78(%rsp)
movq %rdx, %rax
shlq $0x5, %rax
leaq 0x191f70(%rip), %rcx    # 0x1b8a50
leaq (%rax,%rcx), %rbx
addq $0xe, %rbx
movq %r10, 0x38(%rsp)
leal -0x1(%r10), %r14d
movzwl %dx, %eax
movq %rax, 0x50(%rsp)
xorl %r15d, %r15d
movl %edi, %r13d
movw %di, 0x1a(%rsp)
movl %edi, %ebp
movq %rdx, %r10
movq %r9, 0x30(%rsp)
movl %r14d, 0x14(%rsp)
movw %r13w, 0x1e(%rsp)
movl %ebp, 0xc(%rsp)
movswl -0x2(%rbx), %edi
movswq -0x6(%rbx), %rax
shlq $0x5, %rax
movslq -0xe(%rbx), %rbp
subq %r9, %rbp
movslq -0xa(%rbx), %r14
movq 0x80(%rsp), %r13
subq %r13, %r14
leaq 0x191f09(%rip), %rcx    # 0x1b8a50
movslq (%rax,%rcx), %rdx
subq %r9, %rdx
movslq 0x4(%rax,%rcx), %rax
subq %r13, %rax
testl %edi, %edi
js 0x26c0c
testb $0x20, (%rbx)
jne 0x26c0c
movq %r11, %r9
movq %r10, %r11
movq %rsi, %r10
movl %edi, %r13d
shrl $0x3, %r13d
movl %edi, %esi
andl $0x7, %esi
leaq 0x7dc1d(%rip), %rcx     # 0xa47a0
movb (%rsi,%rcx), %sil
leaq 0x3a91d2(%rip), %rcx    # 0x3cfd60
testb %sil, (%r13,%rcx)
jne 0x26bfe
movl %eax, %r13d
imull %ebp, %r13d
movl %edx, %esi
imull %r14d, %esi
subl %esi, %r13d
leal 0x40000(%r13), %esi
cmpl $0x7ffff, %esi          # imm = 0x7FFFF
ja 0x26bfe
movslq %r13d, %rsi
imulq %rsi, %rsi
shrq $0x5, %rsi
movl %edx, %r13d
subl %ebp, %r13d
imull %r13d, %r13d
movl %eax, %ecx
subl %r14d, %ecx
imull %ecx, %ecx
addl %r13d, %ecx
cmpl %esi, %ecx
jl 0x26bfe
movq 0x48(%rsp), %rsi
movswq %si, %rcx
incl %esi
movw %si, 0x42d126(%rip)     # 0x453d10
leaq 0x42cf1f(%rip), %r13    # 0x453b10
movw %di, (%r13,%rcx,2)
movl %esi, %ecx
movq %rcx, 0x48(%rsp)
movq %r10, %rsi
movq %r11, %r10
movq %r9, %r11
movq 0x30(%rsp), %r9
testq %r15, %r15
je 0x26c24
movq 0x50(%rsp), %rcx
addl %r15d, %ecx
movl %esi, %edi
movl %r12d, %r13d
cmpw -0x26(%rbx), %cx
je 0x26c55
movq %rbp, %r13
negq %r13
movq %r11, %rcx
imulq %r14, %rcx
imulq %r8, %r13
addq %rcx, %r13
shrq $0x6, %r13
movslq %ebp, %rcx
imulq 0x60(%rsp), %rcx
imulq 0x58(%rsp), %r14
addq %rcx, %r14
shrq $0x6, %r14
movl %r14d, %edi
movq %rdx, %r12
negq %r12
movq %r11, %rcx
imulq %rax, %rcx
imulq %r8, %r12
addq %rcx, %r12
shrq $0x6, %r12
movslq %edx, %rcx
imulq 0x60(%rsp), %rcx
imulq 0x58(%rsp), %rax
addq %rcx, %rax
movq %rax, %rsi
shrq $0x6, %rsi
cmpl $0xff, %edi
jg 0x26c96
cmpl $0x100, %esi            # imm = 0x100
jl 0x26cb7
movslq %r13d, %rax
movslq %esi, %r14
movq %r14, %rcx
imulq %rax, %rcx
movl %r12d, %edx
negl %edx
movslq %edx, %rdx
movslq %edi, %rbp
imulq %rbp, %rdx
addq %rcx, %rdx
js 0x26d0c
movl 0xc(%rsp), %ebp
leaq (%r10,%r15), %rax
movswq -0x6(%rbx), %rcx
cmpq %rcx, %rax
setg %al
movzwl 0x1e(%rsp), %r13d
cmpw %bp, %r13w
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x26cf2
movswq %bp, %rax
leaq 0x3b8ae7(%rip), %rcx    # 0x3df7d0
movw %r13w, -0x2(%rcx,%rax,2)
movl %ebp, %r13d
incq %r15
addq $0x20, %rbx
movq 0x78(%rsp), %rax
addq %r15, %rax
jne 0x26b13
jmp 0x26f0f
movl %edi, %ecx
negl %ecx
cmpl %ecx, %r13d
jge 0x26d6b
movl %esi, %eax
negl %eax
cmpl %eax, %r12d
jl 0x26cb7
movl 0xc(%rsp), %ebp
movswq %bp, %rcx
leaq 0x244d43(%rip), %rax    # 0x26ba70
movq %rcx, 0x20(%rsp)
movl $0x0, (%rax,%rcx,4)
leal (%rdi,%r13), %eax
leal (%rsi,%r12), %ecx
movl %eax, %edx
subl %ecx, %edx
je 0x26cbb
movl %esi, %ecx
subl %edi, %ecx
movslq %ecx, %rcx
cltq
imulq %rcx, %rax
movslq %edx, %rcx
cqto
idivq %rcx
addl %edi, %eax
xorl %ebp, %ebp
movq 0x20(%rsp), %rdx
jmp 0x26db1
testl %edi, %edi
je 0x26cb7
cmpl %r13d, %edi
jl 0x26cb7
movq 0x40(%rsp), %rcx
imulq %rcx, %rax
cqto
idivq %rbp
movswq 0xc(%rsp), %rdx
movl %r13d, %ebp
notl %ebp
shrl $0x1f, %ebp
addl %ecx, %ebp
addl %eax, %ebp
cmpl 0x38(%rsp), %ebp
cmovgel 0x14(%rsp), %ebp
leaq 0x244cc4(%rip), %rax    # 0x26ba70
movl %ebp, (%rax,%rdx,4)
movl %edi, %eax
leaq 0x24ccd8(%rip), %rcx    # 0x273a90
movl %eax, (%rcx,%rdx,4)
cmpl $0x100, %eax            # imm = 0x100
jl 0x26cb7
cmpl %esi, %r12d
jle 0x26e19
movl %edi, %eax
subl %r13d, %eax
jl 0x26cb7
leaq 0x248ca3(%rip), %rcx    # 0x26fa80
movl 0x14(%rsp), %r14d
movq %rdx, 0x20(%rsp)
movl %r14d, (%rcx,%rdx,4)
leal (%rdi,%r12), %edx
leal (%rsi,%r13), %ecx
subl %ecx, %edx
je 0x26cb7
movl %esi, %ecx
subl %edi, %ecx
movslq %ecx, %rcx
cltq
imulq %rcx, %rax
movslq %edx, %rcx
cqto
idivq %rcx
addl %edi, %eax
movl %r14d, 0x2c(%rsp)
jmp 0x26e70
movl %esi, %eax
negl %eax
cmpl %eax, %r12d
jl 0x26cb7
testl %esi, %esi
je 0x26cb7
movslq %r12d, %rax
movq 0x40(%rsp), %rcx
imulq %rcx, %rax
movq %rdx, 0x20(%rsp)
cqto
idivq %r14
movl %r12d, %edx
sarl $0x1f, %edx
addl %ecx, %edx
addl %eax, %edx
cmpl 0x38(%rsp), %edx
movl 0x14(%rsp), %r14d
cmovgel %r14d, %edx
leaq 0x248c1e(%rip), %rax    # 0x26fa80
movl %edx, 0x2c(%rsp)
movq 0x20(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl %esi, %eax
leaq 0x250c19(%rip), %rcx    # 0x277a90
movq 0x20(%rsp), %rdx
movl %eax, (%rcx,%rdx,4)
cmpl $0x100, %eax            # imm = 0x100
jl 0x26cb7
cmpl 0x2c(%rsp), %ebp
jg 0x26cb7
movq 0x50(%rsp), %rax
addl %r15d, %eax
leaq 0x211bad(%rip), %rcx    # 0x238a50
movq 0x20(%rsp), %rdx
movw %ax, (%rcx,%rdx,2)
leaq 0x3e114d(%rip), %rax    # 0x408000
movl %r13d, (%rax,%rdx,4)
leaq 0x3e5142(%rip), %rax    # 0x40c000
movl %edi, (%rax,%rdx,4)
leaq 0x426fa8(%rip), %rax    # 0x44de70
movzwl 0x1c(%rsp), %ecx
movw %cx, (%rax,%rdx,2)
leaq 0x475038(%rip), %rax    # 0x49bf10
movl %r12d, (%rax,%rdx,4)
leaq 0x47902d(%rip), %rax    # 0x49ff10
movl %esi, (%rax,%rdx,4)
movl 0xc(%rsp), %ebp
incl %ebp
leaq 0x3b88dd(%rip), %rax    # 0x3df7d0
movw %bp, (%rax,%rdx,2)
movw %bp, 0x3b089c(%rip)     # 0x3d779a
jmp 0x26cbb
leaq 0x385606(%rip), %r15    # 0x3ac510
jmp 0x27037
cmpw %bp, 0x1a(%rsp)
jge 0x27022
movq %rsi, %r13
movswq %bp, %rax
movq 0x70(%rsp), %rbp
leaq (,%rbp,2), %rdx
subq %rbp, %rax
shlq $0x2, %rbp
xorl %esi, %esi
movzwl 0x12(%rsp), %ecx
movl %ecx, %edi
leaq 0x3b888b(%rip), %r11    # 0x3df7d0
leaq 0x211b04(%rip), %rbx    # 0x238a50
leaq 0x248b2d(%rip), %r14    # 0x26fa80
leaq 0x244b16(%rip), %r15    # 0x26ba70
leaq (%rdx,%rsi,2), %rcx
movswq (%rbx,%rcx), %r8
shlq $0x5, %r8
leaq 0x191ae2(%rip), %r9     # 0x1b8a50
movzwl 0x8(%r8,%r9), %r10d
movzwl (%r11,%rcx), %r8d
movswq %r8w, %r9
cmpw (%rbx,%r9,2), %r10w
jne 0x26f99
leaq (,%rsi,4), %rcx
addq %rbp, %rcx
movl (%r14,%rcx), %ecx
cmpl (%r15,%r9,4), %ecx
jl 0x26fc0
leaq (%rsi,%rsi), %rcx
movswq %di, %r9
incl %edi
movw %di, 0x3b07f2(%rip)     # 0x3d779c
leaq 0x3b080f(%rip), %r10    # 0x3d77c0
movw %r8w, (%r10,%r9,2)
addq %rdx, %rcx
movw $0xffff, (%r11,%rcx)    # imm = 0xFFFF
incq %rsi
cmpq %rsi, %rax
jne 0x26f5a
cmpw %di, 0x12(%rsp)
leaq 0x38553c(%rip), %r15    # 0x3ac510
leaq 0x255cd5(%rip), %r9     # 0x27ccb0
leaq 0x38f53e(%rip), %r10    # 0x3b6520
jge 0x2701d
movswq %di, %rax
leaq 0x3b27d1(%rip), %rdi    # 0x3d97c0
movq 0x68(%rsp), %r8
leaq 0x3b07c5(%rip), %rcx    # 0x3d77c0
movzwl (%rcx,%r8,2), %esi
movl %esi, %edx
movswq %si, %rcx
movzwl (%r11,%rcx,2), %esi
testw %si, %si
jns 0x27000
movw %dx, (%rdi,%r8,2)
incq %r8
cmpq %rax, %r8
jne 0x26ff4
movq %r13, %rsi
jmp 0x27037
leaq 0x3854e7(%rip), %r15    # 0x3ac510
leaq 0x255c80(%rip), %r9     # 0x27ccb0
leaq 0x38f4e9(%rip), %r10    # 0x3b6520
movzwl 0x42ccd2(%rip), %eax    # 0x453d10
testw %ax, %ax
jg 0x268f4
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq