// File: /workspace/github_cpp_makefile_star2_O1/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, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%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 0x46a2
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x38d8
movl $0xff, %ecx
andl %ecx, %eax
orl $0xff00, %eax           # imm = 0xFF00
movq %r14, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movb $0x0, 0x68(%rsp)
movl $0x0, 0x6c(%rsp)
andl %ecx, %eax
movq %rbx, %rdi
movl %eax, %esi
callq 0x46b8
movzbl 0x60(%rsp), %esi
movq %rbx, %rdi
callq 0x46be
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x7f90
xorl %r13d, %r13d
leaq 0xa0(%rsp), %r14
movq %rsp, %r15
leaq 0x18(%rsp), %r12
leaq 0x88(%rsp), %rbp
movq 0x30(%rsp), %rax
cmpq 0x38(%rsp), %rax
je 0x7f17
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movups (%rax), %xmm0
movaps %xmm0, (%rsp)
leaq 0x30(%rsp), %rdi
callq 0x80d6
movq (%rsp), %rcx
movq 0xa0(%rsp), %rdi
movq 0xa8(%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 0x7e20
movq (%rdx), %r9
movq %rdx, %rax
cmpq 0x8(%r9), %rcx
je 0x7e20
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0x7e1e
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0x7e20
movq %r10, %rax
cmpq %rdi, %rcx
jne 0x7df3
jmp 0x7e20
xorl %eax, %eax
testq %rax, %rax
je 0x7e31
movb $0x1, %cl
cmpq $0x0, (%rax)
jne 0x7f0f
movq %r14, 0x18(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x7074
movq (%rsp), %rsi
movq %rbp, %rdi
callq 0x3136
testb %al, %al
je 0x7e70
movzbl 0x10(%rsp), %esi
movq %rbx, %rdi
callq 0x46b2
movq %rbx, %rdi
callq 0x4680
xorl %ecx, %ecx
jmp 0x7f0f
movq %rbx, %rdi
callq 0x4678
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x32ee
movq 0x18(%rsp), %rbp
movq 0x20(%rsp), %r15
cmpq %r15, %rbp
je 0x7ee6
movq (%rbp), %r12
movq 0x8(%rbp), %r14
movzbl %r14b, %esi
movq %rbx, %rdi
callq 0x46be
movb 0x10(%rsp), %al
incb %al
incl %r13d
movq %r12, 0x70(%rsp)
movq %r14, 0x78(%rsp)
movb %al, 0x80(%rsp)
movl %r13d, 0x84(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x8150
addq $0x10, %rbp
jmp 0x7e96
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
movq %rsp, %r15
je 0x7ef8
callq 0x2200
movb $0x1, %cl
leaq 0xa0(%rsp), %r14
leaq 0x18(%rsp), %r12
leaq 0x88(%rsp), %rbp
testb %cl, %cl
jne 0x7d90
leaq 0xa0(%rsp), %rdi
callq 0x6db6
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7f33
callq 0x2200
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7f52
jmp 0x7f52
jmp 0x7f52
jmp 0x7f52
jmp 0x7f52
movq %rax, %rbx
jmp 0x7f6b
jmp 0x7f59
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x7f6b
callq 0x2200
leaq 0xa0(%rsp), %rdi
callq 0x6db6
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7f87
callq 0x2200
movq %rbx, %rdi
callq 0x2330
nop