// File: /workspace/github_cpp_makefile_star2_O1/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 %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 0x6c16
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 0x46a2
movq 0x8(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x3136
testb %al, %al
je 0x6912
movq %rbx, %rdi
callq 0x4680
jmp 0x6baa
movb $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rsi
movq %rsi, 0x10(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x38d8
movb %al, 0x18(%rsp)
movb $-0x1, 0x19(%rsp)
movq 0x70(%rsp), %rax
movq 0x80(%rsp), %rcx
addq $-0x18, %rcx
cmpq %rcx, %rax
je 0x6966
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rax)
movups 0x10(%rsp), %xmm0
movups %xmm0, (%rax)
addq $0x18, 0x70(%rsp)
jmp 0x6975
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x6e5c
leaq 0xe0(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %rdi, (%rdx)
leaq 0x8(%rsp), %rsi
callq 0x7074
movzbl 0x18(%rsp), %esi
movq %rbx, %rdi
callq 0x46b8
movq 0x50(%rsp), %rax
cmpq %rax, 0x70(%rsp)
je 0x6baa
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 0x2200
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 0x4678
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x32ee
movq 0x28(%rsp), %r13
movq 0x30(%rsp), %r14
cmpq %r14, %r13
je 0x6b7a
incb %bpl
movups (%r13), %xmm0
movaps %xmm0, 0xb0(%rsp)
movb %bpl, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq 0xb0(%rsp), %rsi
movq %r12, %rdi
callq 0x3136
movl %eax, %r15d
testb %al, %al
je 0x6a87
movzbl 0xa0(%rsp), %esi
movq %rbx, %rdi
callq 0x46b2
movq %rbx, %rdi
callq 0x4680
jmp 0x6b68
movq 0xb0(%rsp), %rcx
movq 0xe0(%rsp), %rdi
movq 0xe8(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq %rdx, %rsi
movq (%rdi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x6af1
movq (%rdx), %r9
movq %rdx, %rax
cmpq 0x8(%r9), %rcx
je 0x6af1
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0x6aef
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0x6af1
movq %r10, %rax
cmpq %rdi, %rcx
jne 0x6ac4
jmp 0x6af1
xorl %eax, %eax
testq %rax, %rax
je 0x6afc
cmpq $0x0, (%rax)
jne 0x6b68
leaq 0xe0(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
leaq 0xb0(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x7074
movq 0x70(%rsp), %rax
movq 0x80(%rsp), %rcx
addq $-0x18, %rcx
cmpq %rcx, %rax
je 0x6b56
movq 0xa0(%rsp), %rcx
movq %rcx, 0x10(%rax)
movaps 0x90(%rsp), %xmm0
movups %xmm0, (%rax)
addq $0x18, 0x70(%rsp)
jmp 0x6b68
leaq 0x40(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x6e5c
testb %r15b, %r15b
jne 0x6b9b
addq $0x10, %r13
cmpq %r14, %r13
jne 0x6a36
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6b89
callq 0x2200
movq 0x50(%rsp), %rax
cmpq %rax, 0x70(%rsp)
jne 0x69b4
jmp 0x6baa
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6baa
callq 0x2200
leaq 0xe0(%rsp), %rdi
callq 0x6db6
leaq 0x40(%rsp), %rdi
callq 0x6e18
movq %rbx, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6bde
jmp 0x6bde
jmp 0x6bde
jmp 0x6bde
movq %rax, %rbx
jmp 0x6bf7
jmp 0x6be5
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6bf7
callq 0x2200
leaq 0xe0(%rsp), %rdi
callq 0x6db6
leaq 0x40(%rsp), %rdi
callq 0x6e18
movq %rbx, %rdi
callq 0x2330