// File: /workspace/github_cpp_makefile_star2_O2/jswaff[P]prophet3/src/search.cpp
// Function: qsearch(position*, int, int, int, int, bool, search_stats*, unsigned long long*, undo*)
int32 qsearch(position *pos,int32 alpha,int32 beta,int32 ply,int32 qply,
        bool incheck,search_stats *stats,move *startp,undo *undo_stack) {

    assert(alpha < beta);

    // time check
    if (!analysis_mode && nodes_last_time_check >= 10000) {
        if (milli_timer() >= stop_time) {
            abort_search = true;
            printd("# Aborting qsearch on time...\n");
            return 0;
        }
        nodes_last_time_check = 0;
    } else {
        nodes_last_time_check++;
    }

    // TODO: try hash probe

    int32 stand_pat = eval(pos,false);
    assert(test_eval_symmetry(stand_pat,pos));

    if (stand_pat > alpha) {
        if (stand_pat >= beta) {
            return beta;
        }
        // our static evaluation will serve as the lower bound
        alpha = stand_pat;
    }


    if (ply >= MAX_PLY) {
        return alpha;
    }

    // get ready to search
    uint32 moves_searched=0,moves_pruned=0;
    move_order_dto mo_dto = build_move_order_dto(startp,0,0,0,0,true);

    move *mp;
    //bool just_caps_promos = true; //!incheck || qply>=3;
    while (next(pos,&mp,&mo_dto)) {
        assert(good_move(pos,*mp));
        assert(is_capture(*mp) || get_promopiece(*mp));
        /*if (just_caps_promos && !is_capture(*mp) && !get_promopiece(*mp)) {
            continue;
        }*/

        apply_move(pos,*mp,undo_stack);
        if (in_check(pos,opposite_player(pos->player))) {
            undo_last_move(pos,undo_stack);
            continue;
        }

        // can this capture possibly raise alpha?
        if (!incheck && !get_promopiece(*mp)
                && (piece_val(get_captured_piece(*mp)) + pawn_val*2 + stand_pat) < alpha) {
            undo_last_move(pos,undo_stack);
            moves_pruned++;
            continue;
        }

        // if not a promising capture just skip
        if (!incheck && !get_promopiece(*mp)
                && piece_val(get_piece(*mp)) > piece_val(get_captured_piece(*mp))
                && see(pos,*mp) < 0) {
            undo_last_move(pos,undo_stack);
            moves_pruned++;
            continue;
        }

        bool gives_check = false; // in_check(pos,pos->player);
        stats->qnodes++; // avoids double counting leaf nodes
        int32 score = -qsearch(pos,-beta,-alpha,ply+1,qply+1,gives_check,stats,mo_dto.endp,undo_stack);
        moves_searched++;

        // restore the position
        undo_last_move(pos,undo_stack);

        if (abort_search) {
            return 0;
        }

        if (score > alpha) {
            if (score >= beta) {
                return beta;
            }
            alpha = score;
        }
    }

    assert(moves_searched+moves_pruned==num_moves(pos,true,false));

    return alpha;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %r9d, 0x34(%rsp)
cmpl %edx, %esi
jge 0xbb6c
movl %r8d, %ebx
movl %ecx, %r12d
movl %edx, %r13d
movl %esi, %r15d
movq %rdi, %r14
movq 0x87411e(%rip), %rbp    # 0x87f938
cmpq $0x2710, %rbp           # imm = 0x2710
setb %al
orb 0x874116(%rip), %al     # 0x87f940
je 0xb835
movl %ebx, 0x18(%rsp)
incq %rbp
jmp 0xb84c
callq 0xd470
xorl %ebp, %ebp
cmpl 0x8740ee(%rip), %eax    # 0x87f930
jge 0xbb03
movl %ebx, 0x18(%rsp)
movq %rbp, 0x8740e5(%rip)    # 0x87f938
movq %r14, %rdi
xorl %esi, %esi
callq 0x547b
movl %eax, %ebx
movl %eax, %edi
movq %r14, %rsi
callq 0x120c2
testb %al, %al
je 0xbb8b
cmpl %r15d, %ebx
movl %r12d, %eax
jle 0xb885
movl %ebx, %r15d
cmpl %r13d, %ebx
jge 0xbb1a
cmpl $0x63, %eax
jle 0xb892
movl %r15d, %ebp
jmp 0xbb1d
movl %r15d, 0x4(%rsp)
movq 0xc0(%rsp), %r12
movq 0xb8(%rsp), %rsi
subq $0x8, %rsp
movq $0x0, 0x28(%rsp)
movl %r13d, %ebp
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movl %eax, %r15d
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x1
callq 0xa96b
popq %rax
popq %rcx
addl $0xc8, %ebx
movl %ebp, 0x1c(%rsp)
negl %ebp
movl %ebp, 0x28(%rsp)
incl %r15d
movl %r15d, 0x2c(%rsp)
incl 0x18(%rsp)
leaq 0x8(%rsp), %r15
xorl %ebp, %ebp
movl 0x4(%rsp), %ecx
negl %ecx
movl %ecx, 0x30(%rsp)
movl %ebp, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x8490
testb %al, %al
je 0xbac7
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0x89da
testb %al, %al
je 0xbb2e
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x739a
testb %al, %al
jne 0xb95a
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x7386
testl %eax, %eax
je 0xbb4d
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x2fa5
movl 0x138(%r14), %edi
callq 0xd4d8
movq %r14, %rdi
movl %eax, %esi
callq 0xa61f
testb %al, %al
je 0xb997
movq %r14, %rdi
movq %r12, %rsi
callq 0x31f8
jmp 0xb906
cmpb $0x0, 0x34(%rsp)
jne 0xba3c
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x7386
testl %eax, %eax
jne 0xb9cf
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x7390
movl %eax, %edi
callq 0x4c9c
addl %ebx, %eax
cmpl 0x4(%rsp), %eax
jl 0xba26
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x7386
testl %eax, %eax
jne 0xba38
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x737c
movl %eax, %edi
callq 0x4c9c
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x7390
movl %eax, %edi
callq 0x4c9c
cmpl %eax, 0x14(%rsp)
jle 0xba38
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0xbf0b
testl %eax, %eax
jns 0xba38
movq %r14, %rdi
movq %r12, %rsi
callq 0x31f8
incl %ebp
jmp 0xb906
movl %ebp, 0x10(%rsp)
movq 0xb0(%rsp), %rax
incq 0x20(%rax)
subq $0x8, %rsp
xorl %ebp, %ebp
movq %r14, %rdi
movl 0x30(%rsp), %esi
movl 0x38(%rsp), %edx
movl 0x34(%rsp), %ecx
movl 0x20(%rsp), %r8d
xorl %r9d, %r9d
pushq %r12
pushq 0x78(%rsp)
pushq %rax
callq 0xb7e9
addq $0x20, %rsp
movl %eax, 0x14(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x31f8
cmpb $0x0, 0x873eb6(%rip)    # 0x87f941
jne 0xbb1d
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
movl 0x14(%rsp), %ecx
negl %ecx
cmpl %ecx, 0x4(%rsp)
movl 0x10(%rsp), %ebp
jge 0xb902
movl %ecx, 0x4(%rsp)
cmpl 0x1c(%rsp), %ecx
jl 0xb8f8
movl 0x1c(%rsp), %eax
movl %eax, %ebp
jmp 0xbb1d
addl 0x20(%rsp), %ebp
pushq $0x1
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xdb11
movl 0x4(%rsp), %ecx
movl %ebp, %edx
movl %ecx, %ebp
cmpl %eax, %edx
je 0xbb1d
leaq 0x1e73b(%rip), %rdi     # 0x2a226
leaq 0x1e424(%rip), %rsi     # 0x29f16
leaq 0x1e669(%rip), %rcx     # 0x2a162
movl $0x19f, %edx            # imm = 0x19F
callq 0x20c0
movb $0x1, 0x873e37(%rip)    # 0x87f941
leaq 0x1e6ad(%rip), %rdi     # 0x2a1be
xorl %eax, %eax
callq 0x60fd
jmp 0xbb1d
movl %r13d, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e4dd(%rip), %rdi     # 0x2a012
leaq 0x1e3da(%rip), %rsi     # 0x29f16
leaq 0x1e61f(%rip), %rcx     # 0x2a162
movl $0x16e, %edx            # imm = 0x16E
callq 0x20c0
leaq 0x1e6ab(%rip), %rdi     # 0x2a1ff
leaq 0x1e3bb(%rip), %rsi     # 0x29f16
leaq 0x1e600(%rip), %rcx     # 0x2a162
movl $0x16f, %edx            # imm = 0x16F
callq 0x20c0
leaq 0x1e535(%rip), %rdi     # 0x2a0a8
leaq 0x1e39c(%rip), %rsi     # 0x29f16
leaq 0x1e5e1(%rip), %rcx     # 0x2a162
movl $0x147, %edx            # imm = 0x147
callq 0x20c0
leaq 0x1e64b(%rip), %rdi     # 0x2a1dd
leaq 0x1e37d(%rip), %rsi     # 0x29f16
leaq 0x1e5c2(%rip), %rcx     # 0x2a162
movl $0x158, %edx            # imm = 0x158
callq 0x20c0