// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x6e25(%rip), %rsi      # 0xc2dd
leaq -0xf8(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x3100
leaq -0xf8(%rbp), %rsi
movq %r15, %rdi
callq 0x74d4
leaq -0xf8(%rbp), %rdi
callq 0x3140
movq %r15, -0x58(%rbp)
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x6deb(%rip), %rsi      # 0xc2e4
leaq -0xd8(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x3100
leaq -0xd8(%rbp), %rsi
movq %r15, %rdi
callq 0x749c
leaq -0xd8(%rbp), %rdi
callq 0x3140
movq %r15, -0x48(%rbp)
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x6d74(%rip), %rsi      # 0xc2ae
leaq -0xb8(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0x3100
leaq -0xb8(%rbp), %rsi
movq %r15, %rdi
callq 0x749c
leaq -0xb8(%rbp), %rdi
callq 0x3140
movq %r15, -0x40(%rbp)
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x6d41(%rip), %rsi      # 0xc2bc
leaq -0x98(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x3100
leaq -0x98(%rbp), %rsi
movq %r15, %rdi
callq 0x749c
leaq -0x98(%rbp), %rdi
callq 0x3140
movq %r15, -0x38(%rbp)
pushq $0x28
popq %rdi
callq 0x3200
movq %rax, %r15
leaq 0x6d0f(%rip), %rsi      # 0xc2cb
leaq -0x78(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x3100
leaq -0x78(%rbp), %rsi
movq %r15, %rdi
callq 0x749c
leaq -0x78(%rbp), %rdi
callq 0x3140
leaq -0x50(%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 -0x58(%rbp), %rsi
movq %r13, %rdi
callq 0x74b0
leaq -0x48(%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 0x1bdd(%rip), %rax      # 0x7232
movq %rax, 0x20(%rbx)
leaq 0x1df6(%rip), %rax      # 0x7456
movq %rax, 0x18(%rbx)
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
jmp 0x56b8
jmp 0x56bf
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
jmp 0x56b8
jmp 0x56bf
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
jmp 0x56b8
jmp 0x56bf
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
jmp 0x56b8
jmp 0x56bf
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x3140
jmp 0x56c2
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r15, %rdi
callq 0x3210
movq %rbx, %rdi
callq 0x3320
nop