// File: BlinfoldKing[P]lisval/src/lib/evaluator.cpp
// Function: Evaluator::reduce()
pair<Token*, function<Token*(Token*)>> Evaluator::reduce() {
    Token* op = new Atom("reduce");
    Token* accumulator = new Variable("ACCUMULATOR_PATTERN");
    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(accumulator);
    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) {
            Token* t = NULL;

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

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

            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);

            if (subtitute_list->list.size() < 1) {
                t = new Error("insufficient list size", input);
                return t;
            }

            Token* accumulator = subtitute_list->list[0];
            Variable* accumulator_var = static_cast<Variable*>(accumulator_pattern);

            for (size_t i = 1; i < subtitute_list->list.size(); i++) {
                auto args = generateVariableArgument(input_pattern, subtitute_list->list[i]);
                args.push_back(make_pair(accumulator_var, accumulator));

                Token* output = subtitute_variable(result_pattern, args);
                accumulator = this->eval(output);
            }

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

    return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x160(%rbp)
movq %rdi, %rax
movq %rax, -0x158(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0x148(%rbp)
movb $0x1, -0x4d(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x34a0
movq -0x140(%rbp), %rdx
leaq 0x1aeab(%rip), %rsi     # 0x233ed
leaq -0x38(%rbp), %rdi
callq 0x33b0
jmp 0x854d
movq -0x148(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x11960
jmp 0x855f
movb $0x0, -0x4d(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31c0
leaq -0x39(%rbp), %rdi
callq 0x3300
movq -0x148(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0x170(%rbp)
movb $0x1, -0x7a(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x34a0
movq -0x168(%rbp), %rdx
leaq 0x1ae41(%rip), %rsi     # 0x233f4
leaq -0x78(%rbp), %rdi
callq 0x33b0
jmp 0x85be
movq -0x170(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x11830
jmp 0x85d0
movb $0x0, -0x7a(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x31c0
leaq -0x79(%rbp), %rdi
callq 0x3300
movq -0x170(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0x180(%rbp)
movb $0x1, -0xaa(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x34a0
movq -0x178(%rbp), %rdx
leaq 0x1ad94(%rip), %rsi     # 0x233be
leaq -0xa8(%rbp), %rdi
callq 0x33b0
jmp 0x8638
movq -0x180(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x11830
jmp 0x864d
movb $0x0, -0xaa(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x31c0
leaq -0xa9(%rbp), %rdi
callq 0x3300
movq -0x180(%rbp), %rax
movq %rax, -0x88(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0x190(%rbp)
movb $0x1, -0xda(%rbp)
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x34a0
movq -0x188(%rbp), %rdx
leaq 0x1ad19(%rip), %rsi     # 0x233cc
leaq -0xd8(%rbp), %rdi
callq 0x33b0
jmp 0x86c1
movq -0x190(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x11830
jmp 0x86d6
movb $0x0, -0xda(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x31c0
leaq -0xd9(%rbp), %rdi
callq 0x3300
movq -0x190(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl $0x28, %edi
callq 0x32b0
movq %rax, -0x1a0(%rbp)
movb $0x1, -0x10a(%rbp)
leaq -0x109(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x34a0
movq -0x198(%rbp), %rdx
leaq 0x1ac9f(%rip), %rsi     # 0x233db
leaq -0x108(%rbp), %rdi
callq 0x33b0
jmp 0x874a
movq -0x1a0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x11830
jmp 0x875f
movb $0x0, -0x10a(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x31c0
leaq -0x109(%rbp), %rdi
callq 0x3300
movq -0x1a0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl $0x20, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x1a8(%rbp)
callq 0x11870
jmp 0x87aa
movq -0x1a8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rdi
addq $0x8, %rdi
leaq -0x18(%rbp), %rsi
callq 0x118b0
movq -0x118(%rbp), %rdi
addq $0x8, %rdi
leaq -0x58(%rbp), %rsi
callq 0x118b0
movq -0x118(%rbp), %rdi
addq $0x8, %rdi
leaq -0x88(%rbp), %rsi
callq 0x118b0
movq -0x118(%rbp), %rdi
addq $0x8, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x118b0
movq -0x118(%rbp), %rdi
addq $0x8, %rdi
leaq -0xe8(%rbp), %rsi
callq 0x118b0
movq -0x150(%rbp), %rax
movq -0x118(%rbp), %rcx
movq %rcx, -0x120(%rbp)
movq %rax, -0x138(%rbp)
leaq -0x120(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x8a40
movq -0x160(%rbp), %rdi
movq %rax, -0x130(%rbp)
movq %rdx, -0x128(%rbp)
leaq -0x130(%rbp), %rsi
callq 0x8a70
movq -0x158(%rbp), %rax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x88a8
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 0x88b9
jmp 0x88ca
movq -0x148(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x8a29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x88f2
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 0x8903
jmp 0x8914
movq -0x170(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x8a29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x893f
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 0x8956
jmp 0x8967
movq -0x180(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x8a29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8992
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 0x89a9
jmp 0x89ba
movq -0x190(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x8a29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x89e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x31c0
leaq -0x109(%rbp), %rdi
callq 0x3300
testb $0x1, -0x10a(%rbp)
jne 0x89f9
jmp 0x8a0a
movq -0x1a0(%rbp), %rdi
movl $0x28, %esi
callq 0x32c0
jmp 0x8a29
movq -0x1a8(%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
nopw %cs:(%rax,%rax)