// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %r12
movw $0x0, 0x70(%rsp)
movq $0xf, 0x78(%rsp)
movzbl 0xf8(%rsp), %eax
movzbl 0x100(%rsp), %ebp
addl %ebp, %eax
movl %ecx, 0x2c(%rsp)
cmpl %ecx, %eax
jle 0x86c6
movl %eax, %eax
movq %rax, 0x10(%r12)
jmp 0x8850
movq %rdx, %r14
movq %rsi, %r13
leaq 0xf0(%rsp), %rax
movq (%rax), %r15
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x3136
testb %al, %al
je 0x86f7
movb $0x1, (%r12)
movq %rbp, 0x8(%r12)
jmp 0x8855
movq %r12, 0x20(%rsp)
movq %r14, 0x30(%rsp)
movq (%r13), %rdi
callq 0x4678
leaq 0x88(%rsp), %r12
leaq 0x70(%rsp), %rsi
movq %r12, %rdi
movq %r15, 0x48(%rsp)
movq %r15, %rdx
callq 0x3686
movq (%r12), %r15
movq 0x8(%r12), %rax
movq %rax, 0x50(%rsp)
cmpq %rax, %r15
je 0x882d
movq %r13, 0x40(%rsp)
incb %bpl
movq %rbp, 0x38(%rsp)
movq $-0x1, %rbx
movq 0x30(%rsp), %rcx
movq 0x20(%rsp), %r12
movq 0x40(%rsp), %r14
movq (%r15), %r13
movb $0x1, %al
cmpq %rcx, %r13
je 0x8818
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x38d8
movzbl %al, %r12d
movq (%r14), %rdi
movl %r12d, %esi
callq 0x46be
andq $-0x100, %rbp
orq %r12, %rbp
movq %r13, 0x58(%rsp)
movq %rbp, 0x60(%rsp)
movq 0x38(%rsp), %rax
movb %al, 0x68(%rsp)
movl $0xffffffff, 0x6c(%rsp) # imm = 0xFFFFFFFF
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x58(%rsp), %xmm0
movups %xmm0, (%rsp)
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
movq 0x48(%rsp), %rdx
movl 0x2c(%rsp), %ecx
callq 0x867c
movq 0xb0(%rsp), %rax
movq 0x20(%rsp), %r12
movq %rax, 0x10(%r12)
movups 0xa0(%rsp), %xmm0
movups %xmm0, (%r12)
movb (%r12), %al
movq 0x10(%r12), %rcx
cmpq %rbx, %rcx
cmovaeq %rbx, %rcx
testb %al, %al
cmoveq %rcx, %rbx
xorb $0x1, %al
movq 0x30(%rsp), %rcx
testb %al, %al
je 0x886a
addq $0x8, %r15
cmpq 0x50(%rsp), %r15
jne 0x8761
jmp 0x8839
movq $-0x1, %rbx
movq 0x20(%rsp), %r12
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x884b
callq 0x2200
movq %rbx, 0x10(%r12)
movb $0x0, (%r12)
movq %r12, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x8855
callq 0x2200
jmp 0x8855
jmp 0x8880
movq %rax, %rbx
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x8895
callq 0x2200
movq %rbx, %rdi
callq 0x2330
nop