// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]morat/hex/agentmctsthread.cpp
// Function: Morat::Hex::AgentMCTS::AgentThread::test_bridge_probe(Morat::Hex::Board const&, Morat::Move const&, Morat::Move const&) const
bool AgentMCTS::AgentThread::test_bridge_probe(const Board & board, const Move & move, const Move & test) const {
	//TODO: switch to the same method as policy_bridge.h, maybe even share code
	if(board.dist(move, test) != 1)
		return false;

	bool equals = false;

	int state = 0;
	Side piece = ~board.get(move);
	const MoveValid* neighbors = board.neighbors(move);
	for(int i = 0; i < 8; i++){
		MoveValid cur = neighbors[i % 6];

		bool on = board.on_board(cur);
		Side v = Side::NONE;
		if(on)
			v = board.get(cur);

	//state machine that progresses when it see the pattern, but counting borders as part of the pattern
		if(state == 0){
			if(!on || v == piece)
				state = 1;
			//else state = 0;
		}else if(state == 1){
			if(on){
				if(v == Side::NONE){
					state = 2;
					equals = (test == cur);
				}else if(v != piece)
					state = 0;
				//else (v==piece) => state = 1;
			}
			//else state = 1;
		}else{ // state == 2
			if(!on || v == piece){
				if(equals)
					return true;
				state = 1;
			}else{
				state = 0;
			}
		}
	}
	return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %rsi, %rdi
movq %rdx, %rsi
movq %rcx, %rdx
callq 0x12888
movl %eax, %ecx
movl $0x0, %eax
xorl %r12d, %r12d
cmpl $0x1, %ecx
jne 0x1249b
movsbq (%r15), %rax
movsbq (%r14), %rsi
movsbq 0x1(%r15), %r11
movq 0x10(%r14), %rcx
leaq 0x14b1e(%rip), %r10     # 0x26ea9
movb $0x3, %dl
movb (%rbx), %r8b
movb 0x1(%rbx), %r9b
imulq %rax, %rsi
movq %rsi, %rdi
shlq $0x4, %rdi
addq %r11, %rsi
shlq $0x4, %r11
addq %rcx, %rdi
imulq $0x48, %rsi, %rsi
addq 0x38(%r14), %rsi
subb (%r11,%rdi), %dl
movb (%r10), %dil
pushq $0x8
popq %r10
pushq $0x1
popq %r11
xorl %ebp, %ebp
xorl %r14d, %r14d
addl $-0x1, %r10d
setb %al
jae 0x1249b
movl %ebp, %ebx
subl $0x6, %ebx
movl %edi, %r13d
cmovbl %ebp, %ebx
movl (%rsi,%rbx,4), %r15d
movl %r15d, %ebx
shrl $0x10, %ebx
cmpl $0xffff, %ebx           # imm = 0xFFFF
je 0x123fb
movswq %bx, %r13
shlq $0x4, %r13
movb (%rcx,%r13), %r13b
cmpl $0x1, %r12d
je 0x12420
testl %r12d, %r12d
jne 0x12430
cmpl $0xffff, %ebx           # imm = 0xFFFF
movl %r14d, %r15d
sete %al
cmpb %dl, %r13b
sete %bl
orb %al, %bl
movzbl %bl, %r12d
jmp 0x12491
cmpl $0xffff, %ebx           # imm = 0xFFFF
jne 0x12464
movl %r14d, %r15d
movl %r11d, %r12d
jmp 0x12491
cmpl $0xffff, %ebx           # imm = 0xFFFF
setne %bl
cmpb %dl, %r13b
setne %r13b
andb %bl, %r13b
movl %r13d, %ebx
movl %r13d, %r15d
xorb $0x1, %bl
andb %r14b, %r15b
movzbl %bl, %r12d
testb %r13b, %r13b
jne 0x12491
xorb $0x1, %r14b
testb $0x1, %r14b
jne 0x12491
jmp 0x1249b
cmpb %dil, %r13b
jne 0x12484
movl %r15d, %eax
shrl $0x8, %eax
movl %r8d, %ebx
xorb %r15b, %bl
xorb %r9b, %al
orb %bl, %al
sete %r15b
pushq $0x2
popq %r12
jmp 0x12491
xorl %r12d, %r12d
cmpb %dl, %r13b
movl %r14d, %r15d
sete %r12b
incl %ebp
movl %r15d, %r14d
jmp 0x123c5
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq