// File: 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:
subq $0xa8, %rsp
movq %rdi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movl $0x1, 0x90(%rsp)
movq 0x98(%rsp), %rax
movb 0x3a(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x34f88
jmp 0x35259
movl 0x90(%rsp), %eax
movq 0x98(%rsp), %rcx
cmpl 0x14(%rcx), %eax
jne 0x34fa1
jmp 0x35259
movq 0xa0(%rsp), %rdi
movq 0x98(%rsp), %rsi
movl 0x90(%rsp), %edx
xorl %ecx, %ecx
movl $0xffff3cb0, %r8d       # imm = 0xFFFF3CB0
movl $0xc350, %r9d           # imm = 0xC350
movl $0x1, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x33ff0
movl %eax, 0x94(%rsp)
leaq 0x1dc790(%rip), %rdi    # 0x211780
xorl %esi, %esi
callq 0x25dd0
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x35026
movq 0x1bfb2(%rip), %rdi     # 0x50fb8
leaq 0xe38d(%rip), %rsi      # 0x4339a
callq 0x5290
movq %rax, %rdi
movq 0x1bf74(%rip), %rsi     # 0x50f90
callq 0x52c0
jmp 0x352c4
movq 0x98(%rsp), %rax
testb $0x1, 0x3a(%rax)
je 0x35039
jmp 0x35259
movq 0xa0(%rsp), %rsi
leaq 0x1dc738(%rip), %rdi    # 0x211780
callq 0x25f00
movq 0x1bf64(%rip), %rdi     # 0x50fb8
leaq 0xe34d(%rip), %rsi      # 0x433a8
callq 0x5290
movq %rax, %rdi
leaq 0xe344(%rip), %rsi      # 0x433ae
callq 0x5290
movq %rax, %rdi
movl 0x90(%rsp), %esi
callq 0x5490
leaq 0x1dc6fb(%rip), %rdi    # 0x211780
callq 0x25e00
testb $0x1, %al
jne 0x35090
jmp 0x350ef
leaq 0x1dc6e9(%rip), %rdi    # 0x211780
callq 0x25e20
movl %eax, 0x8c(%rsp)
movq 0x1bf0e(%rip), %rdi     # 0x50fb8
leaq 0xe304(%rip), %rsi      # 0x433b5
callq 0x5290
movq %rax, 0x50(%rsp)
cmpl $0x0, 0x94(%rsp)
jle 0x350d2
movl 0x8c(%rsp), %eax
movl %eax, 0x4c(%rsp)
jmp 0x350df
xorl %eax, %eax
subl 0x8c(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x50(%rsp), %rdi
movl 0x4c(%rsp), %esi
callq 0x5490
jmp 0x35111
movq 0x1bec2(%rip), %rdi     # 0x50fb8
leaq 0xe2c5(%rip), %rsi      # 0x433c2
callq 0x5290
movq %rax, %rdi
movl 0x94(%rsp), %esi
callq 0x5490
movq 0x1bea0(%rip), %rdi     # 0x50fb8
leaq 0xe2ae(%rip), %rsi      # 0x433cd
callq 0x5290
movq %rax, 0x30(%rsp)
movq 0x98(%rsp), %rdi
addq $0x30, %rdi
callq 0x35340
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x54d0
movq %rax, %rdi
leaq 0xe283(%rip), %rsi      # 0x433d4
callq 0x5290
movq %rax, %rdi
movq 0x98(%rsp), %rax
movl 0x1c(%rax), %esi
callq 0x5490
movq %rax, %rdi
leaq 0xe269(%rip), %rsi      # 0x433dc
callq 0x5290
movq %rax, 0x38(%rsp)
movq 0x98(%rsp), %rax
movslq 0x1c(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x98(%rsp), %rdi
addq $0x30, %rdi
callq 0x35340
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movq 0x40(%rsp), %rax
addq $0x1, %rcx
cqto
idivq %rcx
imulq $0x3e8, %rax, %rsi      # imm = 0x3E8
callq 0x54d0
leaq 0x1dc5b8(%rip), %rdi    # 0x211780
callq 0x26080
movq 0x1bde4(%rip), %rdi     # 0x50fb8
movq 0x1bdb5(%rip), %rsi     # 0x50f90
callq 0x52c0
movq 0x98(%rsp), %rax
movl 0x1c(%rax), %eax
movq 0x98(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jne 0x351fa
jmp 0x35259
movq 0x98(%rsp), %rdi
addq $0x30, %rdi
callq 0x35340
shlq %rax
movq 0x98(%rsp), %rcx
cmpq 0x28(%rcx), %rax
jle 0x3521e
jmp 0x35259
movq 0x98(%rsp), %rax
movl 0x1c(%rax), %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x98(%rsp), %rax
movl $0x0, 0x1c(%rax)
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x34f72
movq 0x1bd58(%rip), %rdi     # 0x50fb8
leaq 0xe17b(%rip), %rsi      # 0x433e2
callq 0x5290
movq %rax, 0x18(%rsp)
leaq 0x1dc508(%rip), %rdi    # 0x211780
xorl %esi, %esi
callq 0x25dd0
movzwl %ax, %esi
leaq 0x68(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x263f0
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x5220
movq %rax, 0x28(%rsp)
jmp 0x352a7
movq 0x28(%rsp), %rdi
movq 0x1bcdd(%rip), %rsi     # 0x50f90
callq 0x52c0
jmp 0x352ba
leaq 0x68(%rsp), %rdi
callq 0x5538
addq $0xa8, %rsp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x5538
movq 0x60(%rsp), %rdi
callq 0x54b0
nop