// File: /workspace/github_cpp_makefile_star2_O3/SethKasmann[P]clever-girl/src/search.cpp
// Function: iterative_deepening(State&, SearchInfo&)
void iterative_deepening(State& s, SearchInfo& si)
{
    int score;
    // Iterative deepening.
    for (int d = 1; !si.quit; ++d)
    {
        if (d == si.depth)
            break;

        score = scout_search(s, si, d, 0, Neg_inf, Pos_inf, true, false, true);

        if (lineManager.getPvMove() == nullMove)
        {
            std::cout << "bestmove 0000" << std::endl;
            return;
        }

        if (si.quit)
            break;

        // Confirm all the pv moves are legal.
        lineManager.checkPv(s);

        // Print info to gui.
        std::cout << "info "
                  << "depth " << d;

        if (lineManager.isMate())
        {
            int n = lineManager.getMateInN();
            std::cout << " score mate " << (score > 0 ? n : -n);
        }
        else
        {
            std::cout << " score cp " << score;
        }

        std::cout << " time " 
                  << si.clock.elapsed<std::chrono::milliseconds>()
                  << " nodes " 
                  << si.nodes
                  << " nps " 
                  << si.nodes / (si.clock.elapsed<std::chrono::milliseconds>() + 1) * 1000;
        lineManager.printPv();
        std::cout << std::endl;

        // This check handles the case where the search tree was the same
        // exact size as the previous depth (can occur for a forced draw
        // or mating sequence).
        if (si.nodes == si.prevNodes)
            break;

        // Break if there's not enough time for the next search.
        if (si.clock.elapsed<std::chrono::milliseconds>() * 2 > si.moveTime)
            break;

        // Reset node count.
        si.prevNodes = si.nodes;
        si.nodes = 0;
    }

    std::cout << "bestmove " << toString(lineManager.getPvMove()) << std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x8(%rsp)
cmpb $0x0, 0x3a(%rsi)
jne 0x16129
movq %rsi, %r14
cmpl $0x1, 0x14(%rsi)
je 0x16129
movl $0x2, %r15d
leaq 0x13f7fb(%rip), %r12    # 0x1556e0
movq 0xf0b4(%rip), %rbx      # 0x24fa0
leal -0x1(%r15), %r13d
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movl %r13d, %edx
xorl %ecx, %ecx
movl $0xffff3cb0, %r8d       # imm = 0xFFFF3CB0
movl $0xc350, %r9d           # imm = 0xC350
movl $0x1, %eax
pushq %rax
pushq $0x0
pushq %rax
callq 0x15414
addq $0x20, %rsp
movl %eax, %ebp
movq %r12, %rdi
xorl %esi, %esi
callq 0xf8fa
testw %ax, %ax
je 0x161c5
cmpb $0x0, 0x3a(%r14)
jne 0x16129
movq %r15, 0x10(%rsp)
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0xf972
movl $0x5, %edx
movq %rbx, %rdi
leaq 0x82f8(%rip), %rsi      # 0x1e258
callq 0x4290
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x82ea(%rip), %rsi      # 0x1e25e
callq 0x4290
movq %rbx, %rdi
movl %r13d, %esi
callq 0x43a0
movq %r12, %rdi
callq 0xf906
testb %al, %al
je 0x15fbc
movq %r12, %rdi
callq 0xf90e
movl %eax, %r13d
movl $0xc, %edx
movq %rbx, %rdi
leaq 0x82bb(%rip), %rsi      # 0x1e265
callq 0x4290
movl %r13d, %esi
negl %esi
testl %ebp, %ebp
cmovgl %r13d, %esi
jmp 0x15fd2
movl $0xa, %edx
movq %rbx, %rdi
leaq 0x82a7(%rip), %rsi      # 0x1e272
callq 0x4290
movl %ebp, %esi
movq %r12, %r15
movq %rbx, %rdi
callq 0x43a0
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x8291(%rip), %rsi      # 0x1e27d
callq 0x4290
callq 0x4060
subq 0x30(%r14), %rax
movabsq $0x431bde82d7b634db, %r12 # imm = 0x431BDE82D7B634DB
imulq %r12
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x12, %rdx
addq %rdx, %rsi
movq %rbx, %rdi
callq 0x4200
movq %rax, %r13
movl $0x7, %edx
movq %rax, %rdi
leaq 0x8255(%rip), %rsi      # 0x1e284
callq 0x4290
movl 0x1c(%r14), %esi
movq %r13, %rdi
callq 0x43a0
movq %rax, %r13
movl $0x5, %edx
movq %rax, %rdi
leaq 0x823a(%rip), %rsi      # 0x1e28c
callq 0x4290
movslq 0x1c(%r14), %rbp
callq 0x4060
subq 0x30(%r14), %rax
imulq %r12
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
leaq (%rdx,%rax), %rcx
incq %rcx
movq %rbp, %rax
cqto
idivq %rcx
imulq $0x3e8, %rax, %rsi      # imm = 0x3E8
movq %r13, %rdi
callq 0x4200
movq %r15, %rdi
callq 0xfa60
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4230
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4170
movl 0x1c(%r14), %eax
cmpl 0x20(%r14), %eax
movq %r15, %r12
movq 0x10(%rsp), %r15
je 0x16129
callq 0x4060
subq 0x30(%r14), %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
addq %rdx, %rdx
cmpq 0x28(%r14), %rdx
jg 0x16129
movl 0x14(%r14), %eax
movl 0x1c(%r14), %ecx
movl %ecx, 0x20(%r14)
movl $0x0, 0x1c(%r14)
cmpb $0x0, 0x3a(%r14)
jne 0x16129
leal 0x1(%r15), %ecx
cmpl %eax, %r15d
movl %ecx, %r15d
jne 0x15eec
movq 0xee70(%rip), %rbx      # 0x24fa0
leaq 0x815b(%rip), %rsi      # 0x1e292
movl $0x9, %edx
movq %rbx, %rdi
callq 0x4290
leaq 0x13f595(%rip), %rdi    # 0x1556e0
xorl %esi, %esi
callq 0xf8fa
movzwl %ax, %esi
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0xfb3b
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0x4290
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4230
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4170
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x161b6
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4250
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x807e(%rip), %rsi      # 0x1e24a
movl $0xd, %edx
movq %rbx, %rdi
callq 0x4290
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4230
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4170
movq %rax, %rbx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1622c
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4250
movq %rbx, %rdi
callq 0x43b0