// File: /workspace/github_cpp_makefile_star2_O1/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), %bpl
movslq 0x44(%rdi), %rax
movq 0x28(%rdi,%rax,8), %r13
xorl %eax, %eax
movl %eax, 0x18(%rdi)
movl %eax, 0x20(%rdi)
callq 0x3100
movq %rax, 0x600(%r14)
cvtsi2sdl 0x14(%r14), %xmm0
ucomisd 0x8(%r13), %xmm0
jbe 0x3af5
leaq 0x28(%r14), %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
movzbl %bpl, %eax
movl %eax, 0xc(%rsp)
movq %r13, 0x18(%rsp)
callq 0x3100
subq 0x600(%r14), %rax
cmpq 0x608(%r14), %rax
jge 0x3af5
movl $0x0, 0x1c(%r14)
movq 0x8(%r14), %rax
movb (%rax), %cl
movl 0x8(%rax), %eax
movl %eax, 0x5f8(%r14)
movb %cl, 0x5fc(%r14)
movq $-0x1, %rax
movl $0x0, 0x54(%r14,%rax,4)
movl 0x5f8(%r14), %ecx
imull %ecx, %ecx
incq %rax
cmpq %rcx, %rax
jb 0x3999
cmpq $0x0, 0x38(%r13)
je 0x3a36
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %r13, %r12
movq %r12, 0x5d0(%rsp,%rbp,8)
movq %r12, %rdi
callq 0x9f8c
movq %rax, %r12
movslq (%rax), %r13
incl %r15d
testq %r13, %r13
cmovnel %ebx, %r15d
movq 0x8(%r14), %rdi
movl %r13d, %esi
callq 0x4d24
movl 0x1c(%r14), %ecx
leal 0x1(%rcx), %edx
movl %edx, 0x1c(%r14)
cmpl $0x0, 0x50(%r14,%r13,4)
movb 0x5fc(%r14), %al
jne 0x3a15
notl %ecx
testb %al, %al
cmovnel %ecx, %edx
movl %edx, 0x50(%r14,%r13,4)
xorb $0x1, %al
movb %al, 0x5fc(%r14)
incq %rbp
cmpq $0x0, 0x38(%r12)
je 0x3a2f
cmpl $0x2, %r15d
jl 0x39c3
movq 0x18(%rsp), %r13
jmp 0x3a3b
xorl %ebp, %ebp
movq %r13, %r12
cmpq %r13, %r12
je 0x3a51
movsd 0x8(%r12), %xmm0
ucomisd 0x75c9(%rip), %xmm0     # 0xb018
jb 0x3a90
movl $0x16a0, %edx           # imm = 0x16A0
leaq 0x27b0(%rsp), %rdi
xorl %esi, %esi
callq 0x30e0
movq 0x8(%r14), %rdi
leaq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0x571a
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movl %eax, %ecx
leaq 0x27b0(%rsp), %r8
callq 0xa7c2
movl %ebp, %eax
movq %r12, 0x5d0(%rsp,%rax,8)
movzbl (%r14), %esi
movq %r14, %rdi
callq 0x37ee
movl %eax, %r12d
movq 0x8(%r14), %rdi
callq 0x47a0
cmpl $-0x1, %r12d
je 0x3ae0
incl %ebp
movl $0x1, %esi
subl %r12d, %esi
movl 0xc(%rsp), %r8d
testb %r8b, %r8b
cmovel %r12d, %esi
movq %r14, %rdi
leaq 0x5d0(%rsp), %rdx
movl %ebp, %ecx
callq 0x3b7c
xorps %xmm0, %xmm0
cvtsi2sdl 0x14(%r14), %xmm0
ucomisd 0x8(%r13), %xmm0
ja 0x395a
movslq 0x44(%r14), %rax
movq 0x28(%r14,%rax,8), %rdi
callq 0x9ff8
movq %rax, %rbx
movq %r14, %rdi
callq 0x3bee
cmpl $0x0, (%rbx)
je 0x3b67
movsd 0x7505(%rip), %xmm0     # 0xb020
movq %rbx, %rdi
callq 0x9ebe
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movsd 0x74f8(%rip), %xmm1     # 0xb028
ucomisd %xmm0, %xmm1
ja 0x3b69
cmpb $0x0, 0x8(%rsp)
je 0x3b63
movsd 0x74db(%rip), %xmm0     # 0xb020
movq %rbx, %rdi
callq 0x9ebe
ucomisd 0x74b3(%rip), %xmm0     # 0xb008
jb 0x3b63
cmpl $0x0, (%r13)
movl $0x0, %eax
je 0x3b69
movl (%rbx), %eax
jmp 0x3b69
xorl %eax, %eax
addq $0x3e58, %rsp           # imm = 0x3E58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop