// File: /workspace/github_cpp_makefile_star2_O3/hantaoyin[P]ffxiv-crafting-mcts/ffxiv-crafting-mcts.C
// Function: Driver::simulate()
void simulate() {
    uct.reset(root_state);
    State s(root_state);
    std::vector<State> states;
    double score = 0.;

    const size_t size = training_data.size();
    ++simulate_count;
    bool track_simulation = simulate_count % 16 == 0;
    while(true) {
      if (s.done()) {
        LOG(track_simulation) << "Sample play: done, score = " << std::scientific << std::setprecision(3) << s.score() << "\n";
        score = s.score();
        break;
      }
      training_data.emplace_back(s, std::array<double, TotalActionCount>({}), 0.);
      for (size_t i = 0; i < SimulateCount; ++i) {
        uct.simulateFromState(s, track_simulation && i == SimulateCount - 1);
        // uct.simulateFromState(s, false);
      }
      // Select a move.
      Action ac = uct.select(s, inv_temp);
      LOG(track_simulation) << "Sample play: " << s.DebugString() << " ==> " << Action2Name(ac);
      if (ac == Action::NumActions) {
        LOG(track_simulation) << "Sample play: resigned.\n";
        break;
      } else {
        s.ExecuteAction(ac);
      }
    }

    for (size_t i = 0; i + size < training_data.size(); ++i) {
      auto& example = training_data[size + i];
      State& s = std::get<0>(example);
      uct.set_target_probability(s, inv_temp, std::get<1>(example));
      std::get<2>(example) = score;
    }
    while (training_data.size() > 10000) {
      training_data.pop_front();
    }
    LOG(track_simulation) << "Training data: has " << training_data.size() << " examples.";

    size_t example_id = random_real() * training_data.size();
    const auto& example = training_data[example_id];
    LOG(track_simulation) << "Training data: " << std::get<0>(example).DebugString() << " ==>";
    for (size_t i = 0; i < TotalActionCount; ++i) {
      LOG(track_simulation) << "Training data: " << std::setw(20) << std::setfill(' ')
          << Action2Name(static_cast<Action>(i)) << ": "
          << std::setfill('0') << std::scientific << std::setprecision(3) << std::get<1>(example)[i];
    }
    LOG(track_simulation) << "Training data: " << std::setfill(' ') << std::setw(20) << "Final score:"
        << std::scientific << std::setprecision(3) << std::get<2>(example);
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdi, %rbx
leaq 0x40(%rdi), %r15
addq $0x178, %rdi            # imm = 0x178
callq 0x4972
leaq 0xadb9(%rip), %rsi      # 0xd528
movq %r15, %rdi
callq 0x4880
movl 0xadbb(%rip), %eax      # 0xd538
movl %eax, 0x30(%rsp)
movupd 0xad9f(%rip), %xmm0     # 0xd528
movapd %xmm0, 0x20(%rsp)
movq 0x1f8(%rbx), %rax
movq %rax, %rcx
subq 0x1d8(%rbx), %rcx
sarq $0x3, %rcx
cmpq $0x1, %rax
adcq $-0x1, %rcx
movq %rcx, 0x40(%rsp)
leaq 0x1b0(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x1e0(%rbx), %rax
subq 0x1e8(%rbx), %rax
movq %rax, 0x50(%rsp)
movq (%rbx), %rcx
movq 0x1d0(%rbx), %r13
subq 0x1c0(%rbx), %r13
incq %rcx
movq %rcx, 0x18(%rsp)
movq %rcx, (%rbx)
movswl 0x22(%rsp), %eax
cmpl $0x160c, %eax           # imm = 0x160C
jg 0x2991
cmpw $0x0, 0x26(%rsp)
jle 0x2991
leaq 0x20(%rsp), %rbp
movq 0x18(%rsp), %r14
andl $0xf, %r14d
leaq 0x6(%rsp), %rax
movq %rax, 0x8(%rsp)
movl $0xe8, %edx
leaq 0x60(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x20a0
movq $0x0, 0x58(%rsp)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
leaq 0x58(%rsp), %rcx
callq 0x54c2
movl $0x2710, %r12d          # imm = 0x2710
movq %r12, %rax
xorq $0x1, %rax
xorl %edx, %edx
orq %r14, %rax
sete %dl
movq %r15, %rdi
movq %rbp, %rsi
callq 0x5536
decq %r12
jne 0x2858
movq %r15, %rdi
movq %rbp, %rsi
movsd 0x578b(%rip), %xmm0     # 0x8010
callq 0x5adc
movl %eax, %r12d
testb $0xf, 0x18(%rsp)
je 0x28cb
cmpb $0x1d, %r12b
je 0x29a4
movzbl %r12b, %esi
movq %rbp, %rdi
callq 0x6052
movswl 0x22(%rsp), %eax
cmpl $0x160c, %eax           # imm = 0x160C
jg 0x2991
cmpw $0x0, 0x26(%rsp)
jg 0x2820
jmp 0x2991
movl $0xd, %edx
movq 0x9701(%rip), %rdi      # 0xbfd8
leaq 0x5bfb(%rip), %rsi      # 0x84d9
callq 0x21b0
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x5c0c
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0x96d7(%rip), %rdi      # 0xbfd8
callq 0x21b0
movl $0x5, %edx
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x5bcb(%rip), %rsi      # 0x84e7
callq 0x21b0
movq 0x48(%rsp), %rdi
movzbl %r12b, %eax
shll $0x4, %eax
leaq 0x91b4(%rip), %rcx      # 0xbae8
movq (%rax,%rcx), %rsi
callq 0x2180
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x2959
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x6(%rsp), %rdi
callq 0x549e
cmpb $0x1d, %r12b
jne 0x289e
movq 0x9664(%rip), %rdi      # 0xbfd8
leaq 0x5b72(%rip), %rsi      # 0x84ed
movl $0x17, %edx
callq 0x21b0
leaq 0x5(%rsp), %rdi
callq 0x549e
jmp 0x29a4
testb $0xf, 0x18(%rsp)
je 0x2f0c
cwtl
cmpl $0x160d, %eax           # imm = 0x160D
jge 0x29aa
xorpd %xmm0, %xmm0
jmp 0x29be
movswl 0x24(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x565a(%rip), %xmm0     # 0x8018
movsd %xmm0, 0x8(%rsp)
movq 0x50(%rsp), %rcx
sarq $0x3, %rcx
movabsq $0xf83e0f83e0f83e1, %r12 # imm = 0xF83E0F83E0F83E1
imulq %r12, %rcx
movq 0x40(%rsp), %rax
addq %rcx, %rax
sarq $0x3, %r13
imulq %r12, %r13
addq %rax, %r13
movq %r13, %rax
shlq $0x8, %rax
leaq (%rax,%r13,8), %rbp
movq 0x1f8(%rbx), %rdx
movq 0x1d8(%rbx), %rax
movq %rdx, %rsi
subq %rax, %rsi
sarq $0x3, %rsi
cmpq $0x1, %rdx
adcq $-0x1, %rsi
movq 0x1e0(%rbx), %rcx
movq 0x1e8(%rbx), %rdi
movq %rcx, %r8
subq %rdi, %r8
sarq $0x3, %r8
imulq %r12, %r8
addq %rsi, %r8
movq 0x1c0(%rbx), %r14
movq 0x1d0(%rbx), %rsi
movq %rsi, %r9
subq %r14, %r9
sarq $0x3, %r9
imulq %r12, %r9
addq %r8, %r9
cmpq %r9, %r13
jae 0x2ab1
movq %r14, %rcx
subq 0x1c8(%rbx), %rcx
sarq $0x3, %rcx
imulq %r12, %rcx
addq %r13, %rcx
je 0x2a79
movq (%rax,%rcx,8), %r14
jmp 0x2a7c
addq %rbp, %r14
leaq 0xf0(%r14), %rsi
leaq 0x8(%r14), %rdx
movq %r15, %rdi
movsd 0x557e(%rip), %xmm0     # 0x8010
callq 0x60f4
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r14)
addq $0x108, %rbp            # imm = 0x108
incq %r13
jmp 0x29f9
movq 0x10(%rsp), %r15
movq %rdx, %r8
subq %rax, %r8
sarq $0x3, %r8
cmpq $0x1, %rdx
adcq $-0x1, %r8
subq %rdi, %rcx
sarq $0x3, %rcx
imulq %r12, %rcx
addq %r8, %rcx
subq %r14, %rsi
sarq $0x3, %rsi
imulq %r12, %rsi
addq %rcx, %rsi
cmpq $0x2711, %rsi           # imm = 0x2711
jb 0x2b21
movq %r15, %rdi
callq 0x61e8
movq 0x1f8(%rbx), %rdx
movq 0x1d8(%rbx), %rax
movq 0x1e0(%rbx), %rcx
movq 0x1e8(%rbx), %rdi
movq 0x1c0(%rbx), %r14
movq 0x1d0(%rbx), %rsi
jmp 0x2ab6
testb $0xf, 0x18(%rsp)
je 0x2e3d
leaq 0xa9dd(%rip), %rdi      # 0xd510
callq 0x3248
movsd 0xa9e0(%rip), %xmm1     # 0xd520
movsd 0xa9d0(%rip), %xmm2     # 0xd518
subsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
movq 0x1f8(%rbx), %rcx
movq 0x1d8(%rbx), %rax
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq $0x1, %rcx
adcq $-0x1, %rdx
movq 0x1e0(%rbx), %rcx
subq 0x1e8(%rbx), %rcx
sarq $0x3, %rcx
imulq %r12, %rcx
addq %rdx, %rcx
movq 0x1c0(%rbx), %r13
movq 0x1d0(%rbx), %rdx
subq %r13, %rdx
sarq $0x3, %rdx
imulq %r12, %rdx
addq %rcx, %rdx
movq %rdx, %xmm0
punpckldq 0x553a(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x5542(%rip), %xmm0     # 0x8100
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %rsi
movq %rsi, %rdx
sarq $0x3f, %rdx
subsd 0x543e(%rip), %xmm2     # 0x8020
cvttsd2si %xmm2, %rcx
andq %rdx, %rcx
movq %r13, %rdx
subq 0x1c8(%rbx), %rdx
orq %rsi, %rcx
sarq $0x3, %rdx
imulq %r12, %rdx
addq %rcx, %rdx
je 0x2c0a
movq (%rax,%rdx,8), %r13
jmp 0x2c18
movq %rcx, %rax
shlq $0x8, %rax
leaq (%rax,%rcx,8), %rax
addq %rax, %r13
testb $0xf, 0x18(%rsp)
je 0x2e8f
xorl %ebp, %ebp
movq 0x93ac(%rip), %rbx      # 0xbfd8
leaq 0x58f1(%rip), %rsi      # 0x8524
leaq 0x8eae(%rip), %r12      # 0xbae8
leaq 0x5a2d(%rip), %r15      # 0x866e
movl $0xfffffefb, %r14d      # imm = 0xFFFFFEFB
leaq 0x3(%rsp), %rax
movq %rax, 0x8(%rsp)
testb $0xf, 0x18(%rsp)
je 0x2c66
incq %rbp
cmpq $0x1d, %rbp
jne 0x2c51
jmp 0x2e2b
movl $0xf, %edx
movq %rbx, %rdi
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rcx
movq $0x14, 0x10(%rbx,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0x2cb0
movq %rdi, 0x10(%rsp)
movl $0x20, %esi
callq 0x2170
movq 0x10(%rsp), %rdi
movb $0x1, 0xe1(%rdi)
movb $0x20, 0xe0(%rdi)
movq %rbp, %rax
shlq $0x4, %rax
movq (%rax,%r12), %rsi
movq %rbx, %rdi
callq 0x2180
movq %rax, 0x10(%rsp)
movl $0x2, %edx
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x21b0
movq 0x10(%rsp), %rdx
movq (%rdx), %rax
movq -0x18(%rax), %rcx
movq %rdx, %rsi
addq %rcx, %rsi
cmpb $0x0, 0xe1(%rdx,%rcx)
jne 0x2d23
movq %rsi, 0x40(%rsp)
movq %rsi, %rdi
movl $0x20, %esi
callq 0x2170
movq 0x40(%rsp), %rsi
movb $0x1, 0xe1(%rsi)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movb $0x30, 0xe0(%rsi)
movq -0x18(%rax), %rcx
movq 0x10(%rsp), %rdi
movl 0x18(%rdi,%rcx), %edx
andl %r14d, %edx
orl $0x100, %edx            # imm = 0x100
movl %edx, 0x18(%rdi,%rcx)
movq -0x18(%rax), %rax
movq $0x3, 0x8(%rdi,%rax)
movsd 0x8(%r13,%rbp,8), %xmm0
callq 0x2220
leaq 0x3(%rsp), %rdi
callq 0x549e
incq %rbp
cmpq $0x1d, %rbp
leaq 0x57af(%rip), %rsi      # 0x8524
jne 0x2c51
movq 0x9256(%rip), %rbx      # 0xbfd8
leaq 0x579b(%rip), %rsi      # 0x8524
movl $0xf, %edx
movq %rbx, %rdi
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rcx
leaq (%rbx,%rcx), %r14
cmpb $0x0, 0xe1(%rbx,%rcx)
jne 0x2dc3
movq %r14, %rdi
movl $0x20, %esi
callq 0x2170
movb $0x1, 0xe1(%r14)
movq (%rbx), %rax
movb $0x20, 0xe0(%r14)
movq -0x18(%rax), %rax
movq $0x14, 0x10(%rbx,%rax)
leaq 0x575a(%rip), %rsi      # 0x8539
movl $0xc, %edx
movq %rbx, %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)
movsd (%r13), %xmm0
movq %rbx, %rdi
callq 0x2220
leaq 0x2(%rsp), %rdi
callq 0x549e
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9194(%rip), %rdi      # 0xbfd8
leaq 0x56ba(%rip), %rsi      # 0x8505
movl $0x13, %edx
callq 0x21b0
movq %r15, %rdi
callq 0x5456
movq 0x9174(%rip), %rdi      # 0xbfd8
movq %rax, %rsi
callq 0x20b0
leaq 0x56a6(%rip), %rsi      # 0x8519
movl $0xa, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x4(%rsp), %rdi
callq 0x549e
jmp 0x2b2c
movq 0x9142(%rip), %rdi      # 0xbfd8
leaq 0x5687(%rip), %rsi      # 0x8524
movl $0xf, %edx
callq 0x21b0
leaq 0xf0(%r13), %rsi
leaq 0x60(%rsp), %rdi
callq 0x5c0c
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0x910f(%rip), %rdi      # 0xbfd8
callq 0x21b0
leaq 0x565f(%rip), %rsi      # 0x8534
movl $0x4, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2efd
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x1(%rsp), %rdi
callq 0x549e
jmp 0x2c23
movq 0x90c5(%rip), %r12      # 0xbfd8
leaq 0x55a3(%rip), %rsi      # 0x84bd
movl $0x1b, %edx
movq %r12, %rdi
callq 0x21b0
movq (%r12), %rax
movq -0x18(%rax), %rcx
movl $0xfffffefb, %edx       # imm = 0xFFFFFEFB
andl 0x18(%r12,%rcx), %edx
orl $0x100, %edx            # imm = 0x100
movl %edx, 0x18(%r12,%rcx)
movq -0x18(%rax), %rax
movq $0x3, 0x8(%r12,%rax)
cmpw $0x160d, 0x22(%rsp)     # imm = 0x160D
jge 0x2f60
xorpd %xmm0, %xmm0
jmp 0x2f74
movswl 0x24(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x50a4(%rip), %xmm0     # 0x8018
movq %r12, %rdi
callq 0x2220
leaq 0x5580(%rip), %rsi      # 0x8503
movl $0x1, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x7(%rsp), %rdi
callq 0x549e
movzwl 0x22(%rsp), %eax
jmp 0x299c
movq %rax, %rbx
leaq 0x5(%rsp), %rax
jmp 0x2ff6
jmp 0x2fb0
movq %rax, %rbx
jmp 0x2fd3
movq %rax, %rbx
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2fd3
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x1(%rsp), %rax
jmp 0x2ff6
movq %rax, %rbx
leaq 0x7(%rsp), %rax
jmp 0x2ff6
movq %rax, %rbx
leaq 0x4(%rsp), %rax
jmp 0x2ff6
movq %rax, %rbx
leaq 0x2(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x3025
jmp 0x3022
jmp 0x3022
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x3025
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x3025
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0x549e
movq %rbx, %rdi
callq 0x2290
nop