// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebp
movq (%rdi), %rax
movq %rdx, 0x50(%rsp)
movslq %edx, %r9
movq %rsi, 0x40(%rsp)
movslq %esi, %rbx
leaq (%rbx,%rbx,2), %r11
imulq $0x4b0, %r9, %rdx       # imm = 0x4B0
leaq (%rdx,%r11,4), %rcx
movzwl 0xe(%rax,%rcx), %r10d
shlb $0x2, %bpl
leal 0x4(%rbx), %eax
imull $0x64, %r9d, %esi
leaq 0x10(%rdi), %rcx
movq %rcx, 0x30(%rsp)
cltq
movq %rax, 0x48(%rsp)
movq %rdx, 0x58(%rsp)
leaq 0x1a(%rdx,%r11,4), %r13
movq %rsi, 0x38(%rsp)
leaq 0x1(%rsi), %rax
movq %rax, 0x60(%rsp)
movl %ebp, %r12d
xorl %esi, %esi
movl $0x0, 0xc(%rsp)
movq %r8, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq %r9, 0x10(%rsp)
movl %r10d, 0x8(%rsp)
movq %r11, 0x28(%rsp)
movq %rbx, %rcx
incq %rbx
movq (%rdi), %rax
movslq (%rax), %rdx
cmpq %rdx, %rbx
jge 0x25a4d
cmpq 0x48(%rsp), %rcx
jge 0x25a4d
addq 0x60(%rsp), %rcx
testb %r12b, %r12b
jle 0x259ae
movzwl %cx, %edx
movl %edx, %r11d
cmpw $0x0, 0x1fbe8(%rax,%r11,2)
movq 0x28(%rsp), %r11
jne 0x259ae
movw $0x1, 0x1fbe8(%rax,%rdx,2)
movzwl %cx, %r14d
movl %r14d, %ecx
movswl 0x1fbe8(%rax,%rcx,2), %r15d
cmpl $0x3, %r15d
jl 0x25a3b
movswl (%rax,%r13), %ecx
cmpl %r10d, %ecx
je 0x25a3b
cmpb $0x0, -0x2(%rax,%r13)
js 0x25a3b
movzwl (%r8), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x259e6
cmpl %eax, %r15d
jne 0x25a3b
incl %esi
movq 0x30(%rsp), %rdi
movl %esi, 0x4(%rsp)
callq 0x166c8
movl $0x1, 0xc(%rsp)
testl %eax, %eax
je 0x25a0e
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %rdi
jmp 0x25a28
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movzwl 0x1fbe8(%rax,%r14,2), %eax
movq 0x20(%rsp), %r8
movw %ax, (%r8)
movq 0x10(%rsp), %r9
movl 0x8(%rsp), %r10d
movl 0x4(%rsp), %esi
movq 0x28(%rsp), %r11
addq $0xc, %r13
decb %r12b
cmpw $0x3, %r15w
jl 0x25963
movq 0x50(%rsp), %rcx
leal -0x4(%rcx), %eax
xorl %ebx, %ebx
testl %eax, %eax
cmovgl %eax, %ebx
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rdx
leal -0x64(%rax,%rdx), %r12d
movl %ebx, %edx
movl %ecx, %r13d
movl %ebp, %ebx
movl %edx, 0x48(%rsp)
cmpl %r13d, %edx
jge 0x25b48
testb %bl, %bl
jle 0x25a9f
movq (%rdi), %rax
movzwl %r12w, %ecx
cmpw $0x0, 0x1fbe8(%rax,%rcx,2)
jne 0x25a9f
movw $0x1, 0x1fbe8(%rax,%rcx,2)
decl %r13d
movq (%rdi), %rax
movzwl %r12w, %r14d
movswl 0x1fbe8(%rax,%r14,2), %r15d
cmpl $0x3, %r15d
jl 0x25b37
movl %r13d, %ecx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movswl 0xe(%rax,%r11,4), %ecx
cmpl %r10d, %ecx
je 0x25b37
leaq (%rax,%r11,4), %rax
addq $0xc, %rax
cmpb $0x0, (%rax)
js 0x25b37
movzwl (%r8), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x25aed
cmpl %eax, %r15d
jne 0x25b37
incl %esi
movq 0x30(%rsp), %rdi
movl %esi, 0x4(%rsp)
callq 0x166c8
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r8
testl %eax, %eax
jne 0x25b1b
movq (%rdi), %rax
movzwl 0x1fbe8(%rax,%r14,2), %eax
movw %ax, (%r8)
orl $0x2, 0xc(%rsp)
movq 0x10(%rsp), %r9
movl 0x8(%rsp), %r10d
movl 0x4(%rsp), %esi
movq 0x28(%rsp), %r11
movl 0x48(%rsp), %edx
addl $-0x64, %r12d
decb %bl
cmpw $0x3, %r15w
jl 0x25a76
movq 0x40(%rsp), %r12
leal -0x4(%r12), %eax
xorl %ebx, %ebx
testl %eax, %eax
cmovgl %eax, %ebx
movq 0x38(%rsp), %rax
decl %eax
movq %rax, 0x38(%rsp)
movl %ebp, %r13d
cmpl %r12d, %ebx
jge 0x25c39
movq 0x38(%rsp), %rax
addl %r12d, %eax
testb %r13b, %r13b
jle 0x25b99
movq (%rdi), %rcx
movzwl %ax, %edx
cmpw $0x0, 0x1fbe8(%rcx,%rdx,2)
jne 0x25b99
movw $0x1, 0x1fbe8(%rcx,%rdx,2)
decl %r12d
movq (%rdi), %rcx
movzwl %ax, %r14d
movswl 0x1fbe8(%rcx,%r14,2), %r15d
cmpl $0x3, %r15d
jl 0x25c2b
addq 0x58(%rsp), %rcx
movl %r12d, %eax
leaq (%rax,%rax,2), %rax
movswl 0xe(%rcx,%rax,4), %edx
cmpl %r10d, %edx
je 0x25c2b
leaq (%rcx,%rax,4), %rax
addq $0xc, %rax
cmpb $0x0, (%rax)
js 0x25c2b
movzwl (%r8), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x25be5
cmpl %eax, %r15d
jne 0x25c2b
incl %esi
movq 0x30(%rsp), %rdi
movl %esi, 0x4(%rsp)
callq 0x166c8
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r8
testl %eax, %eax
jne 0x25c13
movq (%rdi), %rax
movzwl 0x1fbe8(%rax,%r14,2), %eax
movw %ax, (%r8)
orl $0x4, 0xc(%rsp)
movq 0x10(%rsp), %r9
movl 0x8(%rsp), %r10d
movl 0x4(%rsp), %esi
movq 0x28(%rsp), %r11
decb %r13b
cmpw $0x3, %r15w
jl 0x25b68
movq 0x50(%rsp), %rcx
addl $0x4, %ecx
movl 0x40(%rsp), %eax
movslq %ecx, %rbx
movq 0x58(%rsp), %rcx
leaq (%rcx,%r11,4), %r12
addq $0x4be, %r12            # imm = 0x4BE
imulq $0x64, %r9, %rcx
leaq (%rcx,%rax), %r13
addq $0x64, %r13
movq %r9, %rcx
incq %r9
movq (%rdi), %rax
movslq 0x4(%rax), %rdx
cmpq %rdx, %r9
jge 0x25d3a
cmpq %rbx, %rcx
jge 0x25d3a
testb %bpl, %bpl
jle 0x25ca3
movzwl %r13w, %ecx
movl %ecx, %edx
cmpw $0x0, 0x1fbe8(%rax,%rdx,2)
jne 0x25ca3
movw $0x1, 0x1fbe8(%rax,%rcx,2)
movzwl %r13w, %r14d
movl %r14d, %ecx
movswl 0x1fbe8(%rax,%rcx,2), %r15d
cmpl $0x3, %r15d
jl 0x25d21
movswl (%rax,%r12), %ecx
cmpl %r10d, %ecx
je 0x25d21
cmpb $0x0, -0x2(%rax,%r12)
js 0x25d21
movzwl (%r8), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x25cdb
cmpl %eax, %r15d
jne 0x25d21
movq %r9, 0x10(%rsp)
incl %esi
movq 0x30(%rsp), %rdi
movl %esi, 0x4(%rsp)
callq 0x166c8
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r8
testl %eax, %eax
jne 0x25d0e
movq (%rdi), %rax
movzwl 0x1fbe8(%rax,%r14,2), %eax
movw %ax, (%r8)
orl $0x8, 0xc(%rsp)
movq 0x10(%rsp), %r9
movl 0x8(%rsp), %r10d
movl 0x4(%rsp), %esi
addq $0x4b0, %r12            # imm = 0x4B0
addq $0x64, %r13
decb %bpl
cmpw $0x3, %r15w
jl 0x25c64
movl 0xc(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop