// File: camargodev[P]n-tile-puzzle/src/algorithms/AStar.cpp
// Function: AStar::execute(unsigned long)
Result AStar :: 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 = buildInitialNode(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(buildNode(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 0x17660
leaq 0x190(%rsp), %rdi
callq 0x12790
leaq 0x178(%rsp), %rdi
callq 0x3eb0
movq 0x48(%rsp), %rdi
movl $0x0, 0x174(%rsp)
callq 0xcd30
jmp 0x17257
movq 0x1e8(%rsp), %rax
movq %rax, 0x150(%rsp)
movq 0x1e8(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x6aa0
movl %eax, 0x3c(%rsp)
jmp 0x17282
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 0x171a0
movq 0x48(%rsp), %rdi
movzbl 0x140(%rsp), %esi
callq 0xcd80
jmp 0x172d6
movq 0x48(%rsp), %rdi
movzbl 0x140(%rsp), %esi
callq 0xcda0
jmp 0x172ea
leaq 0x1c8(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x17690
jmp 0x17301
jmp 0x17303
leaq 0x1c8(%rsp), %rdi
callq 0x176f0
movb %al, 0x3b(%rsp)
jmp 0x17316
movb 0x3b(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x17325
jmp 0x175ff
leaq 0x1c8(%rsp), %rdi
callq 0x17710
movq %rax, 0x30(%rsp)
jmp 0x17339
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x110(%rsp)
movups (%rax), %xmm0
movaps %xmm0, 0x100(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x17720
jmp 0x17364
leaq 0x190(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x129c0
movq %rax, 0x28(%rsp)
jmp 0x17380
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 0x173c0
jmp 0x175fa
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 0x17410
movq 0x20(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x100(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x5890
movb %al, 0x17(%rsp)
jmp 0x17438
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x17442
jmp 0x1748b
movq 0x48(%rsp), %rdi
movzbl 0x110(%rsp), %esi
callq 0xcd60
jmp 0x17456
movq 0x48(%rsp), %rdi
callq 0xccf0
jmp 0x17462
movl $0x1, 0xd4(%rsp)
jmp 0x1760a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x168(%rsp)
movl %eax, 0x164(%rsp)
jmp 0x17631
movq 0x48(%rsp), %rdi
callq 0xccb0
jmp 0x17497
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 0x174ce
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 0x1753e
movl $0x4, 0xd4(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x73e0
jmp 0x175f8
leaq 0x98(%rsp), %rdi
callq 0xa150
movq 0x48(%rsp), %rdi
movups (%rax), %xmm0
movaps %xmm0, 0x80(%rsp)
movzbl 0x88(%rsp), %esi
callq 0xcda0
jmp 0x1756a
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 0x17160
movq 0x8(%rsp), %rsi
leaq 0x1c8(%rsp), %rdi
callq 0x17770
jmp 0x175c1
jmp 0x175c3
leaq 0x98(%rsp), %rdi
callq 0x12a20
jmp 0x17508
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x168(%rsp)
movl %eax, 0x164(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x73e0
jmp 0x17631
jmp 0x175fa
jmp 0x17303
movl $0x1, 0xd4(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x12a40
leaq 0x1c8(%rsp), %rdi
callq 0x177d0
movq 0x40(%rsp), %rax
addq $0x1f8, %rsp            # imm = 0x1F8
retq
leaq 0x190(%rsp), %rdi
callq 0x12a40
leaq 0x1c8(%rsp), %rdi
callq 0x177d0
movq 0x168(%rsp), %rdi
callq 0x34a0
nopl (%rax,%rax)