// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, %r14
movq %rdi, %rbx
xorl %eax, %eax
movq %rax, (%rdi)
movl $0x0, 0x8(%rdi)
movq %rax, 0x30(%rdi)
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0xd0(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movw $0x0, 0x88(%rsp)
movq $0xf, 0x90(%rsp)
callq 0x469a
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x38b6
movzbl %al, %esi
leal 0xff00(%rsi), %eax
movq %r14, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movb $0x0, 0x68(%rsp)
movl $0x0, 0x6c(%rsp)
movq %rbx, %rdi
callq 0x46b0
movzbl 0x60(%rsp), %esi
movq %rbx, %rdi
callq 0x46b6
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x7ed6
movq 0x20(%rsp), %rax
cmpq 0x28(%rsp), %rax
je 0x7e5c
xorl %ebp, %ebp
leaq 0x40(%rsp), %r14
leaq 0x88(%rsp), %r15
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movups (%rax), %xmm0
movaps %xmm0, (%rsp)
leaq 0x20(%rsp), %rdi
callq 0x7f54
movq (%rsp), %rcx
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0x7d73
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0x7e35
movq (%r9), %r9
testq %r9, %r9
je 0x7d73
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0x7d73
cmpq %rdi, %rcx
jne 0x7d50
jmp 0x7e35
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x40(%rsp)
movq %rsp, %rsi
movq %r14, %rdx
callq 0x7054
movq (%rsp), %rsi
movq %r15, %rdi
callq 0x3146
testb %al, %al
jne 0x7e47
movq %rbx, %rdi
callq 0x4670
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x3302
movq 0x40(%rsp), %r13
movq 0x48(%rsp), %r15
cmpq %r15, %r13
je 0x7e1b
movq (%r13), %r12
movq 0x8(%r13), %r14
movzbl %r14b, %esi
movq %rbx, %rdi
callq 0x46b6
movb 0x10(%rsp), %al
incb %al
incl %ebp
movq %r12, 0x70(%rsp)
movq %r14, 0x78(%rsp)
movb %al, 0x80(%rsp)
movl %ebp, 0x84(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x7fd6
addq $0x10, %r13
cmpq %r15, %r13
jne 0x7dca
movq 0x40(%rsp), %r13
testq %r13, %r13
je 0x7e28
movq %r13, %rdi
callq 0x21f0
leaq 0x40(%rsp), %r14
leaq 0x88(%rsp), %r15
movq 0x20(%rsp), %rax
cmpq 0x28(%rsp), %rax
jne 0x7d01
jmp 0x7e5c
movzbl 0x10(%rsp), %esi
movq %rbx, %rdi
callq 0x46aa
movq %rbx, %rdi
callq 0x4678
leaq 0xa0(%rsp), %rdi
callq 0x6d90
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x7e78
callq 0x21f0
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7e99
jmp 0x7e99
jmp 0x7e99
jmp 0x7e99
jmp 0x7e99
jmp 0x7e99
movq %rax, %rbx
jmp 0x7eb2
jmp 0x7ea0
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x7eb2
callq 0x21f0
leaq 0xa0(%rsp), %rdi
callq 0x6d90
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x7ece
callq 0x21f0
movq %rbx, %rdi
callq 0x2320