// File: /workspace/github_cpp_makefile_star2_O3/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 $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 0x91d1(%rip), %rsi      # 0xf322
leaq 0x91d0(%rip), %rdx      # 0xf328
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 0x61ab
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 0x9129(%rip), %rsi      # 0xf2fa
leaq 0x912f(%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 0x622b
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 0x90ba(%rip), %rsi      # 0xf308
leaq 0x90c1(%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 0x62a0
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 0x9054(%rip), %rsi      # 0xf317
leaq 0x9057(%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 0x6313
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 0x6369
movq %r15, (%rsi)
movq 0x10(%r14), %rsi
addq $0x8, %rsi
movq %rsi, 0x10(%r14)
jmp 0x637c
leaq -0xb0(%rbp), %rdx
movq %rbx, %rdi
callq 0x9afe
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0x6393
movq %r12, (%rsi)
movq 0x10(%r14), %rsi
addq $0x8, %rsi
movq %rsi, 0x10(%r14)
jmp 0x63a6
leaq -0x88(%rbp), %rdx
movq %rbx, %rdi
callq 0x9afe
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0x63b6
movq %r13, (%rsi)
addq $0x8, 0x10(%r14)
jmp 0x63c2
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 0x2e20(%rip), %rcx      # 0x9200
movq %rcx, 0x20(%rax)
leaq 0x30cd(%rip), %rcx      # 0x94b8
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 0x641e
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x641e
movq %rax, %rbx
movl $0x28, %esi
movq %r13, %rdi
jmp 0x6494
movq %rax, %rbx
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x6445
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x6445
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x6494
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r14, %rdi
je 0x648c
movq -0x98(%rbp), %rsi
jmp 0x647f
jmp 0x6489
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
cmpq %r14, %rdi
je 0x648c
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x648c
movq %rax, %rbx
movl $0x28, %esi
movq %r15, %rdi
callq 0x31c0
movq %rbx, %rdi
callq 0x32e0
nop