(* Generated by coq-of-rust *)
Require Import CoqOfRust.CoqOfRust.

(*
fn main() {
    use std::arch::asm;

    extern "C" fn foo(arg: i32) -> i32 {
        println!("arg = {}", arg);
        arg * 2
    }

    fn call_foo(arg: i32) -> i32 {
        unsafe {
            let result;
            asm!(
                "call {}",
                // Function pointer to call
                in(reg) foo,
                // 1st argument in rdi
                in("rdi") arg,
                // Return value in rax
                out("rax") result,
                // Mark all registers which are not preserved by the "C" calling
                // convention as clobbered.
                clobber_abi("C"),
            );
            result
        }
    }
}
*)
Definition main (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
  match ε, τ, α with
  | [], [], [] => ltac:(M.monadic (Value.Tuple []))
  | _, _, _ => M.impossible "wrong number of arguments"
  end.

Axiom Function_main : M.IsFunction "inline_assembly_symbol_operands_and_abi_clobbers::main" main.

Module main.
  (*
      extern "C" fn foo(arg: i32) -> i32 {
          println!("arg = {}", arg);
          arg * 2
      }
  *)
  Definition foo (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
    match ε, τ, α with
    | [], [], [ arg ] =>
      ltac:(M.monadic
        (let arg := M.alloc (| arg |) in
        M.read (|
          let~ _ :=
            let~ _ :=
              M.alloc (|
                M.call_closure (|
                  M.get_function (| "std::io::stdio::_print", [] |),
                  [
                    M.call_closure (|
                      M.get_associated_function (| Ty.path "core::fmt::Arguments", "new_v1", [] |),
                      [
                        M.alloc (|
                          Value.Array
                            [ M.read (| Value.String "arg = " |); M.read (| Value.String "
" |) ]
                        |);
                        M.alloc (|
                          Value.Array
                            [
                              M.call_closure (|
                                M.get_associated_function (|
                                  Ty.path "core::fmt::rt::Argument",
                                  "new_display",
                                  [ Ty.path "i32" ]
                                |),
                                [ arg ]
                              |)
                            ]
                        |)
                      ]
                    |)
                  ]
                |)
              |) in
            M.alloc (| Value.Tuple [] |) in
          M.alloc (| BinOp.Wrap.mul (| M.read (| arg |), Value.Integer IntegerKind.I32 2 |) |)
        |)))
    | _, _, _ => M.impossible "wrong number of arguments"
    end.
  
  Axiom Function_foo :
    M.IsFunction "inline_assembly_symbol_operands_and_abi_clobbers::main::foo" foo.
  
  (*
      fn call_foo(arg: i32) -> i32 {
          unsafe {
              let result;
              asm!(
                  "call {}",
                  // Function pointer to call
                  in(reg) foo,
                  // 1st argument in rdi
                  in("rdi") arg,
                  // Return value in rax
                  out("rax") result,
                  // Mark all registers which are not preserved by the "C" calling
                  // convention as clobbered.
                  clobber_abi("C"),
              );
              result
          }
      }
  *)
  Definition call_foo (ε : list Value.t) (τ : list Ty.t) (α : list Value.t) : M :=
    match ε, τ, α with
    | [], [], [ arg ] =>
      ltac:(M.monadic
        (let arg := M.alloc (| arg |) in
        M.read (|
          let~ result := M.copy (| Value.DeclaredButUndefined |) in
          let~ _ := InlineAssembly in
          result
        |)))
    | _, _, _ => M.impossible "wrong number of arguments"
    end.
  
  Axiom Function_call_foo :
    M.IsFunction "inline_assembly_symbol_operands_and_abi_clobbers::main::call_foo" call_foo.
End main.
