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

    int index = 0;

    result.startCountingTime();

    State initial;
    initial.value = initialState;
    initial.heuristic = stateManager.calculateHeuristic(initialState);
    initial.parentBlankPos = -1;
    Node initialNode = buildGBFSInitialNode(initial);
    result.setInitialHeuristicValue(initialNode.state.heuristic);
    result.increaseTotalHeuristicValue(initialNode.state.heuristic);
    open.push(initialNode);

    while (!open.empty()) {
        Node currentNode = open.top();
        open.pop();
       
        if (closed.find(currentNode.state.value) == closed.end()) {
            
            closed.insert(closed.begin(), currentNode.state.value);
            if (stateManager.isGoalState(currentNode.state.value)) {
                result.setOptimalSolutionLenght(currentNode.cost);
                result.stopCountingTime();
                return result;
            }

            result.increaseExpandedNodes();
            
            for (auto successorState : stateManager.produceNextStates(currentNode.state)) {
                result.increaseTotalHeuristicValue(successorState.heuristic);
                open.push(buildGBFSNode(successorState, currentNode.cost+1, ++index));
            } 
        }
        
    }
    return result;
}

// Assembly:
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, 0x48(%rsp)
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq %rsi, 0x1f0(%rsp)
movq %rdx, 0x1e8(%rsp)
callq 0x12750
leaq 0x1c8(%rsp), %rdi
callq 0x159d0
leaq 0x190(%rsp), %rdi
callq 0x12790
leaq 0x178(%rsp), %rdi
callq 0x3eb0
movq 0x48(%rsp), %rdi
movl $0x0, 0x174(%rsp)
callq 0xcd30
jmp 0x155c7
movq 0x1e8(%rsp), %rax
movq %rax, 0x150(%rsp)
movq 0x1e8(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x6aa0
movl %eax, 0x3c(%rsp)
jmp 0x155f2
movl 0x3c(%rsp), %eax
movb %al, 0x158(%rsp)
movb $-0x1, 0x159(%rsp)
movups 0x150(%rsp), %xmm0
movaps %xmm0, 0x120(%rsp)
movq 0x120(%rsp), %rsi
movq 0x128(%rsp), %rdx
leaq 0x138(%rsp), %rdi
callq 0x15510
movq 0x48(%rsp), %rdi
movzbl 0x140(%rsp), %esi
callq 0xcd80
jmp 0x15646
movq 0x48(%rsp), %rdi
movzbl 0x140(%rsp), %esi
callq 0xcda0
jmp 0x1565a
leaq 0x1c8(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x15a00
jmp 0x15671
jmp 0x15673
leaq 0x1c8(%rsp), %rdi
callq 0x15a60
movb %al, 0x3b(%rsp)
jmp 0x15686
movb 0x3b(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x15695
jmp 0x1596f
leaq 0x1c8(%rsp), %rdi
callq 0x15a80
movq %rax, 0x30(%rsp)
jmp 0x156a9
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x110(%rsp)
movups (%rax), %xmm0
movaps %xmm0, 0x100(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x15a90
jmp 0x156d4
leaq 0x190(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x129c0
movq %rax, 0x28(%rsp)
jmp 0x156f0
movq 0x28(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x129f0
movq %rax, 0xf0(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x129a0
testb $0x1, %al
jne 0x15730
jmp 0x1596a
leaq 0x190(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x12840
movq %rax, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x12870
movq 0x18(%rsp), %rdi
movq 0xe8(%rsp), %rsi
leaq 0x100(%rsp), %rdx
callq 0x12800
movq %rax, 0x20(%rsp)
jmp 0x15780
movq 0x20(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x100(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x5890
movb %al, 0x17(%rsp)
jmp 0x157a8
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x157b2
jmp 0x157fb
movq 0x48(%rsp), %rdi
movzbl 0x110(%rsp), %esi
callq 0xcd60
jmp 0x157c6
movq 0x48(%rsp), %rdi
callq 0xccf0
jmp 0x157d2
movl $0x1, 0xd4(%rsp)
jmp 0x1597a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x168(%rsp)
movl %eax, 0x164(%rsp)
jmp 0x159a1
movq 0x48(%rsp), %rdi
callq 0xccb0
jmp 0x15807
movaps 0x100(%rsp), %xmm0
movaps %xmm0, 0xa0(%rsp)
movq 0xa0(%rsp), %rdx
movq 0xa8(%rsp), %rcx
leaq 0xb0(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x5c30
jmp 0x1583e
leaq 0xb0(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x72f0
movq %rax, 0x98(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x9c50
movq %rax, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x12960
testb $0x1, %al
jne 0x158ae
movl $0x4, 0xd4(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x73e0
jmp 0x15968
leaq 0x98(%rsp), %rdi
callq 0xa150
movq 0x48(%rsp), %rdi
movups (%rax), %xmm0
movaps %xmm0, 0x80(%rsp)
movzbl 0x88(%rsp), %esi
callq 0xcda0
jmp 0x158da
movaps 0x80(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movzbl 0x110(%rsp), %ecx
incl %ecx
movl 0x174(%rsp), %r8d
incl %r8d
movl %r8d, 0x174(%rsp)
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x68(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x154d0
movq 0x8(%rsp), %rsi
leaq 0x1c8(%rsp), %rdi
callq 0x15ae0
jmp 0x15931
jmp 0x15933
leaq 0x98(%rsp), %rdi
callq 0x12a20
jmp 0x15878
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x168(%rsp)
movl %eax, 0x164(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x73e0
jmp 0x159a1
jmp 0x1596a
jmp 0x15673
movl $0x1, 0xd4(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x12a40
leaq 0x1c8(%rsp), %rdi
callq 0x15b40
movq 0x40(%rsp), %rax
addq $0x1f8, %rsp            # imm = 0x1F8
retq
leaq 0x190(%rsp), %rdi
callq 0x12a40
leaq 0x1c8(%rsp), %rdi
callq 0x15b40
movq 0x168(%rsp), %rdi
callq 0x34a0
nopl (%rax,%rax)