// File: /workspace/github_cpp_makefile_star2_O1/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 $0x128, %rsp            # imm = 0x128
movq %rdi, %r14
leaq 0x40(%rdi), %r12
addq $0x178, %rdi            # imm = 0x178
callq 0x495e
leaq 0xadb9(%rip), %rsi      # 0xd528
movq %r12, %rdi
callq 0x486c
movl 0xadbb(%rip), %eax      # 0xd538
leaq 0x20(%rsp), %rbp
movl %eax, 0x10(%rbp)
movupd 0xad9b(%rip), %xmm0     # 0xd528
movapd %xmm0, (%rbp)
movq 0x1f8(%r14), %rax
movq %rax, %rcx
subq 0x1d8(%r14), %rcx
sarq $0x3, %rcx
cmpq $0x1, %rax
adcq $-0x1, %rcx
leaq 0x1b0(%r14), %rax
movq %rax, 0x10(%rsp)
movq 0x1e0(%r14), %rax
subq 0x1e8(%r14), %rax
sarq $0x3, %rax
movabsq $0xf83e0f83e0f83e1, %rdx # imm = 0xF83E0F83E0F83E1
imulq %rdx, %rax
addq %rcx, %rax
movq (%r14), %rbx
movq 0x1d0(%r14), %r15
subq 0x1c0(%r14), %r15
sarq $0x3, %r15
imulq %rdx, %r15
addq %rax, %r15
incq %rbx
movq %rbx, (%r14)
leaq 0x6(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpw $0x160c, 0x22(%rsp)     # imm = 0x160C
jg 0x2981
cmpw $0x0, 0x26(%rsp)
jle 0x2981
movl $0xe8, %edx
leaq 0x40(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x20a0
movq $0x0, 0x38(%rsp)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
leaq 0x38(%rsp), %rcx
callq 0x54dc
movl $0x2710, %r13d          # imm = 0x2710
movl %ebx, %eax
andl $0xf, %eax
movq %r13, %rcx
xorq $0x1, %rcx
xorl %edx, %edx
orq %rax, %rcx
sete %dl
movq %r12, %rdi
movq %rbp, %rsi
callq 0x5550
decq %r13
jne 0x285b
movq %r12, %rdi
movq %rbp, %rsi
movsd 0x5783(%rip), %xmm0     # 0x8010
callq 0x5b1e
movl %eax, %r13d
testb $0xf, %bl
je 0x28e4
cmpb $0x1d, %r13b
jne 0x28c9
testb $0xf, %bl
jne 0x28d5
movl $0x17, %edx
movq 0x9727(%rip), %rdi      # 0xbfd8
leaq 0x5c35(%rip), %rsi      # 0x84ed
callq 0x21b0
leaq 0x5(%rsp), %rdi
callq 0x54b8
jmp 0x28d5
movzbl %r13b, %esi
movq %rbp, %rdi
callq 0x60b4
cmpb $0x1d, %r13b
jne 0x280a
jmp 0x2993
movl $0xd, %edx
movq 0x96e8(%rip), %rdi      # 0xbfd8
leaq 0x5be2(%rip), %rsi      # 0x84d9
callq 0x21b0
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x5c6e
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x96be(%rip), %rdi      # 0xbfd8
callq 0x21b0
movl $0x5, %edx
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x5bb2(%rip), %rsi      # 0x84e7
callq 0x21b0
movq 0x18(%rsp), %rdi
movzbl %r13b, %eax
shll $0x4, %eax
leaq 0x916b(%rip), %rcx      # 0xbab8
movq (%rax,%rcx), %rsi
callq 0x2180
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2972
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x6(%rsp), %rdi
callq 0x54b8
jmp 0x289a
testb $0xf, %bl
je 0x2ee6
cmpw $0x160d, 0x22(%rsp)     # imm = 0x160D
jge 0x2999
xorpd %xmm0, %xmm0
jmp 0x29ad
movswl 0x24(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x566b(%rip), %xmm0     # 0x8018
movsd %xmm0, 0x8(%rsp)
movq %r15, %rax
shlq $0x8, %rax
leaq (%rax,%r15,8), %r13
movq 0x1f8(%r14), %rcx
movq 0x1d8(%r14), %rax
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq $0x1, %rcx
adcq $-0x1, %rdx
movq 0x1e0(%r14), %rcx
subq 0x1e8(%r14), %rcx
sarq $0x3, %rcx
movabsq $0xf83e0f83e0f83e1, %rsi # imm = 0xF83E0F83E0F83E1
imulq %rsi, %rcx
addq %rdx, %rcx
movq 0x1c0(%r14), %rbp
movq 0x1d0(%r14), %rdx
subq %rbp, %rdx
sarq $0x3, %rdx
imulq %rsi, %rdx
addq %rcx, %rdx
cmpq %rdx, %r15
jae 0x2a77
movq %rbp, %rcx
subq 0x1c8(%r14), %rcx
sarq $0x3, %rcx
imulq %rsi, %rcx
addq %r15, %rcx
je 0x2a3f
movq (%rax,%rcx,8), %rbp
jmp 0x2a42
addq %r13, %rbp
leaq 0xf0(%rbp), %rsi
leaq 0x8(%rbp), %rdx
movq %r12, %rdi
movsd 0x55b8(%rip), %xmm0     # 0x8010
callq 0x615a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rbp)
addq $0x108, %r13            # imm = 0x108
incq %r15
jmp 0x29be
movq %rsi, %r15
movq 0x10(%rsp), %r12
movq 0x1f8(%r14), %rax
movq %rax, %rcx
subq 0x1d8(%r14), %rcx
sarq $0x3, %rcx
cmpq $0x1, %rax
adcq $-0x1, %rcx
movq 0x1d0(%r14), %rax
movq 0x1e0(%r14), %rdx
subq 0x1e8(%r14), %rdx
sarq $0x3, %rdx
imulq %r15, %rdx
addq %rcx, %rdx
subq 0x1c0(%r14), %rax
sarq $0x3, %rax
imulq %r15, %rax
addq %rdx, %rax
cmpq $0x2711, %rax           # imm = 0x2711
jb 0x2ae0
movq %r12, %rdi
callq 0x624e
jmp 0x2a7f
testb $0xf, %bl
je 0x2d5b
leaq 0xaa20(%rip), %rdi      # 0xd510
callq 0x3219
movsd 0xaa23(%rip), %xmm1     # 0xd520
movsd 0xaa13(%rip), %xmm2     # 0xd518
subsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
movq 0x1f8(%r14), %rcx
movq 0x1d8(%r14), %rax
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq $0x1, %rcx
adcq $-0x1, %rdx
movq 0x1e0(%r14), %rcx
subq 0x1e8(%r14), %rcx
sarq $0x3, %rcx
imulq %r15, %rcx
addq %rdx, %rcx
movq 0x1c0(%r14), %rbp
movq 0x1d0(%r14), %rdx
subq %rbp, %rdx
sarq $0x3, %rdx
imulq %r15, %rdx
addq %rcx, %rdx
movq %rdx, %xmm0
punpckldq 0x557d(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x5585(%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 0x5481(%rip), %xmm2     # 0x8020
cvttsd2si %xmm2, %rcx
andq %rdx, %rcx
movq %rbp, %rdx
subq 0x1c8(%r14), %rdx
orq %rsi, %rcx
sarq $0x3, %rdx
imulq %r15, %rdx
addq %rcx, %rdx
je 0x2bc7
movq (%rax,%rdx,8), %rbp
jmp 0x2bd5
movq %rcx, %rax
shlq $0x8, %rax
leaq (%rax,%rcx,8), %rax
addq %rax, %rbp
testb $0xf, %bl
je 0x2dad
leaq 0x8(%rbp), %r12
xorl %r15d, %r15d
leaq 0x5938(%rip), %rsi      # 0x8524
leaq 0x5a7b(%rip), %r14      # 0x866e
movl $0xfffffefb, %r13d      # imm = 0xFFFFFEFB
leaq 0x3(%rsp), %rax
movq %rax, 0x8(%rsp)
testb $0xf, %bl
je 0x2c1e
addq $0x10, %r15
addq $0x8, %r12
cmpq $0x1d0, %r15            # imm = 0x1D0
jne 0x2c03
jmp 0x2d40
movl $0xf, %edx
movq 0x93ae(%rip), %rdi      # 0xbfd8
callq 0x21b0
movq 0x93a2(%rip), %rdx      # 0xbfd8
movq (%rdx), %rax
movq -0x18(%rax), %rcx
movq $0x14, 0x10(%rdx,%rcx)
movq -0x18(%rax), %rax
leaq (%rdx,%rax), %rdi
cmpb $0x0, 0xe1(%rdx,%rax)
jne 0x2c79
movq %rdi, 0x10(%rsp)
movl $0x20, %esi
callq 0x2170
movq 0x10(%rsp), %rdi
movb %al, 0xe0(%rdi)
movb $0x1, 0xe1(%rdi)
movb $0x20, 0xe0(%rdi)
leaq 0x8e31(%rip), %rax      # 0xbab8
movq (%r15,%rax), %rsi
movq 0x9346(%rip), %rdi      # 0xbfd8
callq 0x2180
movl $0x2, %edx
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x21b0
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
movq %rcx, %rdx
addq %rax, %rdx
cmpb $0x0, 0xe1(%rcx,%rax)
jne 0x2cee
movq %rdx, 0x18(%rsp)
movq %rdx, %rdi
movl $0x20, %esi
callq 0x2170
movq 0x18(%rsp), %rdx
movb %al, 0xe0(%rdx)
movb $0x1, 0xe1(%rdx)
movb $0x30, 0xe0(%rdx)
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rcx
movl 0x18(%rdi,%rcx), %edx
andl %r13d, %edx
orl $0x100, %edx            # imm = 0x100
movl %edx, 0x18(%rdi,%rcx)
movq -0x18(%rax), %rax
movq $0x3, 0x8(%rdi,%rax)
movsd (%r12), %xmm0
callq 0x2220
leaq 0x3(%rsp), %rdi
callq 0x54b8
leaq 0x57e9(%rip), %rsi      # 0x8524
jmp 0x2c08
testb $0xf, %bl
je 0x2e2b
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9276(%rip), %rdi      # 0xbfd8
leaq 0x579c(%rip), %rsi      # 0x8505
movl $0x13, %edx
callq 0x21b0
movq %r12, %rdi
callq 0x5470
movq 0x9256(%rip), %rdi      # 0xbfd8
movq %rax, %rsi
callq 0x20b0
leaq 0x5788(%rip), %rsi      # 0x8519
movl $0xa, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x4(%rsp), %rdi
callq 0x54b8
jmp 0x2ae9
movq 0x9224(%rip), %rdi      # 0xbfd8
leaq 0x5769(%rip), %rsi      # 0x8524
movl $0xf, %edx
callq 0x21b0
leaq 0xf0(%rbp), %rsi
leaq 0x40(%rsp), %rdi
callq 0x5c6e
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x91f1(%rip), %rdi      # 0xbfd8
callq 0x21b0
leaq 0x5741(%rip), %rsi      # 0x8534
movl $0x4, %edx
movq %rax, %rdi
callq 0x21b0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2e1c
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x1(%rsp), %rdi
callq 0x54b8
jmp 0x2bde
movq 0x91a6(%rip), %rbx      # 0xbfd8
leaq 0x56eb(%rip), %rsi      # 0x8524
movl $0xf, %edx
movq %rbx, %rdi
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r14
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0x2e77
movq %r14, %rdi
movl $0x20, %esi
callq 0x2170
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x14, 0x10(%rbx,%rax)
leaq 0x56a3(%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 (%rbp), %xmm0
movq %rbx, %rdi
callq 0x2220
leaq 0x2(%rsp), %rdi
callq 0x54b8
jmp 0x2d49
movq 0x90eb(%rip), %r13      # 0xbfd8
leaq 0x55c9(%rip), %rsi      # 0x84bd
movl $0x1b, %edx
movq %r13, %rdi
callq 0x21b0
movq (%r13), %rax
movq -0x18(%rax), %rcx
movl $0xfffffefb, %edx       # imm = 0xFFFFFEFB
andl 0x18(%r13,%rcx), %edx
orl $0x100, %edx            # imm = 0x100
movl %edx, 0x18(%r13,%rcx)
movq -0x18(%rax), %rax
movq $0x3, 0x8(%r13,%rax)
cmpw $0x160d, 0x22(%rsp)     # imm = 0x160D
jge 0x2f3a
xorpd %xmm0, %xmm0
jmp 0x2f4e
movswl 0x24(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x50ca(%rip), %xmm0     # 0x8018
movq %r13, %rdi
callq 0x2220
leaq 0x55a6(%rip), %rsi      # 0x8503
movl $0x1, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x7(%rsp), %rdi
callq 0x54b8
jmp 0x298a
jmp 0x2f7b
movq %rax, %rbx
jmp 0x2fa9
movq %rax, %rbx
leaq 0x7(%rsp), %rax
jmp 0x2fcc
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2fa9
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x1(%rsp), %rax
jmp 0x2fcc
movq %rax, %rbx
leaq 0x4(%rsp), %rax
jmp 0x2fcc
movq %rax, %rbx
leaq 0x2(%rsp), %rax
jmp 0x2fcc
movq %rax, %rbx
leaq 0x5(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x2ffb
jmp 0x2ff8
jmp 0x2ff8
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2ffb
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x2ffb
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0x54b8
movq %rbx, %rdi
callq 0x2290
nop