// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, -0x30(%rbp)
movq %rsi, %rbx
movq %rdi, -0x40(%rbp)
movl (%rsi), %eax
cmpl $0x1, %eax
je 0x4720
cmpl $0x3, %eax
jne 0x483a
movq -0x30(%rbp), %rax
movq (%rax), %r12
movq 0x8(%rax), %rcx
movq %rcx, %rax
subq %r12, %rax
movq %rax, %r15
sarq $0x6, %r15
testq %r15, %r15
jle 0x46df
movq %rcx, -0x30(%rbp)
andq $-0x40, %rax
addq %r12, %rax
movq %rax, -0x38(%rbp)
incq %r15
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9876
testb %al, %al
jne 0x47f9
leaq 0x10(%r12), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9876
testb %al, %al
jne 0x47fc
leaq 0x20(%r12), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9876
testb %al, %al
jne 0x47fc
leaq 0x30(%r12), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9876
testb %al, %al
jne 0x47fc
addq $0x40, %r12
decq %r15
cmpq $0x1, %r15
jg 0x4669
movq -0x30(%rbp), %rcx
movq %rcx, %rax
movq -0x38(%rbp), %r12
subq %r12, %rax
sarq $0x4, %rax
cmpq $0x1, %rax
je 0x481a
cmpq $0x2, %rax
je 0x4802
cmpq $0x3, %rax
jne 0x484c
movq %rcx, %r15
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9876
testb %al, %al
jne 0x482e
addq $0x10, %r12
jmp 0x4805
movl $0x20, %edi
callq 0x31b0
movq %rax, %r12
movl $0x1, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movq 0x8(%rbx), %rax
cmpq %rax, 0x10(%rbx)
je 0x47f4
movq %r12, %rcx
addq $0x8, %rcx
movq %rcx, -0x38(%rbp)
xorl %r15d, %r15d
movq (%rax,%r15,8), %r13
movl (%r13), %eax
orl $0x2, %eax
cmpl $0x3, %eax
jne 0x47a5
leaq -0x60(%rbp), %r14
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x98a6
movq -0x40(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x4606
movq %rax, %r13
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x47a5
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq -0x40(%rbp), %rdi
movq %r13, %rsi
callq 0x4886
movq %rax, -0x48(%rbp)
movq 0x10(%r12), %rsi
cmpq 0x18(%r12), %rsi
je 0x47cc
movq %rax, (%rsi)
addq $0x8, 0x10(%r12)
jmp 0x47d9
movq -0x38(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x9ecc
incq %r15
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r15
jb 0x475e
movq %r12, %rbx
jmp 0x483a
movq %r12, %r13
movq -0x30(%rbp), %r15
jmp 0x4831
movq %rcx, %r15
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9876
testb %al, %al
jne 0x482e
addq $0x10, %r12
jmp 0x481d
movq %rcx, %r15
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9876
testb %al, %al
cmoveq %r15, %r12
movq %r12, %r13
cmpq %r15, %r13
je 0x484c
movq 0x8(%r13), %rbx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4886
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x487e
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq %rbx, %rdi
callq 0x32e0