// File: pfnet-research[P]pml/src/translate_to_mdp.cpp
// Function: trans_impl(ast::expr_t const&, environment_t<value_info_t> const&)
mdp_with_info_t trans_impl(ast::expr_t const& e, var_env_t const& var_env) {
    using namespace ast;
    switch (e.kind()) {
    case expr_kind_t::Let:
        return create_let_case(
                cast<ast::let_expr_t>(e).name,
                *cast<ast::let_expr_t>(e).init,
                *cast<ast::let_expr_t>(e).body,
                var_env);
    case expr_kind_t::If:
        return create_if_case(
                *cast<if_expr_t>(e).cond_expr,
                *cast<if_expr_t>(e).true_expr,
                *cast<if_expr_t>(e).false_expr,
                var_env);
    case expr_kind_t::Rand:
        return create_rand_case(
                cast<rand_expr_t>(e).start,
                cast<rand_expr_t>(e).end);
    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 create_binop_case(
                *cast<ast::binop_expr_t>(e).lhs,
                *cast<ast::binop_expr_t>(e).rhs,
                e.kind(), var_env);
    case expr_kind_t::Neg:
        return create_neg_case(*cast<ast::neg_expr_t>(e).inner, var_env);
    case expr_kind_t::Int:
        return create_int_case(cast<ast::int_expr_t>(e).n);
    case expr_kind_t::Bool:
        return create_bool_case(cast<ast::bool_expr_t>(e).b);
    case expr_kind_t::Var:
        return create_var_case(cast<ast::var_expr_t>(e).name, var_env);
    case expr_kind_t::Typed:
        return trans_impl(*cast<typed_expr_t>(e).expr, var_env);
    default:
        throw std::logic_error{"unimplemented translation"};
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x40(%rbp)
movq %rdi, %rax
movq %rax, -0x38(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x14, %eax
ja 0x8841d
movq -0x30(%rbp), %rax
leaq 0x17fbc(%rip), %rcx     # 0xa01e4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
callq 0x8ad60
addq $0x8, %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8ad60
movq %rax, %rdi
addq $0x28, %rdi
callq 0x13020
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8ad60
movq %rax, %rdi
addq $0x38, %rdi
callq 0x13020
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rax, %rcx
movq -0x18(%rbp), %r8
callq 0x87cc0
jmp 0x8846d
movq -0x10(%rbp), %rdi
callq 0x8ada0
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13020
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8ada0
movq %rax, %rdi
addq $0x18, %rdi
callq 0x13020
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8ada0
movq %rax, %rdi
addq $0x28, %rdi
callq 0x13020
movq -0x40(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq %rax, %rcx
movq -0x18(%rbp), %r8
callq 0x88530
jmp 0x8846d
movq -0x10(%rbp), %rdi
callq 0x8ade0
movl 0x8(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8ade0
movq -0x40(%rbp), %rdi
movl -0x64(%rbp), %esi
movl 0xc(%rax), %edx
callq 0x862a0
jmp 0x8846d
movq -0x10(%rbp), %rdi
callq 0x8ae20
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13020
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8ae20
movq %rax, %rdi
addq $0x18, %rdi
callq 0x13020
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
callq *(%rax)
movq -0x40(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movl %eax, %ecx
movq -0x18(%rbp), %r8
callq 0x89e00
jmp 0x8846d
movq -0x10(%rbp), %rdi
callq 0x8ae60
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13020
movq -0x40(%rbp), %rdi
movq %rax, %rsi
movq -0x18(%rbp), %rdx
callq 0x89fe0
jmp 0x8846d
movq -0x10(%rbp), %rdi
callq 0x8aea0
movq -0x40(%rbp), %rdi
movl 0x8(%rax), %esi
callq 0x8a3f0
jmp 0x8846d
movq -0x10(%rbp), %rdi
callq 0x8aee0
movq -0x40(%rbp), %rdi
movb 0x8(%rax), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x8a7d0
jmp 0x8846d
movq -0x10(%rbp), %rdi
callq 0x8af20
movq -0x40(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
movq -0x18(%rbp), %rdx
callq 0x8ab80
jmp 0x8846d
movq -0x10(%rbp), %rdi
callq 0x8af60
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13020
movq -0x40(%rbp), %rdi
movq %rax, %rsi
movq -0x18(%rbp), %rdx
callq 0x881e0
jmp 0x8846d
movl $0x10, %edi
callq 0xa1d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
leaq 0x17f45(%rip), %rsi     # 0xa037d
callq 0xa540
jmp 0x8843f
movq -0x80(%rbp), %rdi
movq 0x5eb86(%rip), %rsi     # 0xe6fd0
movq 0x5eb3f(%rip), %rdx     # 0xe6f90
callq 0xa610
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0xa2c0
jmp 0x8847a
movq -0x38(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0xa650
nopw %cs:(%rax,%rax)