// File: /workspace/github_cpp_makefile_star2_O1/NathanWine[P]2048-AI-Solvers/src/Expectimax.cpp
// Function: expectimaxScore(int, Game, bool)
float expectimaxScore(int depth, Game game, bool is_max) {
    std::map<int, weightedmoves> possibilities = game.computePossibilities();    
    if (possibilities.size() == 0) {
        return 0.0;
    }
    else if (is_max) {
        if (depth == 0) {
            return Heuristics::get_h_score(game.state);
        }
        else {
            std::map<int, float> scores;
            for (auto const& entry : possibilities) {
                int move = entry.first;                             // Direction of move
                weightedmoves weighted_subset = entry.second;       // Vector of (prob, game)
                int len = (int) weighted_subset.size();
                if (len > 0) {
                    scores[move] = 0.0;
                    for (int j = 0; j < len; ++j) {
                        scores[move] += Heuristics::get_h_score(weighted_subset[j].second.state) * weighted_subset[j].first;
                    }
                }
            }

            float max_score = -std::numeric_limits<float>::max();
            int max_choice = UP;
            for (auto const& score : scores) {      // Determine best move choice based on tabulated scores
                if (score.second > max_score) {
                    max_score = score.second;
                    max_choice = score.first;
                }
            }

            switch (max_choice) {
                case UP:
                    game.up(false);
                    break;
                case DOWN:
                    game.down(false);
                    break;
                case LEFT: 
                    game.left(false);
                    break;
                case RIGHT:
                    game.right(false);
            }

            return expectimaxScore(depth - 1, game, false);
        }
    }
    else {
        std::map<int, float> scores;
        for (auto const& entry : possibilities) {
            int move = entry.first;                             // Direction of move
            weightedmoves weighted_subset = entry.second;       // Vector of (prob, game)
            int len = (int) weighted_subset.size();
            if (len > 0) {
                scores[move] = 0.0;
                for (int j = 0; j < len; ++j) {
                    scores[move] += expectimaxScore(depth, weighted_subset[j].second, true) * weighted_subset[j].first;
                }
            }
        }
        float sum = 0.0;
        for (auto const& score : scores) {
            sum += score.second;
        }
        return sum / scores.size();
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movl %edi, 0xc(%rsp)
leaq 0x68(%rsp), %r15
movq %r15, %rdi
callq 0x6844
cmpq $0x0, 0x28(%r15)
je 0x8600
testb %bpl, %bpl
je 0x8608
cmpl $0x0, 0xc(%rsp)
je 0x87b0
movq %rbx, 0x30(%rsp)
leaq 0x40(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x70(%rsp), %rax
movq 0x10(%rax), %r15
cmpq %rax, %r15
je 0x8584
leaq 0x18(%rsp), %r12
leaq 0x38(%rsp), %r13
leaq 0x14(%rsp), %rbp
movl 0x20(%r15), %eax
movl %eax, 0x14(%rsp)
leaq 0x28(%r15), %rsi
movq %r12, %rdi
callq 0x8e6a
movq 0x20(%rsp), %r14
subq 0x18(%rsp), %r14
sarq $0x3, %r14
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %r14
testl %r14d, %r14d
jle 0x8563
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3e48
movl $0x0, (%rax)
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl $0x10, %ebx
movq 0x18(%rsp), %rdi
addq %rbx, %rdi
callq 0x9011
movss %xmm0, 0x8(%rsp)
movq 0x18(%rsp), %rax
movss -0x10(%rax,%rbx), %xmm0
movss %xmm0, 0x10(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3e48
movss 0x8(%rsp), %xmm0
mulss 0x10(%rsp), %xmm0
addss (%rax), %xmm0
movss %xmm0, (%rax)
addq $0x28, %rbx
decq %r14
jne 0x8517
movq %r12, %rdi
callq 0x3f54
movq %r15, %rdi
callq 0x2120
movq %rax, %r15
leaq 0x70(%rsp), %rax
cmpq %rax, %r15
jne 0x84c5
movq 0x50(%rsp), %rax
leaq 0x40(%rsp), %r15
cmpq %r15, %rax
movq 0x30(%rsp), %rbx
movl $0x0, %r14d
je 0x85d4
xorl %r14d, %r14d
movss 0x1b2f(%rip), %xmm0     # 0xa0d8
movss 0x24(%rax), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x85b7
movl 0x20(%rax), %r14d
maxss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
movq %rax, %rdi
callq 0x2120
movss 0x8(%rsp), %xmm0
cmpq %r15, %rax
jne 0x85a9
cmpl $0x3, %r14d
ja 0x87e6
movl %r14d, %eax
leaq 0x2034(%rip), %rcx      # 0xa61c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
xorl %esi, %esi
callq 0x673c
jmp 0x87e6
xorps %xmm0, %xmm0
jmp 0x87bc
leaq 0x40(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x70(%rsp), %rax
movq 0x10(%rax), %rcx
cmpq %rax, %rcx
je 0x874b
leaq 0xa0(%rsp), %r14
leaq 0x18(%rsp), %rbx
leaq 0x38(%rsp), %r13
leaq 0x14(%rsp), %rbp
leaq 0x98(%rsp), %r12
movl 0x20(%rcx), %eax
movl %eax, 0x14(%rsp)
movq %rcx, 0x30(%rsp)
leaq 0x28(%rcx), %rsi
movq %rbx, %rdi
callq 0x8e6a
movq 0x20(%rsp), %r15
subq 0x18(%rsp), %r15
sarq $0x3, %r15
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %r15
testl %r15d, %r15d
jle 0x8723
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3e48
movl $0x0, (%rax)
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x10, %ebx
movq 0x18(%rsp), %rsi
movl -0x8(%rsi,%rbx), %eax
addq %rbx, %rsi
movl %eax, 0x98(%rsp)
movq %r14, %rdi
callq 0x2d38
movl 0xc(%rsp), %edi
movq %r12, %rsi
movl $0x1, %edx
callq 0x843c
movss %xmm0, 0x8(%rsp)
movq 0x18(%rsp), %rax
movss -0x10(%rax,%rbx), %xmm0
movss %xmm0, 0x10(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3e48
movss 0x8(%rsp), %xmm0
mulss 0x10(%rsp), %xmm0
addss (%rax), %xmm0
movss %xmm0, (%rax)
movq %r14, %rdi
callq 0x2e18
addq $0x28, %rbx
decq %r15
jne 0x86b0
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x3f54
movq 0x30(%rsp), %rdi
callq 0x2120
movq %rax, %rcx
leaq 0x70(%rsp), %rax
cmpq %rax, %rcx
jne 0x8656
movq 0x50(%rsp), %rax
leaq 0x40(%rsp), %rbx
xorps %xmm1, %xmm1
cmpq %rbx, %rax
je 0x8778
addss 0x24(%rax), %xmm1
movss %xmm1, 0x8(%rsp)
movq %rax, %rdi
callq 0x2120
movss 0x8(%rsp), %xmm1
jmp 0x8758
movq 0x60(%rsp), %rax
testq %rax, %rax
js 0x878c
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x87a4
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
jmp 0x8826
addq $0x8, %rbx
movq %rbx, %rdi
callq 0x9011
movss %xmm0, 0x8(%rsp)
jmp 0x8830
movq %rbx, %rdi
xorl %esi, %esi
callq 0x67e4
jmp 0x87e6
movq %rbx, %rdi
xorl %esi, %esi
callq 0x680c
jmp 0x87e6
movq %rbx, %rdi
xorl %esi, %esi
callq 0x67bc
movl (%rbx), %eax
leaq 0xc0(%rsp), %r15
movl %eax, -0x8(%r15)
addq $0x8, %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2d38
movl 0xc(%rsp), %edi
decl %edi
leaq 0xb8(%rsp), %rsi
xorl %edx, %edx
callq 0x843c
movss %xmm0, 0x8(%rsp)
movq %r15, %rdi
callq 0x2e18
leaq 0x38(%rsp), %rdi
callq 0x3eac
leaq 0x68(%rsp), %rdi
callq 0x3efc
movss 0x8(%rsp), %xmm0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x8897
movq %rax, %rbx
movq %r15, %rdi
callq 0x2e18
jmp 0x888d
jmp 0x886c
jmp 0x8873
jmp 0x8873
jmp 0x886c
movq %rax, %rbx
jmp 0x888d
jmp 0x8873
movq %rax, %rbx
jmp 0x8883
movq %rax, %rbx
movq %r14, %rdi
callq 0x2e18
leaq 0x18(%rsp), %rdi
callq 0x3f54
leaq 0x38(%rsp), %rdi
callq 0x3eac
leaq 0x68(%rsp), %rdi
callq 0x3efc
movq %rbx, %rdi
callq 0x22b0