// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
testq %rdi, %rdi
je 0xb6c6
movq %rsi, %r12
testq %rsi, %rsi
je 0xb6e5
movq %r9, %r15
testq %r9, %r9
je 0xb704
cmpl $0x0, 0x18(%rsp)
jle 0xb723
movl %edx, %r13d
movl %ecx, 0xc(%rsp)
cmpl %edx, %ecx
jle 0xb742
movq %rdi, %rbx
incq 0x18(%r15)
movq 0x3e0(%rsp), %r14
movl $0x0, 0x80(%rsp)
movq 0x1b8(%rdi), %rsi
leaq 0xecae3(%rip), %rdi     # 0xf7ef0
callq 0x6312
cmpl $0x0, 0x28(%r15)
movq %r12, 0x38(%rsp)
movq %r15, 0x10(%rsp)
jle 0xb429
movq 0x30(%r15), %r15
jmp 0xb42c
xorl %r15d, %r15d
movq %rax, %rdi
callq 0x621c
leaq 0xecb15(%rip), %rcx     # 0xf7f50
movq (%rcx), %r8
leaq 0xece2b(%rip), %rcx     # 0xf8270
movq (%rcx), %r9
subq $0x8, %rsp
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rax, %rcx
pushq $0x0
callq 0xb2d7
addq $0x10, %rsp
movq %rsp, %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x8b70
testb %al, %al
je 0xb671
movq 0x3e8(%rsp), %r12
movl 0xc(%rsp), %eax
negl %eax
movl %eax, 0x8(%rsp)
movq 0x18(%rsp), %rax
decl %eax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movq %rsp, %rbp
leaq 0x40(%rsp), %r15
movq (%rsp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x910b
testb %al, %al
je 0xb6a7
movq (%rsp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x30e1
movl 0x138(%rbx), %edi
callq 0xdf74
movq %rbx, %rdi
movl %eax, %esi
callq 0xaf41
testb %al, %al
je 0xb50b
movq %rbx, %rdi
movq %r12, %rsi
callq 0x334b
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x8b70
testb %al, %al
jne 0xb4a7
jmp 0xb66c
testl %r14d, %r14d
jne 0xb52d
movq (%rsp), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rcx
movq %rax, 0x358(%rcx)
movl $0x1, 0x350(%rcx)
movl 0x138(%rbx), %esi
movq %rbx, %rdi
callq 0xaf41
movq (%rsp), %rcx
movq (%rcx), %rsi
movzbl %al, %edx
movq %rbx, %rdi
movq %rdx, 0x28(%rsp)
callq 0x5e8c
movl %r13d, %ecx
negl %ecx
movq 0x30(%rsp), %rdx
leal (%rdx,%rax), %r8d
movl %r14d, 0x24(%rsp)
testl %r14d, %r14d
je 0xb59c
xorl %r10d, %r10d
testl %eax, %eax
sete %r10b
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
movl 0x8(%rsp), %edx
movl $0x1, %r9d
pushq %r12
pushq 0x78(%rsp)
pushq 0x20(%rsp)
pushq %r10
pushq 0x48(%rsp)
pushq $0x0
jmp 0xb5c3
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
movl 0x8(%rsp), %edx
movl $0x1, %r9d
pushq %r12
pushq 0x78(%rsp)
pushq 0x20(%rsp)
pushq $0x0
pushq 0x48(%rsp)
pushq $0x1
callq 0xb761
addq $0x30, %rsp
movl %eax, %r14d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x334b
cmpl $0x1, 0x18(%rsp)
je 0xb5ee
cmpb $0x0, 0x871359(%rip)    # 0x87c941
jne 0xb6a2
negl %r14d
cmpl %r14d, %r13d
jge 0xb64c
movl 0xc(%rsp), %r13d
cmpl %r13d, %r14d
jge 0xb68d
movq (%rsp), %rax
movq (%rax), %rdx
movq 0x38(%rsp), %r13
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
callq 0xc0ed
cmpb $0x0, 0x3f0(%rsp)
je 0xb649
movl 0x8712fc(%rip), %ecx    # 0x87c92c
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r8
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movl %r14d, %edx
callq 0xea5e
movl %r14d, %r13d
movl 0x24(%rsp), %r14d
incl %r14d
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x8b70
testb %al, %al
jne 0xb4a7
jmp 0xb68d
testl %r14d, %r14d
jne 0xb68d
movl 0x138(%rbx), %esi
movq %rbx, %rdi
callq 0xaf41
xorl %ecx, %ecx
testb %al, %al
movl $0xffff0001, %r13d      # imm = 0xFFFF0001
cmovel %ecx, %r13d
movl %r13d, %eax
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
jmp 0xb68d
leaq 0x209b4(%rip), %rdi     # 0x2c062
leaq 0x208b1(%rip), %rsi     # 0x2bf66
leaq 0x2091e(%rip), %rcx     # 0x2bfda
movl $0x4b, %edx
callq 0x20d0
leaq 0x1debc(%rip), %rdi     # 0x29589
leaq 0x20892(%rip), %rsi     # 0x2bf66
leaq 0x208ff(%rip), %rcx     # 0x2bfda
movl $0x38, %edx
callq 0x20d0
leaq 0x2094f(%rip), %rdi     # 0x2c03b
leaq 0x20873(%rip), %rsi     # 0x2bf66
leaq 0x208e0(%rip), %rcx     # 0x2bfda
movl $0x39, %edx
callq 0x20d0
leaq 0x2093a(%rip), %rdi     # 0x2c045
leaq 0x20854(%rip), %rsi     # 0x2bf66
leaq 0x208c1(%rip), %rcx     # 0x2bfda
movl $0x3a, %edx
callq 0x20d0
leaq 0x20921(%rip), %rdi     # 0x2c04b
leaq 0x20835(%rip), %rsi     # 0x2bf66
leaq 0x208a2(%rip), %rcx     # 0x2bfda
movl $0x3b, %edx
callq 0x20d0
leaq 0x2090c(%rip), %rdi     # 0x2c055
leaq 0x20816(%rip), %rsi     # 0x2bf66
leaq 0x20883(%rip), %rcx     # 0x2bfda
movl $0x3c, %edx
callq 0x20d0