// File: /workspace/github_cpp_makefile_star2_O2/antoniogarro[P]Hara/engine.cpp
// Function: Engine::generate_move(bool)
int Engine::generate_move(bool early_pass)
{
  const int EXPAND = 8;
  const double RESIGN_THRESHOLD = 0.10, PASS_THRESHOLD = 0.90;
  
  bool side = main_goban->get_side();
  Node *root = tree.get_root();
  rand_movs = 0, discarded = 0;
  fin_clock = clock();

  while (root->get_visits() < max_playouts && clock() - fin_clock < max_time) {
    Node *node_history[3*MAXSIZE2];
    int nnode_hist = 0, pass = 0;
    simul_len = 0;
    amaf.set_up(main_goban->get_side(), main_goban->get_size());
    Node *node = root;
    while (node->has_childs() && pass < 2) {
      node_history[nnode_hist++] = node;
      node = node->select_child();
      int move = node->get_move();
      if(move == Goban::PASS) pass++;
      else pass = 0;
      main_goban->play_move(move);
      amaf.play(move, ++simul_len);
    }
    if (node->get_visits() >= EXPAND || node == root) {
      Prior priors[MAXSIZE2+1] = {{0,0}};
      int legal_moves[MAXSIZE2+1];
      int nlegal = main_goban->legal_moves(legal_moves);
      //main_goban->init_priors(priors);
      tree.expand(node, legal_moves, nlegal, priors); //TODO: break if expand fails.
    }
    node_history[nnode_hist++] = node;
    int result = play_random_game(HEAVY); //Black wins.
#ifdef DEBUG_INFO
      main_goban->print_goban();
      std::cerr << result << "\n";
#endif
    main_goban->restore();
    if (result == -1) continue;
    if (side) result = 1-result;    
    back_up_results(result, node_history, nnode_hist, side);
#ifdef DEBUG_INFO
      tree.print();
#endif
  }
  Node *best = tree.get_best();
  print_PV();
  if (best->get_move() == Goban::PASS) return Goban::PASS;
  if (best->get_value(1) < RESIGN_THRESHOLD) return -1;
  if (early_pass && best->get_value(1) >= PASS_THRESHOLD && !root->get_move()) return Goban::PASS;
  return best->get_move();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3e58, %rsp           # imm = 0x3E58
movl %esi, 0x8(%rsp)
movq %rdi, %r14
movq 0x8(%rdi), %rax
movb (%rax), %bl
movslq 0x44(%rdi), %rax
movq 0x28(%rdi,%rax,8), %r15
andl $0x0, 0x18(%rdi)
leaq 0x28(%rdi), %rax
movq %rax, 0x10(%rsp)
andl $0x0, 0x20(%rdi)
callq 0x3110
movq %rax, 0x600(%r14)
leaq 0x50(%r14), %r12
movzbl %bl, %eax
movl %eax, 0xc(%rsp)
movq %r15, %rbx
movq %r15, 0x18(%rsp)
cvtsi2sdl 0x14(%r14), %xmm0
ucomisd 0x8(%rbx), %xmm0
jbe 0x3a15
callq 0x3110
subq 0x600(%r14), %rax
cmpq 0x608(%r14), %rax
jge 0x3a15
andl $0x0, 0x1c(%r14)
movq 0x8(%r14), %rax
movzbl (%rax), %esi
movl 0x8(%rax), %edx
movq %r12, %rdi
callq 0x3d58
xorl %r13d, %r13d
movq %rbx, %rbp
xorl %ebx, %ebx
cmpq $0x0, 0x38(%rbp)
je 0x3967
cmpl $0x1, %ebx
jg 0x3967
movq %rbp, 0x5d0(%rsp,%r13,8)
incq %r13
movq %rbp, %rdi
callq 0x8f5a
movq %rax, %rbp
movl (%rax), %r15d
incl %ebx
testl %r15d, %r15d
movl $0x0, %eax
cmovnel %eax, %ebx
movq 0x8(%r14), %rdi
movl %r15d, %esi
callq 0x4b26
movl 0x1c(%r14), %edx
incl %edx
movl %edx, 0x1c(%r14)
movq %r12, %rdi
movl %r15d, %esi
callq 0x3d6a
jmp 0x3912
movq 0x18(%rsp), %rbx
cmpq %rbx, %rbp
je 0x3980
movsd 0x8(%rbp), %xmm0
ucomisd 0x66aa(%rip), %xmm0     # 0xa028
jb 0x39bf
movl $0x16a0, %edx           # imm = 0x16A0
leaq 0x27b0(%rsp), %rdi
xorl %esi, %esi
callq 0x30f0
movq 0x8(%r14), %rdi
leaq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0x53a0
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
movl %eax, %ecx
leaq 0x27b0(%rsp), %r8
callq 0x95d8
movl %r13d, %eax
movq %rbp, 0x5d0(%rsp,%rax,8)
movzbl (%r14), %esi
movq %r14, %rdi
callq 0x37ae
movl %eax, %ebp
movq 0x8(%r14), %rdi
callq 0x4656
cmpl $-0x1, %ebp
je 0x38c9
incl %r13d
pushq $0x1
popq %rsi
subl %ebp, %esi
movl 0xc(%rsp), %r8d
testb %r8b, %r8b
cmovel %ebp, %esi
movq %r14, %rdi
leaq 0x5d0(%rsp), %rdx
movl %r13d, %ecx
callq 0x3a92
jmp 0x38c9
movq 0x10(%rsp), %rdi
callq 0x3d96
movq %rax, %r15
movq %r14, %rdi
callq 0x3b06
xorl %ebp, %ebp
cmpl $0x0, (%r15)
je 0x3a7e
movsd 0x65de(%rip), %xmm0     # 0xa018
movq %r15, %rdi
callq 0x8e80
movsd 0x65d6(%rip), %xmm1     # 0xa020
ucomisd %xmm0, %xmm1
jbe 0x3a55
pushq $-0x1
popq %rbp
jmp 0x3a7e
cmpb $0x0, 0x8(%rsp)
je 0x3a7b
movsd 0x65b4(%rip), %xmm0     # 0xa018
movq %r15, %rdi
callq 0x8e80
ucomisd 0x6594(%rip), %xmm0     # 0xa008
jb 0x3a7b
cmpl $0x0, (%rbx)
je 0x3a7e
movl (%r15), %ebp
movl %ebp, %eax
addq $0x3e58, %rsp           # imm = 0x3E58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq