// File: /workspace/github_cpp_makefile_star2_O2/hantaoyin[P]ffxiv-crafting-mcts/ffxiv-crafting-mcts.C
// Function: UCT::simulateFromState(State const&, bool)
double simulateFromState(const State& s, bool track_simulation) {
    if (s.done()) {
      LOG(track_simulation) << s.DebugString() << ": (UCT)==> done " << s.score()
          << (s.successful() ? " <Finished>." : " <Failed>.");
      return s.score();
    }
    if (all_states.find(s) == all_states.end()) {
      double score = initState(s);
      LOG(track_simulation) << s.DebugString() << "\n(UCT)==> NN estimation = "
                            << std::scientific << std::setprecision(3) << score << ".";
      return score;
    }
    StateStatistics& stat = all_states.at(s);
    LOG(track_simulation) << s.DebugString() << "\n" << stat.DebugString();

    double ucb1_max = -std::numeric_limits<double>::infinity();
    Action ac_max = Action::NumActions;
    double nsq = std::sqrt((double)stat.total_count);
    for (size_t ac_id = 0; ac_id < TotalActionCount; ++ac_id) {
      Action ac = static_cast<Action>(ac_id);
      if(!stat.valid(ac)) continue;
      double u = stat.count(ac) == 0 ? 0. : stat.value(ac) / stat.count(ac);
      u += stat.prior(ac) * nsq / (1 + stat.count(ac));
      if (u > ucb1_max) {
        ucb1_max = u;
        ac_max = ac;
      }
    }
    if (ac_max == Action::NumActions) {
      LOG(track_simulation) << "(UCT)==> <Failed>.";
      ++stat.total_count;
      return 0.;
    } else {
      LOG(track_simulation) << "(UCT)==> picked " << Action2Name(ac_max) << ".";
    }

    State next(s);
    next.ExecuteAction(ac_max);
    double score = simulateFromState(next, track_simulation);
    stat.value(ac_max) += score;
    ++stat.count(ac_max);
    ++stat.total_count;
    return score;
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movzwl 0x2(%rsi), %eax
cmpw $0x160c, %ax            # imm = 0x160C
jg 0x535a
cmpw $0x0, 0x6(%rbx)
jle 0x535a
movq %rdi, %r15
leaq 0x138(%rdi), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x60b6
testq %rax, %rax
je 0x53a7
movq %r14, %rdi
movq %rbx, %rsi
callq 0x60e4
movq %rax, %r14
testb %bpl, %bpl
jne 0x54f3
movq 0x2b8(%r14), %rax
movq %rax, %xmm0
punpckldq 0x1e79(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1e81(%rip), %xmm0     # 0x7110
movq 0x2c0(%r14), %rcx
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
sqrtsd %xmm1, %xmm0
movq %r14, %rdx
addq $0x10, %rdx
movb $0x1d, %dil
movsd 0x1df0(%rip), %xmm1     # 0x70a8
xorl %esi, %esi
movq 0x1e3e(%rip), %xmm2     # 0x7100
movapd 0x1e46(%rip), %xmm3     # 0x7110
movzbl %dil, %r12d
cmpq $0x1d, %rsi
je 0x537b
btq %rsi, %rcx
jae 0x534e
movq -0x8(%rdx), %rdi
testq %rdi, %rdi
je 0x530b
movsd -0x10(%rdx), %xmm4
movq %rdi, %xmm5
punpckldq %xmm2, %xmm5    # xmm5 = xmm5[0],xmm2[0],xmm5[1],xmm2[1]
subpd %xmm3, %xmm5
movapd %xmm5, %xmm6
unpckhpd %xmm5, %xmm6            # xmm6 = xmm6[1],xmm5[1]
addsd %xmm5, %xmm6
divsd %xmm6, %xmm4
jmp 0x530f
xorpd %xmm4, %xmm4
movsd (%rdx), %xmm5
mulsd %xmm0, %xmm5
incq %rdi
movq %rdi, %xmm6
punpckldq %xmm2, %xmm6    # xmm6 = xmm6[0],xmm2[0],xmm6[1],xmm2[1]
subpd %xmm3, %xmm6
movapd %xmm6, %xmm7
unpckhpd %xmm6, %xmm7            # xmm7 = xmm7[1],xmm6[1]
addsd %xmm6, %xmm7
divsd %xmm7, %xmm5
addsd %xmm4, %xmm5
ucomisd %xmm1, %xmm5
cmoval %esi, %r12d
maxsd %xmm1, %xmm5
movapd %xmm5, %xmm1
movl %r12d, %edi
incq %rsi
addq $0x18, %rdx
jmp 0x52ca
testb %bpl, %bpl
jne 0x54b8
cmpw $0x160d, %ax            # imm = 0x160D
jl 0x5398
movswl 0x4(%rbx), %eax
cvtsi2sd %eax, %xmm0
divsd 0x1ca7(%rip), %xmm0     # 0x7020
jmp 0x539c
cmpb $0x1d, %dil
jne 0x5448
testb %bpl, %bpl
jne 0x55b5
incq %rax
movq %rax, 0x2b8(%r14)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x54a3
movq %r15, %rdi
movq %rbx, %rsi
callq 0x43ec
movsd %xmm0, 0x8(%rsp)
testb %bpl, %bpl
je 0x54a3
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x579e
movq 0x5c03(%rip), %rdi      # 0xafd8
leaq 0x10(%rsp), %rsi
callq 0x2140
leaq 0x2174(%rip), %rsi      # 0x755a
movq %rax, %rdi
callq 0x2170
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movl $0xfffffefb, %esi       # imm = 0xFFFFFEFB
andl 0x18(%rax,%rdx), %esi
orl $0x100, %esi            # imm = 0x100
movl %esi, 0x18(%rax,%rdx)
movq -0x18(%rcx), %rcx
movq $0x3, 0x8(%rax,%rcx)
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x2210
leaq 0x212e(%rip), %rsi      # 0x7558
movq %rax, %rdi
callq 0x2170
leaq 0x10(%rsp), %rdi
callq 0x2070
leaq 0x6(%rsp), %rdi
callq 0x5180
jmp 0x54a3
testb %bpl, %bpl
jne 0x55de
movl 0x10(%rbx), %eax
leaq 0x10(%rsp), %r13
movl %eax, 0x10(%r13)
movupd (%rbx), %xmm0
movapd %xmm0, (%r13)
movq %r13, %rdi
movl %r12d, %esi
callq 0x5b0c
movzbl %bpl, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x5212
imulq $0x18, %r12, %rax
movsd %xmm0, 0x8(%rsp)
addsd (%r14,%rax), %xmm0
movsd %xmm0, (%r14,%rax)
incq 0x8(%r14,%rax)
incq 0x2b8(%r14)
movsd 0x8(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x579e
movq 0x5b0c(%rip), %rdi      # 0xafd8
leaq 0x10(%rsp), %rsi
callq 0x2140
leaq 0x205f(%rip), %rsi      # 0x753c
movq %rax, %rdi
callq 0x2170
cmpw $0x160d, 0x2(%rbx)      # imm = 0x160D
jge 0x5560
xorpd %xmm0, %xmm0
jmp 0x5570
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x579e
movq 0x5ad1(%rip), %rdi      # 0xafd8
leaq 0x10(%rsp), %rsi
callq 0x2140
leaq 0x1fb1(%rip), %rsi      # 0x74c9
movq %rax, %rdi
callq 0x2170
movq %rax, %r12
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x5ec2
leaq 0x38(%rsp), %rsi
movq %r12, %rdi
callq 0x2140
leaq 0x38(%rsp), %rdi
callq 0x2070
leaq 0x10(%rsp), %rdi
callq 0x2070
leaq 0x5(%rsp), %rdi
callq 0x5180
jmp 0x5273
movswl 0x4(%rbx), %ecx
cvtsi2sd %ecx, %xmm0
divsd 0x1ab0(%rip), %xmm0     # 0x7020
movq %rax, %rdi
callq 0x2210
cmpw $0x160d, 0x2(%rbx)      # imm = 0x160D
leaq 0x1fc8(%rip), %rcx      # 0x754d
leaq 0x1ff1(%rip), %rsi      # 0x757d
cmovgeq %rcx, %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x10(%rsp), %rdi
callq 0x2070
leaq 0x7(%rsp), %rdi
callq 0x5180
movzwl 0x2(%rbx), %eax
jmp 0x5363
movq 0x5a1c(%rip), %rdi      # 0xafd8
leaq 0x1fb2(%rip), %rsi      # 0x7575
callq 0x2170
leaq 0x4(%rsp), %rdi
callq 0x5180
movq 0x2b8(%r14), %rax
jmp 0x538e
movq 0x59f3(%rip), %rdi      # 0xafd8
leaq 0x1f9c(%rip), %rsi      # 0x7588
callq 0x2170
movl %r12d, %ecx
shll $0x4, %ecx
leaq 0x54f2(%rip), %rdx      # 0xaaf0
movq (%rcx,%rdx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x1f47(%rip), %rsi      # 0x7558
movq %rax, %rdi
callq 0x2170
leaq 0x3(%rsp), %rdi
callq 0x5180
jmp 0x5451
movq %rax, %rbx
leaq 0x4(%rsp), %rdi
jmp 0x5696
movq %rax, %rbx
jmp 0x567d
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x2070
jmp 0x565f
jmp 0x565c
movq %rax, %rbx
jmp 0x5669
movq %rax, %rbx
leaq 0x3(%rsp), %rdi
jmp 0x5696
movq %rax, %rbx
jmp 0x5691
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x2070
leaq 0x5(%rsp), %rdi
jmp 0x5696
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x2070
leaq 0x6(%rsp), %rdi
jmp 0x5696
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x2070
leaq 0x7(%rsp), %rdi
callq 0x5180
movq %rbx, %rdi
callq 0x2270
nop