// File: /workspace/github_cpp_makefile_star2_O1/macfergus[P]baduk/cppsrc/baduk/scoring.cpp
// Function: baduk::removeDeadStones(std::shared_ptr<baduk::GameState const>)
Board removeDeadStones(std::shared_ptr<const GameState> game) {
    const unsigned int num_rounds = 1000;
    const auto orig_board = game->board();
    const auto num_rows = orig_board.numRows();
    const auto num_cols = orig_board.numCols();
    BoardCounter b_count(num_rows, num_cols);
    BoardCounter w_count(num_rows, num_cols);
    for (unsigned int num_done = 0; num_done < num_rounds; ++num_done) {
        // Randomly complete the game
        const auto final_state = completeGame(game);
        // Score the result
        const auto tmap = evaluateTerritory(final_state->board());
        // Count up the status
        for (unsigned int r = 0; r < num_rows; ++r) {
            for (unsigned int c = 0; c < num_cols; ++c) {
                const Point p(r, c);
                const auto status = tmap.at(p);
                if (status == PointStatus::black) {
                    b_count.increment(p);
                } else if (status == PointStatus::white) {
                    w_count.increment(p);
                }
            }
        }
    }

    // Remove dead stones.
    Board cleaned_board(num_rows, num_cols);
    for (unsigned int r = 0; r < num_rows; ++r) {
        for (unsigned int c = 0; c < num_cols; ++c) {
            const Point p(r, c);
            if (orig_board.isEmpty(p)) {
                continue;
            }
            const auto orig_stone = orig_board.at(p);
            if (orig_stone == Stone::black) {
                const double p_white = w_count.get(p) / float(num_rounds);
                if (p_white < DEAD_THRESHOLD) {
                    cleaned_board.place(p, orig_stone);
                }
            } else {
                const double p_black = b_count.get(p) / float(num_rounds);
                if (p_black < DEAD_THRESHOLD) {
                    cleaned_board.place(p, orig_stone);
                }
            }
        }
    }
    return cleaned_board;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5c18, %rsp           # imm = 0x5C18
movq %rdi, %r15
movq %rsi, 0x30(%rsp)
movq (%rsi), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
leaq 0xe8(%rsp), %rbx
movl $0x5b30, %edx           # imm = 0x5B30
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3150
movl 0x8(%rbx), %r14d
movl 0xc(%rbx), %ebx
leaq 0x88(%rsp), %rdi
movl %r14d, %esi
movl %ebx, %edx
callq 0x6c24
leaq 0x58(%rsp), %rdi
movq %r14, 0x8(%rsp)
movl %r14d, %esi
movq %rbx, 0x10(%rsp)
movl %ebx, %edx
callq 0x6c24
xorl %r14d, %r14d
leaq 0xc0(%rsp), %r13
leaq 0xb8(%rsp), %rbx
leaq 0x50(%rsp), %r12
movq %r15, 0x28(%rsp)
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rcx), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x56ea
movq 0x138dc(%rip), %rcx     # 0x18fb8
cmpb $0x0, (%rcx)
je 0x56e6
incl 0x8(%rax)
jmp 0x56ea
lock
incl 0x8(%rax)
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x54c6
movq %r14, 0x38(%rsp)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x570d
callq 0x3d0a
movq 0x40(%rsp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x532a
cmpq $0x0, 0x8(%rsp)
je 0x5797
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq %rax, %rbp
movq %r15, %r14
testq %rax, %rax
je 0x578d
movq %r14, 0x50(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5b2a
cmpq %r13, %rax
je 0x5758
movl 0x28(%rax), %eax
jmp 0x575d
movl $0x2, %eax
leaq 0x58(%rsp), %rdi
cmpl $0x1, %eax
je 0x5773
testl %eax, %eax
jne 0x577b
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x6c42
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r14
decq %rbp
jne 0x573e
incq %r15
cmpq 0x8(%rsp), %r15
jne 0x572e
movq %rbx, %rdi
callq 0x5a8a
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
movq 0x38(%rsp), %r14
je 0x57b3
callq 0x3d0a
incl %r14d
cmpl $0x3e8, %r14d           # imm = 0x3E8
movq 0x28(%rsp), %r15
jne 0x56ba
movq %r15, %rdi
movq 0x8(%rsp), %rbx
movl %ebx, %esi
movq 0x10(%rsp), %rdx
callq 0xfd82
testq %rbx, %rbx
je 0x58c5
xorl %r12d, %r12d
leaq 0xe8(%rsp), %rbx
movq 0x10(%rsp), %rax
movq %rax, %r13
movq %r12, %rbp
testq %rax, %rax
je 0x58b7
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xfe32
testb %al, %al
jne 0x58a1
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x10848
movl %eax, %r14d
testl %eax, %eax
je 0x5868
leaq 0x88(%rsp), %rdi
movq %rbp, %rsi
callq 0x6c82
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0xeb35(%rip), %xmm0     # 0x14380
movss 0xeb31(%rip), %xmm1     # 0x14384
ucomiss %xmm0, %xmm1
jbe 0x58a1
movq %r15, %rdi
movq %rbp, %rsi
movl %r14d, %edx
callq 0xfe56
jmp 0x58a1
leaq 0x58(%rsp), %rdi
movq %rbp, %rsi
callq 0x6c82
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0xeaf9(%rip), %xmm0     # 0x14380
movss 0xeaf5(%rip), %xmm1     # 0x14384
ucomiss %xmm0, %xmm1
jbe 0x58a1
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0xfe56
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rbp
decq %r13
jne 0x5804
incq %r12
cmpq 0x8(%rsp), %r12
jne 0x57f0
leaq 0x58(%rsp), %rdi
callq 0x5ada
leaq 0x88(%rsp), %rdi
callq 0x5ada
movq %r15, %rax
addq $0x5c18, %rsp           # imm = 0x5C18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x590d
movq %rax, %rbx
jmp 0x593b
movq %rax, %rbx
movq 0x20(%rsp), %rdi
jmp 0x5927
movq %rax, %rbx
jmp 0x5922
jmp 0x590d
jmp 0x590d
jmp 0x590d
movq %rax, %rbx
jmp 0x5931
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x5a8a
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x5931
callq 0x3d0a
leaq 0x58(%rsp), %rdi
callq 0x5ada
leaq 0x88(%rsp), %rdi
callq 0x5ada
movq %rbx, %rdi
callq 0x32b0