// File: /workspace/github_cpp_makefile_star2_O1/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 %edx, 0x8(%rsp)
testq %rdi, %rdi
je 0xb6c5
movq %rsi, %r13
testq %rsi, %rsi
je 0xb6e4
movq %r9, %r12
testq %r9, %r9
je 0xb703
cmpl $0x0, 0x28(%rsp)
jle 0xb722
movl %ecx, 0x1c(%rsp)
cmpl 0x8(%rsp), %ecx
jle 0xb741
movq %rdi, %rbx
incq 0x18(%r12)
movq 0x3e0(%rsp), %r15
movl $0x0, 0x80(%rsp)
movq 0x1b8(%rdi), %rsi
leaq 0xecadb(%rip), %rdi     # 0xf7ef0
callq 0x6263
xorl %r14d, %r14d
cmpl $0x0, 0x28(%r12)
movq %r13, 0x30(%rsp)
movq %r12, 0x20(%rsp)
jle 0xb436
movq 0x30(%r12), %r13
jmp 0xb439
xorl %r13d, %r13d
movq 0x3e8(%rsp), %r12
movq %rax, %rdi
callq 0x616d
leaq 0xecb00(%rip), %rcx     # 0xf7f50
movq (%rcx), %r8
leaq 0xece16(%rip), %rcx     # 0xf8270
movq (%rcx), %r9
subq $0x8, %rsp
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rax, %rcx
pushq $0x0
callq 0xb2db
addq $0x10, %rsp
movl 0x1c(%rsp), %eax
negl %eax
movl %eax, 0x18(%rsp)
movq 0x28(%rsp), %rax
decl %eax
movq %rax, 0x38(%rsp)
leaq 0x10(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x8adc
testb %al, %al
je 0xb66b
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x9052
testb %al, %al
je 0xb6a6
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x30e3
movl 0x138(%rbx), %edi
callq 0xdfc4
movq %rbx, %rdi
movl %eax, %esi
callq 0xaf39
testb %al, %al
je 0xb4ff
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3341
jmp 0xb498
movl %r13d, 0xc(%rsp)
testl %r14d, %r14d
jne 0xb527
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 0xaf39
movq 0x10(%rsp), %rcx
movq (%rcx), %rsi
movzbl %al, %r13d
movq %rbx, %rdi
movl %r13d, %edx
callq 0x5de4
movl 0x8(%rsp), %ecx
negl %ecx
testl %r14d, %r14d
je 0xb590
movq 0x38(%rsp), %rdx
leal (%rdx,%rax), %r8d
xorl %r10d, %r10d
testl %eax, %eax
sete %r10b
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
movl 0x18(%rsp), %edx
movl $0x1, %r9d
pushq %r12
pushq 0x78(%rsp)
pushq 0x30(%rsp)
pushq %r10
pushq %r13
pushq $0x0
jmp 0xb5bc
addl 0x38(%rsp), %eax
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
movl 0x18(%rsp), %edx
movl %eax, %r8d
movl $0x1, %r9d
pushq %r12
pushq 0x78(%rsp)
pushq 0x30(%rsp)
pushq $0x0
pushq %r13
pushq $0x1
callq 0xb760
addq $0x30, %rsp
movl %eax, %r13d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3341
cmpl $0x1, 0x28(%rsp)
je 0xb5ea
xorl %ecx, %ecx
cmpb $0x0, 0x87135e(%rip)    # 0x87c941
je 0xb5ea
xorl %r13d, %r13d
jmp 0xb65e
negl %r13d
movb $0x1, %cl
cmpl %r13d, 0x8(%rsp)
jge 0xb652
movl 0x1c(%rsp), %eax
cmpl %eax, %r13d
jge 0xb659
movq 0x10(%rsp), %rax
movq (%rax), %rdx
movq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xc116
cmpb $0x0, 0x3f0(%rsp)
je 0xb644
movl 0x871303(%rip), %ecx    # 0x87c92c
movq 0x20(%rsp), %rax
movq 0x18(%rax), %r8
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl %r13d, %edx
callq 0xeb12
movl %r13d, 0x8(%rsp)
movl 0xc(%rsp), %r13d
movb $0x1, %cl
jmp 0xb65e
movl 0xc(%rsp), %r13d
jmp 0xb65e
xorl %ecx, %ecx
movl %eax, %r13d
incl %r14d
testb %cl, %cl
jne 0xb498
jmp 0xb691
movl 0x8(%rsp), %r13d
testl %r14d, %r14d
jne 0xb691
movl 0x138(%rbx), %esi
movq %rbx, %rdi
callq 0xaf39
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
leaq 0x20935(%rip), %rdi     # 0x2bfe2
leaq 0x20832(%rip), %rsi     # 0x2bee6
leaq 0x2089f(%rip), %rcx     # 0x2bf5a
movl $0x4b, %edx
callq 0x20d0
leaq 0x1de75(%rip), %rdi     # 0x29541
leaq 0x20813(%rip), %rsi     # 0x2bee6
leaq 0x20880(%rip), %rcx     # 0x2bf5a
movl $0x38, %edx
callq 0x20d0
leaq 0x208d0(%rip), %rdi     # 0x2bfbb
leaq 0x207f4(%rip), %rsi     # 0x2bee6
leaq 0x20861(%rip), %rcx     # 0x2bf5a
movl $0x39, %edx
callq 0x20d0
leaq 0x208bb(%rip), %rdi     # 0x2bfc5
leaq 0x207d5(%rip), %rsi     # 0x2bee6
leaq 0x20842(%rip), %rcx     # 0x2bf5a
movl $0x3a, %edx
callq 0x20d0
leaq 0x208a2(%rip), %rdi     # 0x2bfcb
leaq 0x207b6(%rip), %rsi     # 0x2bee6
leaq 0x20823(%rip), %rcx     # 0x2bf5a
movl $0x3b, %edx
callq 0x20d0
leaq 0x2088d(%rip), %rdi     # 0x2bfd5
leaq 0x20797(%rip), %rsi     # 0x2bee6
leaq 0x20804(%rip), %rcx     # 0x2bf5a
movl $0x3c, %edx
callq 0x20d0