// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %r8d, -0x30(%rbp)
movl %ecx, -0x34(%rbp)
testq %r9, %r9
je 0x6ebf
movq %r9, %r14
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %r13
movq 0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movq %rsi, %rdi
callq 0x5ab9
xorl %ebx, %ebx
testb %al, %al
jne 0x6ae0
movzbl (%r14), %esi
movq %r15, %rdi
callq 0x5a91
testb %al, %al
je 0x6af4
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
xorl %esi, %esi
callq 0x40fd
movzbl %al, %ecx
addl %r12d, %ecx
jle 0x6b25
cmpb $0x40, (%r14)
jb 0x6b4a
movq %r15, %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x27dd
movq %r13, %rdi
movq %r15, %rsi
movl -0x34(%rbp), %edx
movl -0x30(%rbp), %ecx
movq %r14, %r8
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6032
movb %al, -0x29(%rbp)
movq %rcx, -0x58(%rbp)
callq 0x2120
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, %rcx
cmpb $0x0, (%r14)
je 0x6b78
xorl %ebx, %ebx
cmpq 0x80(%r13), %rcx
jge 0x6ae0
movq 0x810(%r14), %rax
movq (%rax), %rdx
testl $0xfffff, %edx          # imm = 0xFFFFF
jne 0x6bb7
subq 0x78(%r13), %rcx
jle 0x6bb7
imulq $0x3e8, %rdx, %rax      # imm = 0x3E8
xorl %edx, %edx
divq %rcx
leaq 0x44e8(%rip), %rdi      # 0xb08b
movq %rax, %rsi
xorl %eax, %eax
callq 0x2030
movq 0x810(%r14), %rax
movq (%rax), %rdx
incq %rdx
movq %rdx, (%rax)
movq %r13, -0x98(%rbp)
leaq 0x98(%r13), %rdi
movq 0x48(%r15), %rsi
movq %rdi, -0x88(%rbp)
callq 0x7407
movl $0x0, -0x40(%rbp)
movl $0x0, %r12d
cmpq 0x48(%r15), %rdx
jne 0x6c14
movq %rax, %r13
movq %rax, %rdi
callq 0x51d1
movq %r13, %r12
shrq $0x29, %r12
andl $0x3ffff, %r12d         # imm = 0x3FFFF
movl %r12d, -0x68(%rbp)
cmpl -0x58(%rbp), %eax
jge 0x6e28
leaq 0x4(%r14), %rsi
movq %r15, %rdi
callq 0x49ae
leaq -0x64(%rbp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
movq %r14, %rsi
movl %eax, %edx
movl %r12d, %ecx
callq 0x5e74
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rax
andq $0x0, 0x10(%rax)
movaps %xmm0, (%rax)
movq -0x58(%rbp), %rax
decl %eax
movl %eax, -0x4c(%rbp)
movl -0x30(%rbp), %eax
negl %eax
movl %eax, -0x48(%rbp)
leaq 0x818(%r14), %rax
movq %rax, -0x90(%rbp)
movl $0xffff8ad0, -0x3c(%rbp) # imm = 0xFFFF8AD0
leaq -0x100(%rbp), %r12
movl $0x0, -0x44(%rbp)
movl -0x34(%rbp), %ebx
movq %r14, %rdi
leaq -0x64(%rbp), %rsi
callq 0x5fa6
movl %eax, -0x38(%rbp)
testl %eax, %eax
je 0x6d82
movq -0x80(%rbp), %rax
cmpq %rax, -0x78(%rbp)
je 0x6ca1
movq %rax, -0x78(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x40d2
movl -0x38(%rbp), %esi
movq %r12, %rdi
callq 0x3988
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x40fd
testb %al, %al
jne 0x6d71
movl %ebx, %r8d
negl %r8d
cmpl $0x0, -0x40(%rbp)
movq -0x98(%rbp), %rdi
je 0x6cfc
leaq -0x80(%rbp), %rax
movq %rax, (%rsp)
movq %r12, %rsi
movl -0x4c(%rbp), %edx
movl -0x48(%rbp), %ecx
movq -0x90(%rbp), %r9
callq 0x6a8a
jmp 0x6d19
leaq -0x80(%rbp), %rax
movq %rax, (%rsp)
movq %r12, %rsi
movl -0x4c(%rbp), %edx
movl -0x48(%rbp), %ecx
movq -0x90(%rbp), %r9
callq 0x658b
movl %eax, %r13d
negl %r13d
cmpl %r13d, -0x3c(%rbp)
jge 0x6d68
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
cmpq %rax, 0x8(%rdi)
je 0x6d36
movq %rax, 0x8(%rdi)
movl -0x38(%rbp), %eax
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rsi
callq 0x6f0a
movq -0x60(%rbp), %rdi
movq (%rdi), %rsi
addq $0x4, %rsi
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %rcx
callq 0x6f30
cmpl %r13d, %ebx
cmovlel %r13d, %ebx
movl %r13d, -0x3c(%rbp)
cmpl -0x30(%rbp), %r13d
jge 0x6dca
incl -0x40(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x41a8
jmp 0x6c7c
cmpl $0x0, -0x40(%rbp)
je 0x6e62
xorl %r12d, %r12d
cmpl -0x34(%rbp), %ebx
setne %r12b
incl %r12d
movq 0x48(%r15), %r15
movzbl (%r14), %esi
movl -0x3c(%rbp), %edi
callq 0x6f50
movq -0x88(%rbp), %rdi
movq %r15, %rsi
movl -0x44(%rbp), %edx
movq -0x58(%rbp), %rcx
movl %r12d, %r8d
movl %eax, %r9d
callq 0x7540
jmp 0x6e74
movl -0x38(%rbp), %ebx
testl $0x38000, %ebx          # imm = 0x38000
jne 0x6dec
movl %ebx, %eax
orl $0x1000, %eax           # imm = 0x1000
andl $0x7000, %eax           # imm = 0x7000
cmpl $0x3000, %eax           # imm = 0x3000
jne 0x6e9d
movq 0x48(%r15), %r15
movzbl (%r14), %esi
movl %r13d, %edi
callq 0x6f50
movq -0x88(%rbp), %rdi
movq %r15, %rsi
movl %ebx, %edx
movq -0x58(%rbp), %rcx
xorl %r8d, %r8d
movl %eax, %r9d
callq 0x7540
leaq -0xb0(%rbp), %rdi
callq 0x41a8
movl -0x30(%rbp), %ebx
jmp 0x6e74
movq %r13, %rdi
callq 0x6fce
movzbl (%r14), %esi
movl %eax, %edi
callq 0x6f8e
movl %eax, %ebx
movq %r13, %rdi
callq 0x7020
cmpl $0x2, %eax
je 0x6e8b
cmpl $0x1, %eax
je 0x6e82
testl %eax, %eax
jne 0x6c14
cmpl -0x30(%rbp), %ebx
jl 0x6c14
jmp 0x6e8b
xorl %ebx, %ebx
cmpb $0x0, -0x29(%rbp)
je 0x6e74
movzbl (%r14), %ebx
addl $0xffff8ad0, %ebx       # imm = 0xFFFF8AD0
leaq -0x80(%rbp), %rdi
callq 0x6a76
jmp 0x6ae0
cmpl -0x34(%rbp), %ebx
jg 0x6c14
leaq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdi
callq 0x6f0a
jmp 0x6ae0
movl 0x804(%r14), %eax
cmpl %ebx, %eax
je 0x6dec
movl %eax, 0x808(%r14)
movl %ebx, 0x804(%r14)
jmp 0x6dec
leaq 0x3f9a(%rip), %rdi      # 0xae60
leaq 0x3f96(%rip), %rsi      # 0xae63
leaq 0x41c5(%rip), %rcx      # 0xb099
movl $0x9d, %edx
callq 0x2110
jmp 0x6ee9
jmp 0x6ee4
jmp 0x6ee9
movq %rax, %rbx
jmp 0x6ef8
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x41a8
leaq -0x80(%rbp), %rdi
callq 0x6a76
movq %rbx, %rdi
callq 0x2290
nop