// File: BlinfoldKing[P]lisval/src/lib/evaluator.cpp
// Function: Evaluator::filter()
pair<Token*, function<Token*(Token*)>> Evaluator::filter() {
    Token* op = new Atom("filter");
    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 = 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);
                Token* filter = this->eval(output);
                if (filter->type == TokenType::BOOLEAN) {
                    Boolean* b = static_cast<Boolean*>(filter);
                    if (b->value == true) {
                        res->list.push_back(subtitute_list->list[i]);
                    }
                }
            }

            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 0x1b374(%rip), %rsi     # 0x233e6
leaq -0x38(%rbp), %rdi
callq 0x33b0
jmp 0x807d
movq -0x118(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x11960
jmp 0x808f
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 0x1b2db(%rip), %rsi     # 0x233be
leaq -0x78(%rbp), %rdi
callq 0x33b0
jmp 0x80ee
movq -0x140(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x11830
jmp 0x8100
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 0x1b272(%rip), %rsi     # 0x233cc
leaq -0xa8(%rbp), %rdi
callq 0x33b0
jmp 0x8168
movq -0x150(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x11830
jmp 0x817d
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 0x1b1f8(%rip), %rsi     # 0x233db
leaq -0xd8(%rbp), %rdi
callq 0x33b0
jmp 0x81f1
movq -0x160(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x11830
jmp 0x8206
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 0x8251
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 0x8470
movq -0x130(%rbp), %rdi
movq %rax, -0x100(%rbp)
movq %rdx, -0xf8(%rbp)
leaq -0x100(%rbp), %rsi
callq 0x84a0
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 0x8338
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 0x8349
jmp 0x835a
movq -0x118(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x8466
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8382
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 0x8393
jmp 0x83a4
movq -0x140(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x8466
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x83cf
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 0x83e6
jmp 0x83f7
movq -0x150(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x8466
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x841f
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 0x8436
jmp 0x8447
movq -0x160(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x8466
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