// File: /workspace/github_cpp_makefile_star2_O2/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, %ebx
movq %rsi, %r14
movl %edi, 0xc(%rsp)
leaq 0x68(%rsp), %r15
movq %r15, %rdi
callq 0x5a30
cmpq $0x0, 0x28(%r15)
je 0x7409
testb %bl, %bl
je 0x7411
cmpl $0x0, 0xc(%rsp)
je 0x7611
movq %r14, 0x30(%rsp)
leaq 0x40(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x70(%rsp), %rax
movq 0x10(%rax), %r13
leaq 0x18(%rsp), %r15
leaq 0x38(%rsp), %r12
leaq 0x14(%rsp), %rbp
leaq 0x70(%rsp), %rax
cmpq %rax, %r13
je 0x7588
movl 0x20(%r13), %eax
movl %eax, 0x14(%rsp)
leaq 0x28(%r13), %rsi
movq %r15, %rdi
callq 0x7b7a
movq 0x20(%rsp), %rax
subq 0x18(%rsp), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
movq %rax, %rbx
testl %ebx, %ebx
jle 0x73f1
movq %r12, %rdi
movq %rbp, %rsi
callq 0x39ec
andl $0x0, (%rax)
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
imulq $0x28, %rbx, %rbx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x73f1
movq 0x18(%rsp), %rax
leaq (%rax,%r14), %rdi
addq $0x10, %rdi
callq 0x7ce2
movss %xmm0, 0x8(%rsp)
movq 0x18(%rsp), %rax
movss (%rax,%r14), %xmm0
movss %xmm0, 0x10(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x39ec
movss 0x8(%rsp), %xmm0
mulss 0x10(%rsp), %xmm0
addss (%rax), %xmm0
movss %xmm0, (%rax)
addq $0x28, %r14
jmp 0x739e
movq %r15, %rdi
callq 0x3af6
movq %r13, %rdi
callq 0x2130
movq %rax, %r13
jmp 0x7348
xorps %xmm0, %xmm0
jmp 0x761d
leaq 0x40(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x70(%rsp), %rax
movq 0x10(%rax), %rcx
leaq 0x18(%rsp), %rbx
leaq 0x38(%rsp), %r13
leaq 0x14(%rsp), %rbp
leaq 0xb8(%rsp), %r15
pushq $0x1
popq %r12
leaq 0x70(%rsp), %rax
cmpq %rax, %rcx
je 0x753e
movl 0x20(%rcx), %eax
movl %eax, 0x14(%rsp)
movq %rcx, 0x30(%rsp)
leaq 0x28(%rcx), %rsi
movq %rbx, %rdi
callq 0x7b7a
movq 0x20(%rsp), %rax
subq 0x18(%rsp), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
movq %rax, %rbx
testl %ebx, %ebx
jle 0x751f
movq %r13, %rdi
movq %rbp, %rsi
callq 0x39ec
andl $0x0, (%rax)
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
imulq $0x28, %rbx, %rbx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x751f
movq 0x18(%rsp), %rax
leaq (%rax,%r14), %rsi
addq $0x8, %rsi
movq %r15, %rdi
callq 0x29e8
movl 0xc(%rsp), %edi
movq %r15, %rsi
movl %r12d, %edx
callq 0x72cc
movss %xmm0, 0x8(%rsp)
movq 0x18(%rsp), %rax
movss (%rax,%r14), %xmm0
movss %xmm0, 0x10(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x39ec
movss 0x8(%rsp), %xmm0
mulss 0x10(%rsp), %xmm0
addss (%rax), %xmm0
movss %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
callq 0x2abc
addq $0x28, %r14
jmp 0x74ad
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x3af6
movq 0x30(%rsp), %rdi
callq 0x2130
movq %rax, %rcx
jmp 0x744f
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movq 0x50(%rsp), %rax
leaq 0x40(%rsp), %rbx
cmpq %rbx, %rax
je 0x7571
movss 0x8(%rsp), %xmm0
addss 0x24(%rax), %xmm0
movss %xmm0, 0x8(%rsp)
movq %rax, %rdi
callq 0x2130
jmp 0x7551
movq 0x60(%rsp), %rax
testq %rax, %rax
movss 0x8(%rsp), %xmm1
js 0x75f0
cvtsi2ss %rax, %xmm0
jmp 0x7605
movss 0xb48(%rip), %xmm0      # 0x80d8
xorl %ebx, %ebx
movq 0x50(%rsp), %rax
movq 0x30(%rsp), %r14
leaq 0x40(%rsp), %r15
cmpq %r15, %rax
je 0x75cd
movss 0x24(%rax), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x75b3
movl 0x20(%rax), %ebx
maxss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
movq %rax, %rdi
callq 0x2130
movss 0x8(%rsp), %xmm0
jmp 0x75a1
cmpl $0x3, %ebx
ja 0x7647
movl %ebx, %eax
leaq 0x1041(%rip), %rcx      # 0x861c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
xorl %esi, %esi
callq 0x5944
jmp 0x7647
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
jmp 0x767f
addq $0x8, %r14
movq %r14, %rdi
callq 0x7ce2
movss %xmm0, 0x8(%rsp)
jmp 0x7689
movq %r14, %rdi
xorl %esi, %esi
callq 0x59e0
jmp 0x7647
movq %r14, %rdi
xorl %esi, %esi
callq 0x5a08
jmp 0x7647
movq %r14, %rdi
xorl %esi, %esi
callq 0x59b8
leaq 0x98(%rsp), %rdi
movq %r14, %rsi
callq 0x29e8
movl 0xc(%rsp), %edi
decl %edi
leaq 0x98(%rsp), %rsi
xorl %edx, %edx
callq 0x72cc
movss %xmm0, 0x8(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x2abc
leaq 0x38(%rsp), %rdi
callq 0x3a3a
leaq 0x68(%rsp), %rdi
callq 0x3a92
movss 0x8(%rsp), %xmm0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x76fa
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x2abc
jmp 0x76f0
jmp 0x76ca
jmp 0x76d1
jmp 0x76d1
jmp 0x76ca
movq %rax, %rbx
jmp 0x76f0
jmp 0x76d1
movq %rax, %rbx
jmp 0x76e6
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x2abc
leaq 0x18(%rsp), %rdi
callq 0x3af6
leaq 0x38(%rsp), %rdi
callq 0x3a3a
leaq 0x68(%rsp), %rdi
callq 0x3a92
movq %rbx, %rdi
callq 0x22c0