// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movq %rdi, %rbx
movq %rdi, 0x60(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r14
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
callq 0x5e5c
leaq 0x70(%rsp), %rbp
movl $0x0, (%rbp)
movq %r15, 0x8(%rbp)
movq %rbp, 0x10(%rbp)
movq %rbp, 0x18(%rbp)
movq %r15, 0x20(%rbp)
movq 0x30(%r14), %rax
movq 0x40(%r14), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x504f
movq %rbx, (%rax)
movq 0x40(%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0x40(%rsp)
jmp 0x5063
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x603e
movq 0x40(%rsp), %rcx
movq 0x20(%rsp), %rax
cmpq %rax, %rcx
je 0x52c7
leaq 0xd0(%rsp), %rbx
xorl %r13d, %r13d
leaq 0x68(%rsp), %r14
leaq 0x8(%rsp), %r15
xorl %r12d, %r12d
movq (%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x30(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x50a5
addq $0x8, %rax
jmp 0x50d7
movq 0x28(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x3180
movq 0x38(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x38(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x20(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x5fd2
cmpq %rbp, %rax
jne 0x52aa
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0xf80a
testb %al, %al
je 0x514c
movl 0x8(%rsp), %eax
testl %eax, %eax
je 0x5175
decl %eax
movl 0xc(%rsp), %ecx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %rsp, %rsi
callq 0x5fd2
cmpq %rbp, %rax
jne 0x5186
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x5179
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x5186
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x1017e
testl %eax, %eax
movzbl %r12b, %r12d
movl $0x1, %eax
cmovel %eax, %r12d
movzbl %r13b, %r13d
cmovnel %eax, %r13d
jmp 0x529f
xorl %eax, %eax
jmp 0x518a
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x603e
movl 0x8(%rsp), %eax
movl 0x8(%rbx), %ecx
decl %ecx
cmpl %ecx, %eax
jae 0x51e6
movl 0xc(%rsp), %ecx
shlq $0x20, %rcx
movl %eax, %eax
addq %rcx, %rax
incq %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %rsp, %rsi
callq 0x5fd2
cmpq %rbp, %rax
jne 0x51e6
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x51d9
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x51e6
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x603e
movl 0xc(%rsp), %eax
testl %eax, %eax
je 0x5231
movl 0x8(%rsp), %ecx
decl %eax
shlq $0x20, %rax
orq %rcx, %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %rsp, %rsi
callq 0x5fd2
cmpq %rbp, %rax
jne 0x5242
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x5235
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x5242
xorl %eax, %eax
jmp 0x5246
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x603e
movl 0xc(%rsp), %eax
movl 0xc(%rbx), %ecx
decl %ecx
cmpl %ecx, %eax
jae 0x529f
movl 0x8(%rsp), %ecx
incl %eax
shlq $0x20, %rax
orq %rcx, %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %rsp, %rsi
callq 0x5fd2
cmpq %rbp, %rax
jne 0x529f
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x5292
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x529f
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x603e
movq %r14, %rdi
movq %r15, %rsi
callq 0x6252
movq 0x20(%rsp), %rax
cmpq %rax, 0x40(%rsp)
jne 0x5089
movzbl %r13b, %ebx
shll $0x8, %ebx
movzbl %r12b, %ebp
jmp 0x52cb
xorl %ebp, %ebp
xorl %ebx, %ebx
leaq 0x68(%rsp), %rdi
callq 0x59e4
leaq 0x10(%rsp), %rdi
callq 0x5a34
movzwl %bp, %eax
orl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x52fc
jmp 0x52fc
jmp 0x52fc
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x59e4
leaq 0x10(%rsp), %rdi
callq 0x5a34
movq %rbx, %rdi
callq 0x32b0