// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movl %ecx, %ebp
movl %edx, %r15d
movl %esi, %r13d
movq %rdi, 0x8(%rsp)
movq (%rdi), %rcx
movslq %edx, %rdi
movslq %esi, %rdx
leaq (%rdx,%rdx,2), %rax
imulq $0x4b0, %rdi, %rsi      # imm = 0x4B0
movq %rax, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
leaq (%rsi,%rax,4), %rax
addq %rcx, %rax
addq $0xe, %rax
movzwl (%rax), %esi
movl %esi, 0x4(%rsp)
shlb $0x2, %bpl
movl (%rcx), %esi
addl $0x4, %edx
imull $0x64, %edi, %r12d
leal 0x1(%r12), %edi
movl %r13d, %r9d
movl %ebp, %r11d
xorl %ebx, %ebx
movl %r9d, %r10d
leal 0x1(%r10), %r9d
cmpl %esi, %r9d
jge 0x25a5a
cmpl %edx, %r10d
jge 0x25a5a
addl %edi, %r10d
movzwl %r10w, %r14d
testb %r11b, %r11b
jle 0x259e3
cmpw $0x0, 0x1fbe8(%rcx,%r14,2)
jne 0x259e3
movw $0x1, 0x1fbe8(%rcx,%r14,2)
movzwl 0x1fbe8(%rcx,%r14,2), %r10d
addq $0xc, %rax
decb %r11b
cmpw $0x3, %r10w
jl 0x259a7
movswl (%rax), %ecx
movl $0x0, %r9d
cmpl 0x4(%rsp), %ecx
movq 0x8(%rsp), %r11
je 0x25a62
cmpb $0x0, -0x2(%rax)
movl $0x0, %r9d
js 0x25a62
movzwl (%r8), %eax
cmpw $-0x1, %ax
je 0x25a30
movl $0x0, %r9d
cmpw %ax, %r10w
jne 0x25a62
movq %r8, 0x10(%rsp)
leaq 0x10(%r11), %rdi
movl $0x1, %esi
callq 0x16944
testl %eax, %eax
je 0x25ce6
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r11
jmp 0x25d00
xorl %r9d, %r9d
movq 0x8(%rsp), %r11
leal -0x4(%r15), %eax
testl %eax, %eax
cmovgl %eax, %ebx
movq (%r11), %rax
leal (%r12,%r13), %ecx
addl $-0x64, %ecx
movl %r15d, %edx
movl %ebp, %esi
movq 0x18(%rsp), %r10
cmpl %ebx, %edx
jle 0x25af6
movzwl %cx, %r14d
testb %sil, %sil
jle 0x25aa3
cmpw $0x0, 0x1fbe8(%rax,%r14,2)
jne 0x25aa3
movw $0x1, 0x1fbe8(%rax,%r14,2)
movzwl 0x1fbe8(%rax,%r14,2), %edi
addl $-0x64, %ecx
decl %edx
decb %sil
cmpw $0x3, %di
jl 0x25a7f
movl %edx, %ecx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movswl 0xe(%rax,%r10,4), %ecx
cmpl 0x4(%rsp), %ecx
je 0x25af6
leaq (%rax,%r10,4), %rax
addq $0xc, %rax
cmpb $0x0, (%rax)
js 0x25af6
movzwl (%r8), %eax
cmpw $-0x1, %ax
je 0x25ca0
cmpw %ax, %di
je 0x25ca0
movq %r9, %r14
movl %r9d, (%rsp)
leal -0x4(%r13), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovgl %eax, %ecx
movq (%r11), %rax
leal -0x1(%r12), %esi
movl %r13d, %edx
movl %ebp, %edi
cmpl %ecx, %edx
jle 0x25bc3
leal (%rsi,%rdx), %r9d
movzwl %r9w, %ebx
testb %dil, %dil
jle 0x25b3f
cmpw $0x0, 0x1fbe8(%rax,%rbx,2)
jne 0x25b3f
movw $0x1, 0x1fbe8(%rax,%rbx,2)
movzwl 0x1fbe8(%rax,%rbx,2), %r9d
decl %edx
decb %dil
cmpw $0x3, %r9w
jl 0x25b15
addq 0x20(%rsp), %rax
movl %edx, %ecx
leaq (%rcx,%rcx,2), %rcx
movswl 0xe(%rax,%rcx,4), %edx
cmpl 0x4(%rsp), %edx
je 0x25bc3
leaq (%rax,%rcx,4), %rax
addq $0xc, %rax
cmpb $0x0, (%rax)
js 0x25bc3
movzwl (%r8), %eax
cmpw $-0x1, %ax
je 0x25b87
cmpw %ax, %r9w
jne 0x25bc3
leaq 0x10(%r11), %rdi
movl (%rsp), %esi
incl %esi
movl %esi, (%rsp)
movq %r8, 0x10(%rsp)
callq 0x16944
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r8
testl %eax, %eax
jne 0x25bba
movq (%r11), %rax
movzwl 0x1fbe8(%rax,%rbx,2), %eax
movw %ax, (%r8)
orl $0x4, %r14d
movq 0x18(%rsp), %r10
movq (%r11), %rax
movl 0x4(%rax), %ecx
leal 0x4(%r15), %edx
leal 0x1(%r15), %esi
movslq %esi, %rsi
imulq $0x4b0, %rsi, %rsi      # imm = 0x4B0
leaq (%rsi,%r10,4), %rsi
addq %rax, %rsi
addq $-0x4a2, %rsi           # imm = 0xFB5E
leal (%r12,%r13), %edi
addl $0x64, %edi
movl %r15d, %r9d
leal 0x1(%r9), %r15d
cmpl %ecx, %r15d
jge 0x25c8e
cmpl %edx, %r9d
jge 0x25c8e
movzwl %di, %ebx
testb %bpl, %bpl
jle 0x25c26
cmpw $0x0, 0x1fbe8(%rax,%rbx,2)
jne 0x25c26
movw $0x1, 0x1fbe8(%rax,%rbx,2)
movzwl 0x1fbe8(%rax,%rbx,2), %r9d
addq $0x4b0, %rsi            # imm = 0x4B0
addl $0x64, %edi
decb %bpl
cmpw $0x3, %r9w
jl 0x25bf0
movswl (%rsi), %eax
cmpl 0x4(%rsp), %eax
je 0x25c8e
cmpb $0x0, -0x2(%rsi)
js 0x25c8e
movzwl (%r8), %eax
cmpw $-0x1, %ax
je 0x25c62
cmpw %ax, %r9w
jne 0x25c8e
movq %r8, %r12
leaq 0x10(%r11), %rdi
movl (%rsp), %esi
incl %esi
movq %r11, %r15
callq 0x16944
testl %eax, %eax
jne 0x25c8a
movq (%r15), %rax
movzwl 0x1fbe8(%rax,%rbx,2), %eax
movw %ax, (%r12)
orl $0x8, %r14d
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%r11), %rdi
leal 0x1(%r9), %esi
movl %esi, (%rsp)
movq %r8, 0x10(%rsp)
movq %r9, %rbx
callq 0x16944
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r8
testl %eax, %eax
jne 0x25cd6
movq (%r11), %rax
movzwl 0x1fbe8(%rax,%r14,2), %eax
movw %ax, (%r8)
orl $0x2, %ebx
movq %rbx, %r14
movq 0x18(%rsp), %r10
jmp 0x25afd
movq 0x8(%rsp), %r11
movq (%r11), %rax
movzwl 0x1fbe8(%rax,%r14,2), %eax
movq 0x10(%rsp), %r8
movw %ax, (%r8)
movl $0x1, %r9d
jmp 0x25a62
nop