// File: /workspace/github_cpp_makefile_star2_O3/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 %ecx, 0x24(%rsp)
cmpl %edx, %esi
jge 0xc54f
movl %r9d, %r12d
movl %r8d, %ebx
movl %edx, %r15d
movl %esi, %ebp
movq %rdi, %r14
movq 0x870751(%rip), %r13    # 0x87c938
cmpq $0x2710, %r13           # imm = 0x2710
setb %al
orb 0x870749(%rip), %al     # 0x87c940
je 0xc202
movl %ebx, 0x20(%rsp)
incq %r13
jmp 0xc21a
callq 0xdf08
xorl %r13d, %r13d
cmpl 0x870720(%rip), %eax    # 0x87c930
jge 0xc4b2
movl %ebx, 0x20(%rsp)
movq %r13, 0x870717(%rip)    # 0x87c938
movq %r14, %rdi
xorl %esi, %esi
callq 0x58f7
movl %eax, %ebx
movl %eax, %edi
movq %r14, %rsi
callq 0x13128
testb %al, %al
je 0xc56e
cmpl %ebp, %ebx
jle 0xc24e
movl %ebx, %ebp
cmpl %r15d, %ebx
jge 0xc4c9
cmpl $0x63, 0x24(%rsp)
jle 0xc25d
movl %ebp, %r13d
jmp 0xc4f6
movl %r15d, 0x1c(%rsp)
movq 0xb8(%rsp), %rsi
subq $0x8, %rsp
xorl %r13d, %r13d
leaq 0x40(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x1
callq 0xb2d7
addq $0x10, %rsp
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x8b70
testb %al, %al
je 0xc4ce
movl %r12d, 0x34(%rsp)
movq 0xc0(%rsp), %r12
addl $0xc8, %ebx
movl 0x1c(%rsp), %eax
negl %eax
movl %eax, 0x30(%rsp)
incl 0x24(%rsp)
incl 0x20(%rsp)
xorl %eax, %eax
leaq 0x38(%rsp), %r15
xorl %r13d, %r13d
movl %eax, 0x18(%rsp)
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0x910b
testb %al, %al
je 0xc511
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x7883
testb %al, %al
jne 0xc31b
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x786f
testl %eax, %eax
je 0xc530
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x30e1
movl 0x138(%r14), %edi
callq 0xdf74
movq %r14, %rdi
movl %eax, %esi
callq 0xaf41
testb %al, %al
je 0xc370
movq %r14, %rdi
movq %r12, %rsi
callq 0x334b
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x8b70
testb %al, %al
jne 0xc2dd
jmp 0xc4d2
cmpb $0x0, 0x34(%rsp)
jne 0xc423
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x786f
testl %eax, %eax
jne 0xc3a6
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x7879
movl %eax, %edi
callq 0x4f30
addl %ebx, %eax
cmpl %ebp, %eax
jl 0xc405
movq %r13, 0x28(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x786f
testl %eax, %eax
jne 0xc419
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x7865
movl %eax, %edi
callq 0x4f30
movl %eax, %r13d
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x7879
movl %eax, %edi
callq 0x4f30
cmpl %eax, %r13d
jle 0xc419
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0xc966
testl %eax, %eax
movq 0x28(%rsp), %r13
jns 0xc419
movq %r14, %rdi
movq %r12, %rsi
callq 0x334b
incl 0xc(%rsp)
jmp 0xc353
movl 0xc(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0xc428
movq %r13, 0x28(%rsp)
movq 0xb0(%rsp), %rax
incq 0x20(%rax)
movl %ebp, %edx
negl %edx
subq $0x8, %rsp
xorl %r13d, %r13d
movq %r14, %rdi
movl 0x38(%rsp), %esi
movl 0x2c(%rsp), %ecx
movl 0x28(%rsp), %r8d
xorl %r9d, %r9d
pushq %r12
pushq 0x78(%rsp)
pushq %rax
callq 0xc1b8
addq $0x20, %rsp
movl %eax, 0xc(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x334b
cmpb $0x0, 0x8704c9(%rip)    # 0x87c941
jne 0xc4f6
movl 0xc(%rsp), %eax
negl %eax
cmpl %eax, %ebp
jge 0xc48c
movl %eax, %ebp
cmpl 0x1c(%rsp), %eax
jge 0xc508
movq 0x28(%rsp), %r13
incl %r13d
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x8b70
testb %al, %al
movl 0x18(%rsp), %eax
jne 0xc2d5
jmp 0xc4d6
movb $0x1, 0x870488(%rip)    # 0x87c941
leaq 0x1fd4e(%rip), %rdi     # 0x2c20e
xorl %eax, %eax
callq 0x65d9
jmp 0xc4f6
movl %r15d, %r13d
jmp 0xc4f6
xorl %eax, %eax
jmp 0xc4d6
movl 0xc(%rsp), %eax
addl %eax, %r13d
movq %r14, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xe6d3
movq %r13, %rcx
movl %ebp, %r13d
cmpl %eax, %ecx
jne 0xc58d
movl %r13d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x1c(%rsp), %eax
movl %eax, %r13d
jmp 0xc4f6
leaq 0x1fb4a(%rip), %rdi     # 0x2c062
leaq 0x1fa47(%rip), %rsi     # 0x2bf66
leaq 0x1fc8c(%rip), %rcx     # 0x2c1b2
movl $0x16e, %edx            # imm = 0x16E
callq 0x20d0
leaq 0x1fd18(%rip), %rdi     # 0x2c24f
leaq 0x1fa28(%rip), %rsi     # 0x2bf66
leaq 0x1fc6d(%rip), %rcx     # 0x2c1b2
movl $0x16f, %edx            # imm = 0x16F
callq 0x20d0
leaq 0x1fba2(%rip), %rdi     # 0x2c0f8
leaq 0x1fa09(%rip), %rsi     # 0x2bf66
leaq 0x1fc4e(%rip), %rcx     # 0x2c1b2
movl $0x147, %edx            # imm = 0x147
callq 0x20d0
leaq 0x1fcb8(%rip), %rdi     # 0x2c22d
leaq 0x1f9ea(%rip), %rsi     # 0x2bf66
leaq 0x1fc2f(%rip), %rcx     # 0x2c1b2
movl $0x158, %edx            # imm = 0x158
callq 0x20d0
leaq 0x1fce2(%rip), %rdi     # 0x2c276
leaq 0x1f9cb(%rip), %rsi     # 0x2bf66
leaq 0x1fc10(%rip), %rcx     # 0x2c1b2
movl $0x19f, %edx            # imm = 0x19F
callq 0x20d0