// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, -0x78(%rsp)
movb 0x60(%rsp), %dil
movq 0x50(%rsp), %r10
movq 0x48(%rsp), %rbx
movq 0x40(%rsp), %r14
movw $0xffff, %ax            # imm = 0xFFFF
movq %r9, -0x20(%rsp)
movw %ax, (%r9)
movw %ax, (%r14)
movw %ax, (%rbx)
movl $0x0, (%r10)
testw %cx, %cx
sets %al
leal -0x4(%rdi), %r9d
cmpb $-0x3, %r9b
setb %r9b
orb %al, %r9b
je 0x37473
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl %r8w, %r9d
leal 0x200(%r9), %r8d
movl $0x7ff, %eax            # imm = 0x7FF
andl %eax, %r8d
leaq 0x3968c3(%rip), %r10    # 0x3cdd50
movq %r8, -0x40(%rsp)
movq %rsi, -0x80(%rsp)
movswq (%r10,%r8,2), %rbx
movslq 0x58(%rsp), %r8
imulq %r8, %rbx
shrq $0xe, %rbx
movq -0x78(%rsp), %rsi
leal (%rbx,%rsi), %ebp
movl %ebp, -0x68(%rsp)
andl %eax, %r9d
movq %r9, -0x38(%rsp)
movswq (%r10,%r9,2), %r9
imulq %r8, %r9
shrq $0xe, %r9
movq -0x80(%rsp), %rax
addl %r9d, %eax
movl %eax, -0x6c(%rsp)
movw %cx, 0x3f9063(%rip)     # 0x430540
movl %ebx, %eax
imull %ebx, %eax
movl %r9d, %r8d
imull %r8d, %r8d
addl %eax, %r8d
movq %rbx, -0x28(%rsp)
movslq %ebx, %rax
movq -0x80(%rsp), %rbx
movq %rax, -0x10(%rsp)
movq %r9, -0x30(%rsp)
movslq %r9d, %rax
movq %rax, -0x18(%rsp)
movw $0x1, %ax
movq %rax, -0x58(%rsp)
xorl %ecx, %ecx
movq %rax, -0x50(%rsp)
movl %edx, -0x44(%rsp)
movq %r8, -0x60(%rsp)
leaq 0x3f9015(%rip), %rax    # 0x430540
movq %rcx, (%rsp)
movswq (%rax,%rcx,2), %rax
movq %rax, -0x8(%rsp)
leaq (%rax,%rax,4), %rax
leaq 0x20b51c(%rip), %rcx    # 0x242a60
movswq (%rcx,%rax,8), %r9
movzwl 0x2(%rcx,%rax,8), %eax
addw %r9w, %ax
decl %eax
cmpw %ax, %r9w
jle 0x377ca
movq -0x78(%rsp), %rsi
leaq 0x373fa6(%rip), %rax    # 0x3ab510
movq -0x8(%rsp), %rcx
movzwl (%rax,%rcx,2), %r10d
testw %r10w, %r10w
js 0x377af
movq -0x60(%rsp), %r8
movzwl %r10w, %r9d
imulq $0x2c, %r9, %rbp
leaq 0x24471e(%rip), %rax    # 0x27bcb0
addq %rax, %rbp
xorl %eax, %eax
testb $0x1, %dil
je 0x375a5
cmpw $0x0, 0x26(%rbp)
setne %al
cmpb $0x2, %dil
jb 0x375b8
cmpw $0x0, 0x28(%rbp)
movl $0x1, %ecx
cmovnel %ecx, %eax
testw %ax, %ax
je 0x3765a
movl (%rbp), %eax
movl 0x4(%rbp), %ecx
subl %esi, %eax
movl %eax, %edx
imull -0x28(%rsp), %edx
subl %ebx, %ecx
movl %ecx, %r13d
imull -0x30(%rsp), %r13d
addl %edx, %r13d
jle 0x3765a
testq %r8, %r8
je 0x3765a
movswq 0xe(%rbp), %rbx
leaq 0x3c91ef(%rip), %rdx    # 0x4007e0
movswl (%rdx,%rbx,2), %edx
movzbl 0x15(%rbp), %r14d
imull %r14d, %edx
leal (%rdx,%rdx), %r8d
testb $-0x80, 0xc(%rbp)
movl $0x0, %r15d
cmovel %r15d, %r8d
addl 0x8(%rbp), %r8d
leaq 0x202445(%rip), %r15    # 0x239a60
movl (%r15,%rbx,4), %r15d
testl $0xff0000, %r15d        # imm = 0xFF0000
je 0x3763b
shrl $0x10, %r15d
movsbl %r15b, %r15d
imull %r15d, %r14d
shll $0x2, %r14d
subl %r14d, %r8d
movl -0x44(%rsp), %r14d
cmpl %r14d, %r8d
jl 0x37650
shll $0x2, %edx
subl %edx, %r8d
cmpl %r14d, %r8d
jle 0x37675
movq -0x60(%rsp), %r8
movq -0x80(%rsp), %rbx
leaq 0x37debf(%rip), %rax    # 0x3b5520
movzwl (%rax,%r9,2), %r10d
testw %r10w, %r10w
jns 0x37583
jmp 0x377af
movq %r12, %r11
imull -0x28(%rsp), %ecx
imull -0x30(%rsp), %eax
subl %eax, %ecx
movslq %ecx, %rcx
movq -0x10(%rsp), %r15
movq %r15, %rax
imulq %rcx, %rax
cqto
movq -0x60(%rsp), %r14
idivq %r14
movq %rax, %r8
movq -0x18(%rsp), %r12
imulq %r12, %rcx
movq %rcx, %rax
cqto
idivq %r14
imull %r8d, %r8d
imull %eax, %eax
addl %r8d, %eax
leaq 0x3d795e(%rip), %rcx    # 0x40f020
movswl (%rcx,%rbx,2), %ecx
movzbl 0x14(%rbp), %edx
imull %ecx, %edx
imull %edx, %edx
shrl $0x7, %edx
cmpl %edx, %eax
jbe 0x376e2
movq %r14, %r8
movq %r11, %r12
jmp 0x37655
movl %r13d, %r8d
movq %r15, %rax
imulq %r8, %rax
cqto
idivq %r14
movq %rax, %rcx
leal (%rax,%rsi), %r11d
imulq %r12, %r8
movq %r8, %rax
cqto
idivq %r14
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
movl %eax, %r8d
negl %r8d
cmovsl %eax, %r8d
movq %rsi, %rdi
movq -0x80(%rsp), %rsi
leal (%rax,%rsi), %r14d
addl %edx, %r8d
movl -0x68(%rsp), %edx
subl %edi, %edx
movl %edx, %ebx
negl %ebx
cmovsl %edx, %ebx
movl -0x6c(%rsp), %edx
subl %esi, %edx
movl %edx, %ebp
negl %ebp
cmovsl %edx, %ebp
addl %ebx, %ebp
cmpl %ebp, %r8d
movq %r14, -0x50(%rsp)
jae 0x37793
movq 0x48(%rsp), %rdx
movw %r10w, (%rdx)
movq -0x40(%rsp), %rdx
leaq 0x3965f1(%rip), %r10    # 0x3cdd50
movswq (%r10,%rdx,2), %rdx
movq -0x38(%rsp), %r8
movswq (%r10,%r8,2), %r8
movslq %ecx, %rcx
imulq %rdx, %rcx
cltq
imulq %r8, %rax
addq %rcx, %rax
shrq $0xe, %rax
movq 0x50(%rsp), %rcx
movl %eax, (%rcx)
movl %r14d, -0x6c(%rsp)
movl %r11d, -0x68(%rsp)
movq -0x60(%rsp), %r8
movq %r11, %r12
movq -0x80(%rsp), %rbx
movq -0x78(%rsp), %rsi
movb 0x60(%rsp), %dil
jmp 0x3765a
movq (%rsp), %rcx
incq %rcx
movswq -0x58(%rsp), %rax
cmpq %rax, %rcx
jl 0x37524
jmp 0x37462
movswl %ax, %r13d
movq %r9, %r8
shlq $0x5, %r8
leaq 0x180274(%rip), %rax    # 0x1b7a50
addq %rax, %r8
movq -0x78(%rsp), %rsi
movl %r13d, -0x64(%rsp)
movswq 0xc(%r8), %r10
testq %r10, %r10
js 0x377fd
testb $0x1, %dil
jne 0x37801
xorl %ebp, %ebp
jmp 0x37818
xorl %ebp, %ebp
jmp 0x37837
leaq (%r10,%r10,4), %rax
xorl %ebp, %ebp
leaq 0x20b252(%rip), %rcx    # 0x242a60
cmpw $0x0, 0x22(%rcx,%rax,8)
setne %bpl
cmpb $0x2, %dil
jb 0x37837
leaq (%r10,%r10,4), %rax
leaq 0x20b237(%rip), %rcx    # 0x242a60
cmpw $0x0, 0x24(%rcx,%rax,8)
movl $0x1, %eax
cmovnel %eax, %ebp
testb $0x1, %dil
je 0x3784c
movzwl %bp, %ebp
leal 0x2(%rbp), %eax
cmpw $0x0, 0x1a(%r8)
cmovnel %eax, %ebp
movq %r12, %r11
cmpb $0x2, %dil
jb 0x37866
movzwl %bp, %ebp
movl %ebp, %eax
orl $0x2, %eax
cmpw $0x0, 0x1c(%r8)
cmovnel %eax, %ebp
movswq 0x8(%r8), %rax
shlq $0x5, %rax
movl (%r8), %r14d
movl 0x4(%r8), %r15d
leaq 0x1801d3(%rip), %rcx    # 0x1b7a50
movl (%rax,%rcx), %r12d
movl 0x4(%rax,%rcx), %edx
testw %r10w, %r10w
jns 0x37898
testw %bp, %bp
jne 0x37898
movq %r11, %r12
jmp 0x379a5
movl %r14d, %ecx
subl %esi, %ecx
movq %rsi, %rdi
movq %rbx, %rsi
movl %edx, %ebx
subl %esi, %ebx
imull %ecx, %ebx
movl %r12d, %r13d
subl %edi, %r13d
movl %r15d, %eax
subl %esi, %eax
imull %eax, %r13d
cmpl %r13d, %ebx
jge 0x378d1
movq %r11, %r12
movq %rsi, %rbx
movq %rdi, %rsi
movb 0x60(%rsp), %dil
jmp 0x379a0
movl -0x68(%rsp), %ebx
subl %edi, %ebx
subl %r12d, %r14d
movl -0x6c(%rsp), %r13d
subl %esi, %r13d
subl %edx, %r15d
movl %r15d, %r12d
imull %ebx, %r12d
movl %r14d, %edx
imull %r13d, %edx
subl %edx, %r12d
js 0x3792a
movb $0x1, %dl
movb 0x60(%rsp), %dil
je 0x3798f
imull %ecx, %r15d
imull %eax, %r14d
subl %r14d, %r15d
cmpl %r12d, %r15d
movq -0x78(%rsp), %rsi
jae 0x37994
imull %ebx, %eax
imull %r13d, %ecx
subl %ecx, %eax
cmpl %r12d, %eax
jb 0x37958
jmp 0x37994
imull %ecx, %r15d
imull %eax, %r14d
movb $0x1, %dl
subl %r14d, %r15d
movb 0x60(%rsp), %dil
jg 0x3798f
cmpl %r12d, %r15d
movq -0x78(%rsp), %rsi
jle 0x37994
imull %ebx, %eax
imull %r13d, %ecx
subl %ecx, %eax
jg 0x37994
cmpl %r12d, %eax
jle 0x37994
movslq %r15d, %rax
shlq $0x18, %rax
movslq %r12d, %rcx
cqto
idivq %rcx
movslq %ebx, %r11
cltq
imulq %rax, %r11
shrq $0x18, %r11
addl %esi, %r11d
movslq %r13d, %rcx
imulq %rax, %rcx
shrq $0x18, %rcx
addl -0x80(%rsp), %ecx
movq %rcx, -0x50(%rsp)
xorl %edx, %edx
jmp 0x37994
movq -0x78(%rsp), %rsi
testb %dl, %dl
je 0x379be
movq %r11, %r12
movq -0x80(%rsp), %rbx
movl -0x64(%rsp), %r13d
leal 0x1(%r9), %eax
addq $0x20, %r8
cmpl %r13d, %r9d
movl %eax, %r9d
jne 0x377e9
jmp 0x37563
movzwl %bp, %eax
testl %eax, %eax
movq %r11, %r12
movl -0x64(%rsp), %r13d
je 0x37a48
testb $0x1, %al
je 0x379da
movq -0x20(%rsp), %rcx
movw %r10w, (%rcx)
testb $0x2, %al
movq -0x80(%rsp), %rsi
je 0x379ec
movq 0x40(%rsp), %rax
movw %r9w, (%rax)
movl %r12d, %eax
movq -0x78(%rsp), %rdi
subl %edi, %eax
movq -0x40(%rsp), %rcx
leaq 0x39634e(%rip), %r14    # 0x3cdd50
movswq (%r14,%rcx,2), %rcx
movq -0x50(%rsp), %r11
movl %r11d, %edx
subl %esi, %edx
movq %rdi, %rsi
movq -0x38(%rsp), %rbx
movswq (%r14,%rbx,2), %rbx
cltq
imulq %rcx, %rax
movslq %edx, %rcx
imulq %rbx, %rcx
addq %rax, %rcx
shrq $0xe, %rcx
movq 0x50(%rsp), %rax
movl %ecx, (%rax)
movl %r11d, -0x6c(%rsp)
movl %r12d, -0x68(%rsp)
movb 0x60(%rsp), %dil
testw %r10w, %r10w
js 0x37ab0
movswl -0x58(%rsp), %ecx
movl %ecx, %eax
sarl $0x1f, %eax
andl %ecx, %eax
leaq 0x3f8adf(%rip), %rdx    # 0x430540
leaq (%rdx,%rcx,2), %rdx
addq $-0x2, %rdx
testl %ecx, %ecx
jle 0x37a80
decl %ecx
leaq -0x2(%rdx), %rbx
cmpw %r10w, (%rdx)
movq %rbx, %rdx
jne 0x37a69
incl %ecx
movl %ecx, %eax
movq -0x80(%rsp), %rbx
testl %eax, %eax
jg 0x379a5
movq -0x58(%rsp), %rcx
movswq %cx, %rax
incl %ecx
leaq 0x3f8aa1(%rip), %rdx    # 0x430540
movw %r10w, (%rdx,%rax,2)
movl %ecx, %eax
movq %rax, -0x58(%rsp)
jmp 0x379a5
movq -0x80(%rsp), %rbx
jmp 0x379a5