// File: /workspace/github_c_makefile_star2_O1/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 0x19f60a(%rip), %r15    # 0x1b7a50
leaq (%r15,%rax), %rbx
movswl 0x10(%r15,%rax), %eax
movl %eax, 0xc(%rsp)
leaq 0x161bb5(%rip), %r13    # 0x17a010
testl %esi, %esi
jne 0x18479
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 0x1d8a5
movl %r14d, %edx
movl %eax, 0x8(%rsp)
movzwl 0x8(%rbx), %eax
movswq %ax, %r12
cmpb $0x0, (%r12,%r13)
movl 0xc(%rsp), %r14d
je 0x184ab
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 0x1849c
addq %r15, %r12
cmpw %r14w, 0x10(%r12)
jne 0x18599
movswl %ax, %edi
movl %edi, 0x14(%rsp)
callq 0x1d8a5
movl 0x4(%rsp), %edx
movzwl 0xc(%r12), %ebx
testw %bx, %bx
js 0x1856f
movswq 0xa(%r12), %rax
shlq $0x5, %rax
movswl 0xc(%r15,%rax), %r14d
movl (%r12), %esi
movl 0x4(%r12), %edx
movl %r14d, %edi
callq 0x398a4
movl %eax, 0x10(%rsp)
movl (%r12), %esi
movl 0x4(%r12), %edx
movl %ebp, %r15d
movswl %bx, %ebp
movl %ebp, %edi
callq 0x398a4
movl %eax, %ebx
movl (%r12), %esi
movl 0x4(%r12), %edx
movl %r14d, %edi
callq 0x397ca
movl %eax, %r14d
movl (%r12), %esi
movl 0x4(%r12), %edx
movl %ebp, %edi
movl %r15d, %ebp
leaq 0x19f4fa(%rip), %r15    # 0x1b7a50
callq 0x397ca
movl 0x4(%rsp), %edx
cmpl %eax, %r14d
movl 0xc(%rsp), %r14d
jg 0x1856f
cmpl %ebx, 0x10(%rsp)
jge 0x18599
movl %edx, %edi
movl 0x8(%rsp), %esi
movl 0x14(%rsp), %ebx
movl %ebx, %edx
movl %r14d, %r8d
callq 0x1d92a
cmpw $0x0, 0xa(%r12)
js 0x185ca
movl %ebx, %edi
movl %ebp, %esi
callq 0x18425
movl 0x4(%rsp), %edx
movswq 0xa(%r12), %rax
testq %rax, %rax
js 0x1849c
shlq $0x5, %rax
movzwl 0x8(%r15,%rax), %eax
movswq %ax, %r12
leaq (%r12,%r13), %rcx
cmpb $0x0, (%r12,%r13)
je 0x184b5
jmp 0x1849c
movl %ebx, %edi
callq 0x1d8a5
movl %eax, 0x8(%rsp)
movzwl 0x8(%r12), %eax
movswq %ax, %r12
leaq (%r12,%r13), %rcx
cmpb $0x0, (%r12,%r13)
movl %ebx, %edx
je 0x184b1
jmp 0x1849c