// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: neartag
int neartag(int xs, int ys, int zs, short sectnum, short ange, short *neartagsector, short *neartagwall,
	short *neartagsprite, int *neartaghitdist, int neartagrange, unsigned char tagsearch)
{
	walltype *wal, *wal2;
	spritetype *spr;
	int i, z, zz, xe, ye, ze, x1, y1, z1, x2, y2, intx, inty, intz;
	int topt, topu, bot, dist, offx, offy, vx, vy, vz;
	short tempshortcnt, tempshortnum, dasector, startwall, endwall;
	short nextsector, good;

	*neartagsector = -1; *neartagwall = -1; *neartagsprite = -1;
	*neartaghitdist = 0;

	if (sectnum < 0) return(0);
	if ((tagsearch < 1) || (tagsearch > 3)) return(0);

	vx = mulscale14(sintable[(ange+2560)&2047],neartagrange); xe = xs+vx;
	vy = mulscale14(sintable[(ange+2048)&2047],neartagrange); ye = ys+vy;
	vz = 0; ze = 0;

	clipsectorlist[0] = sectnum;
	tempshortcnt = 0; tempshortnum = 1;

	do
	{
		dasector = clipsectorlist[tempshortcnt];

		startwall = sector[dasector].wallptr;
		endwall = startwall + sector[dasector].wallnum - 1;
		for(z=startwall,wal=&wall[startwall];z<=endwall;z++,wal++)
		{
			wal2 = &wall[wal->point2];
			x1 = wal->x; y1 = wal->y; x2 = wal2->x; y2 = wal2->y;

			nextsector = wal->nextsector;

			good = 0;
			if (nextsector >= 0)
			{
				if ((tagsearch&1) && sector[nextsector].lotag) good |= 1;
				if ((tagsearch&2) && sector[nextsector].hitag) good |= 1;
			}
			if ((tagsearch&1) && wal->lotag) good |= 2;
			if ((tagsearch&2) && wal->hitag) good |= 2;

			if ((good == 0) && (nextsector < 0)) continue;
			if ((x1-xs)*(y2-ys) < (x2-xs)*(y1-ys)) continue;

			if (lintersect(xs,ys,zs,xe,ye,ze,x1,y1,x2,y2,&intx,&inty,&intz) == 1)
			{
				if (good != 0)
				{
					if (good&1) *neartagsector = nextsector;
					if (good&2) *neartagwall = z;
					*neartaghitdist = dmulscale14(intx-xs,sintable[(ange+2560)&2047],inty-ys,sintable[(ange+2048)&2047]);
					xe = intx; ye = inty; ze = intz;
				}
				if (nextsector >= 0)
				{
					for(zz=tempshortnum-1;zz>=0;zz--)
						if (clipsectorlist[zz] == nextsector) break;
					if (zz < 0) clipsectorlist[tempshortnum++] = nextsector;
				}
			}
		}

		for(z=headspritesect[dasector];z>=0;z=nextspritesect[z])
		{
			spr = &sprite[z];

			good = 0;
			if ((tagsearch&1) && spr->lotag) good |= 1;
			if ((tagsearch&2) && spr->hitag) good |= 1;
			if (good != 0)
			{
				x1 = spr->x; y1 = spr->y; z1 = spr->z;

				topt = vx*(x1-xs) + vy*(y1-ys);
				if (topt > 0)
				{
					bot = vx*vx + vy*vy;
					if (bot != 0)
					{
						intz = zs+scale(vz,topt,bot);
						i = tilesizy[spr->picnum]*spr->yrepeat;
						if (spr->cstat&128) z1 += (i<<1);
						if (picanm[spr->picnum]&0x00ff0000) z1 -= ((int)((signed char)((picanm[spr->picnum]>>16)&255))*spr->yrepeat<<2);
						if ((intz <= z1) && (intz >= z1-(i<<2)))
						{
							topu = vx*(y1-ys) - vy*(x1-xs);

							offx = scale(vx,topu,bot);
							offy = scale(vy,topu,bot);
							dist = offx*offx + offy*offy;
							i = (tilesizx[spr->picnum]*spr->xrepeat); i *= i;
							if (dist <= (i>>7))
							{
								intx = xs + scale(vx,topt,bot);
								inty = ys + scale(vy,topt,bot);
								if (klabs(intx-xs)+klabs(inty-ys) < klabs(xe-xs)+klabs(ye-ys))
								{
									*neartagsprite = z;
									*neartaghitdist = dmulscale14(intx-xs,sintable[(ange+2560)&2047],inty-ys,sintable[(ange+2048)&2047]);
									xe = intx;
									ye = inty;
									ze = intz;
								}
							}
						}
					}
				}
			}
		}

		tempshortcnt++;
	} while (tempshortcnt < tempshortnum);
	return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, -0x70(%rsp)
movl %edx, %esi
movb 0x58(%rsp), %r14b
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %r10
movq 0x38(%rsp), %rbx
movw $0xffff, %ax            # imm = 0xFFFF
movq %r9, -0x28(%rsp)
movw %ax, (%r9)
movw %ax, (%rbx)
movw %ax, (%r10)
movl $0x0, (%rdx)
testw %cx, %cx
sets %al
leal -0x4(%r14), %edx
cmpb $-0x3, %dl
setb %dl
orb %al, %dl
je 0x384ec
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl %r8w, %r8d
leal 0x200(%r8), %edx
movl $0x7ff, %eax            # imm = 0x7FF
andl %eax, %edx
leaq 0x39684b(%rip), %r9     # 0x3ced50
movq %rdx, -0x48(%rsp)
movswq (%r9,%rdx,2), %r10
movslq 0x50(%rsp), %rdx
imulq %rdx, %r10
shrq $0xe, %r10
leal (%r10,%rdi), %ebx
movq %rbx, -0x58(%rsp)
andl %eax, %r8d
movq %r8, -0x40(%rsp)
movswq (%r9,%r8,2), %r8
imulq %rdx, %r8
shrq $0xe, %r8
movq -0x70(%rsp), %rax
addl %r8d, %eax
movq %rax, -0x60(%rsp)
movw %cx, 0x3f8ff2(%rip)     # 0x431540
movl %r10d, %eax
imull %r10d, %eax
movl %r8d, %ecx
imull %ecx, %ecx
addl %eax, %ecx
movq %r10, -0x30(%rsp)
movslq %r10d, %rax
movq %rax, -0x18(%rsp)
movq %rcx, %r10
movq %r8, -0x38(%rsp)
movslq %r8d, %rax
movq %rax, -0x20(%rsp)
movw $0x1, %ax
movl %eax, -0x64(%rsp)
xorl %ecx, %ecx
movq %r10, -0x80(%rsp)
movl %esi, -0x74(%rsp)
movq %rdi, -0x50(%rsp)
leaq 0x3f8fa7(%rip), %rax    # 0x431540
movq %rcx, -0x8(%rsp)
movswq (%rax,%rcx,2), %rax
movq %rax, -0x10(%rsp)
leaq (%rax,%rax,4), %rax
leaq 0x20b4ad(%rip), %rcx    # 0x243a60
movswq (%rcx,%rax,8), %r12
movzwl 0x2(%rcx,%rax,8), %eax
addw %r12w, %ax
decl %eax
cmpw %ax, %r12w
jle 0x38854
leaq 0x373f3c(%rip), %rax    # 0x3ac510
movq -0x10(%rsp), %rcx
movzwl (%rax,%rcx,2), %r12d
testw %r12w, %r12w
js 0x38838
movl -0x74(%rsp), %esi
movq -0x70(%rsp), %r8
movq -0x50(%rsp), %rdi
movzwl %r12w, %r9d
imulq $0x2c, %r9, %r15
leaq 0x2446ab(%rip), %rax    # 0x27ccb0
addq %rax, %r15
testb $0x1, %r14b
jne 0x3861b
movb $0x1, %al
cmpb $0x2, %r14b
jae 0x38636
jmp 0x38748
movzwl 0x26(%r15), %ecx
testw %cx, %cx
sete %al
cmpb $0x2, %r14b
jae 0x38636
testw %cx, %cx
jne 0x38647
jmp 0x38748
cmpw $0x0, 0x28(%r15)
sete %cl
testb %al, %cl
jne 0x38748
movl (%r15), %eax
movl 0x4(%r15), %ecx
subl %edi, %eax
movl %eax, %edx
imull -0x30(%rsp), %edx
subl %r8d, %ecx
movl %ecx, %ebx
imull -0x38(%rsp), %ebx
addl %edx, %ebx
jle 0x38748
testl %r10d, %r10d
je 0x38748
movswq 0xe(%r15), %r14
leaq 0x3c9162(%rip), %rdx    # 0x4017e0
movswl (%rdx,%r14,2), %edx
movzbl 0x15(%r15), %r10d
imull %r10d, %edx
leal (%rdx,%rdx), %r8d
testb $-0x80, 0xc(%r15)
movl $0x0, %r13d
cmovel %r13d, %r8d
addl 0x8(%r15), %r8d
leaq 0x2023b6(%rip), %r13    # 0x23aa60
movl (%r13,%r14,4), %ebp
testl $0xff0000, %ebp         # imm = 0xFF0000
je 0x386cd
shrl $0x10, %ebp
movsbl %bpl, %esi
imull %esi, %r10d
movl -0x74(%rsp), %esi
shll $0x2, %r10d
subl %r10d, %r8d
cmpl %esi, %r8d
movq -0x80(%rsp), %r10
jl 0x3873e
shll $0x2, %edx
subl %edx, %r8d
cmpl %esi, %r8d
jg 0x3873e
imull -0x30(%rsp), %ecx
imull -0x38(%rsp), %eax
subl %eax, %ecx
movslq %ecx, %rcx
movq -0x18(%rsp), %r13
movq %r13, %rax
imulq %rcx, %rax
cqto
idivq %r10
movq %rax, %r8
movq -0x20(%rsp), %rbp
imulq %rbp, %rcx
movq %rcx, %rax
cqto
idivq %r10
imull %r8d, %r8d
imull %eax, %eax
addl %r8d, %eax
leaq 0x3d78f9(%rip), %rcx    # 0x410020
movswl (%rcx,%r14,2), %ecx
movzbl 0x14(%r15), %edx
imull %ecx, %edx
imull %edx, %edx
shrl $0x7, %edx
cmpl %edx, %eax
jbe 0x38763
movq -0x70(%rsp), %r8
movb 0x58(%rsp), %r14b
leaq 0x37ddd1(%rip), %rax    # 0x3b6520
movzwl (%rax,%r9,2), %r12d
testw %r12w, %r12w
jns 0x385f6
jmp 0x38838
movl %ebx, %r8d
movq %r13, %rax
imulq %r8, %rax
cqto
idivq %r10
movq %rax, %rcx
imulq %rbp, %r8
movq %r8, %rax
cqto
idivq %r10
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
addl %edx, %esi
movq -0x58(%rsp), %rdx
subl %edi, %edx
movl %edx, %r8d
negl %r8d
cmovsl %edx, %r8d
movq -0x60(%rsp), %rdx
movq -0x70(%rsp), %rdi
subl %edi, %edx
movl %edx, %r10d
negl %r10d
cmovsl %edx, %r10d
addl %r8d, %r10d
movq %rdi, %r8
movq -0x50(%rsp), %rdi
cmpl %r10d, %esi
movq -0x80(%rsp), %r10
movl -0x74(%rsp), %esi
movb 0x58(%rsp), %r14b
jae 0x38748
leal (%rax,%r8), %edx
movq %rdx, -0x60(%rsp)
leal (%rcx,%rdi), %edx
movq %rdx, -0x58(%rsp)
movq 0x40(%rsp), %rdx
movw %r12w, (%rdx)
movq -0x48(%rsp), %rdx
leaq 0x396550(%rip), %r10    # 0x3ced50
movswq (%r10,%rdx,2), %rdx
movq -0x40(%rsp), %rsi
movswq (%r10,%rsi,2), %rsi
movq -0x80(%rsp), %r10
movslq %ecx, %rcx
imulq %rdx, %rcx
cltq
imulq %rsi, %rax
movl -0x74(%rsp), %esi
addq %rcx, %rax
shrq $0xe, %rax
movq 0x48(%rsp), %rcx
movl %eax, (%rcx)
jmp 0x38748
movq -0x8(%rsp), %rcx
incq %rcx
movswq -0x64(%rsp), %rax
cmpq %rax, %rcx
jl 0x38592
jmp 0x384df
movswl %ax, %r11d
movq %r12, %r8
shlq $0x5, %r8
leaq 0x1801ea(%rip), %rax    # 0x1b8a50
addq %rax, %r8
movswq 0xc(%r8), %r9
testq %r9, %r9
js 0x3887e
testb $0x1, %r14b
jne 0x38883
xorl %r15d, %r15d
jmp 0x3889b
xorl %r15d, %r15d
jmp 0x388bb
leaq (%r9,%r9,4), %rax
xorl %r15d, %r15d
leaq 0x20b1cf(%rip), %rcx    # 0x243a60
cmpw $0x0, 0x22(%rcx,%rax,8)
setne %r15b
cmpb $0x2, %r14b
jb 0x388bb
leaq (%r9,%r9,4), %rax
leaq 0x20b1b4(%rip), %rcx    # 0x243a60
cmpw $0x0, 0x24(%rcx,%rax,8)
movl $0x1, %eax
cmovnel %eax, %r15d
testb $0x1, %r14b
je 0x388d3
movzwl %r15w, %r15d
leal 0x2(%r15), %eax
cmpw $0x0, 0x1a(%r8)
cmovnel %eax, %r15d
cmpb $0x2, %r14b
jb 0x388ed
movzwl %r15w, %r15d
movl %r15d, %eax
orl $0x2, %eax
cmpw $0x0, 0x1c(%r8)
cmovnel %eax, %r15d
movswq 0x8(%r8), %rax
shlq $0x5, %rax
movl (%r8), %edx
movl 0x4(%r8), %r13d
leaq 0x18014c(%rip), %rsi    # 0x1b8a50
movl (%rax,%rsi), %ecx
movl 0x4(%rax,%rsi), %ebp
testw %r9w, %r9w
jns 0x3891b
testw %r15w, %r15w
je 0x38ab8
movl %edx, %r10d
movq -0x50(%rsp), %rdi
subl %edi, %r10d
movl %ebp, %r14d
movq -0x70(%rsp), %rsi
subl %esi, %r14d
imull %r10d, %r14d
movl %ecx, %ebx
subl %edi, %ebx
movl %r13d, %eax
subl %esi, %eax
imull %eax, %ebx
cmpl %ebx, %r14d
jl 0x38aae
movq -0x58(%rsp), %rbx
movl %ebx, %r14d
subl %edi, %r14d
subl %ecx, %edx
movq -0x60(%rsp), %rcx
movl %ecx, %ebx
subl %esi, %ebx
subl %ebp, %r13d
movl %r13d, %ebp
imull %r14d, %ebp
movl %edx, %ecx
imull %ebx, %ecx
subl %ecx, %ebp
js 0x389b4
je 0x38aae
imull %r10d, %r13d
imull %eax, %edx
subl %edx, %r13d
cmpl %ebp, %r13d
jae 0x38aae
imull %r14d, %eax
imull %ebx, %r10d
subl %r10d, %eax
cmpl %ebp, %eax
jae 0x38aae
movl %r13d, %eax
shlq $0x18, %rax
movl %ebp, %ecx
xorl %edx, %edx
divq %rcx
movq -0x80(%rsp), %r10
jmp 0x389fa
imull %r10d, %r13d
imull %eax, %edx
subl %edx, %r13d
jg 0x38aae
cmpl %ebp, %r13d
jle 0x38aae
imull %r14d, %eax
imull %ebx, %r10d
subl %r10d, %eax
jg 0x38aae
cmpl %ebp, %eax
movq -0x80(%rsp), %r10
jle 0x38ab3
movslq %r13d, %rax
shlq $0x18, %rax
movslq %ebp, %rcx
cqto
idivq %rcx
movzwl %r15w, %edx
testl %edx, %edx
je 0x38a7f
testb $0x1, %dl
je 0x38a10
movq -0x28(%rsp), %rcx
movw %r9w, (%rcx)
testb $0x2, %dl
je 0x38a1e
movq 0x38(%rsp), %rcx
movw %r12w, (%rcx)
movslq %r14d, %rsi
cltq
imulq %rax, %rsi
shrq $0x18, %rsi
movslq %ebx, %rdi
imulq %rax, %rdi
shrq $0x18, %rdi
movq -0x48(%rsp), %rax
leaq 0x39630e(%rip), %rdx    # 0x3ced50
movswq (%rdx,%rax,2), %rax
movq -0x40(%rsp), %rcx
movswq (%rdx,%rcx,2), %rcx
movslq %esi, %rdx
addl -0x50(%rsp), %esi
movq %rsi, -0x58(%rsp)
imulq %rax, %rdx
movslq %edi, %rax
addl -0x70(%rsp), %edi
movq %rdi, -0x60(%rsp)
imulq %rcx, %rax
addq %rdx, %rax
shrq $0xe, %rax
movq 0x48(%rsp), %rcx
movl %eax, (%rcx)
testw %r9w, %r9w
js 0x38ab3
movswq -0x64(%rsp), %rax
movl %eax, %edx
movb 0x58(%rsp), %r14b
testl %edx, %edx
jle 0x38ad2
leaq -0x1(%rdx), %rcx
leaq 0x3f8a9f(%rip), %rsi    # 0x431540
cmpw %r9w, -0x2(%rsi,%rdx,2)
movq %rcx, %rdx
jne 0x38a92
jmp 0x38ab8
movq -0x80(%rsp), %r10
movb 0x58(%rsp), %r14b
leal 0x1(%r12), %eax
addq $0x20, %r8
cmpl %r11d, %r12d
movl %eax, %r12d
jne 0x38869
jmp 0x385cd
leal 0x1(%rax), %ecx
movl %ecx, -0x64(%rsp)
leaq 0x3f8a60(%rip), %rcx    # 0x431540
movw %r9w, (%rcx,%rax,2)
jmp 0x38ab8