// File: /workspace/github_cpp_makefile_star2_O3/ATOMIC-LICHESS[P]ATOMKRAFT/simple_search.cpp
// Function: test_bestmove(Position&, int)
Move test_bestmove(Position& pos, int maxdepth) {
	Move bestmove = MOVE_NONE;
	

	
	
	
	MoveStack mlist[MAX_MOVES];
	StateInfo st;
	Move m;

	// Generate all legal moves
	MoveStack* last = generate<MV_LEGAL>(pos, mlist);
	
	
	for (int it_depth = 1; it_depth <= maxdepth; ++it_depth) {
	
		bool bSearchPv = true;
		int alpha = -100000;
		int beta  =  100000;
		int score = alpha;
		
		for (MoveStack* cur = mlist; cur != last; cur++) {
			m = cur->move;
			pos.do_move(m, st);
			if (bSearchPv) {
				score = -test_search(pos, -beta, -alpha, it_depth - 1);
			} else {
				score = -test_search(pos, -alpha-1, -alpha, it_depth - 1);
				if (score > alpha) // in fail-soft ... && score < beta ) is common
					score = -test_search(pos, -beta, -alpha, it_depth - 1); // re-search
			}
			pos.undo_move(m);
			
			if(score > alpha) {
				alpha = score; // alpha acts like max in MiniMax
				bestmove = m;
			}
			bSearchPv = false;
		}
		
		
		cout << it_depth << " " << move_to_uci(bestmove, 0) << " " << score << endl;
		
	}
	
	
	return bestmove;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8f8, %rsp            # imm = 0x8F8
movl %esi, %ebx
movq %rdi, %r14
leaq 0xf0(%rsp), %r13
movq %r13, %rsi
callq 0x1a5c0
movq %rax, 0x18(%rsp)
movl %ebx, 0x10(%rsp)
testl %ebx, %ebx
jle 0x28fda
movl $0x1, %eax
movl $0x0, 0x8(%rsp)
movq %r14, 0x40(%rsp)
movq 0x40(%rsp), %rbp
cmpq 0x18(%rsp), %r13
movq %rax, 0x48(%rsp)
je 0x28f08
decl %eax
movl %eax, 0xc(%rsp)
movb $0x1, %r15b
movl $0xfffe7960, %r12d      # imm = 0xFFFE7960
movq %r13, %r14
movl (%r14), %esi
movq %rbp, %rdi
movl %esi, 0x14(%rsp)
leaq 0x50(%rsp), %rdx
callq 0x1f3d6
movl %r12d, %r13d
negl %r13d
testb $0x1, %r15b
jne 0x28eb9
movl %r12d, %esi
notl %esi
movq %rbp, %rdi
movl %r13d, %edx
movl 0xc(%rsp), %ecx
callq 0x2901f
movl %eax, %ebx
negl %ebx
cmpl %ebx, %r12d
jge 0x28ed1
movq %rbp, %rdi
movl $0xfffe7960, %esi       # imm = 0xFFFE7960
movl %r13d, %edx
movl 0xc(%rsp), %ecx
callq 0x2901f
movl %eax, %ebx
negl %ebx
movq %rbp, %rdi
movl 0x14(%rsp), %r15d
movl %r15d, %esi
callq 0x2051a
cmpl %r12d, %ebx
cmovgl %ebx, %r12d
movl 0x8(%rsp), %eax
cmovgl %r15d, %eax
movl %eax, 0x8(%rsp)
addq $0x8, %r14
xorl %r15d, %r15d
cmpq 0x18(%rsp), %r14
jne 0x28e7c
jmp 0x28f0d
movl $0xfffe7960, %ebx       # imm = 0xFFFE7960
movq 0x1408c(%rip), %rdi     # 0x3cfa0
movq 0x48(%rsp), %r12
movl %r12d, %esi
callq 0x5760
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
leaq 0x878d(%rip), %r14      # 0x316c0
movq %r14, %rsi
callq 0x5460
leaq 0x20(%rsp), %rdi
movl 0x8(%rsp), %esi
xorl %edx, %edx
callq 0x179d4
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r15, %rdi
callq 0x5460
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x5460
movq %r15, %rdi
movl %ebx, %esi
callq 0x5760
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5270
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x28fc0
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5400
leal 0x1(%r12), %eax
cmpl 0x10(%rsp), %r12d
leaq 0xf0(%rsp), %r13
jne 0x28e5a
jmp 0x28fe2
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
addq $0x8f8, %rsp            # imm = 0x8F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x29017
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5400
movq %rbx, %rdi
callq 0x5790