// File: /workspace/github_cpp_makefile_star2_O3/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
movswl 0x2(%rsi), %eax
cmpl $0x160c, %eax           # imm = 0x160C
jg 0x56a2
cmpw $0x0, 0x6(%rbx)
jle 0x56a2
movq %rdi, %r15
leaq 0x138(%rdi), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x67b4
testq %rax, %rax
je 0x56d7
movq %r14, %rdi
movq %rbx, %rsi
callq 0x67b4
testq %rax, %rax
je 0x5a0e
movq %rax, %r14
leaq 0x20(%rax), %r12
testb %bpl, %bpl
jne 0x586e
movq 0x2d8(%r14), %rax
movq %rax, %xmm0
punpckldq 0x2b37(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2b3f(%rip), %xmm0     # 0x8100
movq 0x2e0(%r14), %rcx
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
movq %r14, %rsi
addq $0x30, %rsi
movsd 0x2aae(%rip), %xmm3     # 0x8098
movb $0x1d, %dl
xorl %edi, %edi
movq 0x2afa(%rip), %xmm1     # 0x80f0
movapd 0x2b02(%rip), %xmm2     # 0x8100
btq %rdi, %rcx
jae 0x5673
movq -0x8(%rsi), %r8
testq %r8, %r8
je 0x5631
movsd -0x10(%rsi), %xmm4
movq %r8, %xmm5
punpckldq %xmm1, %xmm5    # xmm5 = xmm5[0],xmm1[0],xmm5[1],xmm1[1]
subpd %xmm2, %xmm5
movapd %xmm5, %xmm6
unpckhpd %xmm5, %xmm6            # xmm6 = xmm6[1],xmm5[1]
addsd %xmm5, %xmm6
divsd %xmm6, %xmm4
jmp 0x5635
xorpd %xmm4, %xmm4
movsd (%rsi), %xmm5
mulsd %xmm0, %xmm5
incq %r8
movq %r8, %xmm6
punpckldq %xmm1, %xmm6    # xmm6 = xmm6[0],xmm1[0],xmm6[1],xmm1[1]
subpd %xmm2, %xmm6
movapd %xmm6, %xmm7
unpckhpd %xmm6, %xmm7            # xmm7 = xmm7[1],xmm6[1]
addsd %xmm6, %xmm7
divsd %xmm7, %xmm5
addsd %xmm4, %xmm5
ucomisd %xmm3, %xmm5
maxsd %xmm3, %xmm5
movzbl %dl, %edx
cmoval %edi, %edx
movapd %xmm5, %xmm3
incq %rdi
addq $0x18, %rsi
cmpq $0x1d, %rdi
jne 0x55fe
cmpb $0x1d, %dl
jne 0x579b
testb %bpl, %bpl
jne 0x5988
incq %rax
movq %rax, 0x2d8(%r14)
jmp 0x56b3
testb %bpl, %bpl
jne 0x581f
cwtl
cmpl $0x160d, %eax           # imm = 0x160D
jge 0x56b9
xorpd %xmm0, %xmm0
jmp 0x56cc
movswl 0x4(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x294c(%rip), %xmm0     # 0x8018
movsd %xmm0, 0x8(%rsp)
jmp 0x580a
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4880
movsd %xmm0, 0x8(%rsp)
testb %bpl, %bpl
je 0x580a
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x5c0c
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x68c9(%rip), %rdi      # 0xbfd8
callq 0x21b0
movq %rax, %rbx
leaq 0x2e76(%rip), %rsi      # 0x8594
movl $0x1a, %edx
movq %rax, %rdi
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rcx
movl $0xfffffefb, %edx       # imm = 0xFFFFFEFB
andl 0x18(%rbx,%rcx), %edx
orl $0x100, %edx            # imm = 0x100
movl %edx, 0x18(%rbx,%rcx)
movq -0x18(%rax), %rax
movq $0x3, 0x8(%rbx,%rax)
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x2220
leaq 0x2e2b(%rip), %rsi      # 0x8592
movl $0x1, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x578f
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x6(%rsp), %rdi
callq 0x549e
jmp 0x580a
movzbl %dl, %r13d
testb %bpl, %bpl
movq %r12, 0x30(%rsp)
jne 0x59b6
movl 0x10(%rbx), %eax
leaq 0x10(%rsp), %r12
movl %eax, 0x10(%r12)
movupd (%rbx), %xmm0
movapd %xmm0, (%r12)
movq %r12, %rdi
movl %r13d, %esi
callq 0x6052
movzbl %bpl, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x5536
leaq (,%r13,2), %rax
addq %r13, %rax
movsd %xmm0, 0x8(%rsp)
movq 0x30(%rsp), %rcx
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq 0x8(%rcx,%rax,8)
incq 0x2d8(%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 0x5c0c
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x679b(%rip), %rdi      # 0xbfd8
callq 0x21b0
movq %rax, %r14
leaq 0x2d2a(%rip), %rsi      # 0x8576
movl $0x10, %edx
movq %rax, %rdi
callq 0x21b0
cmpw $0x160d, 0x2(%rbx)      # imm = 0x160D
jge 0x590c
xorpd %xmm0, %xmm0
jmp 0x591c
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x5c0c
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x674c(%rip), %rdi      # 0xbfd8
callq 0x21b0
movq %rax, %r13
leaq 0x2c68(%rip), %rsi      # 0x8503
movl $0x1, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x649e
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r13, %rdi
callq 0x21b0
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x58e2
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x58fd
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x5(%rsp), %rdi
callq 0x549e
jmp 0x55a5
movswl 0x4(%rbx), %eax
cvtsi2sd %eax, %xmm0
divsd 0x26fc(%rip), %xmm0     # 0x8018
movq %r14, %rdi
callq 0x2220
movswl 0x2(%rbx), %ecx
xorl %edx, %edx
cmpl $0x160d, %ecx           # imm = 0x160D
setge %dl
cmpw $0x160d, %cx            # imm = 0x160D
leaq 0x2c48(%rip), %rcx      # 0x8587
leaq 0x2c71(%rip), %rsi      # 0x85b7
cmovgeq %rcx, %rsi
leaq 0xa(,%rdx,2), %rdx
movq %rax, %rdi
callq 0x21b0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5975
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x7(%rsp), %rdi
callq 0x549e
movzwl 0x2(%rbx), %eax
jmp 0x56ab
movq 0x6649(%rip), %rdi      # 0xbfd8
leaq 0x2c19(%rip), %rsi      # 0x85af
movl $0x12, %edx
callq 0x21b0
leaq 0x4(%rsp), %rdi
callq 0x549e
movq 0x2d8(%r14), %rax
jmp 0x5696
movq 0x661b(%rip), %rdi      # 0xbfd8
leaq 0x2bfe(%rip), %rsi      # 0x85c2
movl $0x10, %edx
callq 0x21b0
movl %r13d, %eax
shll $0x4, %eax
leaq 0x610d(%rip), %rcx      # 0xbae8
movq (%rax,%rcx), %rsi
movq 0x65f2(%rip), %rdi      # 0xbfd8
callq 0x2180
leaq 0x2ba0(%rip), %rsi      # 0x8592
movl $0x1, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x3(%rsp), %rdi
callq 0x549e
jmp 0x57ad
leaq 0x2bbe(%rip), %rdi      # 0x85d3
callq 0x21e0
movq %rax, %rbx
leaq 0x4(%rsp), %rdi
jmp 0x5acf
movq %rax, %rbx
jmp 0x5aa5
movq %rax, %rbx
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5a65
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x5a65
jmp 0x5a62
movq %rax, %rbx
jmp 0x5a80
movq %rax, %rbx
leaq 0x3(%rsp), %rdi
jmp 0x5acf
movq %rax, %rbx
jmp 0x5aca
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5a80
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x5(%rsp), %rdi
jmp 0x5acf
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5aa5
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x6(%rsp), %rdi
jmp 0x5acf
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5aca
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x7(%rsp), %rdi
callq 0x549e
movq %rbx, %rdi
callq 0x2290