// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/build.c
// Function: AutoAlignWalls
void AutoAlignWalls(int nWall0, int ply)
{
	int z0, z1, nTile, nWall1, branch, visible, nNextSector, nSector;

	nTile = wall[nWall0].picnum;
	branch = 0;
	if (ply == 0)
	{
			//clear visited bits
		memset(visited,0,sizeof(visited));
		visited[nWall0] = 1;
	}

	z0 = GetWallZPeg(nWall0);

	nWall1 = wall[nWall0].point2;

		//loop through walls at this vertex in CCW order
	while (1)
	{
			//break if this wall would connect us in a loop
		if (visited[nWall1]) break;

		visited[nWall1] = 1;

			//break if reached back of left wall
		if (wall[nWall1].nextwall == nWall0) break;

		if (wall[nWall1].picnum == nTile)
		{
			z1 = GetWallZPeg(nWall1);
			visible = 0;

			nNextSector = wall[nWall1].nextsector;
			if (nNextSector < 0)
				visible = 1;
			else
			{
					//ignore two sided walls that have no visible face
				nSector = wall[wall[nWall1].nextwall].nextsector;
				if (getceilzofslope((short)nSector,wall[nWall1].x,wall[nWall1].y) <
					getceilzofslope((short)nNextSector,wall[nWall1].x,wall[nWall1].y))
					visible = 1;

				if (getflorzofslope((short)nSector,wall[nWall1].x,wall[nWall1].y) >
					getflorzofslope((short)nNextSector,wall[nWall1].x,wall[nWall1].y))
					visible = 1;
			}

			if (visible)
			{
				branch++;
				AlignWalls(nWall0,z0,nWall1,z1,nTile);

					//if wall was 1-sided, no need to recurse
				if (wall[nWall1].nextwall < 0)
				{
					nWall0 = nWall1;
					z0 = GetWallZPeg(nWall0);
					nWall1 = wall[nWall0].point2;
					branch = 0;
					continue;
				}
				else
					AutoAlignWalls(nWall1,ply+1);
			}
		}

		if (wall[nWall1].nextwall < 0) break;
		nWall1 = wall[wall[nWall1].nextwall].point2;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %r14d
movslq %edi, %r12
movq %r12, %rax
shlq $0x5, %rax
leaq 0x19af64(%rip), %r15    # 0x1b1a50
movswl 0x10(%r15,%rax), %ebp
leaq 0x15d517(%rip), %rbx    # 0x174010
movl %esi, 0xc(%rsp)
testl %esi, %esi
jne 0x16b15
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x62f0
movb $0x1, (%r12,%rbx)
movl %r14d, %edi
callq 0x1b7ff
movl %r14d, %edi
movl %eax, 0x8(%rsp)
incl 0xc(%rsp)
movl %ebp, 0x10(%rsp)
shlq $0x5, %r12
movswq 0x8(%r15,%r12), %r12
cmpb $0x0, (%r12,%rbx)
jne 0x16c4e
movb $0x1, (%r12,%rbx)
movq %r12, %r14
shlq $0x5, %r14
movswl 0xa(%r15,%r14), %eax
cmpl %eax, %edi
je 0x16c4e
addq %r15, %r14
cmpw %bp, 0x10(%r14)
jne 0x16c1c
movl %edi, %r13d
movl %r12d, %edi
callq 0x1b7ff
movl %r13d, %edi
movzwl 0xc(%r14), %ebx
testw %bx, %bx
js 0x16bf6
movswq 0xa(%r14), %rax
shlq $0x5, %rax
leaq 0x19aebf(%rip), %rcx    # 0x1b1a50
movswl 0xc(%rcx,%rax), %r15d
movl (%r14), %esi
movl 0x4(%r14), %edx
movl %r15d, %edi
callq 0x371c4
movl %eax, 0x14(%rsp)
movl (%r14), %esi
movl 0x4(%r14), %edx
movswl %bx, %ebp
movl %ebp, %edi
callq 0x371c4
movl %eax, %ebx
movl (%r14), %esi
movl 0x4(%r14), %edx
movl %r15d, %edi
callq 0x370fb
movl %eax, %r15d
movl (%r14), %esi
movl 0x4(%r14), %edx
movl %ebp, %edi
callq 0x370fb
movl %r13d, %edi
cmpl %eax, %r15d
leaq 0x19ae66(%rip), %r15    # 0x1b1a50
movl 0x10(%rsp), %ebp
jg 0x16bf6
cmpl %ebx, 0x14(%rsp)
jge 0x16c1c
movl 0x8(%rsp), %esi
movl %r12d, %edx
movl %ebp, %r8d
callq 0x1b884
cmpw $0x0, 0xa(%r14)
js 0x16c33
movl %r12d, %edi
movl 0xc(%rsp), %esi
callq 0x16aca
movl %r13d, %edi
movswq 0xa(%r14), %r12
testq %r12, %r12
leaq 0x15d3e5(%rip), %rbx    # 0x174010
jns 0x16b2c
jmp 0x16c4e
movl %r12d, %edi
callq 0x1b7ff
movl %eax, 0x8(%rsp)
movl %r12d, %edi
leaq 0x15d3c7(%rip), %rbx    # 0x174010
jmp 0x16b2c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq