// File: /workspace/github_cpp_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movq %rdi, %rbx
andq $0x0, (%rdi)
movq %rdx, %r14
andl $0x0, 0x8(%rdi)
movq %rdx, 0x8(%rsp)
andq $0x0, 0x30(%rdi)
leaq 0xb8(%rsp), %rdi
callq 0x5eae
leaq 0xb0(%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)
andw $0x0, 0x28(%rsp)
movq $0xf, 0x30(%rsp)
movq %rbx, (%rsp)
movq %rbx, %rdi
callq 0x4316
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x2dec
testb %al, %al
je 0x5ca2
movq (%rsp), %rdi
callq 0x42f4
jmp 0x5df4
movb $0x0, 0x78(%rsp)
movq 0x8(%rsp), %rsi
movq %rsi, 0x68(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x34a2
leaq 0x68(%rsp), %rsi
movb %al, 0x8(%rsi)
movb $-0x1, 0x9(%rsi)
leaq 0xb8(%rsp), %rdi
callq 0x5e66
leaq 0x80(%rsp), %rdi
movq 0x10(%rdi), %rsi
leaq 0x8(%rsp), %rdx
callq 0x6380
movzbl 0x70(%rsp), %esi
movq (%rsp), %rdi
callq 0x432c
leaq 0x28(%rsp), %r12
leaq 0x80(%rsp), %r13
leaq 0x40(%rsp), %rbp
movq 0xc8(%rsp), %rax
cmpq %rax, 0xe8(%rsp)
je 0x5df4
movq (%rax), %r15
movq 0x8(%rax), %r14
movb 0x10(%rax), %bl
leaq 0xb8(%rsp), %rdi
callq 0x5e90
movq (%rsp), %rdi
callq 0x42ec
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x2f84
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
incb %bl
cmpq %r15, %r14
je 0x5dc2
movups (%r14), %xmm0
movaps %xmm0, 0x40(%rsp)
movb %bl, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x2dec
testb %al, %al
jne 0x5dd1
movq %r13, %rdi
movq %rbp, %rsi
callq 0x672a
testq %rax, %rax
jne 0x5dbc
movq 0x90(%rsp), %rsi
movq %r13, %rdi
movq %rbp, %rdx
callq 0x6380
leaq 0xb8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x5e66
addq $0x10, %r14
jmp 0x5d5f
leaq 0x10(%rsp), %rdi
callq 0x3bcc
jmp 0x5d0a
movzbl 0x60(%rsp), %esi
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x4326
movq %rbx, %rdi
callq 0x42f4
leaq 0x10(%rsp), %rdi
callq 0x3bcc
leaq 0x80(%rsp), %rdi
callq 0x60bc
leaq 0xb8(%rsp), %rdi
callq 0x6142
movq (%rsp), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5e2e
jmp 0x5e37
jmp 0x5e2e
jmp 0x5e2e
jmp 0x5e2e
movq %rax, %rbx
jmp 0x5e44
jmp 0x5e37
jmp 0x5e37
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3bcc
leaq 0x80(%rsp), %rdi
callq 0x60bc
leaq 0xb8(%rsp), %rdi
callq 0x6142
movq %rbx, %rdi
callq 0x2340