// File: /workspace/github_cpp_makefile_star2_O3/Alcaro[P]bridges/gamemap.cpp
// Function: gamemap::init(char const*)
void gamemap::init(const char * inmap)
{
	width = 0;
	while (inmap[width] != '\n') width++;
	height = 0;
	while (inmap[(width+1) * height] != '\0') height++;
	
	numislands = 0;
	
	has_castles = false;
	an_island[0] = -1; // this value is used if there are zero islands
	an_island[1] = -1;
	an_island[2] = -1;
	an_island[3] = -1;
	an_island[4] = -1;
	
	for (int y=0;y<height;y++)
	for (int x=0;x<width;x++)
	{
		island& here = map[y][x];
		
		int rootpos = y*100+x;
		char mapchar = inmap[y*(width+1) + x];
		
		while (mapchar == '<' || mapchar == '>' || mapchar == 'v' || mapchar == '^')
		{
			if (mapchar == '>') rootpos += 1;
			if (mapchar == '^') rootpos -= 100;
			if (mapchar == '<') rootpos -= 1;
			if (mapchar == 'v') rootpos += 100;
			
			int y2 = rootpos/100;
			int x2 = rootpos%100;
			mapchar = inmap[y2*(width+1) + x2];
		}
		
		if (mapchar == ' ') here.population = -1;
		else if (mapchar == '#') here.population = -2;
		else if (mapchar == 'r') here.population = 80;
		else if (mapchar == 'b') here.population = 81;
		else if (mapchar == 'y') here.population = 82;
		else if (mapchar == 'g') here.population = 83;
		else if (mapchar >= '0' && mapchar <= '9') here.population = mapchar-'0';
		else here.population = mapchar-'A'+10;
		
		here.totbridges = 0;
		here.bridges[0] = 0;
		here.bridges[1] = 0;
		here.bridges[2] = 0;
		here.bridges[3] = 0;
		
		here.bridgelen[0] = -1;
		here.bridgelen[1] = -1;
		here.bridgelen[2] = -1;
		here.bridgelen[3] = -1;
		
		here.rootnode = rootpos;
		
		if (here.population >= 0)
		{
			numislands++;
			if (rootpos == y*100+x)
			{
				an_island[0] = rootpos;
				if (here.population >= 80)
				{
					an_island[here.population-80+1] = rootpos;
					has_castles = true;
				}
			}
			
			for (int x2=x-1;x2>=0;x2--)
			{
				if (map[y][x2].population != -1)
				{
					if (map[y][x2].population == -2) break;
					
					here.bridgelen[2] = x-x2;
					map[y][x2].bridgelen[0] = x-x2;
					for (int x3=x2+1;x3<x;x3++)
					{
						map[y][x3].bridgelen[0] = x-x3;
						map[y][x3].bridgelen[2] = x3-x2;
					}
					
					if (map[y][x2].rootnode == here.rootnode && x-x2==1)
					{
						here.bridges[2] = 3;
						map[y][x2].bridges[0] = 3;
					}
					
					break;
				}
			}
			for (int y2=y-1;y2>=0;y2--)
			{
				if (map[y2][x].population != -1)
				{
					if (map[y2][x].population == -2) break;
					
					here.bridgelen[1] = y-y2;
					map[y2][x].bridgelen[3] = y-y2;
					for (int y3=y2+1;y3<y;y3++)
					{
						map[y3][x].bridgelen[1] = y3-y2;
						map[y3][x].bridgelen[3] = y-y3;
					}
					
					if (map[y2][x].rootnode == here.rootnode && y-y2==1)
					{
						here.bridges[1] = 3;
						map[y2][x].bridges[3] = 3;
					}
					
					break;
				}
			}
		}
	}
}

