// File: /workspace/github_cpp_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x8121(%rip), %rbp      # 0x4cb08
leaq 0x1c082(%rip), %r15     # 0x60a70
leaq 0x1c1f3(%rip), %r12     # 0x60be8
leaq 0x1c204(%rip), %r13     # 0x60c00
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
movl %eax, %eax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44cc2
movq 0x8(%rax), %rdi
movq %rbx, %rsi
callq 0x449cc
testb %al, %al
je 0x44ae5
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44cc2
movq 0x18(%rax), %r14
jmp 0x449fc
movq %r14, %rdi
movq %r15, %rsi
leaq 0x1c1af(%rip), %rdx     # 0x60c18
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44cc2
movq 0x8(%rax), %r14
jmp 0x449fc
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44cc2
movq 0x8(%rax), %rdi
movq %rbx, %rsi
callq 0x449cc
testb %al, %al
je 0x44ae5
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44cc2
movq 0x18(%rax), %rdi
movq %rbx, %rsi
callq 0x449cc
testb %al, %al
je 0x44ae5
movq %r14, %rdi
callq 0x44d78
movq 0x28(%rax), %r14
jmp 0x449fc
xorl %ebx, %ebx
jmp 0x44aeb
movb $0x1, %bl
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bf6d(%rip), %rsi     # 0x60a70
leaq 0x1c01e(%rip), %rdx     # 0x60b28
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44cc2
movq 0x28(%rax), %rcx
movq %rcx, (%rsp)
movq 0x30(%rax), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x44bbf
movq 0x1e472(%rip), %rcx     # 0x62fb0
cmpb $0x0, (%rcx)
je 0x44bbb
incl 0x8(%rax)
jmp 0x44bbf
leaq 0x1bf21(%rip), %rsi     # 0x60a70
leaq 0x1c122(%rip), %rdx     # 0x60c78
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44cc2
addq $0x8, %rax
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x448e8
leaq 0x1beec(%rip), %rsi     # 0x60a70
leaq 0x1c0ed(%rip), %rdx     # 0x60c78
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44cf9
movq 0x10(%rsp), %rdi
addq $0x18, %rax
movq %rax, %rsi
callq 0x446c8
movl %eax, %ebx
movq 0x18(%rsp), %rdi
jmp 0x44caf
lock
incl 0x8(%rax)
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x449cc
testb %al, %al
je 0x44c67
leaq 0x1be96(%rip), %rsi     # 0x60a70
leaq 0x1bf47(%rip), %rdx     # 0x60b28
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44cfe
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0xad30
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %rsp, %rcx
movq %rbx, %rsi
callq 0xc9ec
leaq 0x1be42(%rip), %rsi     # 0x60a70
leaq 0x1bef3(%rip), %rdx     # 0x60b28
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44d03
movq 0x38(%rax), %rdi
movq 0x40(%rax), %r14
testq %r14, %r14
je 0x44c70
movq 0x1e354(%rip), %rax     # 0x62fb0
cmpb $0x0, (%rax)
je 0x44c6b
incl 0x8(%r14)
jmp 0x44c70
xorl %ebx, %ebx
jmp 0x44caa
lock
incl 0x8(%r14)
leaq 0x30(%rsp), %rsi
callq 0x449cc
movl %eax, %ebx
testq %r14, %r14
je 0x44c89
movq %r14, %rdi
callq 0xaa12
leaq 0x30(%rsp), %rdi
callq 0xcc7a
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x44caa
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x92e0
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x44aeb
callq 0xaa12
jmp 0x44aeb
callq 0x9530
movl $0x10, %edi
callq 0x9150
movq %rax, %r14
leaq 0x7f81(%rip), %rsi      # 0x4cc5c
movq %rax, %rdi
callq 0x9100
movq 0x1e306(%rip), %rsi     # 0x62ff0
movq 0x1e2af(%rip), %rdx     # 0x62fa0
movq %r14, %rdi
callq 0x94b0
callq 0x9530
callq 0x9530
callq 0x9530
movq %rax, %rbx
testq %r14, %r14
je 0x44d1d
movq %r14, %rdi
callq 0xaa12
jmp 0x44d1d
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xcc7a
jmp 0x44d2c
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x44d4a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x92e0
jmp 0x44d4a
jmp 0x44d47
movq %rax, %rbx
movq 0x8(%rsp), %rdi
jmp 0x44d59
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x44d70
callq 0xaa12
jmp 0x44d70
movq %rax, %rbx
movq %r14, %rdi
callq 0x9200
movq %rbx, %rdi
callq 0x94e0