// File: /workspace/github_cpp_makefile_star2_O2/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 %rsi, %rbx
movq %rdi, %r14
leaq 0x30(%rsi), %rax
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rax
leaq 0x1d1382(%rip), %r12    # 0x1e36f0
movl %eax, %ebp
movq %rdi, 0x10(%rsp)
cmpb $0x0, 0x3a(%rbx)
jne 0x12530
cmpl 0x14(%rbx), %ebp
je 0x12530
subq $0x8, %rsp
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movl $0xffff3cb0, %r8d       # imm = 0xFFFF3CB0
movl $0xc350, %r9d           # imm = 0xC350
pushq $0x1
popq %rax
pushq %rax
pushq $0x0
pushq %rax
callq 0x11aee
addq $0x20, %rsp
movl %eax, %r13d
movq %r12, %rdi
xorl %esi, %esi
callq 0xce9a
testw %ax, %ax
je 0x12590
cmpb $0x0, 0x3a(%rbx)
jne 0x12530
movq %r12, %rdi
movq %r14, %rsi
callq 0xcf0c
movq 0x10bb4(%rip), %r15     # 0x22f98
movq %r15, %rdi
leaq 0x6e9a(%rip), %rsi      # 0x19288
callq 0x4240
movq %rax, %rdi
leaq 0x6e91(%rip), %rsi      # 0x1928e
callq 0x4240
movq %rax, %rdi
movl %ebp, %esi
callq 0x43c0
movq %r12, %rdi
callq 0xcea6
testb %al, %al
movl %ebp, 0x4(%rsp)
je 0x12452
movq %r12, %rdi
callq 0xceae
movl %eax, %r14d
movq 0x10b6a(%rip), %rdi     # 0x22f98
leaq 0x6e60(%rip), %rsi      # 0x19295
callq 0x4240
movl %r14d, %esi
negl %esi
testl %r13d, %r13d
cmovgl %r14d, %esi
movq 0x10b4b(%rip), %r15     # 0x22f98
movq %rax, %rdi
jmp 0x12467
movq %r15, %rdi
leaq 0x6e46(%rip), %rsi      # 0x192a2
callq 0x4240
movq %rax, %rdi
movl %r13d, %esi
callq 0x43c0
movq %r15, %rdi
leaq 0x6e37(%rip), %rsi      # 0x192ad
callq 0x4240
movq %rax, %r14
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x12614
movq %r14, %rdi
movq %rax, %rsi
callq 0x4210
movq %rax, %rdi
leaq 0x6e14(%rip), %rsi      # 0x192b4
callq 0x4240
movl 0x1c(%rbx), %esi
movq %rax, %rdi
callq 0x43c0
movq %rax, %rdi
leaq 0x6e02(%rip), %rsi      # 0x192bc
callq 0x4240
movq %rax, %r14
movslq 0x1c(%rbx), %r13
movq %rbp, %rdi
callq 0x12614
movq %rax, %rcx
incq %rcx
movq %r13, %rax
cqto
idivq %rcx
imulq $0x3e8, %rax, %rsi      # imm = 0x3E8
movq %r14, %rdi
callq 0x4210
movq %r12, %rdi
callq 0xcfd8
movq %r15, %rdi
callq 0x4120
movl 0x1c(%rbx), %eax
cmpl 0x20(%rbx), %eax
movq 0x10(%rsp), %r14
movl 0x4(%rsp), %ebp
je 0x12530
movq 0x8(%rsp), %rdi
callq 0x12614
addq %rax, %rax
cmpq 0x28(%rbx), %rax
jg 0x12530
movl 0x1c(%rbx), %eax
movl %eax, 0x20(%rbx)
andl $0x0, 0x1c(%rbx)
incl %ebp
jmp 0x12375
movq 0x10a61(%rip), %rdi     # 0x22f98
leaq 0x6d84(%rip), %rsi      # 0x192c2
callq 0x4240
movq %rax, %rbx
leaq 0x1d11a3(%rip), %rdi    # 0x1e36f0
xorl %esi, %esi
callq 0xce9a
movzwl %ax, %esi
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0xd08b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x41e0
movq %rax, %rdi
callq 0x4120
leaq 0x18(%rsp), %rdi
callq 0x4418
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10a01(%rip), %rdi     # 0x22f98
leaq 0x6cdc(%rip), %rsi      # 0x1927a
callq 0x4240
movq %rax, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4120
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4418
movq %rbx, %rdi
callq 0x43d0