// Assembly:
xorl %eax, %eax
movl %eax, (%rdi)
cmpb $0xa, (%rsi,%rax)
leaq 0x1(%rax), %rax
jne 0x22abe
movl %eax, %ecx
xorl %r8d, %r8d
movl %r8d, 0x4(%rdi)
movq %r8, %rdx
imulq %rcx, %rdx
incq %r8
cmpb $0x0, (%rsi,%rdx)
jne 0x22acf
movl $0x0, 0x8(%rdi)
movb $0x0, 0x1d4cc(%rdi)
movq $-0x1, 0x1d4ce(%rdi)
movw $0xffff, 0x1d4d6(%rdi)  # imm = 0xFFFF
cmpq $0x1, %r8
je 0x22f06
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0x1d4ce(%rdi), %rcx
movq %rcx, -0x20(%rsp)
movabsq $0x100000000, %r10      # imm = 0x100000000
leaq 0xc(%rdi), %rcx
movq %rcx, -0x10(%rsp)
movl %eax, %ecx
movq %rcx, -0x50(%rsp)
decq %r8
leaq -0x1(%rax), %rcx
movq %rcx, -0x18(%rsp)
leaq 0x14(%rdi), %rbx
leaq 0x12(%rdi), %rcx
leaq 0x17(%rdi), %rdx
leaq 0x13(%rdi), %r9
xorl %ebp, %ebp
movq %r10, -0x70(%rsp)
movl $0x0, -0x7c(%rsp)
movq %rdi, -0x78(%rsp)
movq %r8, -0x48(%rsp)
cmpq $0x1, %rax
movq %rbx, -0x28(%rsp)
movq %rcx, -0x30(%rsp)
movq %rdx, -0x38(%rsp)
movq %r9, -0x40(%rsp)
jne 0x22bcc
incq %rbp
movq -0x28(%rsp), %rbx
movl $0x4b0, %r8d            # imm = 0x4B0
addq %r8, %rbx
movq -0x30(%rsp), %rcx
addq %r8, %rcx
addq %r10, -0x70(%rsp)
movq -0x38(%rsp), %rdx
addq %r8, %rdx
movq -0x40(%rsp), %r9
addq %r8, %r9
movq -0x48(%rsp), %r8
cmpq %rbp, %r8
jne 0x22b75
jmp 0x22ef8
imulq $0x64, %rbp, %r8
movq %r8, (%rsp)
movq %rbp, %r8
imulq -0x50(%rsp), %r8
movq %r8, -0x8(%rsp)
movq %r9, -0x58(%rsp)
movq %rdx, -0x60(%rsp)
movq %rcx, -0x68(%rsp)
movq %r10, %rdx
xorl %r9d, %r9d
imulq $0x4b0, %rbp, %rcx      # imm = 0x4B0
addq -0x10(%rsp), %rcx
leaq (%r9,%r9,2), %r8
leaq (%rcx,%r8,4), %r12
movq (%rsp), %rcx
leal (%r9,%rcx), %r13d
movq -0x8(%rsp), %rcx
addl %r9d, %ecx
movl %r13d, %r14d
movslq %ecx, %rcx
movb (%rsi,%rcx), %r15b
movzbl %r15b, %ecx
cmpl $0x5d, %ecx
jle 0x22c3d
cmpl $0x71, %ecx
jg 0x22c49
cmpl $0x5e, %ecx
je 0x22c5c
jmp 0x22ccb
cmpl $0x3d, %ecx
jg 0x22c50
cmpl $0x3c, %ecx
je 0x22c5c
jmp 0x22cad
cmpl $0x76, %ecx
je 0x22c5c
jmp 0x22cbc
cmpl $0x3e, %ecx
jne 0x22ce9
incl %r14d
xorl %ecx, %ecx
cmpb $0x3c, %r15b
sete %cl
leal -0x64(%r14), %r8d
cmpb $0x5e, %r15b
cmovnel %r14d, %r8d
subl %ecx, %r8d
leal 0x64(%r8), %r14d
cmpb $0x76, %r15b
cmovnel %r8d, %r14d
movslq %r14d, %r8
imulq $0x51eb851f, %r8, %r11  # imm = 0x51EB851F
movq %r11, %rcx
shrq $0x3f, %rcx
sarq $0x25, %r11
addl %ecx, %r11d
imull $0x64, %r11d, %ecx
subl %ecx, %r8d
movl %eax, %ecx
imull %r11d, %ecx
addl %r8d, %ecx
jmp 0x22c1e
cmpl $0x20, %ecx
je 0x22cda
cmpl $0x23, %ecx
jne 0x22ce9
movb $-0x2, %r8b
jmp 0x22cff
cmpl $0x72, %ecx
je 0x22cdf
cmpl $0x79, %ecx
jne 0x22ce9
movb $0x52, %r8b
jmp 0x22cff
cmpl $0x62, %ecx
je 0x22ce4
cmpl $0x67, %ecx
jne 0x22ce9
movb $0x53, %r8b
jmp 0x22cff
movb $-0x1, %r8b
jmp 0x22cff
movb $0x50, %r8b
jmp 0x22cff
movb $0x51, %r8b
jmp 0x22cff
leal -0x30(%r15), %ecx
addb $-0x37, %r15b
cmpb $0xa, %cl
movzbl %cl, %ecx
movzbl %r15b, %r8d
cmovbl %ecx, %r8d
movb %r8b, (%r12)
movb $0x0, 0x1(%r12)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x4(%r12)
movw %r14w, 0x2(%r12)
testb %r8b, %r8b
js 0x22e66
movl -0x7c(%rsp), %ecx
incl %ecx
movl %ecx, -0x7c(%rsp)
movl %ecx, 0x8(%rdi)
cmpl %r13d, %r14d
jne 0x22d5d
movq -0x20(%rsp), %rcx
movw %r14w, (%rcx)
cmpb $0x50, %r8b
jb 0x22d5d
movzbl %r8b, %ecx
addl %r10d, %ecx
addl $-0x4f, %ecx
movw %r14w, 0x1d4ce(%rdi,%rcx,2)
movb $0x1, 0x1d4cc(%rdi)
movq -0x68(%rsp), %r8
movq %rbx, %rdi
movq %rdx, %rcx
xorl %r13d, %r13d
movq %r8, %r11
leaq (%r9,%r13), %r8
testq %r8, %r8
jle 0x22dff
movq %r13, %r15
movzbl -0x14(%rdi), %r14d
decq %r13
movabsq $-0x100000000, %r8      # imm = 0xFFFFFFFF00000000
addq %r8, %rcx
addq $-0xc, %rdi
leaq -0xc(%r11), %r8
cmpl $0xff, %r14d
je 0x22d6b
cmpl $0xfe, %r14d
je 0x22dff
movl %r13d, %r8d
negb %r8b
movb %r8b, 0x6(%r12)
movb %r8b, -0x4(%rdi)
sarq $0x20, %rcx
cmpq %r9, %rcx
jge 0x22ddc
negq %r15
movb $0x1, %cl
movb %r15b, -0x2(%r11)
movb %cl, (%r11)
addq $0xc, %r11
incb %cl
decq %r15
jne 0x22dca
movzwl -0x6(%rdi), %ecx
cmpw 0x2(%r12), %cx
sete %cl
cmpl $-0x1, %r13d
sete %r8b
testb %r8b, %cl
je 0x22dff
movb $0x3, %cl
movb %cl, 0xa(%r12)
movb %cl, (%rdi)
movq -0x58(%rsp), %r11
movq -0x60(%rsp), %r10
movq -0x70(%rsp), %rcx
xorl %r8d, %r8d
movq %r8, %r15
addq %rbp, %r8
testq %r8, %r8
jle 0x22e57
movq %r11, %r13
movzbl -0x4bb(%r10), %r14d
leaq -0x1(%r15), %r8
movabsq $-0x100000000, %rdi     # imm = 0xFFFFFFFF00000000
addq %rdi, %rcx
movq $-0x4b0, %rdi           # imm = 0xFB50
addq %rdi, %r10
addq %rdi, %r11
cmpl $0xff, %r14d
je 0x22e11
cmpl $0xfe, %r14d
jne 0x22e92
movq -0x78(%rsp), %rdi
movabsq $0x100000000, %r10      # imm = 0x100000000
incq %r9
addq %r10, %rdx
addq $0xc, %rbx
addq $0xc, -0x68(%rsp)
addq $0xc, -0x60(%rsp)
addq $0xc, -0x58(%rsp)
cmpq %r9, -0x18(%rsp)
jne 0x22bf7
jmp 0x22b8f
movl %r8d, %r11d
negb %r11b
movb %r11b, 0x5(%r12)
movb %r11b, -0x4(%r10)
sarq $0x20, %rcx
cmpq %rbp, %rcx
movq -0x78(%rsp), %rdi
jge 0x22eca
negq %r15
movb $0x1, %cl
movb %cl, -0x2(%r13)
movb %r15b, (%r13)
addq $0x4b0, %r13            # imm = 0x4B0
incb %cl
decq %r15
jne 0x22eb4
movzwl -0x9(%r10), %ecx
cmpw 0x2(%r12), %cx
sete %cl
cmpl $-0x1, %r8d
sete %r8b
testb %r8b, %cl
je 0x22e5c
movb $0x3, %cl
movb %cl, 0x9(%r12)
movb %cl, (%r10)
jmp 0x22e5c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop