// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq %rdi, 0x80(%rsp)
leaq 0x10(%rdi), %rax
movq %rax, 0x78(%rsp)
leaq 0x20(%rsp), %rbx
movq %rdx, 0x68(%rsp)
movq 0x80(%rsp), %r12
movups (%r12), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x495e
movups 0x38(%r12), %xmm0
movups %xmm0, 0x58(%rsp)
xorl %r13d, %r13d
cmpq %r14, %r15
je 0x447c
movl (%r15), %edx
movswl %dx, %eax
testb $0x1, %dl
jne 0x4448
movl %eax, %ecx
sarl $0x8, %ecx
imull 0x20(%rsp), %ecx
movsbl %al, %ebp
sarl %ebp
jmp 0x4466
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 0x2594
btsq %rbp, %r13
addq $0x4, %r15
jmp 0x4427
movl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
shlq $0x20, %rax
movq %rax, 0x10(%rsp)
movl %ecx, 0x18(%rsp)
movl $0x80000000, %r15d      # imm = 0x80000000
xorl %eax, %eax
testb %al, %al
jne 0x4593
movl 0x10(%rsp), %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x271c
testb %al, %al
je 0x4572
testb $0x1, %bpl
jne 0x44d8
movswl %bp, %eax
sarl $0x8, %eax
imull 0x20(%rsp), %eax
movsbl %bpl, %ecx
sarl %ecx
jmp 0x44f8
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %edx
incl %edx
imull %eax, %edx
movswl %bp, %esi
movsbl %sil, %ecx
sarl $0x8, %esi
incl %eax
imull %esi, %eax
sarl %ecx
addl %edx, %ecx
addl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movl %r13d, %ecx
orl %eax, %ecx
movslq %ecx, %r12
movq 0x8(%rsp), %rax
movq (%rax), %rax
cmpw $0x7fff, 0x4(%rax,%r12,8) # imm = 0x7FFF
je 0x4647
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x2594
testb %al, %al
je 0x4549
movq 0x8(%rsp), %rax
movq (%rax), %rax
movswl 0x4(%rax,%r12,8), %eax
addl 0x28(%rsp), %eax
cmpl %r15d, %eax
jg 0x455e
jmp 0x4566
movq 0x8(%rsp), %rax
movq (%rax), %rax
movswl 0x4(%rax,%r12,8), %eax
negl %eax
cmpl %eax, %r15d
jge 0x4566
movw %bp, 0x4(%rsp)
movl %eax, %r15d
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x27e4
leaq 0x10(%rsp), %rdi
callq 0x4210
movzwl 0x10(%rsp), %eax
cmpw $-0x2, %ax
setb %cl
orb %al, %cl
testb $0x1, %cl
jne 0x44a9
movq 0x8(%rsp), %rdx
movq (%rdx), %rax
movzwl 0x4(%rsp), %ecx
movw %cx, (%rax,%r13,8)
movq (%rdx), %rax
movw %r15w, 0x4(%rax,%r13,8)
movq 0x4a03(%rip), %r12      # 0x8fb8
movq (%r12), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3260
movq (%r12), %rsi
leaq 0x4(%rsp), %rdi
callq 0x2e54
leaq 0x1dd8(%rip), %rdi      # 0x63b0
movq %r13, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2030
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4615
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %r14, %rdx
movq 0x70(%rsp), %rcx
callq 0x49ef
testb %al, %al
jne 0x43f8
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d85(%rip), %rdi      # 0x63d3
leaq 0x1da0(%rip), %rsi      # 0x63f5
leaq 0x1da8(%rip), %rcx      # 0x6404
movl $0x28, %edx
callq 0x20e0
jmp 0x466a
jmp 0x466a
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x469d
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq %rbx, %rdi
callq 0x2270