// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rdx, -0x48(%rbp)
movq %rsi, %rbx
movl (%rsi), %eax
cmpl $0x1, %eax
movq %rsi, -0x58(%rbp)
je 0x4166
cmpl $0x3, %eax
jne 0x4282
movq %rdi, -0x30(%rbp)
movq -0x48(%rbp), %rcx
movq (%rcx), %rdx
movq 0x8(%rcx), %r15
leaq -0x38(%rbp), %r14
movq %rbx, (%r14)
movq %r15, -0x50(%rbp)
movq %r15, %rcx
movq %rdx, %r15
subq %rdx, %rcx
movq %rcx, %r12
sarq $0x6, %r12
testq %r12, %r12
jle 0x422a
movq %rcx, -0x40(%rbp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x7480
testb %al, %al
jne 0x426d
movq %r14, %rbx
leaq 0x10(%r15), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x7480
testb %al, %al
jne 0x4270
leaq 0x20(%r15), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7480
testb %al, %al
jne 0x4270
leaq 0x30(%r15), %r13
movq %rbx, %r14
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7480
testb %al, %al
jne 0x4270
addq $0x40, %r15
decq %r12
movq -0x40(%rbp), %rcx
addq $-0x40, %rcx
jmp 0x40ea
movq %rdi, -0x30(%rbp)
pushq $0x20
popq %rdi
callq 0x3200
movl $0x1, (%rax)
leaq 0x8(%rax), %r13
xorps %xmm0, %xmm0
andq $0x0, 0x18(%rax)
movq %rax, -0x40(%rbp)
movups %xmm0, 0x8(%rax)
xorl %r14d, %r14d
leaq -0x70(%rbp), %r12
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r14
jae 0x4224
movq (%rax,%r14,8), %r15
movl (%r15), %eax
orl $0x2, %eax
cmpl $0x3, %eax
jne 0x4200
movq %r12, %rdi
movq -0x48(%rbp), %rsi
callq 0x74e6
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4090
movq %rax, %r15
movq %r12, %rdi
callq 0x7bac
movq %rbx, %rdi
movq %r15, %rsi
callq 0x42ea
movq %rax, -0x38(%rbp)
movq %r13, %rdi
leaq -0x38(%rbp), %rsi
callq 0x7abc
movq -0x58(%rbp), %rbx
jmp 0x421c
movq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x42ea
movq %rax, -0x38(%rbp)
movq %r13, %rdi
leaq -0x38(%rbp), %rsi
callq 0x7abc
incq %r14
jmp 0x4193
movq -0x40(%rbp), %rbx
jmp 0x4282
sarq $0x4, %rcx
cmpq $0x1, %rcx
je 0x42ae
cmpq $0x2, %rcx
movq -0x30(%rbp), %rbx
je 0x4258
cmpq $0x3, %rcx
jne 0x42b4
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x7480
testb %al, %al
jne 0x4268
addq $0x10, %r15
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x7480
testb %al, %al
je 0x42ba
movq %r15, %r13
jmp 0x4274
movq %r15, %r13
movq -0x30(%rbp), %rbx
movq -0x48(%rbp), %rax
cmpq 0x8(%rax), %r13
je 0x4294
movq 0x8(%r13), %rbx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq -0x58(%rbp), %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x42ea
movq -0x30(%rbp), %rbx
jmp 0x42be
movq -0x50(%rbp), %r13
jmp 0x4274
addq $0x10, %r15
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x7480
testb %al, %al
movq -0x50(%rbp), %r13
cmovneq %r15, %r13
jmp 0x4274
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x7bac
movq %rbx, %rdi
callq 0x3320