// File: iTruth[P]klotski/klotski_board.cpp
// Function: klotski_board::is_valid(std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>> const&, int, int)
bool klotski_board::is_valid(const klotski_board::situation_type& situation, int dx, int dy){
	if(!is_nums_linear(situation)){
		return false;
	}
	std::vector<int> linear_situation;
	for(const auto& i: situation){
		for(int j: i){
			linear_situation.push_back(j);
		}
	}

	int rn = 0;
	int situation_size = linear_situation.size();
	for (int i = 0; i < situation_size - 1; ++i) {
		if(linear_situation[i] == 0){
			continue;
		}
		for (int j = i + 1; j < situation_size; ++j) {
			if(linear_situation[j] != 0
					&&linear_situation[i] > linear_situation[j]){
				rn ^= 1;
			}
		}
	}

	if((dx & 1) == 0){
		int zero_y = -1;
		for (int i = 0; i < dy; ++i) {
			for (int j = 0; j < dx; ++j) {
				if(situation[i][j] == 0){
					zero_y = i;
				}
			}
		}
		if(zero_y == -1){
			throw std::runtime_error("Can not find zero on klotski board");
		}
		rn ^= (dy - 1 - zero_y) & 1;
	}
	return rn == 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0xe320
testb $0x1, %al
jne 0xe01b
movb $0x0, -0x1(%rbp)
jmp 0xe307
leaq -0x30(%rbp), %rdi
callq 0x5cf0
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x7640
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x7670
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x7a30
testb $0x1, %al
jne 0xe05c
jmp 0xe0e9
leaq -0x40(%rbp), %rdi
callq 0x7aa0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x7d30
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
callq 0x7d60
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xb920
testb $0x1, %al
jne 0xe09e
jmp 0xe0d9
leaq -0x60(%rbp), %rdi
callq 0xb9d0
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x6c(%rbp), %rsi
callq 0x5d10
jmp 0xe0bb
jmp 0xe0bd
leaq -0x60(%rbp), %rdi
callq 0xe690
jmp 0xe08b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xe2fc
jmp 0xe0db
leaq -0x40(%rbp), %rdi
callq 0x7ac0
jmp 0xe046
movl $0x0, -0x80(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x7c10
movl %eax, -0x84(%rbp)
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
movl -0x84(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xe1d3
movslq -0x88(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xe740
cmpl $0x0, (%rax)
jne 0xe13a
jmp 0xe1bf
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0xe1bd
movslq -0x8c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xe740
cmpl $0x0, (%rax)
je 0xe1aa
movslq -0x88(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xe740
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
movslq -0x8c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xe740
movq %rax, %rcx
movl -0x9c(%rbp), %eax
cmpl (%rcx), %eax
jle 0xe1aa
movl -0x80(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0xe1ac
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xe149
jmp 0xe1bf
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0xe109
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xe2e5
movl $0xffffffff, -0x90(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0xe26c
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xe259
movq -0x10(%rbp), %rdi
movslq -0x94(%rbp), %rsi
callq 0xe7c0
movq %rax, %rdi
movslq -0x98(%rbp), %rsi
callq 0xe7e0
cmpl $0x0, (%rax)
jne 0xe246
movl -0x94(%rbp), %eax
movl %eax, -0x90(%rbp)
jmp 0xe248
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0xe20b
jmp 0xe25b
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xe1f6
cmpl $-0x1, -0x90(%rbp)
jne 0xe2d0
movl $0x10, %edi
callq 0x3130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa8(%rbp)
leaq 0xa4c3(%rip), %rsi      # 0x18756
callq 0x30d0
jmp 0xe29a
movq -0xa8(%rbp), %rdi
movq 0x15d48(%rip), %rsi     # 0x23ff0
movq 0x15ce1(%rip), %rdx     # 0x23f90
callq 0x34b0
jmp 0xe31e
movq -0xa8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x3210
jmp 0xe2fc
movl -0x18(%rbp), %eax
subl $0x1, %eax
subl -0x90(%rbp), %eax
andl $0x1, %eax
xorl -0x80(%rbp), %eax
movl %eax, -0x80(%rbp)
cmpl $0x0, -0x80(%rbp)
sete %al
andb $0x1, %al
movb %al, -0x1(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x5e00
jmp 0xe307
leaq -0x30(%rbp), %rdi
callq 0x5e00
jmp 0xe315
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xb0, %rsp
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x34e0
nop