// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0xd0, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0xd8(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x8ea7(%rip), %rsi      # 0xf361
leaq 0x8ea6(%rip), %rdx      # 0xf367
leaq -0xe8(%rbp), %rdi
callq 0x3ba8
movl $0x0, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0xe8(%rbp), %rdi
cmpq %r12, %rdi
je 0x6514
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0x48(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0xb8(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x8e30(%rip), %rsi      # 0xf368
leaq 0x8e3c(%rip), %rdx      # 0xf37b
leaq -0xc8(%rbp), %rdi
callq 0x3ba8
movl $0x3, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq -0xc8(%rbp), %rsi
movq -0xc0(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0xc8(%rbp), %rdi
cmpq %r12, %rdi
je 0x6592
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0x40(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0x98(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x8d7c(%rip), %rsi      # 0xf332
leaq 0x8d82(%rip), %rdx      # 0xf33f
leaq -0xa8(%rbp), %rdi
callq 0x3ba8
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 %r12, %rdi
je 0x6610
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0x38(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0x78(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x8d0f(%rip), %rsi      # 0xf340
leaq 0x8d16(%rip), %rdx      # 0xf34e
leaq -0x88(%rbp), %rdi
callq 0x3ba8
movl $0x3, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0x88(%rbp), %rdi
cmpq %r12, %rdi
je 0x6685
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0x30(%rbp)
movl $0x28, %edi
callq 0x31b0
movq %rax, %r15
leaq -0x58(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x8ca9(%rip), %rsi      # 0xf34f
leaq 0x8cac(%rip), %rdx      # 0xf359
leaq -0x68(%rbp), %rdi
callq 0x3ba8
movl $0x3, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
callq 0x3a18
movq -0x68(%rbp), %rdi
cmpq %r12, %rdi
je 0x66f1
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r15, -0x28(%rbp)
movl $0x20, %edi
callq 0x31b0
movq %rax, %r15
movl $0x1, (%rax)
leaq 0x8(%rax), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movq 0x10(%rax), %rsi
testq %rsi, %rsi
je 0x6732
movq -0x48(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x673e
leaq -0x48(%rbp), %rdx
movq %r12, %rdi
callq 0x9bf4
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x6756
movq -0x40(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x6762
leaq -0x40(%rbp), %rdx
movq %r12, %rdi
callq 0x9bf4
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x677a
movq -0x38(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x6786
leaq -0x38(%rbp), %rdx
movq %r12, %rdi
callq 0x9bf4
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x679e
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x67aa
leaq -0x30(%rbp), %rdx
movq %r12, %rdi
callq 0x9bf4
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x67c2
movq -0x28(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x67ce
leaq -0x28(%rbp), %rdx
movq %r12, %rdi
callq 0x9bf4
movq %r15, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movups %xmm0, 0x18(%rbx)
movq %r14, 0x8(%rbx)
leaq 0x2dfb(%rip), %rax      # 0x95e2
movq %rax, 0x20(%rbx)
leaq 0x3160(%rip), %rax      # 0x9952
movq %rax, 0x18(%rbx)
movq %rbx, %rax
addq $0xd0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r12, %rdi
je 0x688b
movq -0x58(%rbp), %rsi
jmp 0x687e
jmp 0x6888
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r12, %rdi
je 0x688b
movq -0x78(%rbp), %rsi
jmp 0x687e
jmp 0x6888
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r12, %rdi
je 0x688b
movq -0x98(%rbp), %rsi
jmp 0x687e
jmp 0x6888
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
cmpq %r12, %rdi
je 0x688b
movq -0xb8(%rbp), %rsi
jmp 0x687e
jmp 0x6888
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
cmpq %r12, %rdi
je 0x688b
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0x688b
movq %rax, %rbx
movl $0x28, %esi
movq %r15, %rdi
callq 0x31c0
movq %rbx, %rdi
callq 0x32e0