// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movl %r8d, 0x2c(%rsp)
cmpl %edx, %esi
jge 0xc59a
movl %r9d, %ebp
movl %ecx, %r12d
movl %edx, %r13d
movl %esi, %ebx
movq %rdi, %r14
movq 0x870724(%rip), %r15    # 0x87c938
cmpq $0x2710, %r15           # imm = 0x2710
setb %al
orb 0x87071c(%rip), %al     # 0x87c940
je 0xc22b
incq %r15
jmp 0xc23f
callq 0xdf58
xorl %r15d, %r15d
cmpl 0x8706f7(%rip), %eax    # 0x87c930
jge 0xc52d
movq %r15, 0x8706f2(%rip)    # 0x87c938
movq %r14, %rdi
xorl %esi, %esi
callq 0x5846
movl %eax, %r15d
movl %eax, %edi
movq %r14, %rsi
callq 0x131dc
testb %al, %al
je 0xc5b9
cmpl %ebx, %r15d
movl %r15d, %eax
movl %r12d, %r15d
jle 0xc27b
movl %eax, %ebx
cmpl %r13d, %eax
jge 0xc544
movl %eax, 0x28(%rsp)
cmpl $0x63, %r15d
jle 0xc28d
movl %ebx, %r15d
jmp 0xc547
movl %ebp, 0x24(%rsp)
movl %ebx, 0x1c(%rsp)
movl %r13d, %ebp
movq 0xd0(%rsp), %r13
movq 0xc8(%rsp), %rsi
subq $0x8, %rsp
movq $0x0, 0x38(%rsp)
leaq 0x50(%rsp), %r12
movq %r12, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x1
callq 0xb2db
addq $0x10, %rsp
addl $0xc8, 0x28(%rsp)
movl %ebp, 0x3c(%rsp)
negl %ebp
movl %ebp, 0x40(%rsp)
incl %r15d
movl %r15d, 0x44(%rsp)
incl 0x2c(%rsp)
leaq 0x10(%rsp), %rbp
xorl %ebx, %ebx
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x8adc
testb %al, %al
je 0xc4f0
movl %ebx, 0xc(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0x9052
testb %al, %al
je 0xc55c
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x77df
testb %al, %al
jne 0xc34f
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x77cb
testl %eax, %eax
je 0xc57b
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
movq %r13, %rdx
callq 0x30e3
movl 0x138(%r14), %edi
callq 0xdfc4
movq %r14, %rdi
movl %eax, %esi
callq 0xaf39
testb %al, %al
je 0xc3a2
movq %r14, %rdi
movq %r13, %rsi
callq 0x3341
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x8adc
testb %al, %al
jne 0xc311
jmp 0xc4f0
cmpb $0x0, 0x24(%rsp)
jne 0xc450
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x77cb
testl %eax, %eax
jne 0xc3dc
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x77d5
movl %eax, %edi
callq 0x4ed8
addl 0x28(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jl 0xc43a
cmpb $0x0, 0x24(%rsp)
jne 0xc44c
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x77cb
testl %eax, %eax
jne 0xc44c
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x77c1
movl %eax, %edi
callq 0x4ed8
movl %eax, 0x20(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x77d5
movl %eax, %edi
callq 0x4ed8
cmpl %eax, 0x20(%rsp)
jle 0xc44c
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0xc971
testl %eax, %eax
jns 0xc44c
movq %r14, %rdi
movq %r13, %rsi
callq 0x3341
incl %ebx
jmp 0xc387
movl %ebx, 0xc(%rsp)
movq 0xc0(%rsp), %rax
incq 0x20(%rax)
movl 0x1c(%rsp), %ebx
movl %ebx, %edx
negl %edx
subq $0x8, %rsp
movq %r14, %rdi
movl 0x48(%rsp), %esi
movl 0x4c(%rsp), %ecx
movl 0x34(%rsp), %r8d
xorl %r9d, %r9d
pushq %r13
pushq 0x88(%rsp)
pushq %rax
callq 0xc1e1
addq $0x20, %rsp
movl %eax, 0x20(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x3341
cmpb $0x0, 0x87049d(%rip)    # 0x87c941
je 0xc4b1
xorl %r15d, %r15d
movl 0xc(%rsp), %ebx
xorl %ecx, %ecx
jmp 0xc4da
movl 0x20(%rsp), %edx
negl %edx
movb $0x1, %cl
cmpl %edx, %ebx
jge 0xc4cf
movl 0x3c(%rsp), %eax
cmpl %eax, %edx
movl 0xc(%rsp), %ebx
jge 0xc4d5
movl %edx, 0x1c(%rsp)
jmp 0xc4da
movl 0xc(%rsp), %ebx
jmp 0xc4da
xorl %ecx, %ecx
movl %eax, %r15d
movq 0x30(%rsp), %rax
incl %eax
movq %rax, 0x30(%rsp)
testb %cl, %cl
jne 0xc2f7
jmp 0xc547
addl 0x30(%rsp), %ebx
movq %r14, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0xe78d
movl 0x1c(%rsp), %ecx
movl %ecx, %r15d
cmpl %eax, %ebx
je 0xc547
leaq 0x1fce1(%rip), %rdi     # 0x2c1f6
leaq 0x1f9ca(%rip), %rsi     # 0x2bee6
leaq 0x1fc0f(%rip), %rcx     # 0x2c132
movl $0x19f, %edx            # imm = 0x19F
callq 0x20d0
movb $0x1, 0x87040d(%rip)    # 0x87c941
leaq 0x1fc53(%rip), %rdi     # 0x2c18e
xorl %eax, %eax
callq 0x652d
jmp 0xc547
movl %r13d, %r15d
movl %r15d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1fa7f(%rip), %rdi     # 0x2bfe2
leaq 0x1f97c(%rip), %rsi     # 0x2bee6
leaq 0x1fbc1(%rip), %rcx     # 0x2c132
movl $0x16e, %edx            # imm = 0x16E
callq 0x20d0
leaq 0x1fc4d(%rip), %rdi     # 0x2c1cf
leaq 0x1f95d(%rip), %rsi     # 0x2bee6
leaq 0x1fba2(%rip), %rcx     # 0x2c132
movl $0x16f, %edx            # imm = 0x16F
callq 0x20d0
leaq 0x1fad7(%rip), %rdi     # 0x2c078
leaq 0x1f93e(%rip), %rsi     # 0x2bee6
leaq 0x1fb83(%rip), %rcx     # 0x2c132
movl $0x147, %edx            # imm = 0x147
callq 0x20d0
leaq 0x1fbed(%rip), %rdi     # 0x2c1ad
leaq 0x1f91f(%rip), %rsi     # 0x2bee6
leaq 0x1fb64(%rip), %rcx     # 0x2c132
movl $0x158, %edx            # imm = 0x158
callq 0x20d0