// File: 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:
subq $0x948, %rsp            # imm = 0x948
movq %rdi, 0x940(%rsp)
movl %esi, 0x93c(%rsp)
movl $0x0, 0x938(%rsp)
movq 0x940(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x30420
movq %rax, 0x80(%rsp)
movl $0x1, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x93c(%rsp), %eax
jg 0x4f0d6
movb $0x1, 0x7b(%rsp)
movl $0xfffe7960, 0x74(%rsp) # imm = 0xFFFE7960
movl $0x186a0, 0x70(%rsp)    # imm = 0x186A0
movl 0x74(%rsp), %eax
movl %eax, 0x6c(%rsp)
leaq 0x130(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
cmpq 0x80(%rsp), %rax
je 0x4f010
movq 0x60(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x8c(%rsp)
movq 0x940(%rsp), %rdi
movl 0x8c(%rsp), %esi
leaq 0x90(%rsp), %rdx
callq 0x3a390
testb $0x1, 0x7b(%rsp)
je 0x4ef61
movq 0x940(%rsp), %rdi
xorl %esi, %esi
subl 0x70(%rsp), %esi
xorl %edx, %edx
subl 0x74(%rsp), %edx
movl 0x7c(%rsp), %ecx
subl $0x1, %ecx
callq 0x4f0f0
movl %eax, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4efc4
movq 0x940(%rsp), %rdi
xorl %esi, %esi
subl 0x74(%rsp), %esi
subl $0x1, %esi
xorl %edx, %edx
subl 0x74(%rsp), %edx
movl 0x7c(%rsp), %ecx
subl $0x1, %ecx
callq 0x4f0f0
movl %eax, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
cmpl 0x74(%rsp), %eax
jle 0x4efc2
movq 0x940(%rsp), %rdi
xorl %esi, %esi
subl 0x70(%rsp), %esi
xorl %edx, %edx
subl 0x74(%rsp), %edx
movl 0x7c(%rsp), %ecx
subl $0x1, %ecx
callq 0x4f0f0
movl %eax, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4efc4
movq 0x940(%rsp), %rdi
movl 0x8c(%rsp), %esi
callq 0x3c620
movl 0x6c(%rsp), %eax
cmpl 0x74(%rsp), %eax
jle 0x4eff8
movl 0x6c(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x938(%rsp)
movb $0x0, 0x7b(%rsp)
movq 0x60(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x60(%rsp)
jmp 0x4eef1
movl 0x7c(%rsp), %esi
movq 0x1cf9d(%rip), %rdi     # 0x6bfb8
callq 0x6940
movq %rax, %rdi
leaq 0xa536(%rip), %rsi      # 0x59560
callq 0x64b0
movq %rax, 0x18(%rsp)
movl 0x938(%rsp), %esi
leaq 0x40(%rsp), %rdi
movq %rdi, 0x20(%rsp)
xorl %edx, %edx
callq 0x2c960
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x6420
movq %rax, 0x28(%rsp)
jmp 0x4f062
movq 0x28(%rsp), %rdi
leaq 0xa4f2(%rip), %rsi      # 0x59560
callq 0x64b0
movq %rax, 0x10(%rsp)
jmp 0x4f07a
movq 0x10(%rsp), %rdi
movl 0x6c(%rsp), %esi
callq 0x6940
movq %rax, 0x8(%rsp)
jmp 0x4f08f
movq 0x8(%rsp), %rdi
movq 0x1cef5(%rip), %rsi     # 0x6bf90
callq 0x6520
jmp 0x4f0a2
leaq 0x40(%rsp), %rdi
callq 0x6a78
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x4eeb6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x6a78
jmp 0x4f0e5
movl 0x938(%rsp), %eax
addq $0x948, %rsp            # imm = 0x948
retq
movq 0x38(%rsp), %rdi
callq 0x6970
nop