// File: /workspace/github_cpp_makefile_star2_O2/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, 0x8(%rsp)
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 0x3140
movl 0x8(%rbx), %r14d
movl 0xc(%rbx), %ebx
leaq 0x58(%rsp), %rdi
movl %r14d, %esi
movl %ebx, %edx
callq 0x6cf0
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movq %r14, 0x20(%rsp)
movl %r14d, %esi
movq %rbx, 0x18(%rsp)
movl %ebx, %edx
callq 0x6cf0
movabsq $0x100000000, %r14      # imm = 0x100000000
xorl %ebx, %ebx
leaq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0xb8(%rsp), %r13
cmpl $0x3e8, %ebx            # imm = 0x3E8
je 0x56d4
leaq 0x48(%rsp), %r15
movq %r15, %rdi
movq 0x30(%rsp), %rsi
callq 0x5a18
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x54e9
movq %rbx, 0x10(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x3c58
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x53c6
xorl %r15d, %r15d
movq %r15, %rbx
movq 0x18(%rsp), %rbp
cmpq 0x20(%rsp), %r15
je 0x56b6
subq $0x1, %rbp
jb 0x56b1
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5150
movq %r12, %rdi
cmpl $0x1, %eax
je 0x56a4
testl %eax, %eax
jne 0x56ac
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x6d0c
addq %r14, %rbx
jmp 0x5682
incq %r15
jmp 0x5673
movq %r13, %rdi
callq 0x59c0
leaq 0x40(%rsp), %rdi
callq 0x3c58
movq 0x10(%rsp), %rbx
incl %ebx
jmp 0x5620
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0xf9ce
xorl %eax, %eax
leaq 0xe8(%rsp), %r12
leaq 0x58(%rsp), %rbp
movq %rax, %r13
movq 0x18(%rsp), %r15
movq %rax, 0x10(%rsp)
cmpq 0x20(%rsp), %rax
je 0x57c5
subq $0x1, %r15
jb 0x57b8
movq %r12, %rdi
movq %r13, %rsi
callq 0xfa38
testb %al, %al
jne 0x57b0
movq %r12, %rdi
movq %r13, %rsi
callq 0x10118
movl %eax, %ebx
testl %eax, %eax
je 0x5775
movq %rbp, %rdi
movq %r13, %rsi
callq 0x6d4c
movl %eax, %eax
cvtsi2ss %rax, %xmm0
divss 0xec59(%rip), %xmm0     # 0x143b0
movss 0xec55(%rip), %xmm1     # 0x143b4
ucomiss %xmm0, %xmm1
jbe 0x57b0
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0xfa5c
jmp 0x57b0
leaq 0x88(%rsp), %rdi
movq %r13, %rsi
callq 0x6d4c
movl %eax, %eax
cvtsi2ss %rax, %xmm0
divss 0xec1c(%rip), %xmm0     # 0x143b0
movss 0xec18(%rip), %xmm1     # 0x143b4
ucomiss %xmm0, %xmm1
jbe 0x57b0
movq 0x8(%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xfa5c
addq %r14, %r13
jmp 0x570f
movq 0x10(%rsp), %rax
incq %rax
jmp 0x56f7
leaq 0x88(%rsp), %rdi
callq 0x5a2c
leaq 0x58(%rsp), %rdi
callq 0x5a2c
movq 0x8(%rsp), %rax
addq $0x5c18, %rsp           # imm = 0x5C18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5814
movq %rax, %rbx
jmp 0x5840
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
jmp 0x5829
movq %rax, %rbx
jmp 0x5829
jmp 0x5814
jmp 0x5814
jmp 0x5814
movq %rax, %rbx
jmp 0x5833
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x59c0
movq 0x28(%rsp), %rdi
callq 0x3c58
leaq 0x88(%rsp), %rdi
callq 0x5a2c
leaq 0x58(%rsp), %rdi
callq 0x5a2c
movq %rbx, %rdi
callq 0x32b0