// File: /workspace/github_cpp_makefile_star2_O3/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 0x8b71(%rip), %rbp      # 0x4cb08
leaq 0x1bafa(%rip), %r15     # 0x5fa98
leaq 0x1bc6b(%rip), %r12     # 0x5fc10
leaq 0x1bc7c(%rip), %r13     # 0x5fc28
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 0x44272
movq 0x8(%rax), %rdi
movq %rbx, %rsi
callq 0x43f7c
testb %al, %al
je 0x44095
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44272
addq $0x18, %rax
movq (%rax), %r14
jmp 0x43fac
movq %r14, %rdi
movq %r15, %rsi
leaq 0x1bc24(%rip), %rdx     # 0x5fc40
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44272
addq $0x8, %rax
jmp 0x4400a
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44272
movq 0x8(%rax), %rdi
movq %rbx, %rsi
callq 0x43f7c
testb %al, %al
je 0x44095
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44272
movq 0x18(%rax), %rdi
movq %rbx, %rsi
callq 0x43f7c
testb %al, %al
je 0x44095
movq %r14, %rdi
callq 0x44328
addq $0x28, %rax
jmp 0x4400a
xorl %ebx, %ebx
jmp 0x4409b
movb $0x1, %bl
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b9e5(%rip), %rsi     # 0x5fa98
leaq 0x1ba96(%rip), %rdx     # 0x5fb50
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44272
movq 0x28(%rax), %rdi
movq %rdi, (%rsp)
movq 0x30(%rax), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x44173
movq 0x1dec2(%rip), %rcx     # 0x61fb0
cmpb $0x0, (%rcx)
je 0x4416b
incl 0x8(%rax)
jmp 0x44173
leaq 0x1b999(%rip), %rsi     # 0x5fa98
leaq 0x1bb9a(%rip), %rdx     # 0x5fca0
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x44272
addq $0x8, %rax
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x43e98
leaq 0x1b964(%rip), %rsi     # 0x5fa98
leaq 0x1bb65(%rip), %rdx     # 0x5fca0
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x442a9
movq 0x10(%rsp), %rdi
addq $0x18, %rax
movq %rax, %rsi
callq 0x43c78
movl %eax, %ebx
movq 0x18(%rsp), %rdi
jmp 0x4425f
lock
incl 0x8(%rax)
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x43f7c
testb %al, %al
je 0x44217
leaq 0x1b90e(%rip), %rsi     # 0x5fa98
leaq 0x1b9bf(%rip), %rdx     # 0x5fb50
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x442ae
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0xac68
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %rsp, %rcx
movq %rbx, %rsi
callq 0xc924
leaq 0x1b8ba(%rip), %rsi     # 0x5fa98
leaq 0x1b96b(%rip), %rdx     # 0x5fb50
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x9340
testq %rax, %rax
je 0x442b3
movq 0x38(%rax), %rdi
movq 0x40(%rax), %r14
testq %r14, %r14
je 0x44220
movq 0x1dda4(%rip), %rax     # 0x61fb0
cmpb $0x0, (%rax)
je 0x4421b
incl 0x8(%r14)
jmp 0x44220
xorl %ebx, %ebx
jmp 0x4425a
lock
incl 0x8(%r14)
leaq 0x30(%rsp), %rsi
callq 0x43f7c
movl %eax, %ebx
testq %r14, %r14
je 0x44239
movq %r14, %rdi
callq 0xa946
leaq 0x30(%rsp), %rdi
callq 0xcb8c
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x4425a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x92e0
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x4409b
callq 0xa946
jmp 0x4409b
callq 0x9530
movl $0x10, %edi
callq 0x9150
movq %rax, %r14
leaq 0x89d1(%rip), %rsi      # 0x4cc5c
movq %rax, %rdi
callq 0x9100
movq 0x1dd56(%rip), %rsi     # 0x61ff0
movq 0x1dcff(%rip), %rdx     # 0x61fa0
movq %r14, %rdi
callq 0x94b0
callq 0x9530
callq 0x9530
callq 0x9530
movq %rax, %rbx
testq %r14, %r14
je 0x442cd
movq %r14, %rdi
callq 0xa946
jmp 0x442cd
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xcb8c
jmp 0x442dc
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x442fa
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x92e0
jmp 0x442fa
jmp 0x442f7
movq %rax, %rbx
movq 0x8(%rsp), %rdi
jmp 0x44309
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x44320
callq 0xa946
jmp 0x44320
movq %rax, %rbx
movq %r14, %rdi
callq 0x9200
movq %rbx, %rdi
callq 0x94e0