// File: /workspace/github_cpp_makefile_star2_O3/nesro[P]NCursesGomoku/src/board.cpp
// Function: CBoard::searchFor(int&, int&, int&, char const*) const
bool CBoard::searchFor(int & x, int & y, int & direction,
		const char * pattern) const {

	int randomSearch = rand() % 4;

	/* choose one from four directions of searching */
	switch (randomSearch) {
	case 0:
		for (int i = 0; i < BOARD_WIDTH; i++) {
			for (int j = 0; j < BOARD_HEIGHT; j++) {
				for (int searchDirection = 0; searchDirection < 8;
						searchDirection++) {
					for (int l = 0, tmpX = i, tmpY = j;; l++) {
						if (pattern[l] == '\0') {
							x = i;
							y = j;
							direction = searchDirection;

							return true;
						} else if (!outOfBoard(tmpX, tmpY)
								&& m_Stones[tmpY][tmpX] == pattern[l]) {
							tmpX += DIRECTIONS[searchDirection][0];
							tmpY += DIRECTIONS[searchDirection][1];

							continue;
						} else {
							break;
						}
					}
				}
			}
		}
		break;
	case 1:
		for (int i = BOARD_WIDTH - 1; i >= 0; i--) {
			for (int j = 0; j < BOARD_HEIGHT; j++) {
				for (int searchDirection = 0; searchDirection < 8;
						searchDirection++) {
					for (int l = 0, tmpX = i, tmpY = j;; l++) {
						if (pattern[l] == '\0') {
							x = i;
							y = j;
							direction = searchDirection;

							return true;
						} else if (!outOfBoard(tmpX, tmpY)
								&& m_Stones[tmpY][tmpX] == pattern[l]) {
							tmpX += DIRECTIONS[searchDirection][0];
							tmpY += DIRECTIONS[searchDirection][1];

							continue;
						} else {
							break;
						}
					}
				}
			}
		}
		break;
	case 2:
		for (int i = 0; i < BOARD_WIDTH; i++) {
			for (int j = BOARD_HEIGHT - 1; j >= 0; j--) {
				for (int searchDirection = 0; searchDirection < 8;
						searchDirection++) {
					for (int l = 0, tmpX = i, tmpY = j;; l++) {
						if (pattern[l] == '\0') {
							x = i;
							y = j;
							direction = searchDirection;

							return true;
						} else if (!outOfBoard(tmpX, tmpY)
								&& m_Stones[tmpY][tmpX] == pattern[l]) {
							tmpX += DIRECTIONS[searchDirection][0];
							tmpY += DIRECTIONS[searchDirection][1];

							continue;
						} else {
							break;
						}
					}
				}
			}
		}
		break;
	case 3:
		for (int i = BOARD_WIDTH - 1; i >= 0; i--) {
			for (int j = BOARD_HEIGHT - 1; j >= 0; j--) {
				for (int searchDirection = 0; searchDirection < 8;
						searchDirection++) {
					for (int l = 0, tmpX = i, tmpY = j;; l++) {
						if (pattern[l] == '\0') {
							x = i;
							y = j;
							direction = searchDirection;

							return true;
						} else if (!outOfBoard(tmpX, tmpY)
								&& m_Stones[tmpY][tmpX] == pattern[l]) {
							tmpX += DIRECTIONS[searchDirection][0];
							tmpY += DIRECTIONS[searchDirection][1];

							continue;
						} else {
							break;
						}
					}
				}
			}
		}
		break;
	default:
		break;
	}

	return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r8, %r12
movq %rcx, (%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r13
callq 0x2090
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
subl %ecx, %eax
cmpl $0x3, %eax
ja 0x31d3
leaq 0x2461(%rip), %rcx      # 0x53f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addq $0x38, %r13
movb (%r12), %sil
incq %r12
xorl %ecx, %ecx
leaq 0x2454(%rip), %rdi      # 0x5400
xorl %eax, %eax
xorl %edx, %edx
testb %sil, %sil
je 0x31d7
movq %r12, %r8
movl %esi, %r11d
movl %eax, %r9d
movl %ecx, %r10d
cmpl $0xe, %r9d
jg 0x300b
cmpl $0xe, %r10d
jg 0x300b
movl %r9d, %ebp
orl %r10d, %ebp
js 0x300b
movslq %r9d, %rbp
movslq %r10d, %rbx
leaq (%rbp,%rbp,4), %rbp
leaq (%rbp,%rbp,2), %rbp
addq %r13, %rbp
cmpb %r11b, (%rbx,%rbp)
jne 0x300b
addl (%rdi,%rdx,8), %r10d
addl 0x4(%rdi,%rdx,8), %r9d
movb (%r8), %r11b
incq %r8
testb %r11b, %r11b
jne 0x2fc5
jmp 0x31d9
incq %rdx
cmpq $0x8, %rdx
jne 0x2fb0
incl %eax
cmpl $0xf, %eax
jne 0x2fae
incl %ecx
xorl %eax, %eax
cmpl $0xf, %ecx
jne 0x2fac
jmp 0x31e7
addq $0x38, %r13
movb (%r12), %sil
incq %r12
xorl %ecx, %ecx
leaq 0x23c3(%rip), %rdi      # 0x5400
movl $0xe, %eax
xorl %edx, %edx
testb %sil, %sil
je 0x31d7
movq %r12, %r8
movl %esi, %r11d
movl %eax, %r9d
movl %ecx, %r10d
cmpl $0xe, %r9d
jg 0x309e
cmpl $0xe, %r10d
jg 0x309e
movl %r9d, %ebx
orl %r10d, %ebx
js 0x309e
movslq %r9d, %rbx
movslq %r10d, %rbp
leaq (%rbx,%rbx,4), %rbx
leaq (%rbx,%rbx,2), %rbx
addq %r13, %rbx
cmpb %r11b, (%rbp,%rbx)
jne 0x309e
addl (%rdi,%rdx,8), %r10d
addl 0x4(%rdi,%rdx,8), %r9d
movb (%r8), %r11b
incq %r8
testb %r11b, %r11b
jne 0x3059
jmp 0x31d9
incq %rdx
cmpq $0x8, %rdx
jne 0x3044
subl $0x1, %eax
jae 0x3042
incl %ecx
xorl %eax, %eax
cmpl $0xf, %ecx
jne 0x303d
jmp 0x31e7
addq $0x38, %r13
movb (%r12), %sil
incq %r12
movl $0xe, %ecx
leaq 0x232f(%rip), %rdi      # 0x5400
movl $0xe, %eax
xorl %edx, %edx
testb %sil, %sil
je 0x31d7
movq %r12, %r8
movl %esi, %r11d
movl %eax, %r9d
movl %ecx, %r10d
cmpl $0xe, %r9d
jg 0x3132
cmpl $0xe, %r10d
jg 0x3132
movl %r9d, %ebx
orl %r10d, %ebx
js 0x3132
movslq %r9d, %rbx
movslq %r10d, %rbp
leaq (%rbx,%rbx,4), %rbx
leaq (%rbx,%rbx,2), %rbx
addq %r13, %rbx
cmpb %r11b, (%rbp,%rbx)
jne 0x3132
addl (%rdi,%rdx,8), %r10d
addl 0x4(%rdi,%rdx,8), %r9d
movb (%r8), %r11b
incq %r8
testb %r11b, %r11b
jne 0x30ed
jmp 0x31d9
incq %rdx
cmpq $0x8, %rdx
jne 0x30d8
subl $0x1, %eax
jae 0x30d6
xorl %eax, %eax
subl $0x1, %ecx
jae 0x30d1
jmp 0x31e7
addq $0x38, %r13
movb (%r12), %sil
incq %r12
movl $0xe, %ecx
leaq 0x229d(%rip), %rdi      # 0x5400
xorl %eax, %eax
xorl %edx, %edx
testb %sil, %sil
je 0x31d7
movq %r12, %r8
movl %esi, %r11d
movl %eax, %r9d
movl %ecx, %r10d
cmpl $0xe, %r9d
jg 0x31ba
cmpl $0xe, %r10d
jg 0x31ba
movl %r9d, %ebx
orl %r10d, %ebx
js 0x31ba
movslq %r9d, %rbx
movslq %r10d, %rbp
leaq (%rbx,%rbx,4), %rbx
leaq (%rbx,%rbx,2), %rbx
addq %r13, %rbx
cmpb %r11b, (%rbp,%rbx)
jne 0x31ba
addl (%rdi,%rdx,8), %r10d
addl 0x4(%rdi,%rdx,8), %r9d
movb (%r8), %r11b
incq %r8
testb %r11b, %r11b
jne 0x3178
jmp 0x31d9
incq %rdx
cmpq $0x8, %rdx
jne 0x3167
incl %eax
cmpl $0xf, %eax
jne 0x3165
xorl %eax, %eax
subl $0x1, %ecx
jae 0x3163
jmp 0x31e7
xorl %eax, %eax
jmp 0x31e7
xorl %edx, %edx
movl %ecx, (%r15)
movl %eax, (%r14)
movq (%rsp), %rax
movl %edx, (%rax)
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq