// File: /workspace/github_cpp_makefile_star2_O1/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, -0x40(%rbp)
movq %rsi, %rbx
movq %rdi, -0x48(%rbp)
movl (%rsi), %eax
cmpl $0x1, %eax
je 0x4737
cmpl $0x3, %eax
jne 0x4865
movq -0x40(%rbp), %rax
movq (%rax), %r15
movq 0x8(%rax), %rcx
movq %rbx, -0x38(%rbp)
movq %rcx, %rax
subq %r15, %rax
movq %rax, %r12
sarq $0x6, %r12
testq %r12, %r12
jle 0x46ec
movq %rbx, -0x40(%rbp)
movq %rcx, -0x30(%rbp)
andq $-0x40, %rax
addq %r15, %rax
movq %rax, -0x50(%rbp)
incq %r12
leaq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x997c
testb %al, %al
jne 0x4813
leaq 0x10(%r15), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x997c
testb %al, %al
movq -0x40(%rbp), %rbx
jne 0x480d
leaq 0x20(%r15), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x997c
testb %al, %al
jne 0x480d
leaq 0x30(%r15), %r13
movq %r14, %rbx
movq %r14, %rdi
movq %r13, %rsi
callq 0x997c
testb %al, %al
jne 0x4816
addq $0x40, %r15
decq %r12
cmpq $0x1, %r12
movq %rbx, %r14
jg 0x4671
movq -0x50(%rbp), %r15
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rbx
movq %rcx, %rax
subq %r15, %rax
sarq $0x4, %rax
cmpq $0x1, %rax
je 0x483b
cmpq $0x2, %rax
je 0x4820
movq %rcx, %r13
cmpq $0x3, %rax
jne 0x485c
movq %rcx, %r12
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x997c
testb %al, %al
jne 0x4833
addq $0x10, %r15
jmp 0x4823
movl $0x20, %edi
callq 0x31b0
movq %rax, %r15
movl $0x1, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movq 0x8(%rbx), %rax
cmpq %rax, 0x10(%rbx)
je 0x4808
movq %r15, %rcx
addq $0x8, %rcx
movq %rcx, -0x30(%rbp)
xorl %r12d, %r12d
movq (%rax,%r12,8), %r13
movl (%r13), %eax
orl $0x2, %eax
cmpl $0x3, %eax
jne 0x47bc
leaq -0x68(%rbp), %r14
movq %r14, %rdi
movq -0x40(%rbp), %rsi
callq 0x99b0
movq -0x48(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x4602
movq %rax, %r13
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x47bc
movq -0x58(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq -0x48(%rbp), %rdi
movq %r13, %rsi
callq 0x48b2
movq %rax, -0x38(%rbp)
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x47e0
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x47ed
movq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0x9fcc
incq %r12
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r12
jb 0x4775
movq %r15, %rbx
jmp 0x4865
movq -0x30(%rbp), %rcx
jmp 0x485c
movq %r15, %r13
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rbx
jmp 0x485c
movq %rcx, %r12
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x997c
testb %al, %al
je 0x4840
movq %r15, %r13
movq %r12, %rcx
jmp 0x485c
movq %rcx, %r12
jmp 0x4844
addq $0x10, %r15
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x997c
testb %al, %al
movq %r12, %rcx
cmoveq %r12, %r15
movq %r15, %r13
cmpq %rcx, %r13
je 0x4877
movq 0x8(%r13), %rbx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x48(%rbp), %rdi
movq %rbx, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x48b2
movq %rax, %rbx
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x48a9
movq -0x58(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq %rbx, %rdi
callq 0x32e0
nop