// File: 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
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x130(%rbp)
movq %rdi, %rax
movq %rax, -0x128(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x120(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0x118(%rbp)
movb $0x1, -0x4d(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x34a0
movq -0x110(%rbp), %rdx
leaq 0x1b818(%rip), %rsi     # 0x233ba
leaq -0x38(%rbp), %rdi
callq 0x33b0
jmp 0x7bad
movq -0x118(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x11960
jmp 0x7bbf
movb $0x0, -0x4d(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31c0
leaq -0x39(%rbp), %rdi
callq 0x3300
movq -0x118(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0x140(%rbp)
movb $0x1, -0x7a(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x34a0
movq -0x138(%rbp), %rdx
leaq 0x1b7ab(%rip), %rsi     # 0x233be
leaq -0x78(%rbp), %rdi
callq 0x33b0
jmp 0x7c1e
movq -0x140(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x11830
jmp 0x7c30
movb $0x0, -0x7a(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x31c0
leaq -0x79(%rbp), %rdi
callq 0x3300
movq -0x140(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0x150(%rbp)
movb $0x1, -0xaa(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x34a0
movq -0x148(%rbp), %rdx
leaq 0x1b742(%rip), %rsi     # 0x233cc
leaq -0xa8(%rbp), %rdi
callq 0x33b0
jmp 0x7c98
movq -0x150(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x11830
jmp 0x7cad
movb $0x0, -0xaa(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x31c0
leaq -0xa9(%rbp), %rdi
callq 0x3300
movq -0x150(%rbp), %rax
movq %rax, -0x88(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0x160(%rbp)
movb $0x1, -0xda(%rbp)
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x34a0
movq -0x158(%rbp), %rdx
leaq 0x1b6c8(%rip), %rsi     # 0x233db
leaq -0xd8(%rbp), %rdi
callq 0x33b0
jmp 0x7d21
movq -0x160(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x11830
jmp 0x7d36
movb $0x0, -0xda(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x31c0
leaq -0xd9(%rbp), %rdi
callq 0x3300
movq -0x160(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl $0x20, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x168(%rbp)
callq 0x11870
jmp 0x7d81
movq -0x168(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
addq $0x8, %rdi
leaq -0x18(%rbp), %rsi
callq 0x118b0
movq -0xe8(%rbp), %rdi
addq $0x8, %rdi
leaq -0x58(%rbp), %rsi
callq 0x118b0
movq -0xe8(%rbp), %rdi
addq $0x8, %rdi
leaq -0x88(%rbp), %rsi
callq 0x118b0
movq -0xe8(%rbp), %rdi
addq $0x8, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x118b0
movq -0x120(%rbp), %rax
movq -0xe8(%rbp), %rcx
movq %rcx, -0xf0(%rbp)
movq %rax, -0x108(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x7fa0
movq -0x130(%rbp), %rdi
movq %rax, -0x100(%rbp)
movq %rdx, -0xf8(%rbp)
leaq -0x100(%rbp), %rsi
callq 0x7fd0
movq -0x128(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x7e68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31c0
leaq -0x39(%rbp), %rdi
callq 0x3300
testb $0x1, -0x4d(%rbp)
jne 0x7e79
jmp 0x7e8a
movq -0x118(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x7f96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x7eb2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x31c0
leaq -0x79(%rbp), %rdi
callq 0x3300
testb $0x1, -0x7a(%rbp)
jne 0x7ec3
jmp 0x7ed4
movq -0x140(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x7f96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x7eff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x31c0
leaq -0xa9(%rbp), %rdi
callq 0x3300
testb $0x1, -0xaa(%rbp)
jne 0x7f16
jmp 0x7f27
movq -0x150(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x7f96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x7f4f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x31c0
leaq -0xd9(%rbp), %rdi
callq 0x3300
testb $0x1, -0xda(%rbp)
jne 0x7f66
jmp 0x7f77
movq -0x160(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x7f96
movq -0x168(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x20, %esi
callq 0x32c0
movq -0x48(%rbp), %rdi
callq 0x3490
nop