// File: /workspace/github_cpp_makefile_star2_O2/bonetblai[P]mdp-engine/ctp3/../engine/uct.h
// Function: Online::Policy::UCT::uct_t<state_t>::search_tree(state_t const&, unsigned int) const
float search_tree(const T &s, unsigned depth) const {
#ifdef DEBUG
        std::cout << std::setw(2*depth) << "" << "search_tree(" << s << "):";
#endif

        if( (depth == horizon_) || problem_.terminal(s) ) {
#ifdef DEBUG
            std::cout << " end" << std::endl;
#endif
            return 0;
        }

        if( problem_.dead_end(s) ) {
#ifdef DEBUG
            std::cout << " dead-end" << std::endl;
#endif
            return problem_.dead_end_value();
        }

        typename hash_t<T>::iterator it = table_.find(std::make_pair(depth, s));

        if( it == table_.end() ) {
            std::vector<float> values(1 + problem_.number_actions(s), 0);
            std::vector<int> counts(1 + problem_.number_actions(s), 0);
            table_.insert(std::make_pair(std::make_pair(depth, s), data_t(values, counts)));
            float value = evaluate(s, depth);
#ifdef DEBUG
            std::cout << " insert in tree w/ value=" << value << std::endl;
#endif
            return value;
        } else {
            // select action for this node and increase counts
            Problem::action_t a = select_action(s, it->second, depth, true, random_ties_);
            ++it->second.counts_[0];
            ++it->second.counts_[1+a];

            // sample next state
            std::pair<const T, bool> p = problem_.sample(s, a);
            float cost = problem_.cost(s, a);

#ifdef DEBUG
            std::cout << " count=" << it->second.counts_[0]-1
                      << " fetch " << std::setprecision(5) << it->second.values_[1+a]
                      << " a=" << a
                      << " next=" << p.first
                      << std::endl;
#endif

            // do recursion and update value
            float &old_value = it->second.values_[1+a];
            float n = it->second.counts_[1+a];
            float new_value = cost + problem_.discount() * search_tree(p.first, 1 + depth);
            old_value += (new_value - old_value) / n;
            return new_value;
        }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
xorps %xmm0, %xmm0
cmpl %edx, 0x34(%rdi)
je 0x1740b
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
movq 0x8(%rdi), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
xorps %xmm0, %xmm0
testb %al, %al
jne 0x1740b
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x28(%rax)
testb %al, %al
je 0x171f7
movq 0x8(%r14), %rax
movss 0xc(%rax), %xmm0
jmp 0x1740b
leaq 0x48(%r14), %r12
leaq 0x70(%rsp), %rbp
movl %ebx, -0x8(%rbp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xa4d8
leaq 0x68(%rsp), %rsi
movq %r12, %rdi
callq 0x17e9e
movq %rax, %r13
movq %rbp, %rdi
callq 0x8e90
testq %r13, %r13
je 0x1731d
movq %r13, %rdx
subq $-0x80, %rdx
movzbl 0x3c(%r14), %r9d
pushq $0x1
popq %r8
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %ecx
callq 0x174ae
movl %eax, %ebp
movq 0x98(%r13), %rax
incl (%rax)
movslq %ebp, %r12
incl 0x4(%rax,%r12,4)
movq 0x8(%r14), %rsi
leaq 0x110(%rsp), %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0xb318
leaq 0x68(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x17826
leaq 0x110(%rsp), %rdi
callq 0x8e90
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movq %r15, %rsi
movl %ebp, %edx
callq *0x48(%rax)
movss %xmm0, (%rsp)
incq %r12
movq 0x80(%r13), %r15
movq 0x98(%r13), %rax
movl (%rax,%r12,4), %ebp
movq 0x8(%r14), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, 0x4(%rsp)
incl %ebx
leaq 0x68(%rsp), %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x1719e
cvtsi2ss %ebp, %xmm3
mulss 0x4(%rsp), %xmm0
addss (%rsp), %xmm0
movss %xmm0, (%rsp)
movss (%r15,%r12,4), %xmm1
movss (%rsp), %xmm2
subss %xmm1, %xmm2
divss %xmm3, %xmm2
addss %xmm1, %xmm2
movss %xmm2, (%r15,%r12,4)
leaq 0x68(%rsp), %rdi
callq 0x8e90
jmp 0x17406
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x10(%rax)
incl %eax
movslq %eax, %rsi
leaq 0x68(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x20(%rsp), %rdi
leaq 0x110(%rsp), %rcx
callq 0x176a8
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x10(%rax)
incl %eax
movslq %eax, %rsi
leaq 0x68(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x8(%rsp), %rdi
leaq 0x110(%rsp), %rcx
callq 0x176fe
leaq 0x118(%rsp), %r13
movl %ebx, -0x8(%r13)
movq %r13, %rdi
movq %r15, %rsi
callq 0xa4d8
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x17754
leaq 0x68(%rsp), %rdi
leaq 0x110(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x17ddc
leaq 0x68(%rsp), %rsi
movq %r12, %rdi
callq 0x17974
leaq 0x68(%rsp), %rdi
callq 0x1778a
leaq 0x38(%rsp), %rdi
callq 0x1711c
movq %r13, %rdi
callq 0x8e90
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x177a4
movss %xmm0, (%rsp)
leaq 0x8(%rsp), %rdi
callq 0x85dc
leaq 0x20(%rsp), %rdi
callq 0x17132
movss (%rsp), %xmm0
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1744a
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x1778a
jmp 0x17431
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x1711c
jmp 0x17440
movq %rax, %rbx
movq %r13, %rdi
callq 0x8e90
jmp 0x1744d
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x85dc
jmp 0x1745e
jmp 0x1745b
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x17132
jmp 0x17493
movq %rax, %rbx
jmp 0x17493
jmp 0x17471
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0x1748e
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
jmp 0x1748e
movq %rax, %rbx
movq %rbp, %rdi
callq 0x8e90
movq %rbx, %rdi
callq 0x5410
nop