// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x700c(%rip), %rsi      # 0xc2d6
leaq -0xd0(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x3100
leaq -0xd0(%rbp), %rsi
movq %r15, %rdi
callq 0x74d4
leaq -0xd0(%rbp), %rdi
callq 0x3140
movq %r15, -0x50(%rbp)
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x6fa3(%rip), %rsi      # 0xc2ae
leaq -0xb0(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0x3100
leaq -0xb0(%rbp), %rsi
movq %r15, %rdi
callq 0x749c
leaq -0xb0(%rbp), %rdi
callq 0x3140
movq %r15, -0x40(%rbp)
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x6f70(%rip), %rsi      # 0xc2bc
leaq -0x90(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x3100
leaq -0x90(%rbp), %rsi
movq %r15, %rdi
callq 0x749c
leaq -0x90(%rbp), %rdi
callq 0x3140
movq %r15, -0x38(%rbp)
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x6f3e(%rip), %rsi      # 0xc2cb
leaq -0x70(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x3100
leaq -0x70(%rbp), %rsi
movq %r15, %rdi
callq 0x749c
leaq -0x70(%rbp), %rdi
callq 0x3140
leaq -0x48(%rbp), %r12
movq %r15, (%r12)
pushq $0x20
popq %rdi
callq 0x3200
movq %rax, %r15
movl $0x1, (%rax)
leaq 0x8(%rax), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
andq $0x0, 0x18(%rax)
leaq -0x50(%rbp), %rsi
movq %r13, %rdi
callq 0x74b0
leaq -0x40(%rbp), %rsi
movq %r13, %rdi
callq 0x74b0
leaq -0x38(%rbp), %rsi
movq %r13, %rdi
callq 0x74b0
movq %r13, %rdi
movq %r12, %rsi
callq 0x74b0
movq %r15, (%rbx)
andq $0x0, 0x10(%rbx)
movq %r14, 0x8(%rbx)
leaq 0x1bfc(%rip), %rax      # 0x7016
movq %rax, 0x20(%rbx)
leaq 0x1de3(%rip), %rax      # 0x7208
movq %rax, 0x18(%rbx)
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
jmp 0x546f
jmp 0x5476
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
jmp 0x546f
jmp 0x5476
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
jmp 0x546f
jmp 0x5476
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x3140
jmp 0x5479
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r15, %rdi
callq 0x3210
movq %rbx, %rdi
callq 0x3320