// File: cpirc[P]Monochrome/src/search.cpp
// Function: int search<false>(SearchController&, Position&, int, int, int, SearchStack*, std::vector<unsigned int, std::allocator<unsigned int>>&)
int search(SearchController& sc, Position& pos, int depth, int alpha, int beta,
           SearchStack* ss, PV& pv) {
    assert(ss);

    if (is_fifty_moves(pos) || is_threefold(pos, ss->ply)) {
        return 0;
    }

    const bool in_check = is_checked(pos, US);

    // Check extensions
    if (in_check) depth++;

    if (depth <= 0) {
        return quiesce(sc, pos, alpha, beta, ss);
    }

    if (ss->ply >= MAX_PLY) {
        return evaluate(pos);
    }

    // Check time left
    clock_t current_time = clock() * 1000 / CLOCKS_PER_SEC;
    if (ss->ply && current_time >= sc.search_end_time) {
        return 0;
    }

    // Update info
    if (ss->stats->node_count % 1048576 == 0) {
        if (current_time > sc.search_start_time) {
            printf("info nps %" PRIu64 "\n",
                   1000 * (ss->stats->node_count) /
                       (current_time - sc.search_start_time));
        }
    }

    ++ss->stats->node_count;

    // Check transposition table
    Move hash_move = 0;
    TTEntry entry = tt_poll(&sc.tt, pos.hash_key);

    if (entry.hash_key == pos.hash_key) {
        int entry_depth = tt_depth(entry.data);
        hash_move = tt_move(entry.data);

        if (!pv_node && entry_depth >= depth) {
            int entry_eval = eval_from_tt(tt_eval(entry.data), ss->ply);
            int entry_flag = tt_flag(entry.data);

            if (entry_flag == TT_EXACT ||
                (entry_flag == TT_LOWER && entry_eval >= beta) ||
                (entry_flag == TT_UPPER && entry_eval <= alpha)) {
                pv.push_back(hash_move);
                return entry_eval;
            }
        }
    }

    int movecount, value;
    movecount = generate(pos, ss->ml);

    score_moves(pos, ss, movecount, hash_move);

    int legal_moves = 0;
    int best_value = -INF;
    int old_alpha = alpha;

    Move move;
    Move best_move = 0;
    PV child_pv;
    while ((move = next_move(ss, movecount))) {
        child_pv.clear();
        Position npos = pos;

        make_move(npos, move);
        if (is_checked(npos, THEM)) {
            continue;
        }

        ++legal_moves;

        if (legal_moves == 1)
            value =
                -search(sc, npos, depth - 1, -beta, -alpha, ss + 1, child_pv);
        else
            value = -search<false>(sc, npos, depth - 1, -beta, -alpha, ss + 1,
                                   child_pv);

        if (value > best_value) {
            best_value = value;
            best_move = move;

            // Update PV
            pv.clear();
            pv.push_back(move);
            pv.insert(pv.begin() + 1, child_pv.begin(), child_pv.end());

            if (value > alpha) {
                alpha = value;
            }
        }
        if (value >= beta) {
#ifdef TESTING
            ++ss->stats->fail_highs;
            if (legal_moves == 1) ++ss->stats->first_move_fail_highs;
#endif
            if (!promotion_type(move) && move_type(move) != CAPTURE &&
                move_type(move) != ENPASSANT && ss->killers[0] != move) {
                ss->killers[1] = ss->killers[0];
                ss->killers[0] = move;
            }
            tt_add(&sc.tt, pos.hash_key, move, depth, TT_LOWER,
                   eval_to_tt(value, ss->ply));
            return beta;
        }
    }

    if (!legal_moves) {
        if (in_check)
            return -INF + ss->ply;
        else
            return 0;
    }

    // Add entry to transposition table
    int flag = alpha == old_alpha ? TT_UPPER : TT_EXACT;
    tt_add(&sc.tt, pos.hash_key, best_move, depth, flag,
           eval_to_tt(best_value, ss->ply));

#ifdef TESTING
    if (!ss->ply) {
        printf(
            "info string ordering = %lf\n",
            double(ss->stats->first_move_fail_highs) / ss->stats->fail_highs);
    }
#endif

    return alpha;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xc9ce
jmp 0xc9ed
leaq 0x768c(%rip), %rdi      # 0x14061
leaq 0x7688(%rip), %rsi      # 0x14064
movl $0x9d, %edx
leaq 0x78c1(%rip), %rcx      # 0x142a9
callq 0x3150
movq -0x18(%rbp), %rdi
callq 0xa740
testb $0x1, %al
jne 0xca10
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rax
movzbl (%rax), %esi
callq 0xa6f0
testb $0x1, %al
jne 0xca10
jmp 0xca1c
movl $0x0, -0x4(%rbp)
jmp 0xd0b8
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x6b80
andb $0x1, %al
movb %al, -0x31(%rbp)
testb $0x1, -0x31(%rbp)
je 0xca3b
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jg 0xca60
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x20(%rbp), %edx
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %r8
callq 0xb2c0
movl %eax, -0x4(%rbp)
jmp 0xd0b8
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x40, %eax
jl 0xca7d
movq -0x18(%rbp), %rdi
callq 0x3bb0
movl %eax, -0x4(%rbp)
jmp 0xd0b8
callq 0x3160
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
cmpb $0x0, (%rax)
je 0xcabd
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jl 0xcabd
movl $0x0, -0x4(%rbp)
jmp 0xd0b8
movq -0x30(%rbp), %rax
movq 0x810(%rax), %rax
movq (%rax), %rax
andq $0xfffff, %rax          # imm = 0xFFFFF
cmpq $0x0, %rax
jne 0xcb1b
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x78(%rcx), %rax
jle 0xcb19
movq -0x30(%rbp), %rax
movq 0x810(%rax), %rax
imulq $0x3e8, (%rax), %rax    # imm = 0x3E8
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rdx
subq 0x78(%rdx), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
leaq 0x7789(%rip), %rdi      # 0x1429b
movb $0x0, %al
callq 0x3030
jmp 0xcb1b
movq -0x30(%rbp), %rax
movq 0x810(%rax), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movl $0x0, -0x44(%rbp)
movq -0x10(%rbp), %rdi
addq $0x98, %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0xf300
movq %rax, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jne 0xcbed
movq -0x58(%rbp), %rdi
callq 0x97d0
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rdi
callq 0xc960
movl %eax, -0x44(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jl 0xcbeb
movq -0x58(%rbp), %rdi
callq 0xd3d0
movl %eax, %edi
movq -0x30(%rbp), %rax
movzbl (%rax), %esi
callq 0xd360
movl %eax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
callq 0xd460
movl %eax, -0x64(%rbp)
cmpl $0x2, -0x64(%rbp)
je 0xcbd1
cmpl $0x0, -0x64(%rbp)
jne 0xcbc3
movl -0x60(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0xcbd1
cmpl $0x1, -0x64(%rbp)
jne 0xcbe9
movl -0x60(%rbp), %eax
cmpl -0x20(%rbp), %eax
jg 0xcbe9
movq 0x10(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0xd0d0
movl -0x60(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xd0b8
jmp 0xcbeb
jmp 0xcbed
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x4, %rsi
callq 0x8b20
movl %eax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x68(%rbp), %edx
movl -0x44(%rbp), %ecx
callq 0xb000
movl $0x0, -0x70(%rbp)
movl $0xffff8ad0, -0x74(%rbp) # imm = 0xFFFF8AD0
movl -0x20(%rbp), %eax
movl %eax, -0x78(%rbp)
movl $0x0, -0x80(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xbcc0
movq -0x30(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xb1c0
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
je 0xcfb2
leaq -0x98(%rbp), %rdi
callq 0xc980
movq -0x18(%rbp), %rsi
leaq -0x100(%rbp), %rdi
callq 0x6a30
jmp 0xcc72
movl -0x7c(%rbp), %esi
leaq -0x100(%rbp), %rdi
callq 0x5900
jmp 0xcc83
leaq -0x100(%rbp), %rdi
movl $0x1, %esi
callq 0x6b80
movb %al, -0x145(%rbp)
jmp 0xcc9c
movb -0x145(%rbp), %al
testb $0x1, %al
jne 0xcca8
jmp 0xccf1
movl $0x2, -0x110(%rbp)
jmp 0xcf7b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x108(%rbp)
movl %eax, -0x10c(%rbp)
jmp 0xd0aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x108(%rbp)
movl %eax, -0x10c(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x6b60
jmp 0xd0aa
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
cmpl $0x1, -0x70(%rbp)
jne 0xcd50
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %edx
decl %edx
movl -0x24(%rbp), %ecx
negl %ecx
movl -0x20(%rbp), %r8d
negl %r8d
movq -0x30(%rbp), %r9
addq $0x818, %r9             # imm = 0x818
movq %rsp, %rax
leaq -0x98(%rbp), %rsi
movq %rsi, (%rax)
leaq -0x100(%rbp), %rsi
callq 0xbce0
movl %eax, -0x14c(%rbp)
jmp 0xcd41
movl -0x14c(%rbp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, -0x6c(%rbp)
jmp 0xcd9e
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %edx
decl %edx
movl -0x24(%rbp), %ecx
negl %ecx
movl -0x20(%rbp), %r8d
negl %r8d
movq -0x30(%rbp), %r9
addq $0x818, %r9             # imm = 0x818
movq %rsp, %rax
leaq -0x98(%rbp), %rsi
movq %rsi, (%rax)
leaq -0x100(%rbp), %rsi
callq 0xc9a0
movl %eax, -0x150(%rbp)
jmp 0xcd91
movl -0x150(%rbp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x74(%rbp), %eax
jle 0xce85
movl -0x6c(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x80(%rbp)
movq 0x10(%rbp), %rdi
callq 0xc980
movq 0x10(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
callq 0xd0d0
jmp 0xcdce
movq 0x10(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x6a80
movq %rax, -0x128(%rbp)
leaq -0x128(%rbp), %rdi
movl $0x1, %esi
callq 0xd1f0
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0xd230
leaq -0x98(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x6a80
movq -0x168(%rbp), %rdi
movq %rax, -0x130(%rbp)
callq 0x6ab0
movq -0x160(%rbp), %rdi
movq %rax, -0x138(%rbp)
movq -0x118(%rbp), %rsi
movq -0x130(%rbp), %rdx
movq -0x138(%rbp), %rcx
callq 0xd140
movq %rax, -0x158(%rbp)
jmp 0xce67
movq -0x158(%rbp), %rax
movq %rax, -0x140(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0xce83
movl -0x6c(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0xce85
movl -0x6c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0xcf71
movl -0x7c(%rbp), %edi
callq 0x68f0
cmpb $0x0, %al
jne 0xceeb
movl -0x7c(%rbp), %edi
callq 0x66e0
cmpl $0x2000, %eax           # imm = 0x2000
je 0xceeb
movl -0x7c(%rbp), %edi
callq 0x66e0
cmpl $0x3000, %eax           # imm = 0x3000
je 0xceeb
movq -0x30(%rbp), %rax
movl 0x804(%rax), %eax
cmpl -0x7c(%rbp), %eax
je 0xceeb
movq -0x30(%rbp), %rax
movl 0x804(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x808(%rax)
movl -0x7c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x804(%rax)
movq -0x10(%rbp), %rax
addq $0x98, %rax
movq %rax, -0x188(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x180(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x174(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x170(%rbp)
movl -0x6c(%rbp), %edi
movq -0x30(%rbp), %rax
movzbl (%rax), %esi
callq 0xd270
movl %eax, -0x16c(%rbp)
jmp 0xcf34
movl -0x16c(%rbp), %r9d
movl -0x170(%rbp), %ecx
movl -0x174(%rbp), %edx
movq -0x180(%rbp), %rsi
movq -0x188(%rbp), %rdi
xorl %r8d, %r8d
callq 0xf4e0
jmp 0xcf5f
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x110(%rbp)
jmp 0xcf7b
movl $0x0, -0x110(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x6b60
movl -0x110(%rbp), %eax
movl %eax, -0x18c(%rbp)
testl %eax, %eax
je 0xcfad
jmp 0xcf99
movl -0x18c(%rbp), %eax
subl $0x2, %eax
je 0xcc3b
jmp 0xd09c
jmp 0xcc3b
cmpl $0x0, -0x70(%rbp)
jne 0xcff2
testb $0x1, -0x31(%rbp)
je 0xcfdc
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
addl $0xffff8ad0, %eax       # imm = 0xFFFF8AD0
movl %eax, -0x4(%rbp)
movl $0x1, -0x110(%rbp)
jmp 0xd09c
movl $0x0, -0x4(%rbp)
movl $0x1, -0x110(%rbp)
jmp 0xd09c
movl -0x20(%rbp), %eax
movl -0x78(%rbp), %ecx
subl %ecx, %eax
setne %al
movzbl %al, %eax
incl %eax
movl %eax, -0x144(%rbp)
movq -0x10(%rbp), %rax
addq $0x98, %rax
movq %rax, -0x1b0(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x1a8(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x19c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x198(%rbp)
movl -0x144(%rbp), %eax
movl %eax, -0x194(%rbp)
movl -0x74(%rbp), %edi
movq -0x30(%rbp), %rax
movzbl (%rax), %esi
callq 0xd270
movl %eax, -0x190(%rbp)
jmp 0xd05d
movl -0x190(%rbp), %r9d
movl -0x194(%rbp), %r8d
movl -0x198(%rbp), %ecx
movl -0x19c(%rbp), %edx
movq -0x1a8(%rbp), %rsi
movq -0x1b0(%rbp), %rdi
callq 0xf4e0
jmp 0xd08c
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x110(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xc610
jmp 0xd0b8
leaq -0x98(%rbp), %rdi
callq 0xc610
jmp 0xd0c4
movl -0x4(%rbp), %eax
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
movq -0x108(%rbp), %rdi
callq 0x3350