// File: /workspace/github_cpp_makefile_star2_O2/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
imulq $0xc, %rdx, %rax
imulq $0x4b0, %rdi, %rsi      # imm = 0x4B0
movq %rax, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
addq %rsi, %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 0x1f91f
cmpl %edx, %r10d
jge 0x1f91f
addl %edi, %r10d
movzwl %r10w, %r14d
testb %r11b, %r11b
jle 0x1f8aa
cmpw $0x0, 0x1fbe8(%rcx,%r14,2)
jne 0x1f8aa
movw $0x1, 0x1fbe8(%rcx,%r14,2)
movzwl 0x1fbe8(%rcx,%r14,2), %r10d
addq $0xc, %rax
decb %r11b
cmpw $0x3, %r10w
jl 0x1f86e
movswl (%rax), %ecx
movl $0x0, %r9d
cmpl 0x4(%rsp), %ecx
movq 0x8(%rsp), %r11
je 0x1f927
cmpb $0x0, -0x2(%rax)
movl $0x0, %r9d
js 0x1f927
movzwl (%r8), %eax
cmpw $-0x1, %ax
je 0x1f8f7
movl $0x0, %r9d
cmpw %ax, %r10w
jne 0x1f927
movq %r8, 0x10(%rsp)
leaq 0x10(%r11), %rdi
pushq $0x1
popq %rsi
callq 0x12438
testl %eax, %eax
je 0x1fba8
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r11
jmp 0x1fbc2
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 0x1f9ba
movzwl %cx, %r14d
testb %sil, %sil
jle 0x1f968
cmpw $0x0, 0x1fbe8(%rax,%r14,2)
jne 0x1f968
movw $0x1, 0x1fbe8(%rax,%r14,2)
movzwl 0x1fbe8(%rax,%r14,2), %edi
addl $-0x64, %ecx
decl %edx
decb %sil
cmpw $0x3, %di
jl 0x1f944
movl %edx, %ecx
imulq $0x4b0, %rcx, %rcx      # imm = 0x4B0
addq %rcx, %rax
movswl 0xe(%r10,%rax), %ecx
cmpl 0x4(%rsp), %ecx
je 0x1f9ba
addq %r10, %rax
addq $0xc, %rax
cmpb $0x0, (%rax)
js 0x1f9ba
movzwl (%r8), %eax
cmpw $-0x1, %ax
je 0x1fb62
cmpw %ax, %di
je 0x1fb62
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 0x1fa86
leal (%rsi,%rdx), %r9d
movzwl %r9w, %ebx
testb %dil, %dil
jle 0x1fa03
cmpw $0x0, 0x1fbe8(%rax,%rbx,2)
jne 0x1fa03
movw $0x1, 0x1fbe8(%rax,%rbx,2)
movzwl 0x1fbe8(%rax,%rbx,2), %r9d
decl %edx
decb %dil
cmpw $0x3, %r9w
jl 0x1f9d9
addq 0x20(%rsp), %rax
movl %edx, %ecx
imulq $0xc, %rcx, %rcx
movswl 0xe(%rcx,%rax), %edx
cmpl 0x4(%rsp), %edx
je 0x1fa86
addq %rcx, %rax
addq $0xc, %rax
cmpb $0x0, (%rax)
js 0x1fa86
movzwl (%r8), %eax
cmpw $-0x1, %ax
je 0x1fa4a
cmpw %ax, %r9w
jne 0x1fa86
leaq 0x10(%r11), %rdi
movl (%rsp), %esi
incl %esi
movl %esi, (%rsp)
movq %r8, 0x10(%rsp)
callq 0x12438
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r8
testl %eax, %eax
jne 0x1fa7d
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
addq %r10, %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 0x1fb50
cmpl %edx, %r9d
jge 0x1fb50
movzwl %di, %ebx
testb %bpl, %bpl
jle 0x1fae8
cmpw $0x0, 0x1fbe8(%rax,%rbx,2)
jne 0x1fae8
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 0x1fab2
movswl (%rsi), %eax
cmpl 0x4(%rsp), %eax
je 0x1fb50
cmpb $0x0, -0x2(%rsi)
js 0x1fb50
movzwl (%r8), %eax
cmpw $-0x1, %ax
je 0x1fb24
cmpw %ax, %r9w
jne 0x1fb50
movq %r8, %r12
leaq 0x10(%r11), %rdi
movl (%rsp), %esi
incl %esi
movq %r11, %r15
callq 0x12438
testl %eax, %eax
jne 0x1fb4c
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 0x12438
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r8
testl %eax, %eax
jne 0x1fb98
movq (%r11), %rax
movzwl 0x1fbe8(%rax,%r14,2), %eax
movw %ax, (%r8)
orl $0x2, %ebx
movq %rbx, %r14
movq 0x18(%rsp), %r10
jmp 0x1f9c1
movq 0x8(%rsp), %r11
movq (%r11), %rax
movzwl 0x1fbe8(%rax,%r14,2), %eax
movq 0x10(%rsp), %r8
movw %ax, (%r8)
pushq $0x1
popq %r9
jmp 0x1f927
nop