// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movl %edx, -0x60(%rsp)
movq %rsi, -0x68(%rsp)
movq %r9, -0x20(%rsp)
orw $-0x1, (%r9)
movq 0x40(%rsp), %rax
orw $-0x1, (%rax)
movq 0x48(%rsp), %rax
orw $-0x1, (%rax)
movq 0x50(%rsp), %rax
andl $0x0, (%rax)
movb 0x60(%rsp), %r15b
testw %cx, %cx
sets %al
leal -0x4(%r15), %edx
cmpb $-0x3, %dl
setb %dl
orb %al, %dl
jne 0x352eb
movzwl %r8w, %esi
leal 0x200(%rsi), %edx
movl $0x7ff, %eax            # imm = 0x7FF
andl %eax, %edx
leaq 0x39302e(%rip), %r8     # 0x3c7d50
movq %rdx, -0x48(%rsp)
movswq (%r8,%rdx,2), %r9
movslq 0x58(%rsp), %rdx
imulq %rdx, %r9
shrq $0xe, %r9
leal (%r9,%rdi), %r10d
movq %r10, -0x50(%rsp)
andl %eax, %esi
movq %rsi, -0x40(%rsp)
movswq (%r8,%rsi,2), %rsi
imulq %rdx, %rsi
shrq $0xe, %rsi
movq -0x68(%rsp), %rax
addl %esi, %eax
movq %rax, -0x58(%rsp)
movw %cx, 0x3f57d7(%rip)     # 0x42a540
movl %r9d, %eax
imull %r9d, %eax
movl %esi, %r14d
imull %r14d, %r14d
addl %eax, %r14d
movq %r9, -0x28(%rsp)
movslq %r9d, %rax
movq %rax, -0x18(%rsp)
movq %rsi, -0x30(%rsp)
movslq %esi, %rax
movq %rax, -0x38(%rsp)
movw $0x1, %ax
movl %eax, -0x5c(%rsp)
xorl %ecx, %ecx
movq %rdi, -0x78(%rsp)
leaq 0x3f5796(%rip), %rax    # 0x42a540
movq %rcx, -0x8(%rsp)
movswq (%rax,%rcx,2), %rcx
movq %rcx, -0x10(%rsp)
imulq $0x28, %rcx, %rax
leaq 0x207c9c(%rip), %rcx    # 0x23ca60
movswq (%rax,%rcx), %r10
movzwl 0x2(%rax,%rcx), %eax
addw %r10w, %ax
decl %eax
movq %r10, %r8
shlq $0x5, %r8
leaq 0x17cc6e(%rip), %rcx    # 0x1b1a50
addq %rcx, %r8
movswl %ax, %r11d
movl %r11d, -0x7c(%rsp)
movswq -0x5c(%rsp), %r9
cmpl %r11d, %r10d
jg 0x3508f
movswq 0xc(%r8), %r12
testq %r12, %r12
js 0x34e11
testb $0x1, %r15b
jne 0x34e15
xorl %eax, %eax
jmp 0x34e2b
xorl %eax, %eax
jmp 0x34e4a
imulq $0x28, %r12, %rdx
xorl %eax, %eax
leaq 0x207c3e(%rip), %rcx    # 0x23ca60
cmpw $0x0, 0x22(%rdx,%rcx)
setne %al
cmpb $0x2, %r15b
jb 0x34e4a
imulq $0x28, %r12, %rdx
leaq 0x207c24(%rip), %rcx    # 0x23ca60
cmpw $0x0, 0x24(%rdx,%rcx)
movl $0x1, %ecx
cmovnel %ecx, %eax
testb $0x1, %r15b
je 0x34e5f
movzwl %ax, %eax
leal 0x2(%rax), %edx
cmpw $0x0, 0x1a(%r8)
cmovnel %edx, %eax
cmpb $0x2, %r15b
jb 0x34e76
movzwl %ax, %eax
movl %eax, %edx
orl $0x2, %edx
cmpw $0x0, 0x1c(%r8)
cmovnel %edx, %eax
movswq 0x8(%r8), %rdx
shlq $0x5, %rdx
movl (%r8), %ebx
movl 0x4(%r8), %ebp
leaq 0x17cbc3(%rip), %rcx    # 0x1b1a50
movl (%rdx,%rcx), %esi
movl 0x4(%rdx,%rcx), %r13d
testw %r12w, %r12w
jns 0x34ea4
testw %ax, %ax
je 0x35070
movq %r10, -0x70(%rsp)
movq %r9, (%rsp)
movq %r14, %rcx
movq %r15, %r14
movl %ebx, %r11d
subl %edi, %r11d
movq %rdi, %r15
movl %r13d, %r9d
movq -0x68(%rsp), %rdi
subl %edi, %r9d
imull %r11d, %r9d
movl %esi, %r10d
subl %r15d, %r10d
movl %ebp, %edx
subl %edi, %edx
imull %edx, %r10d
cmpl %r10d, %r9d
jge 0x34ee9
movq %r15, %rdi
movq %r14, %r15
jmp 0x35063
movq -0x50(%rsp), %r9
subl %r15d, %r9d
subl %esi, %ebx
movq -0x58(%rsp), %rsi
movl %esi, %r10d
subl %edi, %r10d
subl %r13d, %ebp
movl %ebp, %r13d
imull %r9d, %r13d
movl %ebx, %esi
imull %r10d, %esi
subl %esi, %r13d
js 0x34f4d
movq %r14, %r15
je 0x3505e
imull %r11d, %ebp
imull %edx, %ebx
subl %ebx, %ebp
cmpl %r13d, %ebp
movq %rcx, %r14
jae 0x35057
imull %r9d, %edx
imull %r10d, %r11d
subl %r11d, %edx
cmpl %r13d, %edx
leaq 0x3f55fa(%rip), %rdi    # 0x42a540
jb 0x34f8c
jmp 0x35057
imull %r11d, %ebp
imull %edx, %ebx
subl %ebx, %ebp
movq %r14, %r15
leaq 0x3f55e0(%rip), %rdi    # 0x42a540
jg 0x3505e
cmpl %r13d, %ebp
movq %rcx, %r14
jle 0x35057
imull %r9d, %edx
imull %r10d, %r11d
subl %r11d, %edx
jg 0x35057
cmpl %r13d, %edx
jle 0x35057
movzwl %ax, %r11d
movslq %ebp, %rax
shlq $0x18, %rax
movslq %r13d, %rsi
cqto
idivq %rsi
testl %r11d, %r11d
je 0x3502c
testb $0x1, %r11b
je 0x34fb7
movq -0x20(%rsp), %rdx
movw %r12w, (%rdx)
testb $0x2, %r11b
je 0x34fca
movq 0x40(%rsp), %rdx
movq -0x70(%rsp), %rcx
movw %cx, (%rdx)
movslq %r9d, %rcx
cltq
imulq %rax, %rcx
shrq $0x18, %rcx
movslq %r10d, %r9
imulq %rax, %r9
shrq $0x18, %r9
movq -0x48(%rsp), %rax
leaq 0x392d62(%rip), %rsi    # 0x3c7d50
movswq (%rsi,%rax,2), %rax
movq -0x40(%rsp), %rdx
movswq (%rsi,%rdx,2), %rdx
movslq %ecx, %rsi
addl -0x78(%rsp), %ecx
movq %rcx, -0x50(%rsp)
imulq %rax, %rsi
movslq %r9d, %rax
addl -0x68(%rsp), %r9d
movq %r9, -0x58(%rsp)
imulq %rdx, %rax
addq %rsi, %rax
shrq $0xe, %rax
movq 0x50(%rsp), %rdx
movl %eax, (%rdx)
testw %r12w, %r12w
movq -0x70(%rsp), %r10
movl -0x7c(%rsp), %r11d
js 0x35088
movq (%rsp), %rcx
movl %ecx, %eax
testl %eax, %eax
jle 0x3507c
leaq -0x1(%rax), %rdx
cmpw %r12w, -0x2(%rdi,%rax,2)
movq %rdx, %rax
jne 0x35042
jmp 0x35088
movq -0x78(%rsp), %rdi
jmp 0x35066
movq -0x78(%rsp), %rdi
movq %rcx, %r14
movq -0x70(%rsp), %r10
movl -0x7c(%rsp), %r11d
incl %r10d
addq $0x20, %r8
jmp 0x34dee
leal 0x1(%rcx), %eax
movl %eax, -0x5c(%rsp)
movw %r12w, (%rdi,%rcx,2)
movq -0x78(%rsp), %rdi
jmp 0x35070
movq %r9, %r13
leaq 0x370477(%rip), %rax    # 0x3a5510
movq -0x10(%rsp), %rcx
leaq (%rax,%rcx,2), %rax
movl -0x60(%rsp), %ebp
movq -0x68(%rsp), %r8
movswq (%rax), %r11
testq %r11, %r11
js 0x352da
imulq $0x2c, %r11, %r12
leaq 0x240bed(%rip), %rax    # 0x275cb0
addq %rax, %r12
testb $0x1, %r15b
jne 0x350d9
movb $0x1, %al
cmpb $0x2, %r15b
jae 0x350f5
jmp 0x3520a
movzwl 0x26(%r12), %ecx
testw %cx, %cx
sete %al
cmpb $0x2, %r15b
jae 0x350f5
testw %cx, %cx
jne 0x35107
jmp 0x3520a
cmpw $0x0, 0x28(%r12)
sete %cl
testb %al, %cl
jne 0x3520a
movl (%r12), %eax
movl 0x4(%r12), %ecx
subl %edi, %eax
movl %eax, %edx
imull -0x28(%rsp), %edx
subl %r8d, %ecx
movl %ecx, %r10d
imull -0x30(%rsp), %r10d
addl %edx, %r10d
jle 0x3520a
testl %r14d, %r14d
je 0x3520a
movswq 0xe(%r12), %r9
leaq 0x3c569c(%rip), %rdx    # 0x3fa7e0
movswl (%rdx,%r9,2), %edx
movzbl 0x15(%r12), %r8d
imull %r8d, %edx
leal (%rdx,%rdx), %esi
testb $-0x80, 0xc(%r12)
movl $0x0, %edi
cmovel %edi, %esi
addl 0x8(%r12), %esi
leaq 0x1fe8f0(%rip), %rdi    # 0x233a60
movl (%rdi,%r9,4), %ebx
testl $0xff0000, %ebx         # imm = 0xFF0000
je 0x35191
shrl $0x10, %ebx
movsbl %bl, %edi
imull %edi, %r8d
movl -0x60(%rsp), %ebp
shll $0x2, %r8d
subl %r8d, %esi
movq -0x38(%rsp), %rdi
cmpl %ebp, %esi
movq -0x68(%rsp), %r8
jl 0x35205
shll $0x2, %edx
subl %edx, %esi
cmpl %ebp, %esi
jg 0x35205
imull -0x28(%rsp), %ecx
imull -0x30(%rsp), %eax
subl %eax, %ecx
movslq %ecx, %rcx
movq -0x18(%rsp), %rsi
movq %rsi, %rax
imulq %rcx, %rax
cqto
idivq %r14
movq %rax, %r8
imulq %rdi, %rcx
movq %rcx, %rax
cqto
idivq %r14
imull %r8d, %r8d
imull %eax, %eax
addl %r8d, %eax
leaq 0x3d3e38(%rip), %rcx    # 0x409020
movswl (%rcx,%r9,2), %ecx
movzbl 0x14(%r12), %edx
imull %ecx, %edx
imull %edx, %edx
shrl $0x7, %edx
cmpl %edx, %eax
jbe 0x3521a
movq -0x68(%rsp), %r8
movq -0x78(%rsp), %rdi
leaq 0x37a30f(%rip), %rax    # 0x3af520
leaq (%rax,%r11,2), %rax
jmp 0x350ab
movl %r10d, %r8d
movq %rsi, %rax
imulq %r8, %rax
cqto
idivq %r14
movq %rax, %rcx
imulq %rdi, %r8
movq %r8, %rax
cqto
idivq %r14
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
addl %edx, %esi
movq -0x50(%rsp), %rdx
movq -0x78(%rsp), %r10
subl %r10d, %edx
movl %edx, %edi
negl %edi
cmovsl %edx, %edi
movq -0x58(%rsp), %rdx
movq -0x68(%rsp), %r9
subl %r9d, %edx
movl %edx, %r8d
negl %r8d
cmovsl %edx, %r8d
addl %edi, %r8d
movl -0x60(%rsp), %ebp
cmpl %r8d, %esi
movq %r9, %r8
movq %r10, %rdi
jae 0x3520a
leal (%rax,%r8), %edx
movq %rdx, -0x58(%rsp)
leal (%rcx,%rdi), %edx
movq %rdx, -0x50(%rsp)
movq 0x48(%rsp), %rdx
movw %r11w, (%rdx)
movq -0x48(%rsp), %rdx
leaq 0x392aa5(%rip), %r9     # 0x3c7d50
movswq (%r9,%rdx,2), %rdx
movq -0x40(%rsp), %rsi
movswq (%r9,%rsi,2), %rsi
movslq %ecx, %rcx
imulq %rdx, %rcx
cltq
imulq %rsi, %rax
addq %rcx, %rax
shrq $0xe, %rax
movq 0x50(%rsp), %rcx
movl %eax, (%rcx)
jmp 0x3520a
movq -0x8(%rsp), %rcx
incq %rcx
cmpq %r13, %rcx
jl 0x34da3
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq