// File: Alcaro[P]bridges/generate.cpp
// Function: (anonymous namespace)::loc_generator::valid_bridges_from(int, int, bool, unsigned short&)
uint16_t valid_bridges_from(int x, int y, bool doit, uint16_t& color)
{
	int maxbrilen = 4;
	
	int valid_dirs = 0;
	uint16_t myroot = map.map[y][x].rootnode;
	int8_t bridgemark;
	
	int n_colors = 0;
	
	//right
	bridgemark = (doit ? maxbrilen : 0);
	for (int xx=x+1;xx<map.width && xx<=x+maxbrilen;xx++)
	{
		uint16_t index = y*100 + xx;
		if ((bridgemark-- > 0) && map.towalk[index] == 0)
			map.towalk[index] = 1;
		if (map.towalk[index] >= 3)
		{
			if (map.map[y][xx].rootnode == myroot) break; // can't connect to self
			if (map.map[y][xx].population < 0) break; // can't connect to crossing bridge or reef
			if (color != (uint16_t)-1 && map.towalk[index] != color) break; // can't connect to a castle of another color
			
			if (rand(++n_colors) == 0) color = map.towalk[index];
			valid_dirs |= 1<<0;
			break;
		}
	}
	
	//up
	bridgemark = (doit ? maxbrilen : 0);
	for (int yy=y-1;yy>=0 && yy>=y-maxbrilen;yy--)
	{
		uint16_t index = yy*100 + x;
		if ((bridgemark-- > 0) && map.towalk[index] == 0)
			map.towalk[index] = 1;
		if (map.towalk[index] >= 3)
		{
			if (map.map[yy][x].rootnode == myroot) break;
			if (map.map[yy][x].population < 0) break;
			if (color != (uint16_t)-1 && map.towalk[index] != color) break;
			
			if (rand(++n_colors) == 0) color = map.towalk[index];
			valid_dirs |= 1<<1;
			break;
		}
	}
	
	//left
	bridgemark = (doit ? maxbrilen : 0);
	for (int xx=x-1;xx>=0 && xx>=x-maxbrilen;xx--)
	{
		uint16_t index = y*100 + xx;
		if ((bridgemark-- > 0) && map.towalk[index] == 0)
			map.towalk[index] = 1;
		if (map.towalk[index] >= 3)
		{
			if (map.map[y][xx].rootnode == myroot) break;
			if (map.map[y][xx].population < 0) break;
			if (color != (uint16_t)-1 && map.towalk[index] != color) break;
			
			if (rand(++n_colors) == 0) color = map.towalk[index];
			valid_dirs |= 1<<2;
			break;
		}
	}
	
	//down
	bridgemark = (doit ? maxbrilen : 0);
	for (int yy=y+1;yy<map.height && yy<=y+maxbrilen;yy++)
	{
		uint16_t index = yy*100 + x;
		if ((bridgemark-- > 0) && map.towalk[index] == 0)
			map.towalk[index] = 1;
		if (map.towalk[index] >= 3)
		{
			if (map.map[yy][x].rootnode == myroot) break;
			if (map.map[yy][x].population < 0) break;
			if (color != (uint16_t)-1 && map.towalk[index] != color) break;
			
			if (rand(++n_colors) == 0) color = map.towalk[index];
			valid_dirs |= 1<<3;
			break;
		}
	}
//printf("VALIDFROM[%.3i]=%X\n",y*100+x,valid_dirs);
	
	return valid_dirs;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movb %cl, %al
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq %r8, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x4, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq (%rax), %rax
addq $0xc, %rax
movslq -0x10(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0xc(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movw 0x2(%rax), %ax
movw %ax, -0x2a(%rbp)
movl $0x0, -0x30(%rbp)
testb $0x1, -0x11(%rbp)
je 0x53173
movl -0x24(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x5317a
xorl %eax, %eax
movl %eax, -0x5c(%rbp)
jmp 0x5317a
movl -0x5c(%rbp), %eax
movb %al, -0x2b(%rbp)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x58(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax), %rdx
xorl %eax, %eax
cmpl (%rdx), %ecx
movb %al, -0x5d(%rbp)
jge 0x531ad
movl -0x34(%rbp), %eax
movl -0xc(%rbp), %ecx
addl -0x24(%rbp), %ecx
cmpl %ecx, %eax
setle %al
movb %al, -0x5d(%rbp)
movb -0x5d(%rbp), %al
testb $0x1, %al
jne 0x531b9
jmp 0x53301
imull $0x64, -0x10(%rbp), %eax
addl -0x34(%rbp), %eax
movw %ax, -0x36(%rbp)
movb -0x2b(%rbp), %al
movb %al, %cl
addb $-0x1, %cl
movb %cl, -0x2b(%rbp)
movsbl %al, %eax
cmpl $0x0, %eax
jle 0x53204
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x36(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x53204
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x36(%rbp), %ecx
movw $0x1, 0x1fbe8(%rax,%rcx,2)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x36(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x3, %eax
jl 0x532f1
movq -0x58(%rbp), %rax
movq (%rax), %rax
addq $0xc, %rax
movslq -0x10(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0x34(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0x2(%rax), %eax
movzwl -0x2a(%rbp), %ecx
cmpl %ecx, %eax
jne 0x53255
jmp 0x53301
movq -0x58(%rbp), %rax
movq (%rax), %rax
addq $0xc, %rax
movslq -0x10(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0x34(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jge 0x53283
jmp 0x53301
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x532b1
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x36(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
movq -0x20(%rbp), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
je 0x532b1
jmp 0x53301
movq -0x58(%rbp), %rdi
addq $0x10, %rdi
movl -0x30(%rbp), %esi
addl $0x1, %esi
movl %esi, -0x30(%rbp)
callq 0x2fe60
cmpl $0x0, %eax
jne 0x532e6
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x36(%rbp), %ecx
movw 0x1fbe8(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movl -0x28(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x53301
jmp 0x532f3
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x53189
testb $0x1, -0x11(%rbp)
je 0x5330f
movl -0x24(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x53316
xorl %eax, %eax
movl %eax, -0x64(%rbp)
jmp 0x53316
movl -0x64(%rbp), %eax
movb %al, -0x2b(%rbp)
movl -0x10(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x3c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x3c(%rbp)
movb %al, -0x65(%rbp)
jl 0x53341
movl -0x3c(%rbp), %eax
movl -0x10(%rbp), %ecx
subl -0x24(%rbp), %ecx
cmpl %ecx, %eax
setge %al
movb %al, -0x65(%rbp)
movb -0x65(%rbp), %al
testb $0x1, %al
jne 0x5334d
jmp 0x53495
imull $0x64, -0x3c(%rbp), %eax
addl -0xc(%rbp), %eax
movw %ax, -0x3e(%rbp)
movb -0x2b(%rbp), %al
movb %al, %cl
addb $-0x1, %cl
movb %cl, -0x2b(%rbp)
movsbl %al, %eax
cmpl $0x0, %eax
jle 0x53398
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x3e(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x53398
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x3e(%rbp), %ecx
movw $0x1, 0x1fbe8(%rax,%rcx,2)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x3e(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x3, %eax
jl 0x53485
movq -0x58(%rbp), %rax
movq (%rax), %rax
addq $0xc, %rax
movslq -0x3c(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0xc(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0x2(%rax), %eax
movzwl -0x2a(%rbp), %ecx
cmpl %ecx, %eax
jne 0x533e9
jmp 0x53495
movq -0x58(%rbp), %rax
movq (%rax), %rax
addq $0xc, %rax
movslq -0x3c(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0xc(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jge 0x53417
jmp 0x53495
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x53445
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x3e(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
movq -0x20(%rbp), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
je 0x53445
jmp 0x53495
movq -0x58(%rbp), %rdi
addq $0x10, %rdi
movl -0x30(%rbp), %esi
addl $0x1, %esi
movl %esi, -0x30(%rbp)
callq 0x2fe60
cmpl $0x0, %eax
jne 0x5347a
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x3e(%rbp), %ecx
movw 0x1fbe8(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movl -0x28(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x28(%rbp)
jmp 0x53495
jmp 0x53487
movl -0x3c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x53325
testb $0x1, -0x11(%rbp)
je 0x534a3
movl -0x24(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0x534aa
xorl %eax, %eax
movl %eax, -0x6c(%rbp)
jmp 0x534aa
movl -0x6c(%rbp), %eax
movb %al, -0x2b(%rbp)
movl -0xc(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x44(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x44(%rbp)
movb %al, -0x6d(%rbp)
jl 0x534d5
movl -0x44(%rbp), %eax
movl -0xc(%rbp), %ecx
subl -0x24(%rbp), %ecx
cmpl %ecx, %eax
setge %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
testb $0x1, %al
jne 0x534e1
jmp 0x53629
imull $0x64, -0x10(%rbp), %eax
addl -0x44(%rbp), %eax
movw %ax, -0x46(%rbp)
movb -0x2b(%rbp), %al
movb %al, %cl
addb $-0x1, %cl
movb %cl, -0x2b(%rbp)
movsbl %al, %eax
cmpl $0x0, %eax
jle 0x5352c
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x46(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x5352c
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x46(%rbp), %ecx
movw $0x1, 0x1fbe8(%rax,%rcx,2)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x46(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x3, %eax
jl 0x53619
movq -0x58(%rbp), %rax
movq (%rax), %rax
addq $0xc, %rax
movslq -0x10(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0x44(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0x2(%rax), %eax
movzwl -0x2a(%rbp), %ecx
cmpl %ecx, %eax
jne 0x5357d
jmp 0x53629
movq -0x58(%rbp), %rax
movq (%rax), %rax
addq $0xc, %rax
movslq -0x10(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0x44(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jge 0x535ab
jmp 0x53629
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x535d9
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x46(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
movq -0x20(%rbp), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
je 0x535d9
jmp 0x53629
movq -0x58(%rbp), %rdi
addq $0x10, %rdi
movl -0x30(%rbp), %esi
addl $0x1, %esi
movl %esi, -0x30(%rbp)
callq 0x2fe60
cmpl $0x0, %eax
jne 0x5360e
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x46(%rbp), %ecx
movw 0x1fbe8(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movl -0x28(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x28(%rbp)
jmp 0x53629
jmp 0x5361b
movl -0x44(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x534b9
testb $0x1, -0x11(%rbp)
je 0x53637
movl -0x24(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x5363e
xorl %eax, %eax
movl %eax, -0x74(%rbp)
jmp 0x5363e
movl -0x74(%rbp), %eax
movb %al, -0x2b(%rbp)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x58(%rbp), %rax
movl -0x4c(%rbp), %ecx
movq (%rax), %rdx
xorl %eax, %eax
cmpl 0x4(%rdx), %ecx
movb %al, -0x75(%rbp)
jge 0x53672
movl -0x4c(%rbp), %eax
movl -0x10(%rbp), %ecx
addl -0x24(%rbp), %ecx
cmpl %ecx, %eax
setle %al
movb %al, -0x75(%rbp)
movb -0x75(%rbp), %al
testb $0x1, %al
jne 0x5367e
jmp 0x537c6
imull $0x64, -0x4c(%rbp), %eax
addl -0xc(%rbp), %eax
movw %ax, -0x4e(%rbp)
movb -0x2b(%rbp), %al
movb %al, %cl
addb $-0x1, %cl
movb %cl, -0x2b(%rbp)
movsbl %al, %eax
cmpl $0x0, %eax
jle 0x536c9
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x4e(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x0, %eax
jne 0x536c9
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x4e(%rbp), %ecx
movw $0x1, 0x1fbe8(%rax,%rcx,2)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x4e(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
cmpl $0x3, %eax
jl 0x537b6
movq -0x58(%rbp), %rax
movq (%rax), %rax
addq $0xc, %rax
movslq -0x4c(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0xc(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0x2(%rax), %eax
movzwl -0x2a(%rbp), %ecx
cmpl %ecx, %eax
jne 0x5371a
jmp 0x537c6
movq -0x58(%rbp), %rax
movq (%rax), %rax
addq $0xc, %rax
movslq -0x4c(%rbp), %rcx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movslq -0xc(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jge 0x53748
jmp 0x537c6
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x53776
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x4e(%rbp), %ecx
movswl 0x1fbe8(%rax,%rcx,2), %eax
movq -0x20(%rbp), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
je 0x53776
jmp 0x537c6
movq -0x58(%rbp), %rdi
addq $0x10, %rdi
movl -0x30(%rbp), %esi
addl $0x1, %esi
movl %esi, -0x30(%rbp)
callq 0x2fe60
cmpl $0x0, %eax
jne 0x537ab
movq -0x58(%rbp), %rax
movq (%rax), %rax
movzwl -0x4e(%rbp), %ecx
movw 0x1fbe8(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movl -0x28(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x28(%rbp)
jmp 0x537c6
jmp 0x537b8
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x5364d
movl -0x28(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)