// File: /workspace/github_c_makefile_star2_O3/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 %esi, %ebp
movslq %edi, %r14
movq %r14, %rax
shlq $0x5, %rax
leaq 0x1a0613(%rip), %r15    # 0x1b8a50
leaq (%r15,%rax), %rbx
movswl 0x10(%r15,%rax), %eax
movl %eax, 0xc(%rsp)
leaq 0x162bbe(%rip), %r13    # 0x17b010
testl %esi, %esi
jne 0x18470
movl $0x2000, %edx           # imm = 0x2000
movl %edi, %r12d
movq %r13, %rdi
xorl %esi, %esi
callq 0x62f0
movl %r12d, %edi
movb $0x1, (%r14,%r13)
movl %edi, %r14d
callq 0x1dd39
movl %r14d, %edx
movl %eax, 0x8(%rsp)
movzwl 0x8(%rbx), %eax
movswq %ax, %r12
cmpb $0x0, (%r12,%r13)
movl 0xc(%rsp), %r14d
je 0x184a2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (%r12,%r13), %rcx
incl %ebp
movl %edx, 0x4(%rsp)
movb $0x1, (%rcx)
shlq $0x5, %r12
movswl 0xa(%r15,%r12), %ecx
cmpl %ecx, %edx
je 0x18493
addq %r15, %r12
cmpw %r14w, 0x10(%r12)
jne 0x18590
movswl %ax, %edi
movl %edi, 0x14(%rsp)
callq 0x1dd39
movl 0x4(%rsp), %edx
movzwl 0xc(%r12), %ebx
testw %bx, %bx
js 0x18566
movswq 0xa(%r12), %rax
shlq $0x5, %rax
movswl 0xc(%r15,%rax), %r14d
movl (%r12), %esi
movl 0x4(%r12), %edx
movl %r14d, %edi
callq 0x3abce
movl %eax, 0x10(%rsp)
movl (%r12), %esi
movl 0x4(%r12), %edx
movl %ebp, %r15d
movswl %bx, %ebp
movl %ebp, %edi
callq 0x3abce
movl %eax, %ebx
movl (%r12), %esi
movl 0x4(%r12), %edx
movl %r14d, %edi
callq 0x3aaf4
movl %eax, %r14d
movl (%r12), %esi
movl 0x4(%r12), %edx
movl %ebp, %edi
movl %r15d, %ebp
leaq 0x1a0503(%rip), %r15    # 0x1b8a50
callq 0x3aaf4
movl 0x4(%rsp), %edx
cmpl %eax, %r14d
movl 0xc(%rsp), %r14d
jg 0x18566
cmpl %ebx, 0x10(%rsp)
jge 0x18590
movl %edx, %edi
movl 0x8(%rsp), %esi
movl 0x14(%rsp), %ebx
movl %ebx, %edx
movl %r14d, %r8d
callq 0x1ddbe
cmpw $0x0, 0xa(%r12)
js 0x185c1
movl %ebx, %edi
movl %ebp, %esi
callq 0x1841c
movl 0x4(%rsp), %edx
movswq 0xa(%r12), %rax
testq %rax, %rax
js 0x18493
shlq $0x5, %rax
movzwl 0x8(%r15,%rax), %eax
movswq %ax, %r12
leaq (%r12,%r13), %rcx
cmpb $0x0, (%r12,%r13)
je 0x184ac
jmp 0x18493
movl %ebx, %edi
callq 0x1dd39
movl %eax, 0x8(%rsp)
movzwl 0x8(%r12), %eax
movswq %ax, %r12
leaq (%r12,%r13), %rcx
cmpb $0x0, (%r12,%r13)
movl %ebx, %edx
je 0x184a8
jmp 0x18493