// File: /workspace/github_cpp_makefile_star2_O3/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 0x49ae
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
movq 0x10(%r14), %rcx
cmpq %rax, %rcx
je 0x49aa
movq %r15, %r12
addq $0x8, %r12
leaq 0x8(%rbx), %rdx
movq %rdx, -0x58(%rbp)
xorl %r13d, %r13d
movq 0x10(%rbx), %rsi
movq %rsi, %rdx
subq 0x8(%rbx), %rdx
sarq $0x3, %rdx
movl (%rbx), %edi
cmpq %rdi, %rdx
jae 0x4994
leaq (%rax,%r13,8), %rdx
cmpq 0x18(%rbx), %rsi
je 0x4920
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%rbx)
jmp 0x4929
movq -0x58(%rbp), %rdi
callq 0x9afe
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rsi
movq %rbx, %rdi
callq 0x4886
movq %rax, -0x50(%rbp)
addq $-0x8, 0x10(%rbx)
testq %rax, %rax
je 0x494c
cmpl $0x8, (%rax)
jne 0x4967
leaq (,%r13,8), %rdx
addq 0x8(%r14), %rdx
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x497b
movq (%rdx), %rax
jmp 0x4971
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x4980
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x498c
movq %r12, %rdi
jmp 0x4987
movq %r12, %rdi
leaq -0x50(%rbp), %rdx
callq 0x9afe
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
incq %r13
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rdx, %r13
jb 0x48ef
movq %r15, -0x30(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq 0x20(%rbx), %r14
movq 0x28(%rbx), %r15
cmpq %r14, %r15
je 0x4aed
xorl %r12d, %r12d
xorl %r13d, %r13d
movq -0x30(%rbp), %rdi
movq (%r14,%r12), %rsi
callq 0x7103
testb %al, %al
je 0x49fc
addq %r12, %r14
leaq -0x50(%rbp), %rdi
movq %r14, %rsi
callq 0x9ab4
movq 0x20(%rbx), %r14
movq 0x28(%rbx), %r15
incq %r13
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
addq $0x28, %r12
cmpq %rax, %r13
jb 0x49d4
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
je 0x4aed
leaq 0x8(%rbx), %rax
movq %rax, -0x58(%rbp)
movq 0x10(%rbx), %rsi
movl $0x20, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rsi, %rax
subq 0x8(%rbx), %rax
sarq $0x3, %rax
movl (%rbx), %ecx
cmpq %rcx, %rax
jae 0x4b17
cmpq 0x18(%rbx), %rsi
je 0x4a70
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%rbx)
jmp 0x4a81
movq -0x58(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x9afe
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x8(%rcx,%r13)
je 0x4b8b
leaq (%rcx,%r13), %rdi
addq $-0x18, %rdi
leaq -0x80(%rbp), %rsi
callq *0x18(%rdi)
movq %rax, %r14
movq 0x10(%rbx), %rsi
addq $-0x8, %rsi
movq %rsi, 0x10(%rbx)
testq %rax, %rax
je 0x4ac1
cmpl $0x8, (%r14)
movq %r14, %r15
jne 0x4afc
incq %r12
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
addq $0x28, %r13
cmpq %rax, %r12
jb 0x4a46
jmp 0x4af0
xorl %r15d, %r15d
testq %r15, %r15
jne 0x4af9
movq -0x30(%rbp), %r15
movq %r15, %r14
leaq -0x50(%rbp), %rdi
callq 0x3ac4
movq %r14, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x30, %edi
callq 0x31b0
movq %rax, %r14
leaq -0x68(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0xa811(%rip), %rsi      # 0xf344
leaq 0xa823(%rip), %rdx      # 0xf35d
leaq -0x78(%rbp), %rdi
callq 0x3bb0
movq -0x30(%rbp), %rbx
movl $0x8, (%r14)
leaq 0x8(%r14), %rdi
movq %r14, %rax
addq $0x18, %rax
movq %rax, 0x8(%r14)
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq %rbx, 0x28(%r14)
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0x4afc
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x4afc
callq 0x30e0
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %r15, %rdi
je 0x4bad
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x4bad
movq %rax, %rbx
movl $0x30, %esi
movq %r14, %rdi
callq 0x31c0
jmp 0x4bc7
jmp 0x4bc4
jmp 0x4bc4
jmp 0x4bc4
jmp 0x4bc4
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x3ac4
movq %rbx, %rdi
callq 0x32e0