// File: macfergus[P]baduk/cppsrc/baduk/scoring.cpp
// Function: baduk::evaluateTerritory(baduk::Board const&)
TerritoryMap evaluateTerritory(Board const& board) {
    TerritoryMap tmap;

    for (unsigned int r = 0; r < board.numRows(); ++r) {
        for (unsigned int c = 0; c < board.numCols(); ++c) {
            const Point p(r, c);
            if (board.isEmpty(p)) {
                // If it is empty, figure out the surrounding stones. If
                // they are exclusively one color, it's territory. Otherwise
                // it's dame.
                const auto boundary = collectBoundary(board, p);
                if (boundary.isOnlyBlack()) {
                    tmap.set(p, PointStatus::black);
                } else if (boundary.isOnlyWhite()) {
                    tmap.set(p, PointStatus::white);
                } else {
                    tmap.set(p, PointStatus::neutral);
                }
            } else {
                // If it contains a stone, count it for that color (chinese
                // style)
                const auto stone = board.at(p);
                if (stone == Stone::black) {
                    tmap.set(p, PointStatus::black);
                } else {
                    // not empty, not black, must be white
                    tmap.set(p, PointStatus::white);
                }
            }
        }
    }

    return tmap;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb720, %rsp           # imm = 0xB720
movq %rdi, -0x5bc0(%rbp)
movq %rdi, %rax
movq %rax, -0x5bb8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
callq 0xb770
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x5bc8(%rbp)
movq -0x10(%rbp), %rdi
callq 0xb6a0
movl %eax, -0x5bc4(%rbp)
jmp 0xa9fe
movl -0x5bc8(%rbp), %eax
movl -0x5bc4(%rbp), %ecx
cmpl %ecx, %eax
jae 0xac63
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x5bd0(%rbp)
movq -0x10(%rbp), %rdi
callq 0xb6c0
movl %eax, -0x5bcc(%rbp)
jmp 0xaa33
movl -0x5bd0(%rbp), %eax
movl -0x5bcc(%rbp), %ecx
cmpl %ecx, %eax
jae 0xac53
movl -0x18(%rbp), %esi
movl -0x28(%rbp), %edx
leaq -0x30(%rbp), %rdi
callq 0x9dc0
jmp 0xaa58
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rsi
callq 0x3c650
movb %al, -0x5bd1(%rbp)
jmp 0xaa75
movb -0x5bd1(%rbp), %al
testb $0x1, %al
jne 0xaa84
jmp 0xabbe
movq -0x10(%rbp), %rsi
leaq -0x5b70(%rbp), %rdi
movq %rdi, -0x5be8(%rbp)
movl $0x5b30, %edx           # imm = 0x5B30
callq 0x31b0
movq -0x5be8(%rbp), %rsi
movq -0x30(%rbp), %rax
movq %rax, -0x5b78(%rbp)
movq -0x5b78(%rbp), %rax
movq %rax, -0x5be0(%rbp)
movq %rsp, %rdi
movl $0xb66, %ecx            # imm = 0xB66
rep  movsq (%rsi), %es:(%rdi)
movq -0x5be0(%rbp), %rdi
callq 0xa4c0
movw %ax, -0x5bd4(%rbp)
jmp 0xaae0
movw -0x5bd4(%rbp), %ax
movw %ax, -0x3a(%rbp)
leaq -0x3a(%rbp), %rdi
callq 0xb790
movb %al, -0x5be9(%rbp)
jmp 0xaafc
movb -0x5be9(%rbp), %al
testb $0x1, %al
jne 0xab08
jmp 0xab4c
movq -0x5bc0(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x5b80(%rbp)
movq -0x5b80(%rbp), %rsi
xorl %edx, %edx
callq 0xa480
jmp 0xab2a
jmp 0xabb9
movq -0x5bc0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0xb7f0
jmp 0xac89
leaq -0x3a(%rbp), %rdi
callq 0xb7c0
movb %al, -0x5bea(%rbp)
jmp 0xab5d
movb -0x5bea(%rbp), %al
testb $0x1, %al
jne 0xab69
jmp 0xab90
movq -0x5bc0(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x5b88(%rbp)
movq -0x5b88(%rbp), %rsi
movl $0x1, %edx
callq 0xa480
jmp 0xab8e
jmp 0xabb7
movq -0x5bc0(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x5b90(%rbp)
movq -0x5b90(%rbp), %rsi
movl $0x2, %edx
callq 0xa480
jmp 0xabb5
jmp 0xabb7
jmp 0xabb9
jmp 0xac43
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x5ba0(%rbp)
movq -0x5ba0(%rbp), %rsi
callq 0x3d4c0
movl %eax, -0x5bf0(%rbp)
jmp 0xabe1
movl -0x5bf0(%rbp), %eax
movl %eax, -0x5b94(%rbp)
cmpl $0x0, -0x5b94(%rbp)
jne 0xac1a
movq -0x5bc0(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x5ba8(%rbp)
movq -0x5ba8(%rbp), %rsi
xorl %edx, %edx
callq 0xa480
jmp 0xac18
jmp 0xac41
movq -0x5bc0(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x5bb0(%rbp)
movq -0x5bb0(%rbp), %rsi
movl $0x1, %edx
callq 0xa480
jmp 0xac3f
jmp 0xac41
jmp 0xac43
jmp 0xac45
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xaa19
jmp 0xac55
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xa9e4
movb $0x1, -0x11(%rbp)
testb $0x1, -0x11(%rbp)
jne 0xac79
movq -0x5bc0(%rbp), %rdi
callq 0xb7f0
movq -0x5bb8(%rbp), %rax
addq $0xb720, %rsp           # imm = 0xB720
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x3370
nopw %cs:(%rax,%rax)