// File: /workspace/github_cpp_makefile_star2_O2/pfnet-research[P]pml/src/typechecker.cpp
// Function: typechecker::typecheck(ast::expr_t const&, environment_t<ast::expr_t> const&)
bool typecheck(ast::expr_t const& expr, env_t const& env) {
    using namespace ast;
    switch (expr.kind()) {
    case expr_kind_t::LetFun:
    case expr_kind_t::App:
        throw std::runtime_error{"unimplemented yet"};
    case expr_kind_t::Typed:
        return model_checking(
                *add_bindings(cast<typed_expr_t>(expr).expr, env),
                cast<typed_expr_t>(expr).type);
    case expr_kind_t::Let: {
        auto init = cast<let_expr_t>(expr).init;
        if (!typecheck(*init, env))
            return false;
        std::string name = cast<let_expr_t>(expr).name;
        auto new_env = env.append(name, init);
        auto body = cast<let_expr_t>(expr).body;
        return typecheck(*body, new_env);
        }
    case expr_kind_t::If:
        return
            typecheck(*cast<if_expr_t>(expr).cond_expr, env) &&
            typecheck(*cast<if_expr_t>(expr).true_expr, env) &&
            typecheck(*cast<if_expr_t>(expr).false_expr, env);
    case expr_kind_t::Neg:
        return typecheck(*cast<neg_expr_t>(expr).inner, env);
    case expr_kind_t::Add: case expr_kind_t::Sub:
    case expr_kind_t::Mul: case expr_kind_t::Div:
    case expr_kind_t::Eq: case expr_kind_t::Neq:
    case expr_kind_t::Leq: case expr_kind_t::Geq:
    case expr_kind_t::And: case expr_kind_t::Or:
        return
            typecheck(*cast<binop_expr_t>(expr).lhs, env) &&
            typecheck(*cast<binop_expr_t>(expr).rhs, env);
    case expr_kind_t::Int: case expr_kind_t::Bool: case expr_kind_t::Fun:
    case expr_kind_t::Rand: case expr_kind_t::Var:
        return true; // primitives
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x8a9a(%rip), %r13      # 0x3e4a8
leaq 0x24063(%rip), %r15     # 0x59a78
leaq 0x24214(%rip), %r12     # 0x59c30
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
movl %eax, %eax
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x35cbc
movq 0x8(%rax), %rdi
movq %rbx, %rsi
callq 0x359f4
testb %al, %al
je 0x35ab6
movq %r14, %rdi
callq 0x35cbc
addq $0x18, %rax
movq (%rax), %r14
jmp 0x35a1c
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x9330
testq %rax, %rax
je 0x35bf6
addq $0x8, %rax
jmp 0x35a54
movq %r14, %rdi
callq 0x35c9a
movq 0x8(%rax), %rdi
movq %rbx, %rsi
callq 0x359f4
testb %al, %al
je 0x35ab6
movq %r14, %rdi
callq 0x35c9a
movq 0x18(%rax), %rdi
movq %rbx, %rsi
callq 0x359f4
testb %al, %al
je 0x35ab6
movq %r14, %rdi
callq 0x35c9a
addq $0x28, %rax
jmp 0x35a54
xorl %ebx, %ebx
jmp 0x35bb6
movb $0x1, %bl
jmp 0x35bb6
movq %r14, %rdi
callq 0x35c78
leaq 0x28(%rax), %rsi
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xbde0
movq (%r15), %rdi
movq %rbx, %rsi
callq 0x359f4
testb %al, %al
je 0x35baa
movq %r14, %rdi
callq 0x35c78
addq $0x8, %rax
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x93a0
leaq 0x48(%rsp), %rdi
leaq 0x18(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %rbx, %rsi
callq 0xb9a0
movq %r14, %rdi
callq 0x35c78
addq $0x38, %rax
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0xbde0
movq (%rbx), %rdi
leaq 0x48(%rsp), %rsi
callq 0x359f4
movl %eax, %ebx
leaq 0x40(%rsp), %rdi
callq 0xa248
leaq 0x48(%rsp), %rdi
callq 0xbe14
leaq 0x18(%rsp), %rdi
callq 0x9130
jmp 0x35bac
movq %r14, %rdi
callq 0x35c56
leaq 0x8(%rax), %rsi
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rdx
callq 0x35946
movq (%r15), %rbx
movq %r14, %rdi
callq 0x35c56
addq $0x18, %rax
movq %rbx, %rdi
movq %rax, %rsi
callq 0x35818
movl %eax, %ebx
leaq 0x20(%rsp), %rdi
jmp 0x35bb1
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
callq 0xa248
movl %ebx, %eax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
pushq $0x10
popq %rdi
callq 0x9150
movq %rax, %r14
leaq 0x8a24(%rip), %rsi      # 0x3e5fc
movq %rax, %rdi
callq 0x90f0
movq 0x26409(%rip), %rsi     # 0x5bff0
movq 0x263a2(%rip), %rdx     # 0x5bf90
movq %r14, %rdi
callq 0x94b0
callq 0x9510
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0xa248
jmp 0x35c0d
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0xbe14
jmp 0x35c1c
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x9130
jmp 0x35c2d
jmp 0x35c2a
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
jmp 0x35c3c
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0xa248
jmp 0x35c4e
movq %rax, %rbx
movq %r14, %rdi
callq 0x9210
movq %rbx, %rdi
callq 0x94d0