// File: 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
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdx, -0x8(%rbp)
movq %rcx, -0x10(%rbp)
movq %r8, -0x18(%rbp)
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x70(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x95f0
movq -0x1a0(%rbp), %rax
movq $0x0, -0x78(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x80(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x88(%rbp)
leaq -0x78(%rbp), %rcx
movq %rcx, -0x98(%rbp)
movq %rax, -0x90(%rbp)
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0x90(%rbp), %rcx
callq 0x8d90
movq %rdx, -0x198(%rbp)
movq %rax, -0x190(%rbp)
jmp 0x8af5
movq -0x198(%rbp), %rax
movq -0x190(%rbp), %rcx
movq %rcx, -0xb8(%rbp)
movq %rax, -0xb0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x9650
jmp 0x8b1c
leaq -0xbc(%rbp), %rdi
callq 0x4eb0
jmp 0x8b2a
movl $0x80000000, -0xc0(%rbp) # imm = 0x80000000
leaq -0x70(%rbp), %rdi
callq 0x7a50
movl %edx, -0x1ac(%rbp)
movq %rax, -0x1a8(%rbp)
jmp 0x8b4c
movl -0x1ac(%rbp), %eax
movq -0x1a8(%rbp), %rcx
movq %rcx, -0xe0(%rbp)
movl %eax, -0xd8(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0xc8(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x7ab0
movl %edx, -0x1bc(%rbp)
movq %rax, -0x1b8(%rbp)
jmp 0x8b98
movl -0x1bc(%rbp), %eax
movq -0x1b8(%rbp), %rcx
movq %rcx, -0x100(%rbp)
movl %eax, -0xf8(%rbp)
movl -0xf8(%rbp), %eax
movl %eax, -0xe8(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0xc0(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0xbc(%rbp), %rax
movq %rax, -0x108(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0x158(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rsi
movl -0x158(%rbp), %edx
movl -0xe8(%rbp), %eax
movl %eax, -0x168(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rcx
movl -0x168(%rbp), %r8d
movq -0x108(%rbp), %rdi
movq %rsp, %rax
movq %rdi, 0x20(%rax)
movups -0x128(%rbp), %xmm0
movups -0x118(%rbp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x150(%rbp), %rdi
callq 0x8e00
jmp 0x8c89
movq -0x28(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x9670
movw -0xbc(%rbp), %cx
movw %cx, (%rax)
movw -0xc0(%rbp), %ax
movw %ax, -0x1be(%rbp)
movq -0x28(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x9670
movw -0x1be(%rbp), %cx
movw %cx, 0x4(%rax)
movq 0xa2eb(%rip), %rax      # 0x12fb8
movq (%rax), %rsi
leaq -0x70(%rbp), %rdi
xorl %edx, %edx
callq 0x4850
jmp 0x8cdd
movq 0xa2d4(%rip), %rax      # 0x12fb8
movq (%rax), %rsi
leaq -0xbc(%rbp), %rdi
callq 0x4320
jmp 0x8cf5
movq -0x78(%rbp), %rsi
movl -0xc0(%rbp), %edx
leaq 0x4725(%rip), %rdi      # 0xd42b
xorl %eax, %eax
callq 0x2030
jmp 0x8d0f
leaq -0x70(%rbp), %rdi
callq 0x4f20
movq -0x8(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x178(%rbp), %rdi
movq -0x180(%rbp), %rsi
movq -0x188(%rbp), %rdx
callq 0x9690
testb $0x1, %al
jne 0x8a7f
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x4f20
movq -0xa0(%rbp), %rdi
callq 0x2320
nopl (%rax,%rax)