// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r13
callq 0x2090
pushq $0x4
popq %rcx
cltd
idivl %ecx
cmpl $0x3, %edx
ja 0x3062
leaq 0x250d(%rip), %rax      # 0x53f0
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
addq $0x38, %r13
xorl %ecx, %ecx
leaq 0x2507(%rip), %rsi      # 0x5400
xorl %eax, %eax
cmpl $0xf, %ecx
je 0x30e5
cmpl $0xf, %eax
je 0x2f65
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x2f61
movq %r12, %rdi
movl %ecx, %r8d
movl %eax, %r9d
movb (%rdi), %r10b
testb %r10b, %r10b
je 0x30db
cmpl $0xe, %r9d
jg 0x2f5c
cmpl $0xe, %r8d
jg 0x2f5c
movl %r9d, %r11d
orl %r8d, %r11d
js 0x2f5c
movslq %r9d, %r11
movslq %r8d, %rbp
imulq $0xf, %r11, %r11
addq %r13, %r11
cmpb %r10b, (%rbp,%r11)
jne 0x2f5c
addl (%rsi,%rdx,8), %r8d
addl 0x4(%rsi,%rdx,8), %r9d
incq %rdi
jmp 0x2f1a
incq %rdx
jmp 0x2f0b
incl %eax
jmp 0x2f04
incl %ecx
jmp 0x2ef9
addq $0x38, %r13
xorl %ecx, %ecx
pushq $0xe
popq %rax
leaq 0x2487(%rip), %rdi      # 0x5400
cmpl $0xf, %ecx
je 0x3062
testl %eax, %eax
js 0x2fe1
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x2fdd
movq %r12, %r8
movl %ecx, %r9d
movl %eax, %r10d
movb (%r8), %r11b
testb %r11b, %r11b
je 0x30db
cmpl $0xe, %r10d
jg 0x2fd8
cmpl $0xe, %r9d
jg 0x2fd8
movl %r10d, %ebp
orl %r9d, %ebp
js 0x2fd8
movslq %r10d, %rbp
movslq %r9d, %rsi
imulq $0xf, %rbp, %rbp
addq %r13, %rbp
cmpb %r11b, (%rsi,%rbp)
jne 0x2fd8
addl (%rdi,%rdx,8), %r9d
addl 0x4(%rdi,%rdx,8), %r10d
incq %r8
jmp 0x2f97
incq %rdx
jmp 0x2f88
decl %eax
jmp 0x2f82
incl %ecx
pushq $0xe
popq %rax
jmp 0x2f79
addq $0x38, %r13
pushq $0xe
popq %rax
leaq 0x240a(%rip), %rdi      # 0x5400
movl %eax, %ecx
testl %ecx, %ecx
js 0x3062
testl %eax, %eax
js 0x305b
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x3057
movq %r12, %r8
movl %ecx, %r9d
movl %eax, %r10d
movb (%r8), %r11b
testb %r11b, %r11b
je 0x30db
cmpl $0xe, %r10d
jg 0x3052
cmpl $0xe, %r9d
jg 0x3052
movl %r10d, %ebp
orl %r9d, %ebp
js 0x3052
movslq %r10d, %rbp
movslq %r9d, %rsi
imulq $0xf, %rbp, %rbp
addq %r13, %rbp
cmpb %r11b, (%rsi,%rbp)
jne 0x3052
addl (%rdi,%rdx,8), %r9d
addl 0x4(%rdi,%rdx,8), %r10d
incq %r8
jmp 0x3011
incq %rdx
jmp 0x3002
decl %eax
jmp 0x2ffc
decl %ecx
pushq $0xe
popq %rax
jmp 0x2ff8
xorl %eax, %eax
jmp 0x30e5
addq $0x38, %r13
pushq $0xe
popq %rcx
leaq 0x238c(%rip), %rsi      # 0x5400
xorl %eax, %eax
testl %ecx, %ecx
js 0x30e5
cmpl $0xf, %eax
je 0x30d7
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x30d3
movq %r12, %rdi
movl %ecx, %r8d
movl %eax, %r9d
movb (%rdi), %r10b
testb %r10b, %r10b
je 0x30db
cmpl $0xe, %r9d
jg 0x30ce
cmpl $0xe, %r8d
jg 0x30ce
movl %r9d, %r11d
orl %r8d, %r11d
js 0x30ce
movslq %r9d, %r11
movslq %r8d, %rbp
imulq $0xf, %r11, %r11
addq %r13, %r11
cmpb %r10b, (%rbp,%r11)
jne 0x30ce
addl (%rsi,%rdx,8), %r8d
addl 0x4(%rsi,%rdx,8), %r9d
incq %rdi
jmp 0x3090
incq %rdx
jmp 0x3081
incl %eax
jmp 0x307a
decl %ecx
jmp 0x3074
movl %ecx, (%r15)
movl %eax, (%r14)
movl %edx, (%rbx)
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq