// File: /workspace/github_cpp_makefile_star2_O3/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, 0x70(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, 0x80(%rsp)
leaq 0x10(%rdi), %rax
movq %rax, 0x78(%rsp)
leaq 0x20(%rsp), %rbx
movq %rdx, 0x68(%rsp)
movq %rcx, 0x18(%rsp)
movq 0x80(%rsp), %r13
movups (%r13), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x492c
movups 0x38(%r13), %xmm0
movups %xmm0, 0x58(%rsp)
xorl %r13d, %r13d
cmpq %r14, %r15
je 0x44b7
movl (%r15), %edx
movswl %dx, %eax
testb $0x1, %dl
jne 0x447e
movl %eax, %ecx
sarl $0x8, %ecx
imull 0x20(%rsp), %ecx
movsbl %al, %ebp
sarl %ebp
jmp 0x449c
movl 0x20(%rsp), %ecx
movl 0x24(%rsp), %esi
incl %esi
imull %ecx, %esi
movl %eax, %edi
sarl $0x8, %edi
incl %ecx
imull %edi, %ecx
movsbl %al, %ebp
sarl %ebp
addl %esi, %ebp
addl %ecx, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2574
btsq %rbp, %r13
addq $0x4, %r15
cmpq 0x18(%rsp), %r15
jne 0x4462
movl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
shlq $0x20, %rax
movq %rax, 0x8(%rsp)
movl %ecx, 0x10(%rsp)
movl $0x80000000, %r15d      # imm = 0x80000000
movl 0x8(%rsp), %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2730
testb %al, %al
je 0x4596
movswl %bp, %eax
testb $0x1, %bpl
jne 0x450a
movl %eax, %edx
sarl $0x8, %edx
imull 0x20(%rsp), %edx
movsbl %al, %ecx
sarl %ecx
jmp 0x4528
movl 0x20(%rsp), %edx
movl 0x24(%rsp), %esi
incl %esi
imull %edx, %esi
movl %eax, %ecx
sarl $0x8, %ecx
incl %edx
imull %ecx, %edx
movsbl %al, %ecx
sarl %ecx
addl %esi, %ecx
addl %edx, %ecx
movl $0x1, %eax
shlq %cl, %rax
movl %r13d, %ecx
orl %eax, %ecx
movslq %ecx, %r14
movq (%r12), %rax
cmpw $0x7fff, 0x4(%rax,%r14,8) # imm = 0x7FFF
je 0x4652
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x2574
testb %al, %al
je 0x4571
movq (%r12), %rax
movswl 0x4(%rax,%r14,8), %eax
addl 0x28(%rsp), %eax
cmpl %r15d, %eax
jg 0x4582
jmp 0x458a
movq (%r12), %rax
movswl 0x4(%rax,%r14,8), %eax
negl %eax
cmpl %eax, %r15d
jge 0x458a
movw %bp, 0x4(%rsp)
movl %eax, %r15d
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x27f2
leaq 0x8(%rsp), %rdi
callq 0x4244
movzwl 0x8(%rsp), %eax
cmpw $-0x2, %ax
setb %cl
orb %al, %cl
testb $0x1, %cl
jne 0x44da
movq (%r12), %rax
movzwl 0x4(%rsp), %ecx
movw %cx, (%rax,%r13,8)
movq (%r12), %rax
movw %r15w, 0x4(%rax,%r13,8)
movq 0x49e2(%rip), %r14      # 0x8fb8
movq (%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x329e
movq (%r14), %rsi
leaq 0x4(%rsp), %rdi
callq 0x2e8c
leaq 0x1d9b(%rip), %rdi      # 0x6392
movq %r13, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2030
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x461b
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
movq 0x68(%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
movq %r14, %rdx
movq 0x70(%rsp), %rcx
callq 0x49bd
testb %al, %al
jne 0x442f
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d5c(%rip), %rdi      # 0x63b5
leaq 0x1d77(%rip), %rsi      # 0x63d7
leaq 0x1d7f(%rip), %rcx      # 0x63e6
movl $0x28, %edx
callq 0x20e0
jmp 0x4675
jmp 0x4675
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x468f
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x2270