// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movl %edx, %ebx
movq %rsi, %r14
cmpw $0x160c, 0x2(%rsi)      # imm = 0x160C
jg 0x55f3
cmpw $0x0, 0x6(%r14)
jle 0x55f3
movq %rdi, %r12
leaq 0x138(%rdi), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x6834
testq %rax, %rax
je 0x5621
movq %r15, %rdi
movq %r14, %rsi
callq 0x6834
testq %rax, %rax
je 0x5a4f
movq %rax, %r15
leaq 0x20(%rax), %r13
testb %bl, %bl
jne 0x5899
movsd 0x2d8(%r15), %xmm1
unpcklps 0x2b28(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x2b30(%rip), %xmm1     # 0x8100
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x56f1
sqrtsd %xmm0, %xmm0
jmp 0x56f6
testb %bl, %bl
jne 0x5849
cmpw $0x160d, 0x2(%r14)      # imm = 0x160D
jl 0x57b9
movswl 0x4(%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x29fc(%rip), %xmm0     # 0x8018
jmp 0x57bd
movq %r12, %rdi
movq %r14, %rsi
callq 0x486c
movsd %xmm0, 0x10(%rsp)
testb %bl, %bl
je 0x57c3
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x5c6e
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x6980(%rip), %rdi      # 0xbfd8
callq 0x21b0
movq %rax, %r14
leaq 0x2f2d(%rip), %rsi      # 0x8594
movl $0x1a, %edx
movq %rax, %rdi
callq 0x21b0
movq (%r14), %rax
movq -0x18(%rax), %rcx
movl $0xfffffefb, %edx       # imm = 0xFFFFFEFB
andl 0x18(%r14,%rcx), %edx
orl $0x100, %edx            # imm = 0x100
movl %edx, 0x18(%r14,%rcx)
movq -0x18(%rax), %rax
movq $0x3, 0x8(%r14,%rax)
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x2220
leaq 0x2ee0(%rip), %rsi      # 0x8592
movl $0x1, %edx
movq %rax, %rdi
callq 0x21b0
testb %bl, %bl
je 0x57c3
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x56e2
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xe(%rsp), %rdi
callq 0x54b8
jmp 0x57c3
callq 0x2110
movq 0x2e0(%r15), %rcx
movq %r15, %rdx
addq $0x30, %rdx
movsd 0x298c(%rip), %xmm3     # 0x8098
movb $0x1d, %al
xorl %esi, %esi
movq 0x29d8(%rip), %xmm1     # 0x80f0
movapd 0x29e0(%rip), %xmm2     # 0x8100
btq %rsi, %rcx
jae 0x5795
movq -0x8(%rdx), %rdi
testq %rdi, %rdi
je 0x5753
movsd -0x10(%rdx), %xmm4
movq %rdi, %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 0x5757
xorpd %xmm4, %xmm4
movsd (%rdx), %xmm5
mulsd %xmm0, %xmm5
incq %rdi
movq %rdi, %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 %al, %eax
cmoval %esi, %eax
movapd %xmm5, %xmm3
incq %rsi
addq $0x18, %rdx
cmpq $0x1d, %rsi
jne 0x5720
cmpb $0x1d, %al
jne 0x57d8
testb %bl, %bl
jne 0x59c1
incq 0x2d8(%r15)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x38(%rsp)
movzbl %al, %ebp
testb %bl, %bl
jne 0x59f0
movl 0x10(%r14), %eax
leaq 0x60(%rsp), %r13
movl %eax, 0x10(%r13)
movupd (%r14), %xmm0
movapd %xmm0, (%r13)
movq %r13, %rdi
movl %ebp, %esi
callq 0x60b4
movzbl %bl, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x5550
leaq (,%rbp,2), %rax
addq %rbp, %rax
movsd %xmm0, 0x10(%rsp)
movq 0x38(%rsp), %rcx
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq 0x8(%rcx,%rax,8)
incq 0x2d8(%r15)
jmp 0x57c3
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x5c6e
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x6771(%rip), %rdi      # 0xbfd8
callq 0x21b0
movq %rax, %r15
leaq 0x2d00(%rip), %rsi      # 0x8576
movl $0x10, %edx
movq %rax, %rdi
callq 0x21b0
cmpw $0x160d, 0x2(%r14)      # imm = 0x160D
jge 0x593f
xorpd %xmm0, %xmm0
jmp 0x5950
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x5c6e
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x6721(%rip), %rdi      # 0xbfd8
callq 0x21b0
movq %rax, %rbp
leaq 0x2c3d(%rip), %rsi      # 0x8503
movl $0x1, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x6504
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %rbp, %rdi
callq 0x21b0
testb %bl, %bl
je 0x55b8
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5915
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5930
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xd(%rsp), %rdi
callq 0x54b8
jmp 0x55b8
movswl 0x4(%r14), %eax
cvtsi2sd %eax, %xmm0
divsd 0x26c8(%rip), %xmm0     # 0x8018
movq %r15, %rdi
callq 0x2220
movswl 0x2(%r14), %ecx
xorl %edx, %edx
cmpl $0x160d, %ecx           # imm = 0x160D
setge %dl
cmpw $0x160d, %cx            # imm = 0x160D
leaq 0x2c13(%rip), %rcx      # 0x8587
leaq 0x2c3c(%rip), %rsi      # 0x85b7
cmovgeq %rcx, %rsi
leaq 0xa(,%rdx,2), %rdx
movq %rax, %rdi
callq 0x21b0
testb %bl, %bl
je 0x55fb
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x59b2
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xf(%rsp), %rdi
callq 0x54b8
jmp 0x55fb
movq 0x6610(%rip), %rdi      # 0xbfd8
leaq 0x2be0(%rip), %rsi      # 0x85af
movl $0x12, %edx
callq 0x21b0
testb %bl, %bl
je 0x57b2
leaq 0xc(%rsp), %rdi
callq 0x54b8
jmp 0x57b2
movq 0x65e1(%rip), %rdi      # 0xbfd8
leaq 0x2bc4(%rip), %rsi      # 0x85c2
movl $0x10, %edx
callq 0x21b0
movl %ebp, %eax
shll $0x4, %eax
leaq 0x60a4(%rip), %rcx      # 0xbab8
movq (%rax,%rcx), %rsi
movq 0x65b9(%rip), %rdi      # 0xbfd8
callq 0x2180
leaq 0x2b67(%rip), %rsi      # 0x8592
movl $0x1, %edx
movq %rax, %rdi
callq 0x21b0
testb %bl, %bl
je 0x57e8
leaq 0xb(%rsp), %rdi
callq 0x54b8
jmp 0x57e8
leaq 0x2b7d(%rip), %rdi      # 0x85d3
callq 0x21e0
movq %rax, %rbx
leaq 0xc(%rsp), %rdi
jmp 0x5b10
movq %rax, %rbx
jmp 0x5ae6
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5aa6
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x5aa6
jmp 0x5aa3
movq %rax, %rbx
jmp 0x5ac1
movq %rax, %rbx
leaq 0xb(%rsp), %rdi
jmp 0x5b10
movq %rax, %rbx
jmp 0x5b0b
movq %rax, %rbx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5ac1
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xd(%rsp), %rdi
jmp 0x5b10
movq %rax, %rbx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5ae6
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xe(%rsp), %rdi
jmp 0x5b10
movq %rax, %rbx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5b0b
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xf(%rsp), %rdi
callq 0x54b8
movq %rbx, %rdi
callq 0x2290
nop