// File: /workspace/github_cpp_makefile_star2_O2/jswaff[P]prophet3/src/search.cpp
// Function: search(position*, move_line*, int, int, int, search_stats*, unsigned long long*, undo*, bool)
int32 search(position *pos,move_line *parent_pv,int32 alpha,int32 beta,int32 depth,
        search_stats *stats,move *move_stack,undo *undo_stack,bool show_thinking) {

    assert(pos);
    assert(parent_pv);
    assert(stats);
    assert(depth > 0);
    assert(beta > alpha);

    stats->nodes++;

    move_line pv; pv.n=0;

    // search each move
    int32 moves_searched=0;
    uint64 hash_val = get_hash_entry(&htbl,pos->hash_key);
    move_order_dto mo_dto = build_move_order_dto(move_stack,
            stats->last_pv.n > 0 ? stats->last_pv.mv[0] : 0,
            get_hash_entry_move(hash_val),
            killer1[0],killer2[0],false);
    move *mp;
    while (next(pos,&mp,&mo_dto)) {
        assert(good_move(pos,*mp));
        apply_move(pos,*mp,undo_stack);
        if (in_check(pos,opposite_player(pos->player))) {
            undo_last_move(pos,undo_stack);
            continue;
        }

        if (moves_searched==0) {
            stats->first_line_searched.mv[0] = *mp;
            stats->first_line_searched.n = 1;
        }

        bool gives_check = in_check(pos,pos->player);
        int32 ext = extend(pos,*mp,gives_check);
        int32 score;
        if (moves_searched==0) {
            score = -search_helper(pos,&pv,-beta,-alpha,depth-1+ext,1,
                    true,gives_check,false,stats,mo_dto.endp,undo_stack);
        } else {
            /*score = -search_helper(pos,&pv,-(alpha+1),-alpha,depth-1+ext,1,
                    false,gives_check,(ext==0),stats,endp,undo_stack);
            if (score > alpha && score < beta) {*/
                score = -search_helper(pos,&pv,-beta,-alpha,depth-1+ext,1,
                    false,gives_check,(ext==0),stats,mo_dto.endp,undo_stack);
            //}
        }
        moves_searched++;

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

        // If depth==1, we need to continue with the search to ensure we have something to play.
        if (depth > 1 && abort_search) {
            return 0;
        }

        if (score > alpha) {
            if (score >= beta) {
                return beta;
            }
            alpha = score;
            update_parent_pv(parent_pv,&pv,*mp);
            if (show_thinking) {
                print_thinking_output(parent_pv,depth,alpha,start_time,stats->nodes);
            }
        }
    }

    if (moves_searched==0) {
        return in_check(pos,pos->player) ? -CHECKMATE : DRAWSCORE;
    }

    return alpha;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
movq %r8, 0x28(%rsp)
movl %ecx, 0xc(%rsp)
movl %edx, 0x8(%rsp)
testq %rdi, %rdi
je 0xad3e
movq %rsi, %r12
testq %rsi, %rsi
je 0xad57
movq %r9, %r15
testq %r9, %r9
je 0xad70
cmpl $0x0, 0x28(%rsp)
jle 0xad89
movl 0x8(%rsp), %eax
cmpl %eax, 0xc(%rsp)
jle 0xada2
movq %rdi, %rbx
movq 0x3e0(%rsp), %r14
incq 0x18(%r15)
andl $0x0, 0x80(%rsp)
movq 0x1b8(%rdi), %rsi
leaq 0xf0456(%rip), %rdi     # 0xfaef0
callq 0x5e2e
xorl %r13d, %r13d
cmpl $0x0, 0x28(%r15)
movq %r15, 0x20(%rsp)
movq %r12, 0x30(%rsp)
jle 0xaab9
movq 0x30(%r15), %r15
jmp 0xaabc
xorl %r15d, %r15d
movq 0x3e8(%rsp), %r12
movq %rax, %rdi
callq 0x5d41
leaq 0xf047d(%rip), %rcx     # 0xfaf50
movq (%rcx), %r8
leaq 0xf0793(%rip), %rcx     # 0xfb270
movq (%rcx), %r9
subq $0x8, %rsp
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rax, %rcx
pushq $0x0
callq 0xa96b
popq %rax
popq %rcx
movl 0xc(%rsp), %eax
negl %eax
movl %eax, 0x18(%rsp)
movq 0x28(%rsp), %rax
decl %eax
movq %rax, 0x38(%rsp)
leaq 0x10(%rsp), %r15
movl 0x8(%rsp), %eax
negl %eax
movl %eax, 0x1c(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x8490
testb %al, %al
je 0xacd8
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x89da
testb %al, %al
je 0xad21
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x2fa5
movl 0x138(%rbx), %edi
callq 0xd4d8
movq %rbx, %rdi
movl %eax, %esi
callq 0xa61f
testb %al, %al
je 0xab8a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31f8
jmp 0xab23
testl %r13d, %r13d
jne 0xabad
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x20(%rsp), %rcx
movq %rax, 0x358(%rcx)
movl $0x1, 0x350(%rcx)
movl 0x138(%rbx), %esi
movq %rbx, %rdi
callq 0xa61f
movq 0x10(%rsp), %rcx
movq (%rcx), %rsi
movzbl %al, %r14d
movq %rbx, %rdi
movl %r14d, %edx
callq 0x59f8
movq 0x38(%rsp), %rcx
leal (%rcx,%rax), %r8d
testl %r13d, %r13d
je 0xac12
xorl %r10d, %r10d
testl %eax, %eax
sete %r10b
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
movl 0x18(%rsp), %edx
movl 0x1c(%rsp), %ecx
pushq $0x1
popq %r9
pushq %r12
pushq 0x78(%rsp)
pushq 0x30(%rsp)
pushq %r10
pushq %r14
pushq $0x0
jmp 0xac39
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
movl 0x18(%rsp), %edx
movl 0x1c(%rsp), %ecx
pushq $0x1
popq %r9
pushq %r12
pushq 0x78(%rsp)
pushq 0x30(%rsp)
pushq $0x0
pushq %r14
pushq $0x1
callq 0xadbe
addq $0x30, %rsp
movl %eax, %r14d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31f8
cmpl $0x1, 0x28(%rsp)
je 0xac64
cmpb $0x0, 0x874ce3(%rip)    # 0x87f941
jne 0xad11
negl %r14d
incl %r13d
cmpl %r14d, 0x8(%rsp)
jge 0xab23
cmpl 0xc(%rsp), %r14d
jge 0xad1b
movq 0x10(%rsp), %rax
movq (%rax), %rdx
movq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xb720
movl %r14d, 0x8(%rsp)
cmpb $0x0, 0x3f0(%rsp)
je 0xab19
movl 0x874c79(%rip), %ecx    # 0x87f92c
movq 0x20(%rsp), %rax
movq 0x18(%rax), %r8
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl %r14d, %edx
callq 0xde4e
movl %r14d, 0x8(%rsp)
jmp 0xab19
testl %r13d, %r13d
jne 0xacfb
movl 0x138(%rbx), %esi
movq %rbx, %rdi
callq 0xa61f
xorl %ecx, %ecx
testb %al, %al
movl $0xffff0001, %eax       # imm = 0xFFFF0001
cmovel %ecx, %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x8(%rsp)
jmp 0xacfb
movl 0xc(%rsp), %eax
jmp 0xacf7
leaq 0x1f2ea(%rip), %rdi     # 0x2a012
leaq 0x1f1e7(%rip), %rsi     # 0x29f16
leaq 0x1f254(%rip), %rcx     # 0x29f8a
pushq $0x4b
popq %rdx
callq 0x20c0
leaq 0x1c814(%rip), %rdi     # 0x27559
leaq 0x1f1ca(%rip), %rsi     # 0x29f16
leaq 0x1f237(%rip), %rcx     # 0x29f8a
pushq $0x38
jmp 0xad38
leaq 0x1f28d(%rip), %rdi     # 0x29feb
leaq 0x1f1b1(%rip), %rsi     # 0x29f16
leaq 0x1f21e(%rip), %rcx     # 0x29f8a
pushq $0x39
jmp 0xad38
leaq 0x1f27e(%rip), %rdi     # 0x29ff5
leaq 0x1f198(%rip), %rsi     # 0x29f16
leaq 0x1f205(%rip), %rcx     # 0x29f8a
pushq $0x3a
jmp 0xad38
leaq 0x1f26b(%rip), %rdi     # 0x29ffb
leaq 0x1f17f(%rip), %rsi     # 0x29f16
leaq 0x1f1ec(%rip), %rcx     # 0x29f8a
pushq $0x3b
jmp 0xad38
leaq 0x1f25c(%rip), %rdi     # 0x2a005
leaq 0x1f166(%rip), %rsi     # 0x29f16
leaq 0x1f1d3(%rip), %rcx     # 0x29f8a
pushq $0x3c
jmp 0xad38