// File: /workspace/github_cpp_makefile_star2_O1/BlinfoldKing[P]lisval/src/lib/evaluator.cpp
// Function: Evaluator::eval(Token*)
Token* Evaluator::eval(Token* input) {
    if (input->type == TokenType::LIST) {
        List* l = static_cast<List*>(input);
        List* res = new List();
        for (size_t i = 0; i < l->list.size(); i++) {
            if (this->stack_trace.size() < this->max_stack_call) {
                this->stack_trace.push_back(l->list[i]);
                Token* output = this->eval(l->list[i]);
                this->stack_trace.pop_back();
                if (output && output->type != TokenType::ERROR) {
                    res->list.push_back(output);
                } else {
                    res->list.push_back(l->list[i]);
                }
            }

        }

        input = res;
    }


    vector<pair<Token*, function<Token*(Token*)>>> matches_handler;
    for (size_t i = 0; i < this->state.size(); i++) {
        bool compare = compareSignature(input, this->state[i].first);
        if (compare) {
            matches_handler.push_back(this->state[i]);
        }
    }

    Error* err = NULL;
    for (size_t i = 0; i < matches_handler.size(); i++) {
        if (this->stack_trace.size() < this->max_stack_call) {
            this->stack_trace.push_back(input);
            Token* res = matches_handler[i].second(input);
            this->stack_trace.pop_back();

            if (res) {
                if (res->type != TokenType::ERROR) {
                    return res;
                } else {
                    err = static_cast<Error*>(res);
                }
            }
        } else {
            return new Error("stack call limit exceeded", input);
        }
    }

    if (err) {
        return err;
    };

    return input;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq %rsi, -0x30(%rbp)
cmpl $0x1, (%rsi)
jne 0x49d4
movq %rsi, %r14
movl $0x20, %edi
callq 0x31b0
movq %rax, %r15
movl $0x1, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movq 0x8(%r14), %rax
cmpq %rax, 0x10(%r14)
je 0x49d0
movq %r15, %r12
addq $0x8, %r12
leaq 0x8(%rbx), %rcx
movq %rcx, -0x38(%rbp)
xorl %r13d, %r13d
movq 0x10(%rbx), %rsi
movq %rsi, %rcx
subq 0x8(%rbx), %rcx
sarq $0x3, %rcx
movl (%rbx), %edx
cmpq %rdx, %rcx
jae 0x49b5
leaq (%rax,%r13,8), %rdx
cmpq 0x18(%rbx), %rsi
je 0x4949
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%rbx)
jmp 0x4952
movq -0x38(%rbp), %rdi
callq 0x9bf4
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rsi
movq %rbx, %rdi
callq 0x48b2
movq %rax, -0x50(%rbp)
addq $-0x8, 0x10(%rbx)
testq %rax, %rax
je 0x4975
cmpl $0x8, (%rax)
jne 0x4990
leaq (,%r13,8), %rdx
addq 0x8(%r14), %rdx
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x49a4
movq (%rdx), %rax
jmp 0x499a
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x49a9
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x49b5
movq %r12, %rdi
jmp 0x49b0
movq %r12, %rdi
leaq -0x50(%rbp), %rdx
callq 0x9bf4
incq %r13
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r13
jb 0x4918
movq %r15, -0x30(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq 0x20(%rbx), %r14
cmpq %r14, 0x28(%rbx)
je 0x4a3f
xorl %r13d, %r13d
leaq -0x50(%rbp), %r15
xorl %r12d, %r12d
movq -0x30(%rbp), %rdi
movq (%r14,%r13), %rsi
callq 0x7158
testb %al, %al
je 0x4a16
addq %r13, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x9baa
incq %r12
movq 0x20(%rbx), %r14
movq 0x28(%rbx), %rax
subq %r14, %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
addq $0x28, %r13
cmpq %rax, %r12
jb 0x49f7
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
setne %r14b
je 0x4b1b
leaq 0x8(%rbx), %rax
movq %rax, -0x58(%rbp)
movl $0x20, %r13d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x10(%rbx), %rsi
movq %rsi, %rax
subq 0x8(%rbx), %rax
sarq $0x3, %rax
movl (%rbx), %ecx
cmpq %rcx, %rax
jae 0x4b20
cmpq 0x18(%rbx), %rsi
je 0x4a93
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%rbx)
jmp 0x4aa0
movq -0x58(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x9bf4
movq -0x50(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, -0x80(%rbp)
cmpq $0x0, -0x8(%rax,%r13)
je 0x4bc1
leaq (%rax,%r13), %rdi
addq $-0x18, %rdi
leaq -0x80(%rbp), %rsi
callq *0x18(%rdi)
addq $-0x8, 0x10(%rbx)
movb $0x1, %cl
testq %rax, %rax
je 0x4ae3
cmpl $0x8, (%rax)
jne 0x4add
movq %rax, %r12
jmp 0x4ae3
xorl %ecx, %ecx
movq %rax, -0x38(%rbp)
testb %cl, %cl
je 0x4b94
incq %r15
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
addq $0x28, %r13
cmpq %rax, %r15
setb %r14b
jb 0x4a65
jmp 0x4b94
xorl %r12d, %r12d
jmp 0x4b94
movl $0x30, %edi
callq 0x31b0
movq %rax, %r13
leaq -0x68(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0xa840(%rip), %rsi      # 0xf37c
leaq 0xa852(%rip), %rdx      # 0xf395
leaq -0x78(%rbp), %rdi
callq 0x3ba8
movq -0x30(%rbp), %rbx
movl $0x8, (%r13)
leaq 0x8(%r13), %rdi
movq %r13, %rax
addq $0x18, %rax
movq %rax, 0x8(%r13)
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq %r13, -0x38(%rbp)
movq %rbx, 0x28(%r13)
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0x4b94
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x31c0
testq %r12, %r12
jne 0x4b9d
movq -0x30(%rbp), %r12
testb $0x1, %r14b
cmovneq -0x38(%rbp), %r12
leaq -0x50(%rbp), %rdi
callq 0x3ac0
movq %r12, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x30e0
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0x4be3
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x4be3
movq %rax, %rbx
movl $0x30, %esi
movq %r13, %rdi
callq 0x31c0
jmp 0x4bfd
jmp 0x4bfa
jmp 0x4bfa
jmp 0x4bfa
jmp 0x4bfa
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x3ac0
movq %rbx, %rdi
callq 0x32e0