// File: BlinfoldKing[P]lisval/src/lib/evaluator.cpp
// Function: Evaluator::subtitute_variable(Token*, std::vector<std::pair<Variable*, Token*>, std::allocator<std::pair<Variable*, Token*>>>)
Token* Evaluator::subtitute_variable(Token* result_template, vector<pair<Variable*, Token*>> arguments) {
    Token* token = result_template;
    if (token->type == TokenType::VARIABLE) {
        Variable* var = static_cast<Variable*>(token);
        auto it = find_if(
                arguments.begin(), arguments.end(), [var](pair<Variable*, Token*> curr) {
                return curr.first->str == var->str;
        });

        if (it == arguments.end()) {
            return this->eval(token);
        }

        return it->second;
    } else if (token->type == TokenType::LIST) {
        List* l = static_cast<List*>(token);
        List* result_list = new List();
        for (size_t i = 0; i < l->list.size(); i++) {
            auto curr = l->list[i];
            if (curr->type == TokenType::VARIABLE || curr->type == TokenType::LIST) {
                Token* t = subtitute_variable(curr, arguments);
                result_list->list.push_back(this->eval(t));
            } else {
                result_list->list.push_back(this->eval(curr));
            }
        }

        return result_list;
    }

    return token;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdx, -0xc8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x5715
movq -0xc8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
callq 0x119a0
movq -0xc8(%rbp), %rdi
movq %rax, -0x40(%rbp)
callq 0x119d0
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x5920
movq -0xc8(%rbp), %rdi
movq %rax, -0x38(%rbp)
callq 0x119d0
movq %rax, -0x58(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x11a00
testb $0x1, %al
jne 0x56e6
jmp 0x56ff
movq -0xc0(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x5980
movq %rax, -0x8(%rbp)
jmp 0x58ff
leaq -0x38(%rbp), %rdi
callq 0x11a40
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x58ff
movq -0x28(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x58f5
movq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x20, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
callq 0x11870
jmp 0x5748
movq -0xd0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
callq 0x11a60
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jae 0x58eb
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
movq -0x80(%rbp), %rsi
callq 0x11a80
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpl $0x3, (%rax)
je 0x57bd
movq -0x88(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x589c
movq -0xc8(%rbp), %rsi
movq -0x88(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0xa8(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x11aa0
movq -0xc0(%rbp), %rdi
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
callq 0x5640
movq %rax, -0xe0(%rbp)
jmp 0x5808
leaq -0xa8(%rbp), %rdi
callq 0x11b90
movq -0xe0(%rbp), %rax
movq -0xc0(%rbp), %rdi
movq %rax, -0x90(%rbp)
movq -0x68(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xf8(%rbp)
movq -0x90(%rbp), %rsi
callq 0x5980
movq -0xf8(%rbp), %rdi
movq %rax, -0xb0(%rbp)
leaq -0xb0(%rbp), %rsi
callq 0x11bf0
jmp 0x58d8
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
movl $0x20, %esi
callq 0x32c0
jmp 0x590c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x11b90
jmp 0x590c
movq -0xc0(%rbp), %rdi
movq -0x68(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x100(%rbp)
movq -0x88(%rbp), %rsi
callq 0x5980
movq -0x100(%rbp), %rdi
movq %rax, -0xb8(%rbp)
leaq -0xb8(%rbp), %rsi
callq 0x11bf0
jmp 0x58da
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x575b
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x58ff
jmp 0x58f7
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x3490
nopw %cs:(%rax,%rax)