// File: camargodev[P]n-tile-puzzle/src/algorithms/IDAStar.cpp
// Function: IDAStar::recursiveSearch(Node, unsigned long, int)
IDAStar::Solution IDAStar :: recursiveSearch(Node currentNode, PackedState parent, int fLimit){
    IDAStar::Solution solution;
    StateManager stateManager;
    int val = f(currentNode);

    if(val > fLimit){
        solution.limit = val;
        solution.isSolution = false;
        return solution;
    }

    if(stateManager.isGoalState(currentNode.state.value)){
        solution.isSolution = true;
        solution.cost = currentNode.cost;
        return solution;
    }

    unsigned long long int nextLimit = ULLONG_MAX;

    this->result->increaseExpandedNodes();

    for (auto successorState : stateManager.produceNextPackedStates(currentNode.state.value)) {
        if(successorState != parent){
            State sucState;
            sucState.heuristic = stateManager.calculateHeuristic(successorState);
            this->result->increaseTotalHeuristicValue(sucState.heuristic);
            if(sucState.heuristic < ULLONG_MAX){
                sucState.value = successorState;
                Node sucNode = IDAStar::buildNode(sucState, currentNode.cost+1);
                solution = recursiveSearch(sucNode, currentNode.state.value, fLimit);
                if(solution.isSolution == true){
                    return solution;
                }
                nextLimit = min(nextLimit, solution.limit);
            }
        }            
    }

    solution.limit = nextLimit;
    solution.isSolution = false;
    return solution;
}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movq %rdi, 0x30(%rsp)
movq %rdi, 0x38(%rsp)
leaq 0x170(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rsi, 0x160(%rsp)
movq %rdx, 0x158(%rsp)
movl %ecx, 0x154(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x3eb0
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rdi
movq (%rax), %rcx
movq %rcx, 0x118(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x120(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x128(%rsp)
leaq 0x118(%rsp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x18540
movl %eax, 0x134(%rsp)
movl 0x134(%rsp), %eax
cmpl 0x154(%rsp), %eax
jle 0x186a2
movq 0x30(%rsp), %rax
movslq 0x134(%rsp), %rcx
movq %rcx, 0x10(%rax)
movb $0x0, (%rax)
jmp 0x18957
movq 0x40(%rsp), %rax
movq (%rax), %rsi
leaq 0x138(%rsp), %rdi
callq 0x5890
testb $0x1, %al
jne 0x186bd
jmp 0x186d7
movq 0x30(%rsp), %rax
movq 0x40(%rsp), %rcx
movb $0x1, (%rax)
movzbl 0x10(%rcx), %ecx
movq %rcx, 0x8(%rax)
jmp 0x18957
movq 0x48(%rsp), %rax
movq $-0x1, 0x110(%rsp)
movq (%rax), %rdi
callq 0xccb0
movq 0x40(%rsp), %rax
movq (%rax), %rdx
leaq 0xf0(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x6510
leaq 0xf0(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rdi
callq 0x7720
movq %rax, 0xe8(%rsp)
movq 0x108(%rsp), %rdi
callq 0xab40
movq %rax, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x18450
testb $0x1, %al
jne 0x18770
movl $0x2, 0xdc(%rsp)
jmp 0x18928
leaq 0xe8(%rsp), %rdi
callq 0xb020
movq (%rax), %rax
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
cmpq 0x158(%rsp), %rax
je 0x18914
movq 0xd0(%rsp), %rsi
leaq 0x138(%rsp), %rdi
callq 0x6aa0
movl %eax, 0x2c(%rsp)
jmp 0x187b9
movq 0x48(%rsp), %rax
movl 0x2c(%rsp), %ecx
movb %cl, 0xc8(%rsp)
movq (%rax), %rdi
movzbl 0xc8(%rsp), %esi
callq 0xcda0
jmp 0x187db
movzbl 0xc8(%rsp), %eax
cmpq $-0x1, %rax
jae 0x18912
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rax
movq 0xd0(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
movups 0xc0(%rsp), %xmm0
movaps %xmm0, 0x80(%rsp)
movzbl 0x10(%rax), %r8d
incl %r8d
movq 0x80(%rsp), %rdx
movq 0x88(%rsp), %rcx
leaq 0x98(%rsp), %rdi
callq 0x184b0
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rsi
movq 0xa8(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movups 0x98(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq (%rax), %rdx
movl 0x154(%rsp), %ecx
movq 0x60(%rsp), %rdi
movq %rsp, %rax
movq %rdi, 0x10(%rax)
movaps 0x50(%rsp), %xmm0
movups %xmm0, (%rax)
leaq 0x68(%rsp), %rdi
callq 0x185d0
jmp 0x1888a
movq 0x30(%rsp), %rax
movq 0x68(%rsp), %rcx
movq %rcx, (%rax)
movq 0x70(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x78(%rsp), %rcx
movq %rcx, 0x10(%rax)
movb (%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x188e5
movl $0x1, 0xdc(%rsp)
jmp 0x18928
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x7810
jmp 0x18964
movq 0x30(%rsp), %rsi
addq $0x10, %rsi
leaq 0x110(%rsp), %rdi
callq 0x18ba0
movq %rax, 0x20(%rsp)
jmp 0x18902
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x110(%rsp)
jmp 0x18914
jmp 0x18916
leaq 0xe8(%rsp), %rdi
callq 0x18490
jmp 0x18747
leaq 0xf0(%rsp), %rdi
callq 0x7810
movl 0xdc(%rsp), %eax
subl $0x1, %eax
je 0x18957
jmp 0x18943
movq 0x30(%rsp), %rax
movq 0x110(%rsp), %rcx
movq %rcx, 0x10(%rax)
movb $0x0, (%rax)
movq 0x38(%rsp), %rax
addq $0x168, %rsp            # imm = 0x168
retq
movq 0xb8(%rsp), %rdi
callq 0x34a0
nopw %cs:(%rax,%rax)