// File: /workspace/github_cpp_makefile_star2_O3/BlinfoldKing[P]lisval/src/lib/evaluator.cpp
// Function: Evaluator::map()
pair<Token*, function<Token*(Token*)>> Evaluator::map() {
    Token* op = new Atom("map");
    Token* input_pattern = new Variable("INPUT_PATTERN");
    Token* result_pattern = new Variable("RESULT_PATTERN");
    Token* list_input = new Variable("LIST_INPUT");
    List* signature = new List();
    signature->list.push_back(op);
    signature->list.push_back(input_pattern);
    signature->list.push_back(result_pattern);
    signature->list.push_back(list_input);
    Token* token = signature;
    auto res = make_pair(token, [this](Token* input) {
            List* res = new List();
            Token* t = NULL;

            List* input_list = static_cast<List*>(input);

            Token* input_pattern = input_list->list[1];
            Token* result_pattern = input_list->list[2];
            if (result_pattern->type == TokenType::ERROR) {
                Error* err = static_cast<Error*>(input_list->list[2]);
                result_pattern = err->unwrap();
            }

            Token* list_token = this->eval(input_list->list[3]);
            if (list_token->type != TokenType::LIST) {
                t = new Error("expected a list, got:" + list_token->debug(), input);
                return t;
            }

            List* subtitute_list = static_cast<List*>(list_token);

            for (size_t i = 0; i < subtitute_list->list.size(); i++) {
                auto args = generateVariableArgument(input_pattern, subtitute_list->list[i]);
                Token* output = subtitute_variable(result_pattern, args);

                res->list.push_back(output);
            }

            t = this->eval(res);
            return t;
    });

    return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %r13
movq %rdi, %rbx
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0xc0(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x952f(%rip), %rsi      # 0xf2f6
leaq 0x952b(%rip), %rdx      # 0xf2f9
leaq -0xd0(%rbp), %rdi
callq 0x3bb0
movl $0x0, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0xd0(%rbp), %rdi
cmpq %r14, %rdi
je 0x5e21
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0xd8(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0x98(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x94b3(%rip), %rsi      # 0xf2fa
leaq 0x94b9(%rip), %rdx      # 0xf307
leaq -0xa8(%rbp), %rdi
callq 0x3bb0
movl $0x3, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0xa8(%rbp), %rdi
cmpq %r14, %rdi
je 0x5ea1
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0xb0(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r12
leaq -0x70(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x9444(%rip), %rsi      # 0xf308
leaq 0x944b(%rip), %rdx      # 0xf316
leaq -0x80(%rbp), %rdi
callq 0x3bb0
movl $0x3, (%r12)
leaq 0x8(%r12), %rdi
movq %r12, %rax
addq $0x18, %rax
movq %rax, 0x8(%r12)
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq %r13, -0x38(%rbp)
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x5f16
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r12, -0x88(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r13
leaq -0x48(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x93de(%rip), %rsi      # 0xf317
leaq 0x93e1(%rip), %rdx      # 0xf321
leaq -0x58(%rbp), %rdi
callq 0x3bb0
movl $0x3, (%r13)
leaq 0x8(%r13), %rdi
movq %r13, %rax
addq $0x18, %rax
movq %rax, 0x8(%r13)
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq %rbx, -0x30(%rbp)
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x5f89
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r13, -0x60(%rbp)
movl $0x20, %edi
callq 0x31b0
movq %rax, %r14
movl $0x1, (%rax)
leaq 0x8(%rax), %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
leaq -0xd8(%rbp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x9afe
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0x5fdf
movq %r15, (%rsi)
movq 0x10(%r14), %rsi
addq $0x8, %rsi
movq %rsi, 0x10(%r14)
jmp 0x5ff2
leaq -0xb0(%rbp), %rdx
movq %rbx, %rdi
callq 0x9afe
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0x6009
movq %r12, (%rsi)
movq 0x10(%r14), %rsi
addq $0x8, %rsi
movq %rsi, 0x10(%r14)
jmp 0x601c
leaq -0x88(%rbp), %rdx
movq %rbx, %rdi
callq 0x9afe
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0x602c
movq %r13, (%rsi)
addq $0x8, 0x10(%r14)
jmp 0x6038
leaq -0x60(%rbp), %rdx
movq %rbx, %rdi
callq 0x9afe
movq -0x30(%rbp), %rax
movq %r14, (%rax)
movq $0x0, 0x10(%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x2ed8(%rip), %rcx      # 0x8f2e
movq %rcx, 0x20(%rax)
leaq 0x3175(%rip), %rcx      # 0x91d6
movq %rcx, 0x18(%rax)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x6094
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x6094
movq %rax, %rbx
movl $0x28, %esi
movq %r13, %rdi
jmp 0x610a
movq %rax, %rbx
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x60bb
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x60bb
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x610a
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r14, %rdi
je 0x6102
movq -0x98(%rbp), %rsi
jmp 0x60f5
jmp 0x60ff
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
cmpq %r14, %rdi
je 0x6102
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x6102
movq %rax, %rbx
movl $0x28, %esi
movq %r15, %rdi
callq 0x31c0
movq %rbx, %rdi
callq 0x32e0
nop