// File: /workspace/github_c_makefile_star2_O2/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 0x23f78
cmpb $0x0, 0x381c69(%rip)    # 0x3a54e0
je 0x23897
movl %edi, %eax
andl $0x7, %eax
leaq 0x79f1b(%rip), %rcx     # 0x9d7a0
movb (%rax,%rcx), %al
movl %edi, %ecx
shrl $0x3, %ecx
leaq 0x380a4c(%rip), %rdx    # 0x3a42e0
orb %al, (%rcx,%rdx)
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movw %di, 0x429261(%rip)     # 0x44cb10
movq $0x0, 0x10(%rsp)
movw $0x1, %ax
leaq 0x2523ed(%rip), %r8     # 0x275cb0
leaq 0x38bc56(%rip), %r9     # 0x3af520
leaq 0x3b4eff(%rip), %r13    # 0x3d87d0
xorl %r15d, %r15d
decl %eax
movw %ax, 0x429433(%rip)     # 0x44cd10
movzwl %ax, %eax
leaq 0x429229(%rip), %rcx    # 0x44cb10
movzwl (%rcx,%rax,2), %eax
movw %ax, 0x20(%rsp)
movswl %ax, %ebp
movswq %bp, %rbx
leaq 0x381c12(%rip), %rax    # 0x3a5510
leaq (%rax,%rbx,2), %rax
movswq (%rax), %r14
testq %r14, %r14
js 0x239c8
imulq $0x2c, %r14, %rdi
movzwl 0xc(%rdi,%r8), %ecx
testw %cx, %cx
jns 0x2392b
cmpb $0x0, 0x3331a7(%rip)    # 0x356acc
je 0x239bf
addq %r8, %rdi
cmpb $0x0, 0x14(%rdi)
je 0x239bf
cmpb $0x0, 0x15(%rdi)
setne %dl
movl 0x3ace5f(%rip), %eax    # 0x3d07a4
cmpl $0x800, %eax            # imm = 0x800
setl %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x239bf
testb $0x30, %cl
jne 0x2397f
movl (%rdi), %ecx
movl 0x4(%rdi), %edx
subl 0x2100ed(%rip), %edx    # 0x233a54
subl 0x2100e3(%rip), %ecx    # 0x233a50
imull 0x2450fc(%rip), %ecx    # 0x268a70
imull 0x2450fd(%rip), %edx    # 0x268a78
addl %ecx, %edx
jle 0x239bf
cltq
imulq $0x2c, %rax, %rsi
leaq 0x3b6e44(%rip), %r12    # 0x3da7d0
addq %r12, %rsi
pushq $0x2c
popq %rdx
callq 0x44524
leaq 0x38bb82(%rip), %r9     # 0x3af520
leaq 0x25230b(%rip), %r8     # 0x275cb0
movslq 0x3acdf8(%rip), %rax    # 0x3d07a4
leal 0x1(%rax), %ecx
movl %ecx, 0x3acdef(%rip)    # 0x3d07a4
imulq $0x2c, %rax, %rax
movw %r14w, 0x1e(%rax,%r12)
leaq (%r9,%r14,2), %rax
jmp 0x23902
movl %ebp, %eax
andl $0x7, %eax
leaq 0x79dcc(%rip), %r9      # 0x9d7a0
movb (%rax,%r9), %al
sarl $0x3, %ebp
movslq %ebp, %rcx
leaq 0x3a537b(%rip), %rdx    # 0x3c8d60
orb %al, (%rcx,%rdx)
movzwl 0x3acdad(%rip), %eax    # 0x3d079c
movl %eax, 0x24(%rsp)
movswq %ax, %rax
movq %rax, 0x60(%rsp)
movzwl 0x3acd96(%rip), %r12d   # 0x3d079a
movswq %r12w, %rax
movq %rax, 0x68(%rsp)
imulq $0x28, %rbx, %rax
leaq 0x219048(%rip), %rcx    # 0x23ca60
movswq (%rax,%rcx), %r10
movzwl 0x2(%rax,%rcx), %eax
addw %r10w, %ax
movzwl 0x4292e3(%rip), %ecx    # 0x44cd10
movq %rcx, 0x28(%rsp)
movslq 0x210017(%rip), %rcx    # 0x233a50
movq %rcx, 0x78(%rsp)
movslq 0x21000f(%rip), %rcx    # 0x233a54
movq %rcx, 0x70(%rsp)
movslq 0x24501f(%rip), %rcx    # 0x268a70
movq %rcx, 0x58(%rsp)
movslq 0x24501b(%rip), %rcx    # 0x268a78
movq %rcx, 0x50(%rsp)
movslq 0x245013(%rip), %rcx    # 0x268a7c
movq %rcx, 0x48(%rsp)
movslq 0x244fff(%rip), %rcx    # 0x268a74
movq %rcx, 0x40(%rsp)
movslq 0x3ee3e3(%rip), %rcx    # 0x411e64
movq %rcx, 0x38(%rsp)
movswq %ax, %rax
movq %rax, 0x80(%rsp)
movq %r10, %rax
shlq $0x5, %rax
leaq 0x18dfb0(%rip), %rcx    # 0x1b1a50
leaq (%rax,%rcx), %rbx
addq $0xe, %rbx
movl 0x79d82(%rip), %eax     # 0x9d830
movq %rax, 0x30(%rsp)
leal -0x1(%rax), %esi
xorl %r14d, %r14d
movl %r12d, %ecx
movq 0x10(%rsp), %r9
movl %esi, 0x1c(%rsp)
movswq %r12w, %r8
cmpq 0x80(%rsp), %r10
jge 0x23e72
movw %cx, 0x22(%rsp)
movl %r12d, 0xc(%rsp)
movswl -0x2(%rbx,%r14), %ecx
movswq -0x6(%rbx,%r14), %rax
shlq $0x5, %rax
movslq -0xe(%rbx,%r14), %r12
movq 0x78(%rsp), %rbp
subq %rbp, %r12
movslq -0xa(%rbx,%r14), %r11
movq 0x70(%rsp), %rsi
subq %rsi, %r11
leaq 0x18df3e(%rip), %rdi    # 0x1b1a50
movslq (%rax,%rdi), %rdx
subq %rbp, %rdx
movslq 0x4(%rax,%rdi), %rax
subq %rsi, %rax
testl %ecx, %ecx
js 0x23bca
testb $0x20, (%rbx,%r14)
jne 0x23bca
movq %r9, 0x10(%rsp)
movl %ecx, %ebp
shrl $0x3, %ebp
movl %ecx, %edi
andl $0x7, %edi
leaq 0x79c56(%rip), %rsi     # 0x9d7a0
movb (%rdi,%rsi), %dil
leaq 0x3a520b(%rip), %r9     # 0x3c8d60
testb %dil, (%rbp,%r9)
jne 0x23bc5
movl %eax, %ebp
imull %r12d, %ebp
movl %edx, %edi
imull %r11d, %edi
subl %edi, %ebp
leal 0x40000(%rbp), %edi
cmpl $0x7ffff, %edi          # imm = 0x7FFFF
ja 0x23bc5
movslq %ebp, %rdi
imulq %rdi, %rdi
shrq $0x5, %rdi
movl %edx, %ebp
subl %r12d, %ebp
imull %ebp, %ebp
movl %eax, %r9d
subl %r11d, %r9d
imull %r9d, %r9d
addl %ebp, %r9d
cmpl %edi, %r9d
jl 0x23bc5
movq 0x28(%rsp), %r9
movswq %r9w, %rdi
incl %r9d
movw %r9w, 0x42915f(%rip)    # 0x44cd10
leaq 0x428f58(%rip), %rbp    # 0x44cb10
movw %cx, (%rbp,%rdi,2)
movl %r9d, %ecx
movq %rcx, 0x28(%rsp)
movq 0x10(%rsp), %r9
testq %r14, %r14
je 0x23bdd
movl %r9d, %ecx
movl %r15d, %ebp
cmpw -0x26(%rbx,%r14), %r10w
je 0x23c12
movq %r12, %rbp
negq %rbp
movq 0x58(%rsp), %rcx
imulq %r11, %rcx
imulq 0x50(%rsp), %rbp
addq %rcx, %rbp
shrq $0x6, %rbp
movslq %r12d, %rcx
imulq 0x48(%rsp), %rcx
imulq 0x40(%rsp), %r11
addq %rcx, %r11
shrq $0x6, %r11
movl %r11d, %ecx
movq %rdx, %r15
negq %r15
movq 0x58(%rsp), %rdi
imulq %rax, %rdi
imulq 0x50(%rsp), %r15
addq %rdi, %r15
shrq $0x6, %r15
movslq %edx, %rdx
imulq 0x48(%rsp), %rdx
imulq 0x40(%rsp), %rax
addq %rdx, %rax
movq %rax, %r9
shrq $0x6, %r9
cmpl $0xff, %ecx
jg 0x23c58
cmpl $0x100, %r9d            # imm = 0x100
jl 0x23c79
movslq %ebp, %rax
movslq %r9d, %r11
movq %r11, %rdx
imulq %rax, %rdx
movl %r15d, %edi
negl %edi
movslq %edi, %rdi
movslq %ecx, %r12
imulq %r12, %rdi
addq %rdx, %rdi
js 0x23cad
movl 0xc(%rsp), %r12d
movswq -0x6(%rbx,%r14), %rax
cmpq %rax, %r10
movzwl 0x22(%rsp), %ecx
jle 0x23ca1
cmpw %r12w, %cx
jge 0x23ca1
movswq %r12w, %rax
movw %cx, -0x2(%r13,%rax,2)
movl %r12d, %ecx
incq %r10
addq $0x20, %r14
jmp 0x23ac5
movl %ecx, %edx
negl %edx
cmpl %edx, %ebp
jge 0x23cfe
movl %r9d, %eax
negl %eax
cmpl %eax, %r15d
jl 0x23c79
movl 0xc(%rsp), %r12d
movswq %r12w, %r8
leaq 0x240da1(%rip), %rax    # 0x264a70
andl $0x0, (%rax,%r8,4)
leal (%rcx,%rbp), %eax
leal (%r9,%r15), %edi
movl %eax, %edx
subl %edi, %edx
je 0x23c7e
movl %r9d, %edi
subl %ecx, %edi
movslq %edi, %rdi
cltq
imulq %rdi, %rax
movslq %edx, %rdi
cqto
idivq %rdi
addl %ecx, %eax
xorl %r12d, %r12d
jmp 0x23d44
testl %ecx, %ecx
je 0x23c79
cmpl %ebp, %ecx
jl 0x23c79
movq 0x38(%rsp), %rsi
imulq %rsi, %rax
cqto
idivq %r12
movl %ebp, %r12d
notl %r12d
shrl $0x1f, %r12d
addl %esi, %r12d
addl %eax, %r12d
cmpl 0x30(%rsp), %r12d
cmovgel 0x1c(%rsp), %r12d
leaq 0x240d32(%rip), %rax    # 0x264a70
movl %r12d, (%rax,%r8,4)
movl %ecx, %eax
leaq 0x248d45(%rip), %rdx    # 0x26ca90
movl %eax, (%rdx,%r8,4)
cmpl $0x100, %eax            # imm = 0x100
jl 0x23c79
cmpl %r9d, %r15d
jle 0x23da4
movl %ecx, %eax
subl %ebp, %eax
jl 0x23c79
leaq 0x244d10(%rip), %rdx    # 0x268a80
movl 0x1c(%rsp), %esi
movl %esi, (%rdx,%r8,4)
leal (%rcx,%r15), %edx
leal (%r9,%rbp), %edi
subl %edi, %edx
je 0x23c79
movl %r9d, %edi
subl %ecx, %edi
movslq %edi, %rdi
cltq
imulq %rdi, %rax
movslq %edx, %rdi
cqto
idivq %rdi
addl %ecx, %eax
movl %esi, %edx
jmp 0x23def
movl %r9d, %eax
negl %eax
cmpl %eax, %r15d
jl 0x23c79
testl %r9d, %r9d
je 0x23c79
movslq %r15d, %rax
movq 0x38(%rsp), %rsi
imulq %rsi, %rax
cqto
idivq %r11
movl %r15d, %edx
sarl $0x1f, %edx
addl %esi, %edx
addl %eax, %edx
cmpl 0x30(%rsp), %edx
movl 0x1c(%rsp), %esi
cmovgel %esi, %edx
leaq 0x244c98(%rip), %rax    # 0x268a80
movl %edx, (%rax,%r8,4)
movl %r9d, %eax
leaq 0x24cc9a(%rip), %rdi    # 0x270a90
movl %eax, (%rdi,%r8,4)
cmpl $0x100, %eax            # imm = 0x100
jl 0x23c79
cmpl %edx, %r12d
jg 0x23c79
leaq 0x42305b(%rip), %rax    # 0x446e70
movzwl 0x20(%rsp), %edx
movw %dx, (%rax,%r8,2)
leaq 0x20dc2a(%rip), %rax    # 0x231a50
movw %r10w, (%rax,%r8,2)
leaq 0x3dd1ce(%rip), %rax    # 0x401000
movl %ebp, (%rax,%r8,4)
leaq 0x3e11c3(%rip), %rax    # 0x405000
movl %ecx, (%rax,%r8,4)
leaq 0x4710c8(%rip), %rax    # 0x494f10
movl %r15d, (%rax,%r8,4)
leaq 0x4750bd(%rip), %rax    # 0x498f10
movl %r9d, (%rax,%r8,4)
movl 0xc(%rsp), %r12d
incl %r12d
movw %r12w, (%r13,%r8,2)
movw %r12w, 0x3ac92d(%rip)   # 0x3d079a
jmp 0x23c7e
movq %r9, 0x10(%rsp)
movq 0x68(%rsp), %rdi
leaq (%rdi,%rdi), %rcx
leaq (,%rdi,4), %rdx
xorl %r9d, %r9d
leaq 0x240bde(%rip), %rsi    # 0x264a70
leaq 0x244be7(%rip), %r14    # 0x268a80
movswq 0x24(%rsp), %rax
cmpq %r8, %rdi
jge 0x23f14
movq %rdi, %r12
leaq (%rcx,%r9), %r10
leaq 0x20db9e(%rip), %rbp    # 0x231a50
movswq (%rbp,%r10), %rdi
shlq $0x5, %rdi
leaq 0x18db8d(%rip), %r11    # 0x1b1a50
movzwl 0x8(%rdi,%r11), %edi
movzwl (%r13,%r10), %r11d
movswq %r11w, %rbx
cmpw (%rbp,%rbx,2), %di
jne 0x23ee7
leaq (%rdx,%r9,2), %rdi
movl (%r14,%rdi), %edi
cmpl (%rsi,%rbx,4), %edi
jl 0x23f08
leal 0x1(%rax), %edi
movl %edi, 0x24(%rsp)
movw %di, 0x3ac8a7(%rip)     # 0x3d079c
leaq 0x3ac8c4(%rip), %rdi    # 0x3d07c0
movw %r11w, (%rdi,%rax,2)
orw $-0x1, (%r13,%r10)
movq %r12, %rdi
incq %rdi
addq $0x2, %r9
jmp 0x23e99
leaq 0x251d95(%rip), %r8     # 0x275cb0
leaq 0x38b5fe(%rip), %r9     # 0x3af520
leaq 0x3ac897(%rip), %rdi    # 0x3d07c0
leaq 0x3ae890(%rip), %r10    # 0x3d27c0
movq 0x60(%rsp), %rsi
cmpq %rax, %rsi
jge 0x23f59
movzwl (%rdi,%rsi,2), %edx
movl %edx, %ecx
movswq %dx, %rdx
movzwl (%r13,%rdx,2), %edx
testw %dx, %dx
jns 0x23f3e
movw %cx, (%r10,%rsi,2)
incq %rsi
jmp 0x23f35
movq 0x28(%rsp), %rax
testw %ax, %ax
jg 0x238d4
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq