// File: /workspace/github_cpp_makefile_star2_O1/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, 0x60(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rbx
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5e46
leaq 0x70(%rsp), %rbp
movl $0x0, (%rbp)
movq %r14, 0x8(%rbp)
movq %rbp, 0x10(%rbp)
movq %rbp, 0x18(%rbp)
movq %r14, 0x20(%rbp)
movq 0x30(%rbx), %rax
movq 0x40(%rbx), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x506c
movq 0x60(%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x507b
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x602e
movq 0x20(%rsp), %rax
cmpq %rax, 0x40(%rsp)
je 0x52d6
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 0x50bf
addq $0x8, %rax
jmp 0x50f1
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 0x5fbc
cmpq %rbp, %rax
jne 0x52b9
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0xfe32
testb %al, %al
je 0x5166
movl 0x8(%rsp), %eax
testl %eax, %eax
je 0x519c
decl %eax
movl 0xc(%rsp), %ecx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %rsp, %rsi
callq 0x5fbc
cmpq %rbp, %rax
jne 0x519c
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x518f
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x519c
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x10848
testl %eax, %eax
movzbl %r12b, %r12d
movl $0x1, %eax
cmovel %eax, %r12d
movzbl %r13b, %r13d
cmovnel %eax, %r13d
jmp 0x52ae
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x602e
movl 0x8(%rsp), %eax
movl 0x8(%rbx), %ecx
decl %ecx
cmpl %ecx, %eax
jae 0x51f9
incl %eax
movl 0xc(%rsp), %ecx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %rsp, %rsi
callq 0x5fbc
cmpq %rbp, %rax
jne 0x51f9
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x51ec
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x51f9
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x602e
movl 0xc(%rsp), %eax
testl %eax, %eax
je 0x5251
movl 0x8(%rsp), %ecx
decl %eax
shlq $0x20, %rax
orq %rcx, %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %rsp, %rsi
callq 0x5fbc
cmpq %rbp, %rax
jne 0x5251
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x5244
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x5251
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x602e
movl 0xc(%rsp), %eax
movl 0xc(%rbx), %ecx
decl %ecx
cmpl %ecx, %eax
jae 0x52ae
movl 0x8(%rsp), %ecx
incl %eax
shlq $0x20, %rax
orq %rcx, %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %rsp, %rsi
callq 0x5fbc
cmpq %rbp, %rax
jne 0x52ae
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x52a1
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x52ae
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x602e
movq %r14, %rdi
movq %r15, %rsi
callq 0x6242
movq 0x20(%rsp), %rax
cmpq %rax, 0x40(%rsp)
jne 0x50a3
movzbl %r13b, %ebx
shll $0x8, %ebx
movzbl %r12b, %ebp
jmp 0x52da
xorl %ebp, %ebp
xorl %ebx, %ebx
leaq 0x68(%rsp), %rdi
callq 0x59ea
leaq 0x10(%rsp), %rdi
callq 0x5a3a
movzwl %bp, %eax
orl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x530b
jmp 0x530b
jmp 0x530b
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x59ea
leaq 0x10(%rsp), %rdi
callq 0x5a3a
movq %rbx, %rdi
callq 0x32b0