// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: wallmost
int wallmost(short *mostbuf, int w, int sectnum, unsigned char dastat)
{
	int bad, i, j, t, y, z, inty, intz, xcross, yinc, fw;
	int x1, y1, z1, x2, y2, z2, xv, yv, dx, dy, dasqr, oz1, oz2;
	int s1, s2, s3, s4, ix1, ix2, iy1, iy2;

	if (dastat == 0)
	{
		z = sector[sectnum].ceilingz-globalposz;
		if ((sector[sectnum].ceilingstat&2) == 0) return(owallmost(mostbuf,w,z));
	}
	else
	{
		z = sector[sectnum].floorz-globalposz;
		if ((sector[sectnum].floorstat&2) == 0) return(owallmost(mostbuf,w,z));
	}

	i = thewall[w];
	if (i == sector[sectnum].wallptr) return(owallmost(mostbuf,w,z));

	x1 = wall[i].x; x2 = wall[wall[i].point2].x-x1;
	y1 = wall[i].y; y2 = wall[wall[i].point2].y-y1;

	fw = sector[sectnum].wallptr; i = wall[fw].point2;
	dx = wall[i].x-wall[fw].x; dy = wall[i].y-wall[fw].y;
	dasqr = krecipasm(nsqrtasm(dx*dx+dy*dy));

	if (xb1[w] == 0)
		{ xv = cosglobalang+sinviewingrangeglobalang; yv = singlobalang-cosviewingrangeglobalang; }
	else
		{ xv = x1-globalposx; yv = y1-globalposy; }
	i = xv*(y1-globalposy)-yv*(x1-globalposx); j = yv*x2-xv*y2;
	if (klabs(j) > klabs(i>>3)) i = divscale28(i,j);
	if (dastat == 0)
	{
		t = mulscale15(sector[sectnum].ceilingheinum,dasqr);
		z1 = sector[sectnum].ceilingz;
	}
	else
	{
		t = mulscale15(sector[sectnum].floorheinum,dasqr);
		z1 = sector[sectnum].floorz;
	}
	z1 = dmulscale24(dx*t,mulscale20(y2,i)+((y1-wall[fw].y)<<8),
						 -dy*t,mulscale20(x2,i)+((x1-wall[fw].x)<<8))+((z1-globalposz)<<7);


	if (xb2[w] == xdimen-1)
		{ xv = cosglobalang-sinviewingrangeglobalang; yv = singlobalang+cosviewingrangeglobalang; }
	else
		{ xv = (x2+x1)-globalposx; yv = (y2+y1)-globalposy; }
	i = xv*(y1-globalposy)-yv*(x1-globalposx); j = yv*x2-xv*y2;
	if (klabs(j) > klabs(i>>3)) i = divscale28(i,j);
	if (dastat == 0)
	{
		t = mulscale15(sector[sectnum].ceilingheinum,dasqr);
		z2 = sector[sectnum].ceilingz;
	}
	else
	{
		t = mulscale15(sector[sectnum].floorheinum,dasqr);
		z2 = sector[sectnum].floorz;
	}
	z2 = dmulscale24(dx*t,mulscale20(y2,i)+((y1-wall[fw].y)<<8),
						 -dy*t,mulscale20(x2,i)+((x1-wall[fw].x)<<8))+((z2-globalposz)<<7);


	s1 = mulscale20(globaluclip,yb1[w]); s2 = mulscale20(globaluclip,yb2[w]);
	s3 = mulscale20(globaldclip,yb1[w]); s4 = mulscale20(globaldclip,yb2[w]);
	bad = (z1<s1)+((z2<s2)<<1)+((z1>s3)<<2)+((z2>s4)<<3);

	ix1 = xb1[w]; ix2 = xb2[w];
	iy1 = yb1[w]; iy2 = yb2[w];
	oz1 = z1; oz2 = z2;

	if ((bad&3) == 3)
	{
		//clearbufbyte(&mostbuf[ix1],(ix2-ix1+1)*sizeof(mostbuf[0]),0L);
		for (i=ix1; i<=ix2; i++) mostbuf[i] = 0;
		return(bad);
	}

	if ((bad&12) == 12)
	{
		//clearbufbyte(&mostbuf[ix1],(ix2-ix1+1)*sizeof(mostbuf[0]),ydimen+(ydimen<<16));
		for (i=ix1; i<=ix2; i++) mostbuf[i] = ydimen;
		return(bad);
	}

	if (bad&3)
	{
			//inty = intz / (globaluclip>>16)
		t = divscale30(oz1-s1,s2-s1+oz1-oz2);
		inty = yb1[w] + mulscale30(yb2[w]-yb1[w],t);
		intz = oz1 + mulscale30(oz2-oz1,t);
		xcross = xb1[w] + scale(mulscale30(yb2[w],t),xb2[w]-xb1[w],inty);

		//t = divscale30((x1<<4)-xcross*yb1[w],xcross*(yb2[w]-yb1[w])-((x2-x1)<<4));
		//inty = yb1[w] + mulscale30(yb2[w]-yb1[w],t);
		//intz = z1 + mulscale30(z2-z1,t);

		if ((bad&3) == 2)
		{
			if (xb1[w] <= xcross) { z2 = intz; iy2 = inty; ix2 = xcross; }
			//clearbufbyte(&mostbuf[xcross+1],(xb2[w]-xcross)*sizeof(mostbuf[0]),0L);
			for (i=xcross+1; i<=xb2[w]; i++) mostbuf[i] = 0;
		}
		else
		{
			if (xcross <= xb2[w]) { z1 = intz; iy1 = inty; ix1 = xcross; }
			//clearbufbyte(&mostbuf[xb1[w]],(xcross-xb1[w]+1)*sizeof(mostbuf[0]),0L);
			for (i=xb1[w]; i<=xcross; i++) mostbuf[i] = 0;
		}
	}

	if (bad&12)
	{
			//inty = intz / (globaldclip>>16)
		t = divscale30(oz1-s3,s4-s3+oz1-oz2);
		inty = yb1[w] + mulscale30(yb2[w]-yb1[w],t);
		intz = oz1 + mulscale30(oz2-oz1,t);
		xcross = xb1[w] + scale(mulscale30(yb2[w],t),xb2[w]-xb1[w],inty);

		//t = divscale30((x1<<4)-xcross*yb1[w],xcross*(yb2[w]-yb1[w])-((x2-x1)<<4));
		//inty = yb1[w] + mulscale30(yb2[w]-yb1[w],t);
		//intz = z1 + mulscale30(z2-z1,t);

		if ((bad&12) == 8)
		{
			if (xb1[w] <= xcross) { z2 = intz; iy2 = inty; ix2 = xcross; }
			//clearbufbyte(&mostbuf[xcross+1],(xb2[w]-xcross)*sizeof(mostbuf[0]),ydimen+(ydimen<<16));
			for (i=xcross+1; i<=xb2[w]; i++) mostbuf[i] = ydimen;
		}
		else
		{
			if (xcross <= xb2[w]) { z1 = intz; iy1 = inty; ix1 = xcross; }
			//clearbufbyte(&mostbuf[xb1[w]],(xcross-xb1[w]+1)*sizeof(mostbuf[0]),ydimen+(ydimen<<16));
			for (i=xb1[w]; i<=xcross; i++) mostbuf[i] = ydimen;
		}
	}

	y = (scale(z1,xdimenscale,iy1)<<4);
	yinc = ((scale(z2,xdimenscale,iy2)<<4)-y) / (ix2-ix1+1);
	qinterpolatedown16short(&mostbuf[ix1],ix2-ix1+1,y+(globalhoriz<<16),yinc);

	if (mostbuf[ix1] < 0) mostbuf[ix1] = 0;
	if (mostbuf[ix1] > ydimen) mostbuf[ix1] = ydimen;
	if (mostbuf[ix2] < 0) mostbuf[ix2] = 0;
	if (mostbuf[ix2] > ydimen) mostbuf[ix2] = ydimen;

	return(bad);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movslq %edx, %rax
imulq $0x28, %rax, %rax
leaq 0x21b3a4(%rip), %r10    # 0x23ca60
leaq (%r10,%rax), %r12
movl 0x243399(%rip), %r9d    # 0x264a60
testl %ecx, %ecx
je 0x216dd
movl 0x8(%r12), %edx
subl %r9d, %edx
testb $0x2, 0xe(%r12)
jne 0x216ed
jmp 0x21706
movl 0x4(%r12), %edx
subl %r9d, %edx
testb $0x2, 0xc(%r12)
je 0x21706
movslq %esi, %r15
leaq 0x210359(%rip), %rdi    # 0x231a50
movswq (%rdi,%r15,2), %rdi
movswq (%rax,%r10), %rax
cmpw %ax, %di
jne 0x2171c
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21db5
movl %ecx, 0x14(%rsp)
movl %r9d, 0x28(%rsp)
shlq $0x5, %rdi
leaq 0x190320(%rip), %rcx    # 0x1b1a50
movl (%rdi,%rcx), %r13d
movl 0x4(%rdi,%rcx), %r14d
movswq 0x8(%rdi,%rcx), %rdx
shlq $0x5, %rdx
movl (%rdx,%rcx), %ebp
movl 0x4(%rdx,%rcx), %edx
movl %ebp, 0x50(%rsp)
subl %r13d, %ebp
movl %edx, 0x4c(%rsp)
subl %r14d, %edx
movl %edx, 0xc(%rsp)
shlq $0x5, %rax
movswq 0x8(%rax,%rcx), %rdx
shlq $0x5, %rdx
movl (%rax,%rcx), %esi
movl 0x4(%rax,%rcx), %r9d
movl (%rdx,%rcx), %eax
movl 0x4(%rdx,%rcx), %edi
movl %esi, 0x24(%rsp)
subl %esi, %eax
movl %r9d, 0x54(%rsp)
subl %r9d, %edi
movl %eax, 0x18(%rsp)
imull %eax, %eax
movl %edi, 0x10(%rsp)
imull %edi, %edi
addl %eax, %edi
callq 0x22169
movl %eax, %edi
callq 0x2213b
movl %eax, %esi
leaq 0x2432c4(%rip), %rax    # 0x264a70
movq %r15, 0x40(%rsp)
movslq (%rax,%r15,4), %rdi
testq %rdi, %rdi
je 0x217db
movl 0x212290(%rip), %eax    # 0x233a50
movl %r13d, %r9d
subl %eax, %r9d
movl 0x212288(%rip), %ecx    # 0x233a54
movl %r14d, %r8d
movl %ecx, 0x3c(%rsp)
subl %ecx, %r8d
movl %r8d, %edx
jmp 0x2180b
movl 0x247292(%rip), %r9d    # 0x268a74
addl 0x247287(%rip), %r9d    # 0x268a70
movl 0x247289(%rip), %edx    # 0x268a78
subl 0x247287(%rip), %edx    # 0x268a7c
movl 0x212259(%rip), %ecx    # 0x233a54
movl 0x21224f(%rip), %eax    # 0x233a50
movl %r14d, %r8d
movl %ecx, 0x3c(%rsp)
subl %ecx, %r8d
movl %r8d, %r11d
imull %r9d, %r11d
movl %eax, 0x48(%rsp)
movl %eax, %ecx
subl %r13d, %ecx
movl %ecx, %eax
imull %edx, %eax
addl %r11d, %eax
imull %ebp, %edx
imull 0xc(%rsp), %r9d
subl %r9d, %edx
movl %edx, %r9d
negl %r9d
cmovsl %edx, %r9d
movl %eax, %r10d
sarl $0x3, %r10d
movl %r10d, %r11d
negl %r11d
cmovsl %r10d, %r11d
cmpl %r11d, %r9d
jbe 0x2185d
cltq
shlq $0x1c, %rax
movslq %edx, %r9
cqto
idivq %r9
movl 0x14(%rsp), %edx
xorl %r9d, %r9d
testb %dl, %dl
setne %r9b
movslq %esi, %rsi
movswq 0x12(%r12,%r9,8), %rdx
movq %rsi, 0x30(%rsp)
imulq %rsi, %rdx
shrq $0xf, %rdx
movl 0x4(%r12,%r9,4), %esi
movl 0x18(%rsp), %r9d
imull %edx, %r9d
subl 0x54(%rsp), %r14d
movslq 0xc(%rsp), %r11
cltq
subl 0x24(%rsp), %r13d
movq %rax, %r10
imulq %r11, %r10
shrq $0x14, %r10
shll $0x8, %r14d
addl %r14d, %r10d
movl 0x10(%rsp), %r15d
negl %r15d
movl %r15d, 0x10(%rsp)
imull %r15d, %edx
movslq %r9d, %r9
movslq %r10d, %r10
imulq %r9, %r10
movslq %ebp, %r9
imulq %r9, %rax
shrq $0x14, %rax
shll $0x8, %r13d
addl %r13d, %eax
movslq %edx, %rdx
cltq
imulq %rdx, %rax
addq %r10, %rax
shrq $0x18, %rax
subl 0x28(%rsp), %esi
shll $0x7, %esi
leaq 0x247184(%rip), %rdx    # 0x268a80
movq 0x40(%rsp), %r10
movl (%rdx,%r10,4), %r10d
movl 0x7bf25(%rip), %edx     # 0x9d830
decl %edx
cmpl %edx, %r10d
jne 0x2192e
movl 0x247158(%rip), %edx    # 0x268a70
subl 0x247156(%rip), %edx    # 0x268a74
movl 0x247157(%rip), %r15d   # 0x268a7c
addl 0x24714c(%rip), %r15d   # 0x268a78
jmp 0x21940
movl 0x50(%rsp), %edx
subl 0x48(%rsp), %edx
movl 0x4c(%rsp), %r15d
subl 0x3c(%rsp), %r15d
addl %eax, %esi
imull %edx, %r8d
imull %r15d, %ecx
addl %r8d, %ecx
imull %r15d, %ebp
movl 0xc(%rsp), %eax
imull %edx, %eax
subl %eax, %ebp
movl %ebp, %eax
negl %eax
cmovsl %ebp, %eax
movl %ecx, %edx
sarl $0x3, %edx
movl %edx, %r8d
negl %r8d
cmovsl %edx, %r8d
cmpl %r8d, %eax
jbe 0x21987
movslq %ecx, %rax
shlq $0x1c, %rax
movslq %ebp, %rcx
cqto
idivq %rcx
movq %rax, %rcx
movq 0x40(%rsp), %r8
movl %r10d, 0xc(%rsp)
movslq %r10d, %r10
xorl %eax, %eax
cmpb $0x0, 0x14(%rsp)
setne %al
movswq 0x12(%r12,%rax,8), %rdx
imulq 0x30(%rsp), %rdx
shrq $0xf, %rdx
movl 0x4(%r12,%rax,4), %r12d
movl 0x18(%rsp), %ebp
imull %edx, %ebp
movslq %ecx, %rax
imulq %rax, %r11
shrq $0x14, %r11
addl %r11d, %r14d
movl 0x10(%rsp), %r11d
imull %edx, %r11d
imulq %rax, %r9
shrq $0x14, %r9
addl %r9d, %r13d
movslq %ebp, %rax
movslq %r14d, %rcx
imulq %rax, %rcx
movslq %r11d, %rax
movslq %r13d, %rdx
imulq %rax, %rdx
addq %rcx, %rdx
shrq $0x18, %rdx
subl 0x28(%rsp), %r12d
shll $0x7, %r12d
addl %edx, %r12d
movslq 0x24b076(%rip), %rdx    # 0x26ca80
leaq 0x24b07f(%rip), %rax    # 0x26ca90
movslq (%rax,%r8,4), %r9
movq %r9, %rax
imulq %rdx, %rax
shrq $0x14, %rax
leaq 0x24f069(%rip), %rcx    # 0x270a90
movl (%rcx,%r8,4), %ecx
movl %ecx, 0x10(%rsp)
movslq %ecx, %rcx
imulq %rcx, %rdx
shrq $0x14, %rdx
movslq 0x25304f(%rip), %rbp    # 0x274a90
movq %rbp, %r15
movq %r9, 0x28(%rsp)
imulq %r9, %r15
shrq $0x14, %r15
imulq %rcx, %rbp
shrq $0x14, %rbp
xorl %r8d, %r8d
movl %esi, %r9d
subl %eax, %r9d
setl %r8b
xorl %r11d, %r11d
cmpl %edx, %r12d
setl %r11b
leal (%r8,%r11,2), %r14d
xorl %r8d, %r8d
movl %esi, %r11d
subl %r15d, %r11d
setg %r8b
xorl %r13d, %r13d
cmpl %ebp, %r12d
setg %r13b
shll $0x3, %r13d
leal (%r13,%r8,4), %r8d
leal (%r8,%r14), %r13d
cmpl $0x3, %r14d
jne 0x21ab1
cmpq %r10, %rdi
jg 0x21da3
andw $0x0, (%rbx,%rdi,2)
incq %rdi
jmp 0x21a9e
cmpl $0xc, %r8d
jne 0x21ad0
cmpq %r10, %rdi
jg 0x21da3
movzwl 0x252fcd(%rip), %eax    # 0x274a94
movw %ax, (%rbx,%rdi,2)
incq %rdi
jmp 0x21ab7
testl %r14d, %r14d
je 0x21b79
movl %r13d, 0x24(%rsp)
addl %esi, %edx
addl %r12d, %eax
subl %eax, %edx
movslq %r9d, %rax
shlq $0x1e, %rax
movslq %edx, %r9
cqto
idivq %r9
movl %ecx, %edx
movq 0x28(%rsp), %r13
subl %r13d, %edx
movslq %edx, %r9
cltq
imulq %rax, %r9
shrq $0x1e, %r9
addl %r13d, %r9d
movl %r12d, %edx
subl %esi, %edx
movslq %edx, %rdx
imulq %rax, %rdx
shrq $0x1e, %rdx
addl %esi, %edx
movq %rdx, 0x18(%rsp)
movl 0xc(%rsp), %edx
subl %edi, %edx
imulq %rcx, %rax
shrq $0x1e, %rax
movslq %eax, %r13
movslq %edx, %rax
imulq %r13, %rax
movslq %r9d, %r13
cqto
idivq %r13
leal (%rdi,%rax), %r13d
cmpl $0x2, %r14d
jne 0x21b93
testl %eax, %eax
movslq %r13d, %rdx
cmovsl 0xc(%rsp), %r13d
movl %r13d, 0x14(%rsp)
movl 0x24(%rsp), %r13d
movl 0x10(%rsp), %r14d
cmpq %r10, %rdx
jge 0x21bb9
andw $0x0, 0x2(%rbx,%rdx,2)
incq %rdx
jmp 0x21b69
movl %r12d, %r14d
movl %esi, 0x18(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rax
movl %eax, 0x30(%rsp)
jmp 0x21bff
movq %rdi, %rax
cmpl 0xc(%rsp), %r13d
cmovlel %r13d, %edi
movl %r13d, %r14d
movslq %r13d, %rdx
movl 0x24(%rsp), %r13d
cmpq %rdx, %rax
jg 0x21bda
andw $0x0, (%rbx,%rax,2)
incq %rax
jmp 0x21baa
testl %eax, %eax
movq 0x18(%rsp), %rax
cmovsl %r12d, %eax
cmovsl %r14d, %r9d
movq %rax, %r14
movl %esi, 0x18(%rsp)
movq 0x28(%rsp), %rax
movl %eax, 0x30(%rsp)
jmp 0x21c04
movl 0xc(%rsp), %eax
cmpl %eax, %r14d
movq 0x18(%rsp), %rdx
cmovgl %esi, %edx
movl %edx, 0x18(%rsp)
cmovgl 0x28(%rsp), %r9d
movl %r12d, %r14d
movl %eax, 0x14(%rsp)
movl %r9d, 0x30(%rsp)
movl 0x10(%rsp), %r9d
testl %r8d, %r8d
je 0x21cb1
movl %r9d, 0x10(%rsp)
addl %esi, %ebp
addl %r12d, %r15d
subl %r15d, %ebp
movslq %r11d, %rax
shlq $0x1e, %rax
movslq %ebp, %r9
cqto
idivq %r9
movl %ecx, %edx
movq 0x28(%rsp), %r11
subl %r11d, %edx
movslq %edx, %r9
cltq
imulq %rax, %r9
shrq $0x1e, %r9
addl %r11d, %r9d
subl %esi, %r12d
movslq %r12d, %r11
imulq %rax, %r11
shrq $0x1e, %r11
addl %esi, %r11d
movq 0x40(%rsp), %rdx
leaq 0x242e10(%rip), %rsi    # 0x264a70
movslq (%rsi,%rdx,4), %rsi
movl 0xc(%rsp), %ebp
movl %ebp, %edx
subl %esi, %edx
imulq %rax, %rcx
shrq $0x1e, %rcx
movslq %ecx, %rcx
movslq %edx, %rax
imulq %rcx, %rax
movslq %r9d, %rcx
cqto
idivq %rcx
leal (%rsi,%rax), %ecx
cmpl $0x8, %r8d
jne 0x21cb7
testl %eax, %eax
movl 0x14(%rsp), %ebp
cmovnsl %ecx, %ebp
movslq %ecx, %rcx
cmpq %r10, %rcx
jge 0x21cd4
movzwl 0x252ded(%rip), %edx    # 0x274a94
movw %dx, 0x2(%rbx,%rcx,2)
incq %rcx
jmp 0x21c9b
movl 0x14(%rsp), %ebp
jmp 0x21ce3
cmpl %ebp, %ecx
cmovlel %ecx, %edi
movslq %ecx, %rax
cmpq %rax, %rsi
jg 0x21ced
movzwl 0x252dc9(%rip), %edx    # 0x274a94
movw %dx, (%rbx,%rsi,2)
incq %rsi
jmp 0x21cbf
testl %eax, %eax
cmovsl %r14d, %r11d
cmovsl 0x10(%rsp), %r9d
movl %r11d, %r14d
movl 0x18(%rsp), %eax
movl 0x30(%rsp), %ecx
jmp 0x21d0a
cmpl %ebp, %ecx
cmovgl 0x18(%rsp), %r11d
cmovgl 0x30(%rsp), %r9d
movl %r11d, %eax
movl %r9d, %ecx
movl 0x14(%rsp), %ebp
movl 0x10(%rsp), %r9d
movslq 0x252d87(%rip), %rsi    # 0x274a98
cltq
imulq %rsi, %rax
movslq %ecx, %rcx
cqto
idivq %rcx
movq %rax, %rcx
movslq %r14d, %rax
imulq %rsi, %rax
movslq %r9d, %rsi
cqto
idivq %rsi
subl %ecx, %eax
shll $0x4, %ecx
shll $0x4, %eax
movl %ebp, %esi
subl %edi, %esi
incl %esi
cltd
idivl %esi
movslq %edi, %rdx
leaq (%rbx,%rdx,2), %r14
movl 0x252d4d(%rip), %edx    # 0x274a9c
shll $0x10, %edx
addl %ecx, %edx
movq %r14, %rdi
movl %eax, %ecx
callq 0x4448f
movzwl (%r14), %eax
testw %ax, %ax
jns 0x21d6e
andw $0x0, (%r14)
xorl %eax, %eax
movzwl %ax, %ecx
movl 0x252d1d(%rip), %eax    # 0x274a94
cmpl %ecx, %eax
jge 0x21d7f
movw %ax, (%r14)
movslq %ebp, %rax
movzwl (%rbx,%rax,2), %ecx
testw %cx, %cx
jns 0x21d92
andw $0x0, (%rbx,%rax,2)
xorl %ecx, %ecx
movzwl %cx, %edx
movl 0x252cf9(%rip), %ecx    # 0x274a94
cmpl %edx, %ecx
jge 0x21da3
movw %cx, (%rbx,%rax,2)
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq