// File: /workspace/github_cpp_makefile_star2_O3/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 0x56df
movq 0x138e7(%rip), %rcx     # 0x18fb8
cmpb $0x0, (%rcx)
je 0x56db
incl 0x8(%rax)
jmp 0x56df
lock
incl 0x8(%rax)
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x54b1
movq %r14, 0x38(%rsp)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x5702
callq 0x3ce2
movq 0x40(%rsp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x531b
cmpq $0x0, 0x8(%rsp)
je 0x5784
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
movq %rax, %r15
movq %rbp, %r14
testq %rax, %rax
je 0x577a
movq %r14, 0x50(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5b2c
cmpq %r13, %rax
je 0x5768
movl 0x28(%rax), %eax
leaq 0x58(%rsp), %rdi
cmpl $0x1, %eax
je 0x5760
testl %eax, %eax
jne 0x5768
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x6c42
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r14
decq %r15
jne 0x5732
incq %rbp
cmpq 0x8(%rsp), %rbp
jne 0x5722
movq %rbx, %rdi
callq 0x5a8c
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
movq 0x38(%rsp), %r14
je 0x57a0
callq 0x3ce2
incl %r14d
cmpl $0x3e8, %r14d           # imm = 0x3E8
movq 0x28(%rsp), %r15
jne 0x56af
movq %r15, %rdi
movq 0x8(%rsp), %rbx
movl %ebx, %esi
movq 0x10(%rsp), %rdx
callq 0xf75a
testq %rbx, %rbx
je 0x58b2
xorl %r12d, %r12d
leaq 0xe8(%rsp), %rbx
movq 0x10(%rsp), %rax
movq %rax, %r13
movq %r12, %rbp
testq %rax, %rax
je 0x58a4
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xf80a
testb %al, %al
jne 0x588e
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1017e
movl %eax, %r14d
testl %eax, %eax
je 0x5855
leaq 0x88(%rsp), %rdi
movq %rbp, %rsi
callq 0x6c82
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0xeb48(%rip), %xmm0     # 0x14380
movss 0xeb44(%rip), %xmm1     # 0x14384
ucomiss %xmm0, %xmm1
jbe 0x588e
movq %r15, %rdi
movq %rbp, %rsi
movl %r14d, %edx
callq 0xf82e
jmp 0x588e
leaq 0x58(%rsp), %rdi
movq %rbp, %rsi
callq 0x6c82
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0xeb0c(%rip), %xmm0     # 0x14380
movss 0xeb08(%rip), %xmm1     # 0x14384
ucomiss %xmm0, %xmm1
jbe 0x588e
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0xf82e
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rbp
decq %r13
jne 0x57f1
incq %r12
cmpq 0x8(%rsp), %r12
jne 0x57dd
leaq 0x58(%rsp), %rdi
callq 0x5adc
leaq 0x88(%rsp), %rdi
callq 0x5adc
movq %r15, %rax
addq $0x5c18, %rsp           # imm = 0x5C18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x58fa
movq %rax, %rbx
jmp 0x5928
movq %rax, %rbx
movq 0x20(%rsp), %rdi
jmp 0x5914
movq %rax, %rbx
jmp 0x590f
jmp 0x58fa
jmp 0x58fa
jmp 0x58fa
movq %rax, %rbx
jmp 0x591e
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x5a8c
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x591e
callq 0x3ce2
leaq 0x58(%rsp), %rdi
callq 0x5adc
leaq 0x88(%rsp), %rdi
callq 0x5adc
movq %rbx, %rdi
callq 0x32b0
nop