// File: /workspace/github_cpp_makefile_star2_O2/grogers0[P]dots_and_boxes/BruteForce.cpp
// Function: do_brute_force_one_level(Board const&, std::vector<std::pair<Edge, short>, std::allocator<std::pair<Edge, short>>>&, __gnu_cxx::__normal_iterator<Edge*, std::vector<Edge, std::allocator<Edge>>>, __gnu_cxx::__normal_iterator<Edge*, std::vector<Edge, std::allocator<Edge>>>, __gnu_cxx::__normal_iterator<Edge*, std::vector<Edge, std::allocator<Edge>>>)
void do_brute_force_one_level(const Board &oldboard,
        std::vector<std::pair<Edge, short>> &table,
        std::vector<Edge>::iterator first, std::vector<Edge>::iterator middle,
        std::vector<Edge>::iterator last)
{
    do {
        Board board(oldboard);

        unsigned long idx = 0;
        std::for_each(first, middle, [&] (Edge edge)
                {
                    idx |= (((unsigned long)1) << board.edge_index(edge));
                    board.move(0, edge);
                });
        board.reset_score();

        Edge bestMove;
        int bestScoreDiff = INT_MIN;

        std::for_each(board.edge_begin(), board.edge_end(), [&] (Edge edge)
                {
                    if (!board.is_move_valid(edge))
                        return;
                    int nextIdx = idx | (((unsigned long)1) << board.edge_index(edge));
                    assert(table[nextIdx].second != SHRT_MAX);
                    bool tookSquare = board.move(0, edge);
                    if (tookSquare) {
                        int nextScore = board.get_score(0) +
                            table[nextIdx].second;
                        if (nextScore > bestScoreDiff) {
                            bestScoreDiff = nextScore;
                            bestMove = edge;
                        }
                        
                    } else {
                        int nextScore = -table[nextIdx].second;
                        if (nextScore > bestScoreDiff) {
                            bestScoreDiff = nextScore;
                            bestMove = edge;
                        }
                    }
                    board.unmove(0, edge);
                });

        table[idx].first = bestMove;
        table[idx].second = bestScoreDiff;

        board.print(stdout);
        bestMove.print(stdout);
        printf("index: %lu, expect to win by: %d\n\n", idx, bestScoreDiff);
    } while (boost::next_combination(first, middle, last));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, 0x30(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, 0x38(%rsp)
leaq 0x40(%rsp), %rbx
movq 0x3f0e(%rip), %r14      # 0x7fb8
movq %rdx, 0x28(%rsp)
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x4448
xorl %ebp, %ebp
cmpq 0x20(%rsp), %r15
je 0x40f4
movl (%r15), %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x29e6
movl %eax, %r14d
movq %rbx, %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x2442
btsq %r14, %rbp
addq $0x4, %r15
movq 0x3ec6(%rip), %r14      # 0x7fb8
jmp 0x40be
movl 0x4c(%rsp), %eax
movl %eax, 0x48(%rsp)
movl 0x40(%rsp), %eax
movl 0x44(%rsp), %ecx
shlq $0x20, %rax
movq %rax, 0x10(%rsp)
movl %ecx, 0x18(%rsp)
movl $0x80000000, %r12d      # imm = 0x80000000
xorl %eax, %eax
testb $0x1, %al
sete %cl
cmpw $-0x2, %ax
setae %al
testb %cl, %al
jne 0x41d5
movl 0x10(%rsp), %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x2564
testb %al, %al
je 0x41ba
movq %rbx, %rdi
movl %r14d, %esi
callq 0x29e6
pushq $0x1
popq %rdx
movl %eax, %ecx
shlq %cl, %rdx
movl %ebp, %eax
orl %edx, %eax
movslq %eax, %r15
movq (%r13), %rax
cmpw $0x7fff, 0x4(%rax,%r15,8) # imm = 0x7FFF
je 0x4255
movq %rbx, %rdi
xorl %esi, %esi
movl %r14d, %edx
callq 0x2442
testb %al, %al
je 0x4193
movq (%r13), %rax
movswl 0x4(%rax,%r15,8), %eax
addl 0x48(%rsp), %eax
cmpl %r12d, %eax
jg 0x41a4
jmp 0x41ad
movq (%r13), %rax
movswl 0x4(%rax,%r15,8), %eax
negl %eax
cmpl %eax, %r12d
jge 0x41ad
movw %r14w, 0xc(%rsp)
movl %eax, %r12d
movq %rbx, %rdi
xorl %esi, %esi
movl %r14d, %edx
callq 0x25ce
leaq 0x10(%rsp), %rdi
callq 0x3eb2
movzwl 0x10(%rsp), %eax
movq 0x3de8(%rip), %r14      # 0x7fb8
jmp 0x4119
movq (%r13), %rax
movzwl 0xc(%rsp), %ecx
movw %cx, (%rax,%rbp,8)
movq (%r13), %rax
movw %r12w, 0x4(%rax,%rbp,8)
movq (%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3036
movq (%r14), %rsi
leaq 0xc(%rsp), %rdi
callq 0x2d08
leaq 0x1185(%rip), %rdi      # 0x5392
movq %rbp, %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2030
leaq 0x50(%rsp), %rdi
callq 0x2b04
movq 0x28(%rsp), %r15
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x4476
testb %al, %al
jne 0x40af
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1159(%rip), %rdi      # 0x53b5
leaq 0x1174(%rip), %rsi      # 0x53d7
leaq 0x117c(%rip), %rcx      # 0x53e6
pushq $0x28
popq %rdx
callq 0x20f0
jmp 0x4276
jmp 0x4276
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2b04
movq %rbx, %rdi
callq 0x22a0