// File: 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:
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, 0x50(%rsp)
movq %rdi, %rax
movq %rax, 0x48(%rsp)
movq %rsi, 0x1f0(%rsp)
movq %rdx, 0x1e8(%rsp)
callq 0x12750
leaq 0x198(%rsp), %rdi
callq 0x12780
leaq 0x160(%rsp), %rdi
callq 0x12790
leaq 0x148(%rsp), %rdi
callq 0x3eb0
movq 0x50(%rsp), %rdi
callq 0xcd30
jmp 0x1233c
movq 0x1e8(%rsp), %rsi
leaq 0x148(%rsp), %rdi
callq 0x5890
movb %al, 0x47(%rsp)
jmp 0x12357
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x12361
jmp 0x12396
movq 0x50(%rsp), %rdi
callq 0xccf0
jmp 0x1236d
movl $0x1, 0x138(%rsp)
jmp 0x126fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x140(%rsp)
movl %eax, 0x13c(%rsp)
jmp 0x12724
movb $0x0, 0x130(%rsp)
movq 0x1e8(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x1e8(%rsp), %rsi
leaq 0x148(%rsp), %rdi
callq 0x6aa0
movl %eax, 0x40(%rsp)
jmp 0x123c9
movl 0x40(%rsp), %eax
movb %al, 0x128(%rsp)
movb $-0x1, 0x129(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x127a0
jmp 0x123f3
leaq 0x160(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x12840
movq %rax, 0x110(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x12870
movq 0x30(%rsp), %rdi
movq 0x118(%rsp), %rsi
leaq 0x1e8(%rsp), %rdx
callq 0x12800
movq %rax, 0x38(%rsp)
jmp 0x12443
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rax
movq %rax, 0x108(%rsp)
movzbl 0x128(%rsp), %esi
callq 0xcd80
jmp 0x12464
jmp 0x12466
leaq 0x198(%rsp), %rdi
callq 0x128a0
xorb $-0x1, %al
testb $0x1, %al
jne 0x1247e
jmp 0x126f2
leaq 0x198(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x128c0
movq 0x28(%rsp), %rdi
movq 0x10(%rax), %rcx
movq %rcx, 0x100(%rsp)
movups (%rax), %xmm0
movaps %xmm0, 0xf0(%rsp)
callq 0x128f0
movq 0x50(%rsp), %rdi
callq 0xccb0
jmp 0x124bd
movaps 0xf0(%rsp), %xmm0
movaps %xmm0, 0xc0(%rsp)
movq 0xc0(%rsp), %rdx
movq 0xc8(%rsp), %rcx
leaq 0xd0(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x5c30
jmp 0x124f4
leaq 0xd0(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x72f0
movq %rax, 0xb8(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x9c50
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x12960
testb $0x1, %al
jne 0x12557
movl $0x4, 0x138(%rsp)
jmp 0x126d2
leaq 0xb8(%rsp), %rdi
callq 0xa150
movups (%rax), %xmm0
movaps %xmm0, 0xa0(%rsp)
movb 0x100(%rsp), %al
incb %al
movb %al, 0x90(%rsp)
movaps 0xa0(%rsp), %xmm0
movaps %xmm0, 0x80(%rsp)
movq 0xa0(%rsp), %rsi
leaq 0x148(%rsp), %rdi
callq 0x5890
movb %al, 0x27(%rsp)
jmp 0x125aa
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x125b4
jmp 0x1260a
movq 0x50(%rsp), %rdi
movzbl 0x90(%rsp), %esi
callq 0xcd60
jmp 0x125c8
movq 0x50(%rsp), %rdi
callq 0xccf0
jmp 0x125d4
movl $0x1, 0x138(%rsp)
jmp 0x126d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x140(%rsp)
movl %eax, 0x13c(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x73e0
jmp 0x12724
leaq 0x160(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x129c0
movq %rax, 0x18(%rsp)
jmp 0x12626
movq 0x18(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x129f0
movq %rax, 0x70(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x129a0
testb $0x1, %al
jne 0x12657
jmp 0x126be
leaq 0x160(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x12840
movq %rax, 0x60(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x12870
movq 0x8(%rsp), %rdi
movq 0x68(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
callq 0x12800
movq %rax, 0x10(%rsp)
jmp 0x1269b
movq 0x10(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x127a0
jmp 0x126bc
jmp 0x126be
jmp 0x126c0
leaq 0xb8(%rsp), %rdi
callq 0x12a20
jmp 0x1252e
leaq 0xd0(%rsp), %rdi
callq 0x73e0
movl 0x138(%rsp), %eax
subl $0x4, %eax
jne 0x126fd
jmp 0x126ed
jmp 0x12466
movl $0x1, 0x138(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x12a40
leaq 0x198(%rsp), %rdi
callq 0x12a50
movq 0x48(%rsp), %rax
addq $0x1f8, %rsp            # imm = 0x1F8
retq
leaq 0x160(%rsp), %rdi
callq 0x12a40
leaq 0x198(%rsp), %rdi
callq 0x12a50
movq 0x140(%rsp), %rdi
callq 0x34a0
nopl (%rax,%rax)