// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movq %rsi, %r13
movq %rdi, %rbx
movl $0x28, %edi
callq 0x31b0
movq %rax, %r14
leaq -0xf0(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x8e4e(%rip), %rsi      # 0xf329
leaq 0x8e4d(%rip), %rdx      # 0xf32f
leaq -0x100(%rbp), %rdi
callq 0x3bb0
movl $0x0, (%r14)
leaq 0x8(%r14), %rdi
movq %r14, %rax
addq $0x18, %rax
movq %rax, 0x8(%r14)
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0x100(%rbp), %rdi
cmpq %r15, %rdi
je 0x6535
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r14, -0x108(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0xc8(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x8dd5(%rip), %rsi      # 0xf330
leaq 0x8de1(%rip), %rdx      # 0xf343
leaq -0xd8(%rbp), %rdi
callq 0x3bb0
movl $0x3, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq -0xd8(%rbp), %rsi
movq -0xd0(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0xd8(%rbp), %rdi
cmpq %r14, %rdi
je 0x65b5
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0xe0(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r12
leaq -0xa0(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x8d1f(%rip), %rsi      # 0xf2fa
leaq 0x8d25(%rip), %rdx      # 0xf307
leaq -0xb0(%rbp), %rdi
callq 0x3bb0
movl $0x3, (%r12)
leaq 0x8(%r12), %rdi
movq %r12, %rax
addq $0x18, %rax
movq %rax, 0x8(%r12)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq %r13, -0x30(%rbp)
movq -0xb0(%rbp), %rdi
cmpq %r14, %rdi
je 0x663c
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r12, -0xb8(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r13
leaq -0x78(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x8ca9(%rip), %rsi      # 0xf308
leaq 0x8cb0(%rip), %rdx      # 0xf316
leaq -0x88(%rbp), %rdi
callq 0x3bb0
movl $0x3, (%r13)
leaq 0x8(%r13), %rdi
movq %r13, %rax
addq $0x18, %rax
movq %rax, 0x8(%r13)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq %r15, -0x38(%rbp)
movq -0x88(%rbp), %rdi
cmpq %r14, %rdi
je 0x66b8
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r13, -0x90(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r14
leaq -0x50(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x8c3c(%rip), %rsi      # 0xf317
leaq 0x8c3f(%rip), %rdx      # 0xf321
leaq -0x60(%rbp), %rdi
callq 0x3bb0
movl $0x3, (%r14)
leaq 0x8(%r14), %rdi
movq %r14, %rax
addq $0x18, %rax
movq %rax, 0x8(%r14)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq %rbx, -0x40(%rbp)
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0x672a
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r14, -0x68(%rbp)
movl $0x20, %edi
callq 0x31b0
movq %rax, %rbx
movl $0x1, (%rax)
leaq 0x8(%rax), %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
leaq -0x108(%rbp), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x9afe
movq 0x10(%rbx), %rsi
cmpq 0x18(%rbx), %rsi
je 0x6784
movq -0x38(%rbp), %rax
movq %rax, (%rsi)
movq 0x10(%rbx), %rsi
addq $0x8, %rsi
movq %rsi, 0x10(%rbx)
jmp 0x6797
leaq -0xe0(%rbp), %rdx
movq %r15, %rdi
callq 0x9afe
movq 0x10(%rbx), %rsi
cmpq 0x18(%rbx), %rsi
je 0x67ae
movq %r12, (%rsi)
movq 0x10(%rbx), %rsi
addq $0x8, %rsi
movq %rsi, 0x10(%rbx)
jmp 0x67c1
leaq -0xb8(%rbp), %rdx
movq %r15, %rdi
callq 0x9afe
movq 0x10(%rbx), %rsi
cmpq 0x18(%rbx), %rsi
je 0x67d8
movq %r13, (%rsi)
movq 0x10(%rbx), %rsi
addq $0x8, %rsi
movq %rsi, 0x10(%rbx)
jmp 0x67eb
leaq -0x90(%rbp), %rdx
movq %r15, %rdi
callq 0x9afe
movq 0x10(%rbx), %rsi
movq -0x30(%rbp), %r12
cmpq 0x18(%rbx), %rsi
je 0x67ff
movq %r14, (%rsi)
addq $0x8, 0x10(%rbx)
jmp 0x680b
leaq -0x68(%rbp), %rdx
movq %r15, %rdi
callq 0x9afe
movq -0x40(%rbp), %rax
movq %rbx, (%rax)
movq $0x0, 0x10(%rax)
movq %r12, 0x8(%rax)
leaq 0x2cbd(%rip), %rcx      # 0x94e2
movq %rcx, 0x20(%rax)
leaq 0x301c(%rip), %rcx      # 0x984c
movq %rcx, 0x18(%rax)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0x690e
movq -0x50(%rbp), %rsi
jmp 0x6901
jmp 0x690b
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r14, %rdi
je 0x6884
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x6884
movq %rax, %rbx
movl $0x28, %esi
movq %r13, %rdi
jmp 0x6916
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
cmpq %r14, %rdi
je 0x68b4
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x68b4
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x6916
movq %rax, %rbx
movq -0xd8(%rbp), %rdi
cmpq %r14, %rdi
je 0x68e1
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x68e1
movq %rax, %rbx
movl $0x28, %esi
movq %r15, %rdi
jmp 0x6916
movq %rax, %rbx
movq -0x100(%rbp), %rdi
cmpq %r15, %rdi
je 0x690e
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x690e
movq %rax, %rbx
movl $0x28, %esi
movq %r14, %rdi
callq 0x31c0
movq %rbx, %rdi
callq 0x32e0
nop