// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: nextsectorneighborz
int nextsectorneighborz(short sectnum, int thez, short topbottom, short direction)
{
	walltype *wal;
	int i, testz, nextz;
	short sectortouse;

	if (direction == 1) nextz = 0x7fffffff; else nextz = 0x80000000;

	sectortouse = -1;

	wal = &wall[sector[sectnum].wallptr];
	i = sector[sectnum].wallnum;
	do
	{
		if (wal->nextsector >= 0)
		{
			if (topbottom == 1)
			{
				testz = sector[wal->nextsector].floorz;
				if (direction == 1)
				{
					if ((testz > thez) && (testz < nextz))
					{
						nextz = testz;
						sectortouse = wal->nextsector;
					}
				}
				else
				{
					if ((testz < thez) && (testz > nextz))
					{
						nextz = testz;
						sectortouse = wal->nextsector;
					}
				}
			}
			else
			{
				testz = sector[wal->nextsector].ceilingz;
				if (direction == 1)
				{
					if ((testz > thez) && (testz < nextz))
					{
						nextz = testz;
						sectortouse = wal->nextsector;
					}
				}
				else
				{
					if ((testz < thez) && (testz > nextz))
					{
						nextz = testz;
						sectortouse = wal->nextsector;
					}
				}
			}
		}
		wal++;
		i--;
	} while (i != 0);

	return(sectortouse);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movw %cx, %ax
movw %dx, %cx
movw %di, %dx
movw %dx, -0x2(%rbp)
movl %esi, -0x8(%rbp)
movw %cx, -0xa(%rbp)
movw %ax, -0xc(%rbp)
movswl -0xc(%rbp), %eax
cmpl $0x1, %eax
jne 0x4fc4e
movl $0x7fffffff, -0x24(%rbp) # imm = 0x7FFFFFFF
jmp 0x4fc55
movl $0x80000000, -0x24(%rbp) # imm = 0x80000000
movw $0xffff, -0x26(%rbp)    # imm = 0xFFFF
movswq -0x2(%rbp), %rcx
leaq 0x230959(%rip), %rax    # 0x2805c0
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movswq (%rax), %rcx
leaq 0x1a5937(%rip), %rax    # 0x1f55b0
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movswq -0x2(%rbp), %rcx
leaq 0x230930(%rip), %rax    # 0x2805c0
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movswl 0x2(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movswl 0xc(%rax), %eax
cmpl $0x0, %eax
jl 0x4fd9a
movswl -0xa(%rbp), %eax
cmpl $0x1, %eax
jne 0x4fd28
movq -0x18(%rbp), %rax
movswq 0xc(%rax), %rcx
leaq 0x2308f8(%rip), %rax    # 0x2805c0
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x20(%rbp)
movswl -0xc(%rbp), %eax
cmpl $0x1, %eax
jne 0x4fd02
movl -0x20(%rbp), %eax
cmpl -0x8(%rbp), %eax
jle 0x4fd00
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x4fd00
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movw 0xc(%rax), %ax
movw %ax, -0x26(%rbp)
jmp 0x4fd26
movl -0x20(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x4fd24
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jle 0x4fd24
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movw 0xc(%rax), %ax
movw %ax, -0x26(%rbp)
jmp 0x4fd26
jmp 0x4fd98
movq -0x18(%rbp), %rax
movswq 0xc(%rax), %rcx
leaq 0x230888(%rip), %rax    # 0x2805c0
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x20(%rbp)
movswl -0xc(%rbp), %eax
cmpl $0x1, %eax
jne 0x4fd72
movl -0x20(%rbp), %eax
cmpl -0x8(%rbp), %eax
jle 0x4fd70
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x4fd70
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movw 0xc(%rax), %ax
movw %ax, -0x26(%rbp)
jmp 0x4fd96
movl -0x20(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x4fd94
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jle 0x4fd94
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movw 0xc(%rax), %ax
movw %ax, -0x26(%rbp)
jmp 0x4fd96
jmp 0x4fd98
jmp 0x4fd9a
movq -0x18(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x18(%rbp)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x4fc9e
movswl -0x26(%rbp), %eax
popq %rbp
retq
nop