// File: /workspace/github_cpp_makefile_star2_O2/macfergus[P]baduk/cppsrc/baduk/scoring.cpp
// Function: baduk::collectBoundary(baduk::Board, baduk::Point)
Boundary collectBoundary(Board board, Point origin) {
    Boundary b;
    std::queue<Point> q;
    std::set<Point> visited;

    const auto check_and_enqueue = [&q, &visited](Point new_point) {
        if (visited.find(new_point) == visited.end()) {
            q.push(new_point);
        }
    };

    q.push(origin);
    while (!q.empty()) {
        const auto p = q.front();
        q.pop();
        if (visited.find(p) != visited.end()) {
            continue;
        }
        if (board.isEmpty(p)) {
            // Enqueue its neighbors.
            if (p.row() > 0) {
                check_and_enqueue(Point(p.row() - 1, p.col()));
            }
            if (p.row() < board.numRows() - 1) {
                check_and_enqueue(Point(p.row() + 1, p.col()));
            }
            if (p.col() > 0) {
                check_and_enqueue(Point(p.row(), p.col() - 1));
            }
            if (p.col() < board.numCols() - 1) {
                check_and_enqueue(Point(p.row(), p.col() + 1));
            }
        } else {
            const auto stone = board.at(p);
            if (stone == Stone::black) {
                b.includes_black = true;
            } else {
                // Not empty, not black, must be white
                b.includes_white = true;
            }
        }
        visited.insert(p);
    }

    return b;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
leaq 0x50(%rsp), %rbx
movq %rdi, (%rbx)
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x58b0
leaq 0x28(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq %r14, 0x10(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x60da
xorl %r13d, %r13d
leaq 0x20(%rsp), %r15
leaq 0x8(%rsp), %r12
xorl %r14d, %r14d
movq 0x68(%rsp), %rax
movzbl %r14b, %ebp
movzbl %r13b, %ebx
cmpq %rax, 0x88(%rsp)
je 0x5338
movq (%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x630e
movq %r15, %rdi
movq %r12, %rsi
callq 0x604c
leaq 0x28(%rsp), %rcx
cmpq %rcx, %rax
jne 0x5209
movq 0x8(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0xfa38
testb %al, %al
je 0x5286
movl 0x8(%rsp), %esi
testl %esi, %esi
je 0x52ab
decl %esi
movl 0xc(%rsp), %eax
shlq $0x20, %rax
orq %rax, %rsi
leaq 0x10(%rsp), %rdi
callq 0x538a
movl 0x8(%rsp), %eax
jmp 0x52ad
movq 0x8(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x10118
testl %eax, %eax
pushq $0x1
popq %rax
cmovel %eax, %ebx
cmovnel %eax, %ebp
movl %ebx, %r13d
movl %ebp, %r14d
jmp 0x5328
xorl %eax, %eax
leaq 0xe0(%rsp), %rcx
movl 0x8(%rcx), %ecx
decl %ecx
cmpl %ecx, %eax
jae 0x52d9
movl 0xc(%rsp), %ecx
shlq $0x20, %rcx
movl %eax, %eax
leaq (%rcx,%rax), %rsi
incq %rsi
leaq 0x10(%rsp), %rdi
callq 0x538a
movl 0xc(%rsp), %esi
testl %esi, %esi
je 0x52fe
movl 0x8(%rsp), %eax
decl %esi
shlq $0x20, %rsi
orq %rax, %rsi
leaq 0x10(%rsp), %rdi
callq 0x538a
movl 0xc(%rsp), %esi
jmp 0x5300
xorl %esi, %esi
leaq 0xe0(%rsp), %rax
movl 0xc(%rax), %eax
decl %eax
cmpl %eax, %esi
jae 0x5328
movl 0x8(%rsp), %eax
incl %esi
shlq $0x20, %rsi
orq %rax, %rsi
leaq 0x10(%rsp), %rdi
callq 0x538a
movq %r15, %rdi
movq %r12, %rsi
callq 0x6364
jmp 0x5209
leaq 0x20(%rsp), %rdi
callq 0x5910
leaq 0x58(%rsp), %rdi
callq 0x5968
shll $0x8, %ebp
orl %ebx, %ebp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x536b
jmp 0x536b
jmp 0x536b
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5910
leaq 0x58(%rsp), %rdi
callq 0x5968
movq %rbx, %rdi
callq 0x32b0