// File: /workspace/github_cpp_makefile_star2_O3/camargodev[P]n-tile-puzzle/src/algorithms/BFS.cpp
// Function: BFS::execute(unsigned long)
Result BFS :: execute(PackedState initialState) {
    Result result;
    OpenSet open;
    ClosedSet closed;
    StateManager stateManager;

    result.startCountingTime();

    if (stateManager.isGoalState(initialState)) {
        result.stopCountingTime();
        return result;
    }

    Node initialNode;
    initialNode.cost = 0;
    initialNode.state.value = initialState;
    initialNode.state.heuristic = stateManager.calculateHeuristic(initialState);
    initialNode.state.parentBlankPos = -1;
    
    open.push_back(initialNode);
    closed.insert(closed.begin(), initialState);

    result.setInitialHeuristicValue(initialNode.state.heuristic);

    while (!open.empty()) {
        Node currentNode = open.front();
        open.pop_front();
        result.increaseExpandedNodes();
        for (auto successorState : stateManager.produceNextStates(currentNode.state)) {
            Node successorNode;
            successorNode.cost = currentNode.cost + 1;
            successorNode.state = successorState;
            if (stateManager.isGoalState(successorState.value)) {
                result.setOptimalSolutionLenght(successorNode.cost);
                result.stopCountingTime();
                return result;
            }
            if (closed.find(successorState.value) == closed.end()) {
                closed.insert(closed.begin(), successorState.value);
                open.push_back(successorNode);
            }
        }
    }
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdx, %r14
movq %rdi, %rbx
movq %rdx, 0x8(%rsp)
xorl %eax, %eax
movq %rax, (%rdi)
movl $0x0, 0x8(%rdi)
movq %rax, 0x30(%rdi)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %esi, %esi
callq 0x6bf0
leaq 0x110(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movw $0x0, 0xc8(%rsp)
movq $0xf, 0xd0(%rsp)
movq %rbx, %rdi
callq 0x469a
leaq 0xc8(%rsp), %rdi
movq %r14, %rsi
callq 0x3146
testb %al, %al
je 0x690f
movq %rbx, %rdi
callq 0x4678
jmp 0x6b82
movb $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rsi
movq %rsi, 0x10(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x38b6
movb %al, 0x18(%rsp)
movb $-0x1, 0x19(%rsp)
movq 0x70(%rsp), %rax
movq 0x80(%rsp), %rcx
addq $-0x18, %rcx
cmpq %rcx, %rax
je 0x6963
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rax)
movups 0x10(%rsp), %xmm0
movups %xmm0, (%rax)
addq $0x18, 0x70(%rsp)
jmp 0x6972
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x6e3c
leaq 0xe0(%rsp), %r14
leaq 0x28(%rsp), %rdx
movq %r14, (%rdx)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x7054
movzbl 0x18(%rsp), %esi
movq %rbx, %rdi
callq 0x46b0
movq 0x50(%rsp), %rax
cmpq %rax, 0x70(%rsp)
je 0x6b82
leaq 0xc8(%rsp), %r12
movq (%rax), %r15
movq 0x8(%rax), %r13
movb 0x10(%rax), %bpl
movq 0x60(%rsp), %rcx
addq $-0x18, %rcx
cmpq %rcx, %rax
je 0x69d3
addq $0x18, %rax
jmp 0x6a00
movq 0x58(%rsp), %rdi
callq 0x21f0
movq 0x68(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x68(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x58(%rsp)
leaq 0x1f8(%rax), %rcx
movq %rcx, 0x60(%rsp)
movq %rax, 0x50(%rsp)
movq %rbx, %rdi
callq 0x4670
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x3302
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %r13
cmpq %r13, %r15
je 0x6b3c
incb %bpl
movups (%r15), %xmm0
movaps %xmm0, 0xb0(%rsp)
movb %bpl, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq 0xb0(%rsp), %rsi
movq %r12, %rdi
callq 0x3146
testb %al, %al
jne 0x6b5b
movq 0xb0(%rsp), %rcx
movq 0xe0(%rsp), %rdi
movq 0xe8(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0x6ac3
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0x6b2a
movq (%r9), %r9
testq %r9, %r9
je 0x6ac3
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0x6ac3
cmpq %rdi, %rcx
jne 0x6aa3
jmp 0x6b2a
movq %r14, 0xc0(%rsp)
movq %r14, %rdi
leaq 0xb0(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x7054
movq 0x70(%rsp), %rax
movq 0x80(%rsp), %rcx
addq $-0x18, %rcx
cmpq %rcx, %rax
je 0x6b18
movq 0xa0(%rsp), %rcx
movq %rcx, 0x10(%rax)
movaps 0x90(%rsp), %xmm0
movups %xmm0, (%rax)
addq $0x18, 0x70(%rsp)
jmp 0x6b2a
leaq 0x40(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x6e3c
addq $0x10, %r15
cmpq %r13, %r15
jne 0x6a36
movq 0x28(%rsp), %r15
testq %r15, %r15
je 0x6b49
movq %r15, %rdi
callq 0x21f0
movq 0x50(%rsp), %rax
cmpq %rax, 0x70(%rsp)
jne 0x69b4
jmp 0x6b82
movzbl 0xa0(%rsp), %esi
movq %rbx, %rdi
callq 0x46aa
movq %rbx, %rdi
callq 0x4678
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6b82
callq 0x21f0
leaq 0xe0(%rsp), %rdi
callq 0x6d90
leaq 0x40(%rsp), %rdi
callq 0x6df2
movq %rbx, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6bb8
jmp 0x6bbf
jmp 0x6bb8
jmp 0x6bb8
jmp 0x6bb8
movq %rax, %rbx
jmp 0x6bd1
jmp 0x6bbf
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6bd1
callq 0x21f0
leaq 0xe0(%rsp), %rdi
callq 0x6d90
leaq 0x40(%rsp), %rdi
callq 0x6df2
movq %rbx, %rdi
callq 0x2320