// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rbx
andq $0x0, (%rdi)
andl $0x0, 0x8(%rdi)
andq $0x0, 0x30(%rdi)
movq %rdx, %r14
andq $0x0, 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)
andw $0x0, 0x88(%rsp)
movq $0xf, 0x90(%rsp)
callq 0x4316
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x34a2
movzbl %al, %esi
leal 0xff00(%rsi), %eax
movq %r14, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movb $0x0, 0x50(%rsp)
andl $0x0, 0x54(%rsp)
movq %rbx, %rdi
callq 0x432c
movzbl 0x48(%rsp), %esi
movq %rbx, %rdi
callq 0x4332
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x69a8
xorl %r13d, %r13d
leaq 0xa0(%rsp), %r14
movq %rsp, %r15
leaq 0x88(%rsp), %r12
movq 0x20(%rsp), %rax
cmpq 0x28(%rsp), %rax
je 0x693a
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movups (%rax), %xmm0
movaps %xmm0, (%rsp)
leaq 0x20(%rsp), %rdi
callq 0x69be
movq %r14, %rdi
movq %r15, %rsi
callq 0x672a
testq %rax, %rax
jne 0x6844
movq 0xb0(%rsp), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x6380
movq (%rsp), %rsi
movq %r12, %rdi
callq 0x2dec
testb %al, %al
jne 0x6925
movq %rbx, %rdi
callq 0x42ec
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
leaq 0x70(%rsp), %rdi
movq %r12, %rsi
callq 0x2f84
movq 0x70(%rsp), %r15
movq 0x78(%rsp), %r12
cmpq %r12, %r15
je 0x6916
movq (%r15), %rbp
movq 0x8(%r15), %r14
movzbl %r14b, %esi
movq %rbx, %rdi
callq 0x4332
movb 0x10(%rsp), %al
incb %al
incl %r13d
movq %rbp, 0x58(%rsp)
movq %r14, 0x60(%rsp)
movb %al, 0x68(%rsp)
movl %r13d, 0x6c(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x69d6
addq $0x10, %r15
jmp 0x68cd
leaq 0x70(%rsp), %rdi
callq 0x3bcc
jmp 0x6831
movzbl 0x10(%rsp), %esi
movq %rbx, %rdi
callq 0x4326
movq %rbx, %rdi
callq 0x42f4
leaq 0xa0(%rsp), %rdi
callq 0x60bc
leaq 0x20(%rsp), %rdi
callq 0x69ec
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6974
jmp 0x6974
jmp 0x6974
jmp 0x6974
jmp 0x6974
jmp 0x6974
jmp 0x6974
movq %rax, %rbx
jmp 0x6988
jmp 0x697b
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3bcc
leaq 0xa0(%rsp), %rdi
callq 0x60bc
leaq 0x20(%rsp), %rdi
callq 0x69ec
movq %rbx, %rdi
callq 0x2340
nop