// File: pfnet-research[P]pml/src/translate_to_mdp.cpp
// Function: create_if_case(ast::expr_t const&, ast::expr_t const&, ast::expr_t const&, environment_t<value_info_t> const&)
mdp_with_info_t create_if_case(
        ast::expr_t const& cond,
        ast::expr_t const& tr, ast::expr_t const& fl,
        var_env_t const& var_env) {

    auto cond_ = trans_impl(cond, var_env);
    auto tr_ = trans_impl(tr, var_env);
    auto fl_ = trans_impl(fl, var_env);

    auto result_mdp = mdp::mdp_t::merge(
            mdp::mdp_t::merge(std::move(cond_.mdp), std::move(tr_.mdp)),
            std::move(fl_.mdp));

    // [] location=accept-of-cond & cond -> 1:location'=init-of-tr
    auto concat_to_true = make_concat_with_cond(
            cond_.accept, tr_.init,
            make<mdp::var_expr_t>(cond_.value.name));
    // [] location=accept-of-cond & !cond -> 1:location'=init-of-fl
    auto concat_to_false = make_concat(
            cond_.accept, fl_.init,
            make<mdp::neg_expr_t>(make<mdp::var_expr_t>(cond_.value.name)));

    auto accept_loc = translation_data::fresh_location();
    auto result_var_name = translation_data::fresh_var();
    // [] location=accept-of-tr -> 1:location'=accept & value_name'=value_name-of-tr
    auto phi_true = make_concat(
            tr_.accept, accept_loc,
            make<mdp::binop_expr_t>(
                make<mdp::var_expr_t>(result_var_name + "'"),
                make<mdp::var_expr_t>(tr_.value.name),
                mdp::binop_kind_t::Eq));
    // [] location=accept-of-fl -> 1:location'=accept & value_name'=value_name-of-fl
    auto phi_false = make_concat(
            fl_.accept, accept_loc,
            make<mdp::binop_expr_t>(
                make<mdp::var_expr_t>(result_var_name+"'"),
                make<mdp::var_expr_t>(fl_.value.name),
                mdp::binop_kind_t::Eq));

    result_mdp.commands.push_back(concat_to_true);
    result_mdp.commands.push_back(concat_to_false);
    result_mdp.commands.push_back(phi_true);
    result_mdp.commands.push_back(phi_false);

    if (!tr_.value.bound != !fl_.value.bound)
        throw std::logic_error{"invalid if expression"};

    auto result_var = tr_.value.bound ?
        mdp::variable_t{result_var_name, *tr_.value.bound | *fl_.value.bound, 0} :
        mdp::variable_t{result_var_name, true};

    result_mdp.variables.push_back(result_var);

    return mdp_with_info_t {
        result_mdp, cond_.init, accept_loc,
        value_info_t {
            result_var_name,
            result_var.is_int() ?
                util::make_optional(result_var.as_int().bound) :
                util::nullopt
        }
    };
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5c0, %rsp            # imm = 0x5C0
movq %rdi, -0x520(%rbp)
movq %rdi, %rax
movq %rax, -0x518(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
callq 0x881e0
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x168(%rbp), %rdi
callq 0x881e0
jmp 0x8858a
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x881e0
jmp 0x885a0
leaq -0x2e8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
leaq -0x168(%rbp), %rdx
callq 0xa960
jmp 0x885bc
leaq -0x280(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
leaq -0x218(%rbp), %rdx
callq 0xa960
jmp 0x885d8
leaq -0x2e8(%rbp), %rdi
callq 0xc340
movl -0x5c(%rbp), %eax
movl %eax, -0x528(%rbp)
movl -0x100(%rbp), %eax
movl %eax, -0x524(%rbp)
leaq -0x58(%rbp), %rsi
leaq -0x330(%rbp), %rdi
callq 0x88500
jmp 0x8860b
leaq -0x320(%rbp), %rdi
movq %rdi, -0x530(%rbp)
leaq -0x330(%rbp), %rsi
callq 0x7b480
movl -0x528(%rbp), %esi
movl -0x524(%rbp), %edx
movq -0x530(%rbp), %rcx
leaq -0x310(%rbp), %rdi
callq 0x87870
jmp 0x88646
leaq -0x320(%rbp), %rdi
callq 0xceb0
leaq -0x330(%rbp), %rdi
callq 0x7a6b0
movl -0x5c(%rbp), %eax
movl %eax, -0x538(%rbp)
movl -0x1b0(%rbp), %eax
movl %eax, -0x534(%rbp)
leaq -0x58(%rbp), %rsi
leaq -0x388(%rbp), %rdi
callq 0x88500
jmp 0x88685
leaq -0x378(%rbp), %rdi
leaq -0x388(%rbp), %rsi
callq 0x88fa0
jmp 0x8869a
leaq -0x368(%rbp), %rdi
movq %rdi, -0x540(%rbp)
leaq -0x378(%rbp), %rsi
callq 0x8bab0
movl -0x538(%rbp), %esi
movl -0x534(%rbp), %edx
movq -0x540(%rbp), %rcx
leaq -0x358(%rbp), %rdi
callq 0x873d0
jmp 0x886d5
leaq -0x368(%rbp), %rdi
callq 0xceb0
leaq -0x378(%rbp), %rdi
callq 0x8bae0
leaq -0x388(%rbp), %rdi
callq 0x7a6b0
callq 0x8b550
movl %eax, -0x544(%rbp)
jmp 0x88706
movl -0x544(%rbp), %eax
movl %eax, -0x38c(%rbp)
leaq -0x3b0(%rbp), %rdi
callq 0x8b570
jmp 0x88720
movl -0xfc(%rbp), %eax
movl %eax, -0x54c(%rbp)
movl -0x38c(%rbp), %eax
movl %eax, -0x548(%rbp)
leaq 0x17540(%rip), %rdx     # 0x9fc7f
leaq -0x428(%rbp), %rdi
leaq -0x3b0(%rbp), %rsi
callq 0x8b610
jmp 0x88754
leaq -0x408(%rbp), %rdi
leaq -0x428(%rbp), %rsi
callq 0x86fb0
jmp 0x88769
leaq -0xf8(%rbp), %rsi
leaq -0x438(%rbp), %rdi
callq 0x88500
jmp 0x8877e
movl $0x8, -0x43c(%rbp)
leaq -0x3f8(%rbp), %rdi
leaq -0x408(%rbp), %rsi
leaq -0x438(%rbp), %rdx
leaq -0x43c(%rbp), %rcx
callq 0x884c0
jmp 0x887ab
leaq -0x3e8(%rbp), %rdi
movq %rdi, -0x558(%rbp)
leaq -0x3f8(%rbp), %rsi
callq 0x7a500
movl -0x54c(%rbp), %esi
movl -0x548(%rbp), %edx
movq -0x558(%rbp), %rcx
leaq -0x3d8(%rbp), %rdi
callq 0x873d0
jmp 0x887e6
leaq -0x3e8(%rbp), %rdi
callq 0xceb0
leaq -0x3f8(%rbp), %rdi
callq 0x7a670
leaq -0x438(%rbp), %rdi
callq 0x7a6b0
leaq -0x408(%rbp), %rdi
callq 0x7a6b0
leaq -0x428(%rbp), %rdi
callq 0xa240
movl -0x1ac(%rbp), %eax
movl %eax, -0x560(%rbp)
movl -0x38c(%rbp), %eax
movl %eax, -0x55c(%rbp)
leaq 0x1743e(%rip), %rdx     # 0x9fc7f
leaq -0x4b8(%rbp), %rdi
leaq -0x3b0(%rbp), %rsi
callq 0x8b610
jmp 0x88856
leaq -0x498(%rbp), %rdi
leaq -0x4b8(%rbp), %rsi
callq 0x86fb0
jmp 0x8886b
leaq -0x1a8(%rbp), %rsi
leaq -0x4c8(%rbp), %rdi
callq 0x88500
jmp 0x88880
movl $0x8, -0x4cc(%rbp)
leaq -0x488(%rbp), %rdi
leaq -0x498(%rbp), %rsi
leaq -0x4c8(%rbp), %rdx
leaq -0x4cc(%rbp), %rcx
callq 0x884c0
jmp 0x888ad
leaq -0x478(%rbp), %rdi
movq %rdi, -0x568(%rbp)
leaq -0x488(%rbp), %rsi
callq 0x7a500
movl -0x560(%rbp), %esi
movl -0x55c(%rbp), %edx
movq -0x568(%rbp), %rcx
leaq -0x468(%rbp), %rdi
callq 0x873d0
jmp 0x888e8
leaq -0x478(%rbp), %rdi
callq 0xceb0
leaq -0x488(%rbp), %rdi
callq 0x7a670
leaq -0x4c8(%rbp), %rdi
callq 0x7a6b0
leaq -0x498(%rbp), %rdi
callq 0x7a6b0
leaq -0x4b8(%rbp), %rdi
callq 0xa240
leaq -0x230(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x8b8e0
jmp 0x88939
leaq -0x230(%rbp), %rdi
leaq -0x358(%rbp), %rsi
callq 0x8b8e0
jmp 0x8894e
leaq -0x230(%rbp), %rdi
leaq -0x3d8(%rbp), %rsi
callq 0x8b8e0
jmp 0x88963
leaq -0x230(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x8b8e0
jmp 0x88978
leaq -0x168(%rbp), %rdi
addq $0x70, %rdi
addq $0x20, %rdi
callq 0x8b950
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x56c(%rbp)
leaq -0x218(%rbp), %rdi
addq $0x70, %rdi
addq $0x20, %rdi
callq 0x8b950
movb %al, %cl
movl -0x56c(%rbp), %eax
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x88cbb
movl $0x10, %edi
callq 0xa1d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x578(%rbp)
leaq 0x17924(%rip), %rsi     # 0xa0308
callq 0xa540
jmp 0x889eb
movq -0x578(%rbp), %rdi
movq 0x5e5d7(%rip), %rsi     # 0xe6fd0
movq 0x5e590(%rip), %rdx     # 0xe6f90
callq 0xa610
jmp 0x88f9a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f82
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0xc340
jmp 0x88f6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x320(%rbp), %rdi
callq 0xceb0
leaq -0x330(%rbp), %rdi
callq 0x7a6b0
jmp 0x88f5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88b0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x368(%rbp), %rdi
callq 0xceb0
leaq -0x378(%rbp), %rdi
callq 0x8bae0
leaq -0x388(%rbp), %rdi
callq 0x7a6b0
jmp 0x88f52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f46
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88bca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88bbe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88bb2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x3e8(%rbp), %rdi
callq 0xceb0
leaq -0x3f8(%rbp), %rdi
callq 0x7a670
leaq -0x438(%rbp), %rdi
callq 0x7a6b0
leaq -0x408(%rbp), %rdi
callq 0x7a6b0
leaq -0x428(%rbp), %rdi
callq 0xa240
jmp 0x88f3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88c70
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88c64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88c58
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
leaq -0x478(%rbp), %rdi
callq 0xceb0
leaq -0x488(%rbp), %rdi
callq 0x7a670
leaq -0x4c8(%rbp), %rdi
callq 0x7a6b0
leaq -0x498(%rbp), %rdi
callq 0x7a6b0
leaq -0x4b8(%rbp), %rdi
callq 0xa240
jmp 0x88f2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f22
movq -0x578(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
callq 0xa2c0
jmp 0x88f22
leaq -0x168(%rbp), %rdi
addq $0x70, %rdi
addq $0x20, %rdi
callq 0x8b950
testb $0x1, %al
jne 0x88cd5
jmp 0x88d49
leaq -0xd8(%rbp), %rdi
callq 0x8b9a0
movq %rax, -0x580(%rbp)
jmp 0x88cea
leaq -0x188(%rbp), %rdi
callq 0x8b9a0
movq %rax, -0x588(%rbp)
jmp 0x88cff
movq -0x588(%rbp), %rsi
movq -0x580(%rbp), %rdi
callq 0x88fd0
movq %rax, -0x590(%rbp)
jmp 0x88d1b
movq -0x590(%rbp), %rax
movq %rax, -0x508(%rbp)
leaq -0x500(%rbp), %rdi
leaq -0x3b0(%rbp), %rsi
leaq -0x508(%rbp), %rdx
xorl %ecx, %ecx
callq 0x7a3c0
jmp 0x88d47
jmp 0x88d65
leaq -0x500(%rbp), %rdi
leaq -0x3b0(%rbp), %rsi
movl $0x1, %edx
callq 0x8b9c0
jmp 0x88d63
jmp 0x88d65
leaq -0x260(%rbp), %rdi
leaq -0x500(%rbp), %rsi
callq 0x8bb00
jmp 0x88d7a
movq -0x520(%rbp), %rdi
movq %rdi, %rax
movq %rax, -0x598(%rbp)
leaq -0x280(%rbp), %rsi
callq 0x8b6f0
jmp 0x88d99
movq -0x520(%rbp), %rdi
movl -0x60(%rbp), %eax
movl %eax, 0x68(%rdi)
movl -0x38c(%rbp), %eax
movl %eax, 0x6c(%rdi)
addq $0x70, %rdi
movq %rdi, -0x5a8(%rbp)
movq %rdi, %rax
movq %rax, -0x5a0(%rbp)
leaq -0x3b0(%rbp), %rsi
callq 0xa190
jmp 0x88dd2
movq -0x5a0(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x5b8(%rbp)
leaq -0x500(%rbp), %rdi
callq 0xbd80
movb %al, -0x5a9(%rbp)
jmp 0x88df8
movb -0x5a9(%rbp), %al
testb $0x1, %al
jne 0x88e04
jmp 0x88e30
leaq -0x500(%rbp), %rdi
callq 0x33880
movq %rax, -0x5c0(%rbp)
jmp 0x88e19
movq -0x5c0(%rbp), %rsi
movq -0x5b8(%rbp), %rdi
callq 0x89020
jmp 0x88e2e
jmp 0x88e3c
movq -0x5b8(%rbp), %rdi
callq 0x8bb70
leaq -0x500(%rbp), %rdi
callq 0xdda0
leaq -0x468(%rbp), %rdi
callq 0xce20
leaq -0x3d8(%rbp), %rdi
callq 0xce20
leaq -0x3b0(%rbp), %rdi
callq 0xa240
leaq -0x358(%rbp), %rdi
callq 0xce20
leaq -0x310(%rbp), %rdi
callq 0xce20
leaq -0x280(%rbp), %rdi
callq 0xc340
leaq -0x218(%rbp), %rdi
callq 0x79690
leaq -0x168(%rbp), %rdi
callq 0x79690
leaq -0xc8(%rbp), %rdi
callq 0x79690
movq -0x518(%rbp), %rax
addq $0x5c0, %rsp            # imm = 0x5C0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
jmp 0x88f0a
movq -0x5a8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x170(%rbp)
movl %eax, -0x174(%rbp)
callq 0xa240
movq -0x598(%rbp), %rdi
callq 0xc340
leaq -0x500(%rbp), %rdi
callq 0xdda0
leaq -0x468(%rbp), %rdi
callq 0xce20
leaq -0x3d8(%rbp), %rdi
callq 0xce20
leaq -0x3b0(%rbp), %rdi
callq 0xa240
leaq -0x358(%rbp), %rdi
callq 0xce20
leaq -0x310(%rbp), %rdi
callq 0xce20
leaq -0x280(%rbp), %rdi
callq 0xc340
leaq -0x218(%rbp), %rdi
callq 0x79690
leaq -0x168(%rbp), %rdi
callq 0x79690
leaq -0xc8(%rbp), %rdi
callq 0x79690
movq -0x170(%rbp), %rdi
callq 0xa650
nopw (%rax,%rax)